forked from paphko/mmm-weatherchart
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode_helper.js
146 lines (108 loc) · 4.53 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
144
145
146
var http = 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("Starting node helper: " + this.name);
},
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 self = this;
var options = {
host: payload.domain,
path: payload.path
};
var imgType = '.svg';
console.log("=> http.get, options: {}, {}", options.host, options.path);
http.get(options, function (response) {
var svgFiles = payload.mmDir + 'modules/mmm-weatherchart/cache/*.svg';
var cachedFile = new Date().getTime() + imgType;
var imagePath = '/modules/mmm-weatherchart/cache/' + cachedFile;
var imagePathAbs = payload.mmDir + imagePath.substring(1);
var incomingData = '';
console.log(" http.get, vars:\n {}\n {}\n {}\n {}", svgFiles, cachedFile, imagePath, imagePathAbs);
response.on('data', function(chunk){
console.log(" ..getting svg data");
incomingData += chunk; // this probably won't work for png.
});
response.on('end', function(){
if(payload.customiseSVG){
console.log(" ..got svg data, will fiddle");
console.log("imagePath = " + imagePath);
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, '!'+imagePathAbs]);
}
else{
console.log("Customise SVG failed, sending FAILED notification ");
self.sendSocketNotification("FAILED", false);
}
});
});
}
},
writeFile: function(data, path){
console.log("writing file....");
fs.writeFile(path, data, 'utf-8', function(err) {
if(err) {
console.log(err);
return false;
}
console.log("The file was saved!");
});
return true;
},
readSVG: function(svgFilepath){
var self = this;
console.log(">> readSVG");
console.log("svgFilepath = " + svgFilepath);
var svgData = fs.readFileSync(svgFilepath,'utf8');
return svgData;
console.log("<< readSVG");
},
customiseSVG: function(meteogram, customColours, customSize, svgFilepath){
var self = this;
console.log(">> customiseSVG");
console.log("colouring in....");
customColours.forEach(function(value, key) {
console.log(key + ' ==> ' + value);
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
console.log("resizing....");
customSize.forEach(function(value, key) {
console.log(key + ' ==> ' + value);
var reg = new RegExp(key, "g");
meteogram = meteogram.replace(reg, value);
});
}
if(!self.writeFile(meteogram, svgFilepath)){
return false;
}
try { // validate result
let svgi = new SVG(meteogram);
svgi.report();
}
catch (error){
console.log(error);
// return false;
}
console.log("<< customiseSVG");
return true;
},
});