forked from maradine/ps2bot
/
MaintenanceEngine.java
139 lines (117 loc) · 4.1 KB
/
MaintenanceEngine.java
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.util.LinkedList;
import java.util.HashMap;
import org.pircbotx.PircBotX;
import org.pircbotx.Colors;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
import java.util.HashMap;
import java.util.Set;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.BiMap;
import java.util.Calendar;
public class MaintenanceEngine implements Runnable {
private PircBotX bot;
private String channel;
private boolean onSwitch;
private int timeout;
private String soeapikey;
private Properties props;
private HashBiMap<String, SimpleTime> schedule;
public MaintenanceEngine(PircBotX bot, Properties props) {
this.bot = bot;
this.props = props;
this.channel = props.getProperty("irc_channel");
this.soeapikey = props.getProperty("soeapikey");
onSwitch = false;
timeout = 10000;
schedule = HashBiMap.create();
}
public void setTimeout(int set) {
timeout = set;
}
public int getTimeout() {
return timeout;
}
public void purgeSchedule() {
schedule.clear();
}
public HashBiMap<String, SimpleTime> getSchedule() {
return schedule;
}
public void addSchedule(String event, SimpleTime time) {
schedule.put(event, time);
}
public void addSchedule(String event, int hours, int minutes) {
SimpleTime time = new SimpleTime(hours, minutes);
schedule.forcePut(event, time);
}
public void turnOn() {
onSwitch = true;
}
public void turnOff() {
onSwitch = false;
}
public void run() {
while (true) {
try {
//bot.sendMessage(channel, "Entering run() block.");
Calendar now;
//are we even turned on?
if (onSwitch) {
//what time is it?
now = Calendar.getInstance();
int nowHours = now.get(Calendar.HOUR_OF_DAY);
int nowMinutes = now.get(Calendar.MINUTE);
//bot.sendMessage(channel, "Checking events for "+nowHours+":"+nowMinutes);
//is there an event for this minute?
SimpleTime st = new SimpleTime(nowHours,nowMinutes);
//System.out.println("Constructed a SimpleTime to use as a key with elements "+st.getHours()+":"+st.getMinutes());
BiMap<SimpleTime, String> inverted = schedule.inverse();
//System.out.println("Inverted map has "+ inverted.size() +" elements");
Set<SimpleTime> keySet = inverted.keySet();
for (SimpleTime s : keySet) {
//System.out.println("key "+s.getHours()+":"+s.getMinutes());
}
String eventString = inverted.get(st);
//System.out.println("Just inverted and got the event string: "+eventString);
if (eventString != null) {
if (eventString.equals("cat")) {
System.out.println("if cats");
bot.sendMessage(channel, "This is the cat event!");
}
if (eventString.equals("florida")) {
System.out.println("if florida");
bot.sendMessage(channel, "This is the florida event!");
}
if (eventString.equals("nightlykills")) {
bot.sendMessage(channel, "Running nightly kill aggregation routines.");
HashMap<String, Integer> results = DailyAggregator.aggregateKills(props);
int rowsDeleted = results.get("deletedRows");
int secondsElapsed = results.get("elapsedSeconds");
if (rowsDeleted >=0 && secondsElapsed >=0) {
bot.sendMessage(channel,"Daily kill aggregator execution complete. Aggregation job took "+secondsElapsed+" seconds. "+rowsDeleted+" rows removed from raw table.");
} else {
bot.sendMessage(channel, "Received no result set from the aggregation job. Bro, do you even collect?");
}
}
}
} else {
//bot.sendMessage(channel, "Turned off, doing nothing.");
}
//caluclate time to next minute
now = Calendar.getInstance();
int millis = 1000 - now.get(Calendar.MILLISECOND);
int seconds = 59 - now.get(Calendar.SECOND);
int sleepMillis = millis + (1000 * seconds);
//sleep
//bot.sendMessage(channel, "Sleeping for "+sleepMillis+" millis.");
Thread.sleep(sleepMillis);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
bot.sendMessage(channel, "Totally unhandled exception - check the live feed, sparky.");
}
}
}
}