forked from MetalBunker/WashingBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWashingBot.js
182 lines (151 loc) · 6.5 KB
/
WashingBot.js
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
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;
function OnStart() {
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("Waiting...");
app.AddLayout(lay);
setupMonitor();
miniSlackBotInstance = miniSlackBot.create({
token: config.slackToken,
defaultChannelName: config.slackChannelName,
events: {
onConnecting: function (){
txtConnection.SetText("Connecting...");
},
onConnectionOpened: function (isReconnect) {
txtConnection.SetText("Connected :)");
if (isReconnect) {
miniSlackBotInstance.sendMessage("I'm back :space_invader:");
}
else {
miniSlackBotInstance.sendMessage("Hello... boooooo! :ghost:");
}
},
onConnectionError: function () {
txtConnection.SetText("Connection error :'(");
},
onConnectionClosed: function () {
txtConnection.SetText("Disconnected :(");
},
onMessage : function (message, channelId) {
var command = message.split(" ");
if (command.length > 0 && command[0] != "") {
switch (command[0]) {
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);
}
}
}
});
say("Happy washing!");
}
function btnStart_OnTouch() {
monitor.start();
}
function btnStop_OnTouch() {
monitor.stop();
}
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!");
miniSlackBotInstance.sendMessage(">>> :no_entry: Washing canceled :(");
};
config.onWaitingWashingStart = function(){
txtStatus.SetText("Waiting for start...");
say("Waiting for start.");
miniSlackBotInstance.sendMessage(">>> Machine configured. Waiting for start... (Let's pray together :pray:)");
};
config.onWashingStarted = function(){
miniSlackBotInstance.sendMessage("Washing started!");
say("Washing start, detected!");
};
config.onWashingNotStarted = function(notificationCount){
txtStatus.SetText("Washing never started! Still waiting (" + notificationCount + ")...");
miniSlackBotInstance.sendMessage(":warning: Washing never started! Still waiting (" + notificationCount + ")...");
say("Washing never started! Still waiting");
};
config.onWashingMovement = function(washingDurationMinutes) {
txtStatus.SetText("Washing for " + washingDurationMinutes + "mins...");
}
config.onFinished = function(washingDurationMinutes) {
txtStatus.SetText("Laundry finished!!! Duration: " + washingDurationMinutes + "\nWaiting for mulo...");
miniSlackBotInstance.sendMessage("Mulo a colgar la ropa!! :clock3: Duración del lavado: " + washingDurationMinutes);
say("Laundry finished.");
}
config.onFinishedReminder = function(minsSinceFinish, reminderCount) {
miniSlackBotInstance.sendMessage(":information_source: " + reminderCount + " - Recordá colgar la ropa, hace " + minsSinceFinish + "mins. que terminó!");
say("Remember, remember the clothes!");
};
config.onPersonMovement = function() {
btnStart.SetVisibility("Show");
btnStop.SetVisibility("Hide");
txtStatus.SetText(txtStatus.GetText() + "\nMulo detected!");
miniSlackBotInstance.sendMessage(">>> Mulo colgando ropa! :white_check_mark:");
say("Enjoy. You fucking moolo!");
}
// 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);
}