-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreset.js
105 lines (84 loc) · 2.37 KB
/
reset.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
// Copyrigth (C) Pascal Martin, 2014.
//
// NAME
//
// reset - a tool to reset all sprinklers.
//
// SYNOPSYS
//
// This tools stop all zones. It is intended to be run after the sprinkler
// software stop, either an intended stop or an unintended stop.
//
// The goal is to avoid water to flow out of control.
//
var os = require('os');
var fs = require('graceful-fs');
var moment = require('moment-timezone');
var path = require('./path');
var hardware = require('./hardware');
// Some of our default vars
var zonecount = 0;
var errorLog = function (text) {
console.log ('[ERROR] '+text);
}
var infoLog = function (text) {
console.log ('[INFO] '+text);
}
var options = new Object();
process.argv.forEach(function(val, index, array) {
if ((val == '--debug') || (val == '-d')) {
options.debug = true;
}
});
var debugLog = function (text) {}
if (options.debug) {
debugLog = function (text) {
console.log ('[DEBUG] '+moment().format('YYYY/MM/DD HH:mm')+' '+text);
}
}
infoLog ('system reset (all zones)');
///////////////////////////////////////
// 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;
}
}
function activateConfig () {
debugLog ('activating new configuration');
hardware.configure (hardwareConfig, config, options);
// Calculate the real counts from the configuration we loaded.
resetCounts();
}
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.stack);
}
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.stack);
}
///////////////////////////////////////
// START UP THE APP
//////////////////////////////////////
// Shut down all the zones.
//
for(var i = 0; i < zonecount; i++){
hardware.setZone (i, false);
}
hardware.apply();
// Give the program some time to flush out any pending action.
setTimeout(function(){process.exit(1)}, 1000);