-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
429 lines (354 loc) · 12.7 KB
/
index.html
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<!DOCTYPE html>
<html lang="en" ng-app="microwave-app" ng-strict-di>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Language" content="en">
<meta name="viewport" content="width=480">
<title>Microwave</title>
<link rel="stylesheet" type="text/css" href="/3rdparty/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/3rdparty/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="/start.css">
<script src="/3rdparty/jquery/2.1.4/jquery.min.js"></script>
<script src="/3rdparty/bootstrap/3.3.6/js/bootstrap.js"></script>
<script src="/3rdparty/angularjs/1.4.5/angular.js"></script>
<script src="/3rdparty/angularjs/1.4.5/angular-route.js"></script>
<base href="/">
</head>
<body>
<!-- Microwave screen is 480x800 pixels -->
<div id="microwave_screen"><!-- Two containers required to enforce strict sizing -->
<ng-view id="microwave_container"></ng-view>
</div>
<script>
"use strict";
var myApp = angular.module('microwave-app',['ngRoute']);
myApp.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'start.html',
controller: 'start_ctrl',
controllerAs: 'ctrl',
})
.when('/time/:seconds', {
templateUrl: 'time_based.html',
controller: 'time_ctrl',
controllerAs: 'time',
})
.when('/time/', {
templateUrl: 'time_based.html',
controller: 'time_ctrl',
controllerAs: 'time',
})
.when('/qcook/', {
templateUrl: 'qcook.html',
controller: 'qcook_index_ctrl',
controllerAs: 'index',
})
.when('/qcook/:target', {
templateUrl: 'qcook_target.html',
controller: 'qcook_script_ctrl',
controllerAs: 'script',
})
.when('/recipie/', {
templateUrl: 'recipies.html',
controller: 'recipies_index_ctrl',
controllerAs: 'index',
})
.when('/recipie/:target', {
templateUrl: 'recipie_target.html',
controller: 'recipies_script_ctrl',
controllerAs: 'script',
})
.when('/settings/', {
templateUrl: 'settings.html',
controller: 'dummy_ctrl',
controllerAs: 'ctrl',
})
.when('/temperature/:degrees', {
templateUrl: 'temperature_based.html',
controller: 'temperature_ctrl',
controllerAs: 'ctrl',
})
.when('/temperature/', {
templateUrl: 'temperature_based.html',
controller: 'temperature_ctrl',
controllerAs: 'ctrl',
});
$locationProvider.html5Mode(true);
}
]);
myApp.controller('start_ctrl', ['$scope', '$http', '$timeout', 'microwave', function($scope, $http, $timeout, microwave) {
var vm = this;
this.qcook_entries = [];
this.temperature_color = temperature_color; // Allow access from expressions
$http.get('/qcook.json').then(function(res) {
vm.qcook_entries = res.data;
});
// Want to adjust the circularness of the buttons after updating
// Need to do this after the DOM is updated
// Angular doesn't provide ordering
$scope.$watch("qcook_entries", function(newval, oldval) {
// Delay to run after the digest cycles
// NOTE: Runs once when $watch is initialised
// TODO: Can I do this with ng-style?
$timeout(function(){
$('.btn-round').each(function(index, elem) { square_element(elem); });
},0,false);
})
$scope.$watch(angular.bind(this, function() {
return microwave.state;
}), function (new_val, old_val) {
console.log("State changed", old_val, "->", new_val);
if (new_val == "temperature") {
window.location.href = '/temperature/';
} else if (new_val == "time") {
window.location.href = '/time/';
}
});
}]);
myApp.controller('qcook_index_ctrl', ['$http', function($http) {
var vm = this;
this.entries = [];
$http.get('/qcook.json').then(function(res) {
vm.entries = res.data;
});
}]);
myApp.controller('recipies_index_ctrl', ['$http', function($http) {
var vm = this;
this.entries = [];
$http.get('/recipies.json').then(function(res) {
vm.entries = res.data;
});
}]);
myApp.controller('qcook_script_ctrl', ['$http', '$routeParams', function($http, $routeParams) {
var vm = this;
this.scripts = [];
this.details = {};
this.default = {
"title" : "One touch cooking",
"description" : "One touch quick options for your common cooking needs.",
"steps" : [
"Push a button",
"???",
"EAT!"
]
};
$http.get('/qcook.json').then(function(res) {
// JSON should really be a hash, but it makes the handling much harder
// The cost of searching should be minor, and the final implementation
// Will probably include severside filtering
vm.scripts = res.data;
vm.details = vm.default;
// Search for the key entry
angular.forEach(vm.scripts, function(entry) {
if(entry.key == $routeParams.target)
vm.details = entry;
});
});
}]);
myApp.controller('recipies_script_ctrl', ['$http', '$routeParams', function($http, $routeParams) {
var vm = this;
this.scripts = [];
this.details = {};
this.default = {
"title" : "One touch cooking",
"description" : "One touch quick options for your common cooking needs.",
"steps" : [
"Push a button",
"???",
"EAT!"
]
};
$http.get('/recipies.json').then(function(res) {
// JSON should really be a hash, but it makes the handling much harder
// The cost of searching should be minor, and the final implementation
// Will probably include severside filtering
vm.scripts = res.data;
vm.details = vm.default;
// Search for the key entry
angular.forEach(vm.scripts, function(entry) {
if(entry.key == $routeParams.target)
vm.details = entry;
});
});
}]);
myApp.controller('dummy_ctrl', [function() {}]);
myApp.controller('temperature_ctrl', ['$routeParams', 'microwave', '$scope', function($routeParams, microwave, $scope) {
var vm = this;
this.microwave = microwave;
if ("degrees" in $routeParams) {
// If we start with degrees we are in control, kick off the process
// If we start without degrees we are just an observer
var degrees = parseInt($routeParams.degrees, 10);
microwave.set_power(100);
microwave.start_temperature(degrees);
}
temp_dial();
draw_power_bg( document.getElementById("power_triangle") );
draw_fixed_power_marker( document.getElementById("power_marker") );
$scope.$watch(angular.bind(this, function() {
return this.microwave.temperature;
}), function (new_val) {
$("#temp_dial").val(new_val).trigger('change');
});
$scope.$watch(angular.bind(this, function() {
return this.microwave.target_temperature;
}), function (new_val) {
$("#temp_target_dial").val(new_val).trigger('change');
});
$scope.$watch(angular.bind(this, function() {
return document.getElementById("power_pcnt").innerHTML;
}), function (new_val) {
console.log("Power changed - Client", new_val, microwave.power);
if (new_val != microwave.power) {
microwave.set_power(new_val);
}
});
$scope.$watch(angular.bind(this, function() {
return this.microwave.power;
}), function (new_val, old_val) {
console.log("Power changed - Serverside", new_val, old_val);
set_power_marker(new_val);
});
$scope.$watch(angular.bind(this, function() {
return this.microwave.state;
}), function (new_val, old_val) {
console.log("State changed", old_val, "->", new_val);
if (old_val == "temperature" && new_val == "stopped") {
// Was running, now not
window.location.href = '/';
}
});
$("#temp_target_dial").trigger('configure', {"release": function(value) {
console.log("Release", value);
// TODO: Angular like this is aweful, release also trigger by angular setting value
if (microwave.target_temperature != value)
microwave.set_target_temperature(value);
}});
}]);
myApp.controller('time_ctrl', ['$routeParams', 'microwave', '$scope', function($routeParams, microwave, $scope) {
var vm = this;
this.microwave = microwave;
if ("seconds" in $routeParams) {
// If we start with seconds we are in control, kick off the process
// If we start without seconds we are just an observer
var seconds = parseInt($routeParams.seconds, 10);
microwave.set_power(100);
microwave.start_time(seconds);
}
// time_dial($("#time_dial"), this.seconds);
time_dial($("#time_dial"), 0);
draw_power_bg( document.getElementById("power_triangle") );
draw_fixed_power_marker( document.getElementById("power_marker") );
$scope.$watch(angular.bind(this, function() {
return this.microwave.time_remaining;
}), function (new_val) {
$("#time_dial").trigger('configure', {"remaining":new_val, "max":microwave.time});
});
$scope.$watch(angular.bind(this, function() {
return this.microwave.state;
}), function (new_val, old_val) {
console.log("State changed", new_val, old_val);
if (old_val == "time" && new_val == "stopped") {
// Was running, now not
window.location.href = '/'
}
});
$scope.$watch(angular.bind(this, function() {
return document.getElementById("power_pcnt").innerHTML;
}), function (new_val) {
console.log("Power changed - Client", new_val, microwave.power);
if (new_val != microwave.power) {
microwave.set_power(new_val);
}
});
$scope.$watch(angular.bind(this, function() {
return this.microwave.power;
}), function (new_val, old_val) {
console.log("Power changed - Serverside", new_val, old_val);
set_power_marker(new_val);
});
$("#time_dial").trigger('configure', {"release": function(value) {
// Value is number of points from maximum
// TODO: Allow time to be increased
var remaining = microwave.time - Math.round(value);
microwave.start_time(remaining);
}});
}]);
myApp.factory("microwave", ['$q', '$timeout', '$rootScope', function ($q, $timeout, $rootScope) {
var status = {
"temperature" : 25.5,
"power" : 100,
"state" : "disconnected",
"time" : 10.0,
"time_remaining" : null,
"target_temperature" : 80
};
var ws = new WebSocket("ws://"+window.location.host+"/control");
var transmit_queue = [];
ws.onopen = function(e) {
console.log("WS OPEN");
// Empty out the queue
while(transmit_queue.length) {
ws.send(transmit_queue.pop());
}
};
ws.onmessage = function(e) {
console.log("IN", e.data);
// Notify controllers of changes
$rootScope.$apply(function() {
// Copy contents, assigning to status doesn't update the returned object
Object.assign(status, JSON.parse(e.data));
});
};
ws.upload = function(obj) {
console.log("UPLOAD", ws.readyState, ws);
// Wrap send function
if (ws.readyState == ws.OPEN) {
console.log("SENDING", obj);
ws.send(JSON.stringify(obj));
} else {
transmit_queue.push(JSON.stringify(obj));
}
};
// TODO: Handle disconnections
// TODO: Handle pushes
// Can't track changes to the status object, $rootScope.$watch doesn't
// seem to do it for me. Use direct functions for now.
status.set_power = function (power) {
console.log("SETTING POWER", power);
ws.upload({"power" : power});
};
status.start_time = function (time_s) {
console.log("STARTING WITH TIME", time_s);
ws.upload({"state" : "time", "time" : time_s});
};
status.start_temperature = function (temperature) {
console.log("STARTING WITH TEMPERATURE", temperature);
ws.upload({"state" : "temperature", "target_temperature" : temperature});
};
status.adjust_time = function (diff_s) {
var time = status.time + diff_s;
console.log("SETTING TIME", time);
ws.upload({"time": time});
};
status.set_time_remaining = function (time) {
ws.upload({"time": time});
};
status.set_target_temperature = function (temperature) {
ws.upload({"target_temperature": temperature});
};
status.stop = function() {
ws.upload({"state" : "stopped"});
}
$rootScope.$watch(status, function(new_val, old_val) {
console.log("TAWCH Service", new_val, old_val);
}, true);
return status;
}]);
</script>
<script src="/utilities.js"></script>
</body>
</html>