forked from szech/mmm-weatherchart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
114 lines (90 loc) · 3.55 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
var https = require('https');
var fs = require('fs');
var del = require('del');
var request = require('request');
var NodeHelper = require("node_helper");
var HashMap = require("hashmap");
var SVG = require('svgi');
module.exports = NodeHelper.create({
start: function() {
console.log(this.name + ": Starting node helper");
},
socketNotificationReceived: function(notification, payload) {
console.log("Downloading weather map with signal: " + notification + " From URL: " + payload.domain + payload.path);
var self = this;
var success = false;
if (notification === "FETCH_MAP"){
var options = {
host: payload.domain,
path: payload.path
};
https.get(options, function (response) {
var svgFiles = payload.mmDir + 'modules/mmm-weatherchart/cache/*.svg';
var cachedFile = new Date().getTime() + '.svg';
var imagePath = '/modules/mmm-weatherchart/cache/' + cachedFile;
var imagePathAbs = payload.mmDir + imagePath.substring(1);
var incomingData = '';
response.on('data', function(chunk){
incomingData += chunk;
});
response.on('end', function(){
if(payload.customiseSVG){
var customColours = new HashMap(payload.customColours);
var customSize = new HashMap(payload.customSize);
success = self.customiseSVG(incomingData, customColours, customSize, imagePathAbs);
}
else { // just write the image
success = self.writeFile(incomingData, imagePathAbs);
}
if(success == true){
self.sendSocketNotification("MAPPED", imagePath);
//del([svgFiles]);
}
else{
console.error("Customise SVG failed, sending FAILED notification ");
self.sendSocketNotification("FAILED", false);
}
});
});
}
},
writeFile: function(data, path){
fs.writeFile(path, data, 'utf-8', function(err) {
if(err) {
console.error(err);
return false;
}
});
return true;
},
readSVG: function(svgFilepath){
var self = this;
var svgData = fs.readFileSync(svgFilepath,'utf8');
return svgData;
},
customiseSVG: function(meteogram, customColours, customSize, svgFilepath){
var self = this;
customColours.forEach(function(value, key) {
var reg = new RegExp(key,"g"); // not the safest way to do this, but #yolo
meteogram = meteogram.replace(reg, value);
});
if(customSize.size > 0) { // optional resize
customSize.forEach(function(value, key) {
var reg = new RegExp(key);
meteogram = meteogram.replace(reg, value);
});
}
if(!self.writeFile(meteogram, svgFilepath)){
return false;
}
try { // validate result (wip)
let svgi = new SVG(meteogram);
svgi.report();
}
catch (error){
console.error(error);
// return false;
}
return true;
},
});