-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpiryChecker.java
More file actions
105 lines (89 loc) · 3.15 KB
/
ExpiryChecker.java
File metadata and controls
105 lines (89 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//The expiry checker runs through the list of fooditems every 5 minutes and notifies the mobile app if any item expires in a certain time
import java.util.LinkedList;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
class ExpiryChecker implements Runnable {
private Database<FoodItem> db;
private DatagramSocket socket;
public static final String androidInetAddressString = "127.0.0.1";
public static final int androidPort = 1078;
public ExpiryChecker(Database db){
this.db = db;
try{
socket = new DatagramSocket();
socket.setSoTimeout(20000);
} catch(SocketException e){
ReaderClass.println("Error setting up the android communication socket");
}
}
public boolean sendNotificationToAndroidApp(String notificationString){
// create the byte array
byte[] byteArray = new byte[ReaderClass.datagramLength];
byteArray[0] = '5'; //opcode 5 for 'Notify User'
byteArray[1] = FoodItem.opcodeDelimiter.getBytes()[0];
byte[] notifStringAsBytes = notificationString.getBytes();
System.arraycopy(notifStringAsBytes, 0, byteArray, 2, notifStringAsBytes.length);
byteArray[notifStringAsBytes.length + 2] = FoodItem.opcodeDelimiter.getBytes()[0];
DatagramPacket p = null;
try{
p = new DatagramPacket(byteArray, byteArray.length, InetAddress.getByName(androidInetAddressString), androidPort);
} catch(UnknownHostException e){
ReaderClass.println("No host by name " + androidInetAddressString);
}
try {
socket.send(p);
} catch(IOException e){
ReaderClass.println("Error sending packet to android app");
}
try{
socket.receive(p); //reuse the same packet, we don't need it after it's sent
}catch (SocketTimeoutException e){
ReaderClass.println("The android did not respond");
return false;
}catch(IOException e){
return false;
}
return true;
}
public void run(){
ReaderClass.println("ExpiryChecker is alive");
while(true){
try{
Thread.sleep(300000); //sleep for 5 minutes
} catch(InterruptedException e){
//might do something here, if the sleep is interrupted
}
ReaderClass.println("ExpiryChecker is checking for expiring items");
//check through the database
for(int i = 0; i < db.size(); ++i){
FoodItem checkItem = db.get(i);
float expiryDate = 0;
boolean expiryDateIsInHours = false;
if (checkItem.needsWarning()){
expiryDate = checkItem.expiresInDays();
if (expiryDate < 1) {
expiryDate = checkItem.expiresInHours();
expiryDateIsInHours = true;
}
}
if (expiryDate != 0){
String s = (db.numberOfInstances(checkItem) > 1) ? "s expire in " : " expires in ";
String warningString = checkItem.getName() + s + expiryDate + (expiryDateIsInHours ? " hours." : " days.");
ReaderClass.println("sending to android : " + warningString);
if (sendNotificationToAndroidApp(warningString)){
for (int j = 0; j < db.size(); ++j){
FoodItem f = db.get(j);
if (f.equals(checkItem)) f.warned();
}
}
}
}
}
}
}