forked from MetalBunker/WashingBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWashingBot.js
More file actions
220 lines (182 loc) · 7.46 KB
/
WashingBot.js
File metadata and controls
220 lines (182 loc) · 7.46 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
app.LoadScript("config.js");
app.LoadScript("washingMonitor.js");
app.LoadScript("miniSlackBot.js");
var btnStart = null,
btnStop = null,
txtStatus = null,
txtConnection = null,
monitor = null,
miniSlackBotInstance = null,
netClient = null,
udpIpAddress = null;
function OnStart() {
setupUI();
setupNetClient();
setupMonitor();
setupSlackBot();
say("Happy washing!");
}
function setupNetClient(){
if (!config.sendUdpUpdates) return;
netClient = app.CreateNetClient("UDP");
udpIpAddress = config.udpIpAddress ? config.udpIpAddress : netClient.GetBroadcastAddress();
}
function setupUI(){
lay = app.CreateLayout("Linear", "VCenter,FillXY");
txtStatus = app.CreateText("", 0.8, 0.3, "Multiline");
lay.AddChild(txtStatus);
txtStatus.SetText("Ready to serve!");
btnStart = app.CreateButton("Start Washing", 0.5, 0.3, "Alum");
lay.AddChild(btnStart);
btnStart.SetOnTouch(btnStart_OnTouch);
btnStop = app.CreateButton("Stop!", 0.5, 0.3, "Alum");
lay.AddChild(btnStop);
btnStop.SetOnTouch(btnStop_OnTouch);
btnStop.SetVisibility("Hide");
txtConnection = app.CreateText("", 0.8, 0.2, "Multiline");
lay.AddChild(txtConnection);
txtConnection.SetText("Slack integration disabled");
app.AddLayout(lay);
}
function btnStart_OnTouch() {
monitor.start();
}
function btnStop_OnTouch() {
monitor.stop();
}
function setupSlackBot(){
if (!config.slackToken) return;
miniSlackBotInstance = miniSlackBot.create({
token: config.slackToken,
defaultChannelName: config.slackChannelName,
events: {
onConnecting: function (){
txtConnection.SetText("Connecting to Slack...");
},
onConnectionOpened: function (isReconnect) {
txtConnection.SetText("Connected to Slack :)");
if (isReconnect) {
miniSlackBotInstance.sendMessage("I'm back :space_invader:");
}
else {
miniSlackBotInstance.sendMessage("Hello... boooooo! :ghost:");
}
},
onConnectionError: function () {
txtConnection.SetText("Slack connection error :'(");
},
onConnectionClosed: function () {
txtConnection.SetText("Disconnected from Slack :(");
},
onMessage : function (message, channelId) {
var command = message.split(" ");
if (command.length > 0 && command[0] != "") {
switch (command[0].toLowerCase()) {
case "time":
if (monitor.hasFinished()) {
miniSlackBotInstance.sendMessage(":clock3: It took me " + monitor.getWashingDurationInMinutes() + " mins. to do the washing", channelId);
return;
}
if (monitor.getStartTime()) {
miniSlackBotInstance.sendMessage(":clock3: I've been washing for " + monitor.getWashingDurationInMinutes() + " mins. :smiley:", channelId);
return;
}
miniSlackBotInstance.sendMessage("I haven't started yet! :smiley_cat:", channelId);
break;
case "say":
if (command.length == 1) {
miniSlackBotInstance.sendMessage("What do you want me to say?", channelId);
}
else {
say(command.slice(1).join(" "));
}
break;
default:
miniSlackBotInstance.sendMessage("You lost me there (I'm not trained to process that request).", channelId);
break;
}
}
else {
miniSlackBotInstance.sendMessage(":squirrel: I need your clothes, your boots, and your motorcycle!", channelId);
}
}
}
});
}
function setupMonitor(){
// We augment the config object for the washingMonitor with
// the callbacks for each event
config.onInit = function(){
console.log("Init!");
};
config.onStart = function(){
btnStart.SetVisibility("Hide");
btnStop.SetVisibility("Show");
txtStatus.SetText("Waiting for cell placing...");
say("Please place the phone on the washing machine.");
};
config.onStop = function(){
btnStop.SetVisibility("Hide");
btnStart.SetVisibility("Show");
txtStatus.SetText("Ready to serve!");
notifySlack(">>> :no_entry: Washing canceled :(");
};
config.onWaitingWashingStart = function(){
txtStatus.SetText("Waiting for start...");
say("Waiting for start.");
notifySlack(">>> Machine configured. Waiting for start... (Let's pray together :pray:)");
};
config.onWashingStarted = function(){
notifySlack("Washing started!");
say("Washing start, detected!");
};
config.onWashingNotStarted = function(notificationCount){
txtStatus.SetText("Washing never started! Still waiting (" + notificationCount + ")...");
notifySlack(":warning: Washing never started! Still waiting (" + notificationCount + ")...");
say("Washing never started! Still waiting");
};
config.onWashingMovement = function(washingDurationMinutes, x, y, z) {
txtStatus.SetText("Washing for " + washingDurationMinutes + " mins...");
sendPacket(x, y, z);
}
config.onFinished = function(washingDurationMinutes) {
txtStatus.SetText("Laundry finished!!! Duration: " + washingDurationMinutes + "\nWaiting for human...");
notifySlack("Human go hang the clothes!! :clock3: Washing duration: " + washingDurationMinutes);
say("Laundry finished.");
}
config.onFinishedReminder = function(minsSinceFinish, reminderCount) {
notifySlack(":information_source: " + reminderCount + " - Remember to hang the clothes, washing has finished " + minsSinceFinish + " mins. ago!");
say("Remember, remember the clothes!");
};
config.onPersonMovement = function() {
btnStart.SetVisibility("Show");
btnStop.SetVisibility("Hide");
txtStatus.SetText(txtStatus.GetText() + "\nHuman detected!");
notifySlack(">>> Human hanging clothes! :white_check_mark:");
say("Enjoy. You poor human!");
}
// Logs all the events, just for testing
config.onEvent = function (eventType, params){
console.log(eventType);
console.log(JSON.stringify(params));
};
monitor = washingMonitor.init(config);
}
function say(speech) {
app.TextToSpeech(speech, 1.0, 1.0);
}
function notifySlack(message){
if (!config.slackToken) return;
miniSlackBotInstance.sendMessage(message);
}
// Sends a packet by UDP with the format "x|y|z". Useful for debugging or graphing
// while washing.
// We substract 9 from the Z axis so the value is closer to the other two, useful
// for graphing.
function sendPacket(x,y,z){
if (!config.sendUdpUpdates) return;
// We substract 9 from z axis so the value is closer to the other two, useful
// for graphing.
var packet = x + "|"+ y +"|"+ (z-9);
netClient.SendDatagram( packet, "UTF-8", udpIpAddress, config.udpPort );
}