-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrasplogger.js
79 lines (70 loc) · 2.18 KB
/
rasplogger.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
const dotenv = require('dotenv');
dotenv.config()
const CronJob = require('cron').CronJob;
const Influx = require('influxdb-nodejs');
const { program } = require('commander');
program.version('1.0.0');
/** @type {string[]} */
const permittedModules = [
'nftcollections',
'cryptogas',
'cryptohistory',
'cryptosupply',
'cryptotransactions',
'destiny',
'fitbit',
'lights',
'parking',
'pv',
'revenuesheet',
'router',
'temperature',
'thermostat',
];
program.option('-u, --url <url>', 'influx database url');
program
.command('once <module>')
.description('run rasplogger once for a specific module')
.action((module) => {
if (!permittedModules.includes(module)) {
throw Error('this module is not permitted');
}
const influxUrl = process.env.INFLUXDB_URL || program.url;
if (!influxUrl) {
throw Error(
'influxurl is required, use -u <url> or set INFLUXDB_URL env variable'
);
}
const logFunction = require(`./modules/${module}.js`);
const influxClient = new Influx(influxUrl);
logFunction(influxClient);
});
program
.command('cron <module> <frequency>')
.description('run rasplogger using cron for a specific module')
.action((module, frequency) => {
if (!permittedModules.includes(module)) {
throw Error('this module is not permitted');
}
const influxUrl = process.env.INFLUXDB_URL || program.url;
if (!influxUrl) {
throw Error(
'influxurl is required, use -u <url> or set INFLUXDB_URL env variable'
);
}
const logFunction = require(`./modules/${module}.js`);
const influxClient = new Influx(influxUrl);
const cronJob = new CronJob({
cronTime: frequency,
onTick: function () {
logFunction(influxClient);
},
start: true,
timeZone: 'Europe/Amsterdam',
});
cronJob.start();
console.log(
`${new Date().toISOString()} RaspLogger activated module ${module}`
);
});
program.parse(process.argv);