-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
40 lines (30 loc) · 1.17 KB
/
node_helper.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
var NodeHelper = require('node_helper')
const fs = require('fs')
module.exports = NodeHelper.create({
requiresVersion: '2.22.0',
start: function(){
console.log('Starting NodeHelper for ' + this.name)
},
calculateUsage: function(payload) {
let usage = payload.dailyUsage.toFixed(2);
let refnum = 100;
const file = `${__dirname}/input.txt`
let laststored = fs.readFileSync(file, 'utf8')
let todaysRemaining = parseFloat(laststored) - usage
let newRemain = todaysRemaining.toFixed(2)
if(newRemain <= 0) {
let refreshNum = parseFloat(refnum - parseFloat(usage)).toFixed(2);
fs.writeFileSync(file, refreshNum.toString(), 'utf8')
newRemain = refnum;
} else {
fs.writeFileSync(file, newRemain.toString(), 'utf8')
console.log(`Writing ${newRemain} to ${file}`)
}
this.sendSocketNotification('GAS_MONITOR_SEND', newRemain.toString())
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'GAS_MONITOR_GET') {
this.calculateUsage(payload)
}
}
})