-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
143 lines (132 loc) · 4.44 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
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
var NodeHelper = require("node_helper");
const fs=require('fs')
const csv=require('csvtojson')
const moment=require('moment')
const sqlite3 = require('sqlite3').verbose();
// add require of other javascripot components here
// var xxx = require('yyy') here
module.exports = NodeHelper.create({
csv_header:"date, gold, paladium"+'\n',
// chars allowed in prices
regex:/[^0-9.]/g,
dataFolder:'/data/',
init(){
console.log("init module helper SampleModule");
},
start() {
console.log('Starting module helper:' +this.name);
},
stop(){
console.log('Stopping module helper: ' +this.name);
},
getfile() {
var self=this
const csvFilePath = this.path + this.dataFolder+this.config.datafile;
csv()
.fromFile(csvFilePath)
.then((jsonObj)=>{
// send data to [modulname].js
var metals={}
for(var metal of self.config.metals){
metals[metal]=[]
}
var x = 0
if(jsonObj.length>this.limit){
if(this.config.debug)
console.log("reducing "+jsonObj.length +"by "+this.config.limit)
x = jsonObj.length-this.config.limit;
if(this.config.debug)
console.log("start loop at "+x)
}
var i=0;
for(i=x; i< jsonObj.length; i++){
var row=jsonObj[i];
for(var metal of self.config.metals){
if(this.config.debug)
console.log("row="+JSON.stringify(row) +" metal="+metal)
if(row[metal]!=undefined){
var d = row.datum.split('.')
d=this.swapSpliceInPlace(d,0,2).join('-')
metals[metal].push({x:d , y: parseInt(row[metal])})
if(this.config.debug)
console.log(self.name+" point={x:"+row.datum + ",y:" + parseInt(row[metal])+"}");
}
}
}
if(this.config.debug)
console.log(this.name + " sending data to module + i="+i)
self.sendSocketNotification("newdata", metals);
})
},
swapSpliceInPlace(list, iA, iB){
list[iA] = list.splice(iB, 1, list[iA])[0];
return list;
},
getDbData(){
var self = this
if(this.config.debug)
console.log(this.name+" opening database="+this.path+this.dataFolder+this.config.database)
var db = new sqlite3.Database(this.path+this.dataFolder+this.config.database,sqlite3.OPEN_ONLY);
var metals={}
for(var metal of self.config.metals){
metals[metal]=[]
}
db.serialize(function() {
var stmt="select datum,"+self.config.metals.join(',')+ " from edelmetallpreise order by datum limit "+self.config.limit+" offset ((select count(*) from edelmetallpreise)-"+self.config.limit+")"
if(self.config.debug)
console.log("stmt ="+stmt)
db.all(stmt,[], (err, rows) => {
if(err){
console.log("error ="+ JSON.stringify(err))
}
else {
rows.forEach((row) => {
for(var metal of self.config.metals){
if(self.config.debug)
console.log("row="+JSON.stringify(row) +" metal="+metal)
if(row[metal]!=undefined){
metals[metal].push({x:row.datum , y: parseInt(row[metal])})
if(self.config.debug)
console.log(self.name+" point={x:"+row.datum + ",y:" + parseInt(row[metal])+"}");
}
}
})
if(self.config.debug)
console.log(self.name+" sending data to module"+JSON.stringify(metals))
self.sendSocketNotification("newdata",metals)
}
});
db.close();
});
if(self.config.debug)
console.log(self.name+" db data completed")
},
// handle messages from our module// each notification indicates a different messages
// payload is a data structure that is different per message.. up to you to design this
socketNotificationReceived(notification, payload) {
if(this.config && this.config.debug)
console.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
// if config message from module
if (notification === "CONFIG") {
// save payload config info
this.config=payload
}
// get the csv now
else if(notification === "get_prices") {
if(this.config.type=='db')
this.getDbData()
else
this.getfile()
} else if(notification=='abc'){
// create the csv file if it doesn't exist
if(!fs.existsSync(this.path + this.dataFolder+this.config.datafile)){
// create the initial file with header
fs.writeFile(this.path + this.dataFolder+this.config.datafile,this.csv_header, (error)=>{
if(error){
console.log(this.name + " unable to write prices file")
}
})
}
}
},
});