-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
1046 lines (911 loc) · 32.4 KB
/
server.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var os = require('os');
var fs = require('graceful-fs');
var dgram = require('dgram');
var moment = require('moment-timezone');
var express = require('express');
var bodyParser = require('body-parser');
var staticPages = require('serve-static');
var path = require('./path');
var event = require('./event');
var hardware = require('./hardware');
var calendar = require('./calendar');
var weather = require('./weather');
var wateringindex = require('./wateringindex');
// Some of our default vars
var zoneTimer = null;
var zoneInterval = null;
var running = {};
var runqueue = [];
var currOn = 0;
var buttonTimer = null;
var lastScheduleCheck = -1;
var lastWeatherUpdateRecorded = 0;
var lastWateringIndexUpdateRecorded = 0;
var zonecount = 0;
var programcount = 0;
const rainDelayInterval = 86340000; // 1 day - 1 minute.
var rainTimer = 0;
var errorLog = function (text) {
console.log ('[ERROR] '+text);
}
var options = new Object();
process.argv.forEach(function(val, index, array) {
if (val == '--debug') {
options.debug = true;
}
});
var debugLog = function (text) {}
if (options.debug) {
debugLog = function (text) {
console.log ('[DEBUG] '+moment().format('YYYY/MM/DD HH:mm')+' '+text);
}
}
debugLog ('starting sprinkler');
///////////////////////////////////////
// LOAD THE PROGRAM CONFIGURATION
//////////////////////////////////////
// Count the number of items (protected against the worst possible case).
function resetCounts() {
if (config.zones) {
zonecount = config.zones.length;
}
else {
zonecount = 0;
}
if (config.programs) {
programcount = config.programs.length;
}
else {
programcount = 0;
}
}
function activateConfig () {
debugLog ('activating new configuration');
event.configure(config, options);
hardware.configure (hardwareConfig, config, options);
hardware.rainInterrupt (rainCallback);
hardware.buttonInterrupt (buttonCallback);
calendar.configure(config, options);
weather.configure(config, options);
wateringindex.configure(config, options);
// Calculate the real counts from the configuration we loaded.
resetCounts();
}
function saveConfig (body, activate) {
var data = JSON.stringify(body);
fs.writeFile(path.userConfig(), data, function (err) {
if (err) {
errorLog('failed to save configuration data: '+err.message);
return;
}
debugLog('Configuration saved successfully.');
config = body;
if (activate) {
activateConfig();
}
});
}
try {
var hardwareConfig = fs.readFileSync(path.hardwareConfig());
hardwareConfig = JSON.parse(hardwareConfig);
}
catch (err) {
errorLog('There has been an error loading or parsing the hardware config: '+err)
}
var config = fs.readFileSync(path.userConfig());
try {
config = JSON.parse(config);
debugLog("User configuration parsed");
activateConfig();
}
catch (err) {
errorLog('There has been an error parsing the user config: '+err)
}
if (config.on == null) {
config.on = true;
}
if (!config.weather) {
config.weather = new Object();
config.weather.enable = false;
}
if (!config.wateringindex) {
config.wateringindex = new Object();
config.wateringindex.enable = false;
}
// Now that the configuration is available, quickly declare
// a system exception catch-all, so that we can set all zones off
// before the software exits.
//
process.on('uncaughtException', function(err) {
errorLog('Caught exception: ' + err.stack);
zonesOff()
event.record({action: 'END'});
setTimeout(function(){process.exit(1)}, 1000);
});
// For testing purpose only, do not uncomment otherwise!
//
//setTimeout(function(){ thisdoesnotexist(); }, 3000);
///////////////////////////////////////
// CONFIGURE THE WEBSERVER
//////////////////////////////////////
var app = express();
app.use(staticPages(__dirname+'/public'));
app.use(bodyParser.json()); // Used when posting an edited config.
// Routes
// This URL is a way to enable/disable automatic watering completely.
app.get('/onoff', function(req, res){
if (config.on == false) {
config.on = true;
event.record({action: 'ON'});
res.json({status:'ok',hostname:os.hostname(),msg:'Watering enabled'});
} else {
config.on = false;
event.record({action: 'OFF'});
res.json({status:'ok',hostname:os.hostname(),msg:'Watering disabled'});
}
saveConfig (config);
});
app.get('/config', function(req, res){
res.json(config);
});
app.get('/config/zones', function(req, res){
res.json(config.zones);
});
app.post('/config', function(req, res){
//debugLog(req.body);
saveConfig (req.body, true);
res.json({status:'ok',msg:'config saved'});
});
app.get('/status', function(req, res){
var now = new Date().getTime();
var response = {
status:'ok',
on:config.on,
hostname:os.hostname(),
weather:{
enabled:weather.enabled(),
status:weather.status(),
updated:weather.updated(),
adjustment:weather.adjustment(),
source:'WEATHER'
},
wateringindex:{
enabled:wateringindex.enabled(),
status:wateringindex.status(),
updated:wateringindex.updated(),
adjustment:wateringindex.adjustment(),
source:wateringindex.source()
},
calendars:calendar.status(),
raindelay:config.raindelay,
running:running,
queue:runqueue
};
if ((config.raindelay) && (now < rainTimer)) {
response.raintimer = new Date(rainTimer);
}
res.json(response);
});
// This URL is to simulate the physical button.
app.get('/button', function(req, res){
buttonCallback({output: true});
if (currOn == 0) {
res.json({status:'ok',hostname:os.hostname(),msg:'No zone turned on'});
}
else {
var zone = currOn - 1;
res.json({status:'ok',hostname:os.hostname(),msg:'Current zone is '+config.zones[zone].name+' ('+zone+')'});
}
});
// This URL is a manual rain delay.
app.get('/raindelay', function(req, res){
if (! config.raindelay) {
config.raindelay = true;
saveConfig (config);
}
var now = new Date().getTime();
if (now > rainTimer) {
// There was no rain delay pending: start a new one.
rainTimer = now + rainDelayInterval;
} else {
// A rain delay is pending: extend it.
rainTimer = rainTimer + rainDelayInterval;
}
var until = new Date(rainTimer);
res.json({status:'ok',hostname:os.hostname(),msg:'Programs delayed until '+until.getDate()+'/'+(until.getMonth()+1)+'/'+until.getFullYear()});
});
// This URL is a way to enable/disable the rain delay feature.
app.get('/raindelay/:flag', function(req, res){
var old = config.raindelay;
if (req.params.flag == 'true') {
config.raindelay = true;
res.json({status:'ok',hostname:os.hostname(),msg:'Rain delay enabled'});
} else {
config.raindelay = false;
rainTimer = 0;
res.json({status:'ok',hostname:os.hostname(),msg:'Rain delay disabled'});
}
if (old != config.raindelay) {
saveConfig (config);
}
});
// This URL is a way to enable/disable the weather adjustment feature.
app.get('/weather/:flag', function(req, res){
var old = config.weather.enable;
if (req.params.flag == 'true') {
config.weather.enable = true;
res.json({status:'ok',hostname:os.hostname(),msg:'Weather adjustment enabled'});
} else {
config.weather.enable = false;
res.json({status:'ok',hostname:os.hostname(),msg:'Weather adjustment disabled'});
}
if (old != config.weather.enable) {
saveConfig (config);
}
});
// This URL is a way to enable/disable the watering index adjustment feature.
app.get('/wateringindex/:flag', function(req, res){
var old = config.wateringindex.enable;
if (req.params.flag == 'true') {
config.wateringindex.enable = true;
res.json({status:'ok',hostname:os.hostname(),msg:'Watering Index adjustment enabled'});
} else {
config.wateringindex.enable = false;
res.json({status:'ok',hostname:os.hostname(),msg:'Watering Index adjustment disabled'});
}
if (old != config.wateringindex.enable) {
saveConfig (config);
wateringindex.configure (config, options);
}
});
app.get('/refresh', function(req, res){
activateConfig();
res.json({status:'ok',hostname:os.hostname(),msg:'Refresh initiated'});
});
app.get('/history', function(req, res){
// Finding all the history for all zones
event.find({}, function (response) {
response.hostname = os.hostname();
res.json(response);
});
});
app.get('/history/latest', function(req, res){
// Finding the latest historical event
// This is a way to tell if something new has happened and
// let the client know when to ask for all events (see /history).
res.json({_id:event.latest()});
});
app.get('/system/history', function(req, res){
// Finding all the system events
event.find({action: {$nin:['START', 'END', 'CANCEL']}}, function (response) {
response.hostname = os.hostname();
res.json(response);
});
});
function retrieveProgramById (id) {
if (id.match(/C/)) {
var index = parseInt(id.substring(1));
var programs = calendar.programs();
if(index>=0 && index<programs.length){
return programs[index];
}
return null;
}
if (id.match(/L/)) {
var index = parseInt(id.substring(1));
if(index>=0 && index<config.programs.length){
return config.programs[index];
}
return null;
}
var index = parseInt(id);
if(index>=0 && index<config.programs.length){
return config.programs[index];
}
return null;
}
app.get('/program/:id/history', function(req, res){
// Finding the history's main events for this program
var program = retrieveProgramById (req.params.id);
if (program) {
event.find({program: program.name}, function (response) {
response.hostname = os.hostname();
res.json(response);
});
}
else {
errorHandler(res,''+req.params.id+' is not a valid program');
}
});
app.get('/program/:id/full/history', function(req, res){
// Finding all the history for this program
var program = retrieveProgramById (req.params.id);
if (program) {
event.find({$or:[{program:program.name}, {parent:program.name}]}, function (response) {
response.hostname = os.hostname();
res.json(response);
});
}
else {
errorHandler(res,''+req.params.id+' is not a valid program');
}
});
app.get('/program/:id/on', function(req, res){
var program = retrieveProgramById (req.params.id);
if (program) {
programOn(program);
res.json({status:'ok',hostname:os.hostname(),msg:'started program: '+program.name});
}
else {
errorHandler(res,''+req.params.id+' is not a valid program');
}
});
app.get('/zone/:id/history', function(req, res){
// Finding all the history for this zone
event.find({ zone: parseInt(req.params.id) }, function (response) {
response.hostname = os.hostname();
res.json(response);
});
});
app.get('/zone/:id/on/:seconds', function(req, res){
if(req.params.id>=0 && req.params.id<zonecount){
zoneOnManual(req.params.id,req.params.seconds);
res.json({status:'ok',hostname:os.hostname(),msg:'started zone: '+config.zones[req.params.id].name});
}
else {
errorHandler(res,''+req.params.id+' is not a valid zone')
}
});
app.get('/zone/off', function(req, res){
killQueue();
res.json({status:'ok',hostname:os.hostname(),msg:'all zones have been turned off'});
});
app.get('/calendar/programs', function(req, res){
res.json(calendar.programs());
});
app.get('/weather', function(req, res){
if (weather.status()) {
res.json({
status:'ok',
hostname:os.hostname(),
temperature:weather.temperature(),
high:weather.high(),
low:weather.low(),
humidity:weather.humidity(),
rain:weather.rain(),
rainsensor:weather.rainsensor(),
windspeed:weather.windspeed(),
winddirection:weather.winddirection(),
pressure:weather.pressure(),
dewpoint:weather.dewpoint(),
adjustment:weather.adjustment()});
} else {
res.json({status:'ok'});
}
});
app.get('/hardware/info', function(req, res){
res.json(hardware.info());
});
// End of chain: if the URL requested matches nothing, return an error.
//
app.use(function (req, res, next) {
errorLog('404 Not found - '+req.url);
res.json(404, { status: 'error', msg: 'Not found, sorry...' });
});
///////////////////////////////////////
// SCHEDULER
//////////////////////////////////////
// Analyze one watering program to see if it must be activated.
function scheduleOneProgram (program, now) {
// Eliminate immediately a program that would not start
// at this exact time of day.
if (now.format('HH:mm') != program.start) return false;
// Eliminate a program that has become obsolete.
if (program.until) {
if (program.until.isBefore(now)) return false;
}
debugLog ('Exact time of day for program '+program.name);
// Eliminate occurrences that have been excluded (either modified and
// replaced by an exception, or deleted).
if (program.exclusions) {
for (var j = 0; j < program.exclusions.length; j++) {
if (Math.abs(now.diff(program.exclusions[j])) < 60000) {
debugLog ('Program '+program.name+' is excluded today');
return false; // This occurrence was excluded.
}
}
}
// Check the date when the program starts (or started) to be active.
if (program.date) {
var date = moment(program.date+' '+program.start, 'YYYYMMDD HH:mm');
var delta = now.diff(date, 'days');
debugLog ('delta from '+date.format()+' to '+now.format()+' is '+delta);
if (delta < 0) return false; // Starts at a future date.
} else {
// No start date yet: force the program to start today.
program.date = now.format('YYYYMMDD');
delta = 0;
debugLog ('make program '+program.name+' start today');
}
// Now check if the program should be activated today.
switch (program.repeat) {
case 'weekly':
// Runs weekly, on specific days of the week.
debugLog ('Checking day for program '+program.name+' (weekly)');
if (program.days[now.day()]) {
return true;
}
break;
case 'daily':
// Runs daily, at some day interval.
debugLog ('Checking day for program '+program.name+' (daily, interval='+program.interval+', delta='+delta+')');
if ((delta % program.interval) == 0) {
return true;
}
break;;
default:
// Otherwise, this program runs at the specified date (once).
debugLog ('Checking day for program '+program.name+' (once, delta='+delta+')');
program.active = false; // Do not run it anymore.
if (delta == 0) {
return true;
}
}
return false;
}
// Go through one list of watering programs to search one to activate.
//
function scheduleProgramList (programs, now) {
if (programs == null) return;
for (var i = 0; i < programs.length; i++) {
var program = programs[i];
// Eliminate immediately a program that was disabled.
// (this also disable all associated exception programs).
if (! program.active) continue;
// Allow enabling a program for a specific season only (user-defined)
// (this also impacts all associated exception programs).
if (program.season) {
if (config.seasons) {
var giveup = false;
for (var si = 0; si < config.seasons.length; si++) {
if (config.seasons[si].name == program.season) {
if (config.seasons[si].weekly) {
if (! config.seasons[si].weekly[now.week()]) {
giveup = true;
}
} else if (config.seasons[si].monthly) {
if (! config.seasons[si].monthly[now.month()]) {
giveup = true;
}
}
break;
}
}
if (giveup) continue;
}
}
// Each exception is a non-repeat program on its own, which replaces
// the normal program's occurrence.
var launched = false;
if (program.exceptions) {
for (var j = 0; j < program.exceptions.length; j++) {
if (scheduleOneProgram(program.exceptions[j], now)) {
programOn(program.exceptions[j]);
launched = true;
break;
}
}
}
if (! launched) {
if (scheduleOneProgram(program, now)) {
programOn(program);
}
}
}
}
// Schedule all watering programs.
//
function scheduler () {
if (config.on == false) return;
var now = moment();
now.millisecond(0);
now.second(0);
var thisminute = now.minute();
if (thisminute == lastScheduleCheck) return;
lastScheduleCheck = thisminute;
// Rain sensor(s) handling.
// In this design, rain detection does not abort an active program
// on its track, it only disables launching new programs.
// We check the status of the rain sensor(s) even if the rain timer
// is armed: this pushes the timer to one day after the end of the rain.
// The rationale is that we hope that this will make the controller's
// behavior more predictable. This is just a (debatable) choice.
if (config.raindelay) {
if (hardware.rainSensor() || weather.rainsensor()) {
var nextTimer = rainDelayInterval + now;
if (nextTimer > rainTimer) {
rainTimer = nextTimer;
}
}
if (rainTimer > +now) return;
}
scheduleProgramList (config.programs, now);
scheduleProgramList (calendar.programs(), now);
}
///////////////////////////////////////
// START UP THE APP
//////////////////////////////////////
app.listen(config.webserver.port);
debugLog('Listening on port '+config.webserver.port);
// turn off all zones
killQueue();
// Add the listener for recurring program schedules
//
// The calendar programs are kept separate from the programs in config,
// so that they are not saved to config.json as a side effect.
// Another solution would be to build a list of programs to execute, separate
// from the config object, that would be an union of config and calendar.
//
// We schedule every 10s to start reasonably close to the beginning of
// a 1mn period. The scheduler is responsible for handling frequent calls.
//
setInterval(function(){
scheduler();
},10000);
// Add the listener for periodic information refresh.
//
// This does not need to be fast (here: 1mn), but each refresh function
// called is free to do nothing until a longer delay has expired.
//
setInterval(function(){
calendar.refresh();
weather.refresh();
wateringindex.refresh();
var update = weather.updated();
if (weather.status() && (update > lastWeatherUpdateRecorded)) {
event.record({action: 'UPDATE', source:'WEATHER', temperature: weather.temperature(), windspeed: weather.windspeed(), humidity: weather.humidity(), rain: weather.rain(), adjustment: weather.adjustment()});
lastWeatherUpdateRecorded = update;
}
update = wateringindex.updated();
if (wateringindex.status() && (update > lastWateringIndexUpdateRecorded)) {
event.record({action: 'UPDATE', source:wateringindex.source(), adjustment: wateringindex.adjustment()});
lastWateringIndexUpdateRecorded = update;
}
},60000);
// Start auto discovery UDP broadcast ping
//
if (config.udp == null) {
config.udp = new Object();
config.udp.port = config.webserver.port;
}
var message = new Buffer("sprinkler "+config.webserver.port);
var socket = dgram.createSocket("udp4");
// TBD: better use callback: socket.bind(config.webserver.port, function() {
socket.bind(config.udp.port);
setTimeout(function(){
socket.setBroadcast(true);
}, 3000);
setInterval(function(){
socket.send(message, 0, message.length, 41234, '255.255.255.255', function(err, bytes) {
if(err){
errorLog('cannot send periodic broadcast signature: '+err);
}
});
},6000);
// Do not remove this event: one side effect is that it ensures that
// there is always an event created, and thus we know the latest event.
//
event.record({action: 'STARTUP'});
///////////////////////////////////////
// HELPERS
//////////////////////////////////////
function errorHandler(res, msg) {
errorLog(msg);
res.json(500, { status: 'error', msg: msg });
}
function clearTimers() {
if (zoneInterval != null) {
clearInterval(zoneInterval);
zoneInterval = null;
}
if (zoneTimer != null) {
clearTimeout(zoneTimer);
zoneTimer = null;
}
}
function zoneOnManual(index,seconds) {
killQueue();
runqueue.push({zone:index,seconds:seconds,parent:null});
processQueue();
}
function programOn(program) {
debugLog ('Running program '+program.name);
if ((!program.options) || (!program.options.append)) {
killQueue();
}
// We need to clone the list of zones because we remove each item from
// the list of zones in the queue after it has run its course: without
// cloning we would destroy the list of zones in the program itself.
//
// We build the zone activation list in two phases:
// Phase 1: retrieve the list of zones, calculate the adjusted runtime.
// Phase 2: run the program as many times as necessary to cover the
// adjusted time with runs no longer than the configured pulse.
// Put the minimal pause between each iteration to cover the
// configured pause.
//
var zonecontext = new Array();
var timeremaining = 0;
for (var i = 0; i < program.zones.length; i++) {
var zone = + program.zones[i].zone;
var seconds = + program.zones[i].seconds;
zonecontext[i] = new Object();
zonecontext[i].zone = zone;
zonecontext[i].raw = seconds;
// Add the capability to disable one zone, when activated from
// a program. Keep the ability to control it manually. This is
// typically for a zone with a problem (broken pipe, leak, etc).
// This way one can avoid this zone without modifying all programs.
//
var zoneconfig = config.zones[zone];
if (zoneconfig.manual) {
event.record({action: 'SKIP', zone:zone, parent: program.name, seconds: 0});
zonecontext[i].adjusted = 0;
continue;
}
// Each zone may have its own predefined adjustment settings, or else
// use the "default" one. Use the weather adjustment only if there
// is no predefined adjustment settings for that zone and weather
// adjustment is enabled.
var source = null;
var adjusted = seconds;
var adjustname = zoneconfig.adjust;
if (adjustname == null) {
adjustname = "default";
}
var adjust = null;
if (config.adjust != null) {
for (var ai = 0; ai < config.adjust.length; ai++) {
if (config.adjust[ai].name == adjustname) {
adjust = config.adjust[ai];
}
}
}
if (adjust != null) {
// Predefined adjustments take priority.
var ratio = 100;
if (adjust.weekly != null) {
ratio = adjust.weekly[moment().week()];
source = adjustname+' (weekly)'
} else if (adjust.monthly != null) {
ratio = adjust.monthly[moment().month()];
source = adjustname+' (monthly)'
}
adjusted = Math.floor(((seconds * ratio) + 50) / 100);
} else {
if (wateringindex.enabled()) {
// Adjust the zone duration according to the watering index.
adjusted = wateringindex.adjust(seconds);
source = wateringindex.source();
} else if (weather.enabled()) {
// Adjust the zone duration according to the weather.
// Note that we do not adjust a manual activation on
// a manual zone start: the user knows what he is doing.
//
adjusted = weather.adjust(seconds);
source = "WEATHER";
}
}
timeremaining += adjusted
zonecontext[i].source = source;
zonecontext[i].adjusted = adjusted;
zonecontext[i].ratio = Math.floor((adjusted * 100) / seconds);
if (zoneconfig.pulse) {
zonecontext[i].pulse = zoneconfig.pulse;
zonecontext[i].pause = zoneconfig.pause;
} else {
zonecontext[i].pulse = zonecontext[i].adjusted
zonecontext[i].pause = 0;
}
}
// In phase 2, loop as long as there is still a zone that must be run.
//
while (timeremaining > 0) {
timeremaining = 0;
var pause = 0;
for (var i = 0; i < program.zones.length; i++) {
if (zonecontext[i].adjusted <= 0) continue;
var runtime = zonecontext[i].adjusted;
if (runtime > zonecontext[i].pulse) {
runtime = zonecontext[i].pulse;
zonecontext[i].adjusted -= runtime;
if ((zonecontext[i].adjusted < 15) &&
(zonecontext[i].adjusted < zonecontext[i].pulse)) {
// Forget the last run since it is too short.
zonecontext[i].adjusted = 0;
} else if (pause < zonecontext[i].pause) {
// There will be a next run: plan for the pause.
pause = zonecontext[i].pause;
}
} else {
zonecontext[i].adjusted = 0;
}
timeremaining += zonecontext[i].adjusted; // time left after this.
var zone = + zonecontext[i].zone;
if (zonecontext[i].source != null) {
runqueue.push({
zone:zone,
seconds:runtime,
adjust:zonecontext[i].source,
ratio:zonecontext[i].ratio,
parent:program.name});
} else {
runqueue.push({zone:zone,seconds:runtime,parent:program.name});
}
}
if (pause > 0) {
runqueue.push({zone:null,seconds:pause,parent:program.name});
}
}
var logentry = {
action: 'START',
program: program.name
};
if (wateringindex.status()) {
logentry.adjustment = wateringindex.adjustment();
logentry.source = wateringindex.source();
} else if (weather.status()) {
logentry.temperature = weather.temperature();
logentry.windspeed = weather.windspeed();
logentry.humidity = weather.humidity();
logentry.rain = weather.rain();
logentry.adjustment = weather.adjustment();
logentry.source = 'WEATHER';
}
event.record(logentry);
processQueue();
}
// Control on or off the master valve of the specified zone (if any).
//
function zoneMaster (index, on) {
if (config.zones[index].master !== undefined) {
var master = config.zones[index].master;
if ((master != index) && (master >= 0) && (master < zonecount)) {
if (on) {
// Open this master before its own master, if any
hardware.setZone (master, true);
hardware.apply();
zoneMaster (master, true);
} else {
// Close this master after its own master, if any.
zoneMaster (master, false);
hardware.setZone (master, false);
hardware.apply();
}
}
}
}
// Shut down all the zones and stop the current action.
//
function zonesOff() {
debugLog('shutting off all zones');
if (running != null) {
// if we are currently running something, log that we interrupted it.
if (running.remaining > 0) {
if(running.remaining == 1) running.remaining = 0;
var runtime = running.seconds-running.remaining;
event.record({action: 'CANCEL', zone: running.zone-0, parent: running.parent, seconds: running.seconds, runtime: runtime});
} else if (running.parent) {
event.record({action: 'CANCEL', program: running.parent});
}
running = null;
}
for(var i = 0; i < zonecount; i++){
hardware.setZone (i, false);
}
hardware.apply();
}
function killQueue() {
debugLog('clearing the queue');
zonesOff();
runqueue = [];
clearTimers();
}
function processQueue() {
// is anything in the queue?
if(runqueue.length) {
// start working on the next item in the queue
running = runqueue.shift();
if(running.seconds <= 0) {
// Skip that zone.
processQueue();
return;
}
if (running.zone == null) { // This is a pause.
zoneTimer = setTimeout(function(){
running = {parent:running.parent}; // Wait time.
processQueue();
},running.seconds*1000);
return;
}
if ((running.zone == undefined) || (running.zone < 0) || (running.zone >= zonecount)) {
// Don't process an invalid program.
errorLog('Invalid zone '+running.zone);
return;
}
if (running.adjust != null) {
event.record({action: 'START', zone:running.zone-0, parent: running.parent, seconds: running.seconds, adjust:running.adjust, ratio:running.ratio});
} else {
event.record({action: 'START', zone:running.zone-0, parent: running.parent, seconds: running.seconds});
}
running.remaining = running.seconds;
// Make sure that the zone does not open after its master (to avoid
// risks of backflow).
hardware.setZone (running.zone, true);
hardware.apply();
zoneMaster (running.zone, true);
// clear any timers that are currently running
clearTimers();
// count down the time remaining
zoneInterval = setInterval(function(){
if(running.zone != null){
running.remaining = running.remaining - 1;
}
},1000)
// start a countdown timer for the zone watering time
zoneTimer = setTimeout(function(){
// Make sure that the master does not close after the zone (to
// avoid risks of backflow).
zoneMaster (running.zone, false);
hardware.setZone (running.zone, false);
hardware.apply();
if (running.parent) {
if (runqueue.length) {
if (running.parent != runqueue[0].parent) {
event.record({action: 'END', program: running.parent});
}
} else {
event.record({action: 'END', program: running.parent});
}
}
running = {parent:running.parent}; // Wait time.