-
Notifications
You must be signed in to change notification settings - Fork 3
/
homeMeter.js
237 lines (188 loc) · 5.58 KB
/
homeMeter.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
var gpio = require('rpi-gpio');
var fs = require('fs');
var winston = require('winston');
var helpers = require('./lib/helpers');
var argv = require('minimist')(process.argv.slice(2));
if (!fs.existsSync('wiring.js')) {
console.log('Failed to locate wiring.js (Use wiring.js.dist as an example)');
process.exit(0);
}
var wiring = require('./wiring.js');
// Available runtime parameters configuration
GLOBAL.validParams = require('./parameters.js').params;
// This one displays configuration help
String.prototype.repeat = function(num) {
return new Array( num + 1 ).join( this );
}
var c = {};
var defaults = {};
var logger = null;
var fileTransport = null;
var cfgParam = require('./lib/cfgParams.js');
// Loading command line parameters
for (var name in argv) {
if (name == '_' || name.substring(0,1) == '$') continue;
var val = cfgParam.read(name, argv[name], true);
if (val !== null) {
c[name] = val;
} else {
console.log("Configuration error: invalid parameter " + name + ". See node homeMeter.js --help to see available parameters\n");
process.exit(0);
}
}
// Read file config
if (c.c) {
if (!fs.existsSync(c.c)) {
console.log("Configuration file " + c.c + " does not exist!");
process.exit(0);
}
var cfgData = JSON.parse(fs.readFileSync(c.c, 'utf8'));
for (var name in cfgData) {
var val = cfgParam.read(name, cfgData[name]);
if (val !== null) {
c[name] = val;
} else {
console.log("Configuration file: invalid parameter " + name + ". See node homeMeter.js --help to see available parameters\n");
process.exit(0);
}
}
}
//
// Logging
//
if (c.v) c.debugLevel = 'info';
if (c.vv) c.debugLevel = 'verbose';
var transports = [];
var dateFunc = function() { var d = new Date(); return d.getFullYear() + '-' + ('0' + (d.getMonth() + 1)).substr(-2) + '-' + ('0' + d.getDate()).substr(-2) + ' ' + ('0' + d.getHours()).substr(-2) + ':' + ('0' + d.getMinutes()).substr(-2) + ':' + ('0' + d.getSeconds()).substr(-2)}
// Set up console logging
if (!c.silent) {
transports.push(new (winston.transports.Console)(
{
timestamp: dateFunc,
level: c.debugLevel
}));
}
winston.level = c.debugLevel;
winston.showLevel = false;
if (c.logFile) {
fileTransport = new (winston.transports.File) (
{
filename: c.logFile,
json: false,
showLevel: false,
colorize: false,
timestamp: dateFunc,
level: c.debugLevel
});
transports.push(fileTransport);
}
logger = new (winston.Logger) ({
transports: transports
});
logger.exitOnError = false;
// Logger func
function dd(msg, level) {
if (!logger) return;
if (!msg) return;
if (!level) level = 'info';
logger.log(level, msg);
}
global.dd = dd;
//
// Applying defaults
//
for (var name in GLOBAL.validParams) {
if (validParams[name]['default'] && !c.hasOwnProperty(name)) {
c[name] = validParams[name]['default'];
defaults[name] = true;
}
}
//
// Display help screen
//
if (c.help) {
require('./lib/showHelp.js').help(GLOBAL.validParams);
process.exit(0);
}
var publishedModules = {};
//
// Main
//
for (var pin in wiring.pins) {
var module = new (require('./lib/meters/' + wiring.pins[pin].type))(wiring.pins[pin]);
gpio.setup(pin, wiring.pins[pin].pinMode, wiring.pins[pin].edgeMode);
wiring.pins[pin].module = module;
if (wiring.pins[pin].submitter) {
var idx = 'sub' + wiring.pins[pin].submitter.charAt(0).toUpperCase() + wiring.pins[pin].submitter.slice(1);
// Check for submitter
if (c && c.hasOwnProperty(idx)) {
var submitterConfig = c[idx];
module.submitter = new (require('./lib/submitters/' + wiring.pins[pin].submitter))(module, submitterConfig);
} else {
module.submitter = false;
}
}
publishedModules[wiring.pins[pin].alias] = module;
dd('Published module "' + wiring.pins[pin].type + '" as "' + wiring.pins[pin].alias + '"');
}
// Read dumped data
if (c.dumpFile && fs.existsSync(c.dumpFile) && Object.keys(publishedModules).length) {
dd('Restoring values from ' + c.dumpFile);
dumpData = JSON.parse(fs.readFileSync(c.dumpFile));
for (var alias in dumpData) {
if (alias in publishedModules) publishedModules[alias].restore(dumpData[alias]);
}
}
// Gpio pins handler
gpio.on('change', function(pin, value) {
dd('GPIO event on pin ' + pin + ' with value "' + value + '"', 'verbose');
if (pin in wiring.pins) {
if (!('triggerValue' in wiring.pins[pin]) || wiring.pins[pin].triggerValue == value) {
var dataChanged = wiring.pins[pin].module.trigger();
dd('Triggered "' + wiring.pins[pin].module.alias + '" meter');
if (dataChanged) dump();
}
}
})
// Start local web server
if (c.localServerPort) {
var server = new (require('./lib/webserver'))({
port: c.localServerPort,
data: {publishedModules: publishedModules}
});
dd('Started web server on port ' + c.localServerPort);
}
//
// Signals
//
process.on('SIGINT', function() {
dd('Got SIGINT!');
process.exit(0);
});
process.on('SIGTERM', function() {
dd('Got SIGTERM!');
process.exit(0);
});
process.on('SIGHUP', function() {
dd('Got SIGHUP');
if (fileTransport) {
dd('Rotating logs');
helpers.winstonRotate(fileTransport);
}
});
// Exit: what should I do?
process.on('exit', function(code) {
// Dump my stuff
dump();
dd('Bye!');
});
function dump() {
if (c.dumpFile) {
dd('Dumping everything to ' + c.dumpFile);
var dumpData = {};
for (var alias in publishedModules) {
dumpData[alias] = publishedModules[alias].dump();
}
fs.writeFileSync(c.dumpFile, JSON.stringify(dumpData));
}
}