/
server.js
executable file
·82 lines (64 loc) · 1.65 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
#!/usr/bin/env node
'use strict';
/*
Process input parameters.
*/
const path = require('path');
const argv = require('yargs').
usage('Usage:' + path.basename(__filename) + ' [args]').
example(path.basename(__filename) + ' -p 8081 -c config.json', ' - executed by default').
alias('p', 'port').
describe('p', 'port').
default('p', '8081').
alias('c', 'config').
describe('c', 'config.json').
default('c', 'config.json').
alias('t', 'thingspeak').
describe('t', 'thingspeak.json').
default('t', 'thingspeak.json').
alias('d', 'dweet').
describe('d', 'dweet.json').
default('d', 'dweet.json').
help('h').
alias('h', 'help').argv;
const fs = require('fs');
const Hazyair = require('./hazyair');
let hazyair;
function close() {
hazyair.close(() => {
process.exit(0);
});
}
let config;
try {
config = fs.readFileSync(argv.config, 'utf8');
} catch(error) {
console.error(`Failed to read ${argv.config} file.`);
config = '{}';
}
config = JSON.parse(config);
let thingspeak;
try {
thingspeak = fs.readFileSync(argv.thingspeak, 'utf8');
} catch(error) {
console.error(`Failed to read ${argv.thingspeak} file.`);
thingspeak = '{}';
}
thingspeak = JSON.parse(thingspeak);
let dweet;
try {
dweet = fs.readFileSync(argv.dweet, 'utf8');
} catch(error) {
console.error(`Failed to read ${argv.dweet} file.`);
dweet = '{}';
}
dweet = JSON.parse(dweet);
hazyair = new Hazyair(config);
hazyair.thingspeak(thingspeak);
hazyair.dweet(dweet);
hazyair.start();
process.on('SIGINT', () => close());
process.on('SIGTERM', () => close());
hazyair.listen({
port: argv.port
});