-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode_helper.js
More file actions
333 lines (297 loc) · 16.1 KB
/
node_helper.js
File metadata and controls
333 lines (297 loc) · 16.1 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* Magic Mirror
* Module: MMM-Surf
*
* By PrivacyWonk
* CC BY-NC 4.0 Licensed.
*/
var NodeHelper = require('node_helper');
var request = require('request');
var moment = require('moment');
const exec = require('child_process').exec;
var helperDebug = "";
var tempTime = "";
module.exports = NodeHelper.create({
start: function() {
console.log(moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' MMM-Surf helper started ...');
//DarkSky Forecast
this.DSfetcherRunning = false;
this.DSPayload = "";
//NOAA Water Temp and Tides
this.NOAAfetcherRunning = false;
this.NOAATidePayload = "";
this.NOAATempPayload = "";
//Magicseaweed Surf Forecast
this.MAGICfetcherRunning = false;
this.magicseaweed = "";
this.started = false;
},
scheduleUpdate: function() {
var nextload = this.config.updateInterval;
tempTime = moment();
tempTime.add(nextload, 'ms');
console.log ("INITIAL LOAD - NEXT LOAD TIME: " +tempTime.format('YYYY-MM-DD >> HH:mm:ss.SSSZZ'));
var self = this;
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' UPDATE: Next update set for: ' + tempTime.format('YYYY-MM-DD >> HH:mm:ss.SSSZZ');
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
this.updateTimer = setInterval(function() {
if (self.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' UPDATE: scheduleUpdate() triggered. Fetching new data';
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
self.fetchNOAAData(self);
self.fetchDarkSky(self);
self.fetchMagicseaweedData(self);
tempTime = moment();
tempTime.add(nextload, 'ms');
console.log ("UPDATE LOAD - NEXT LOAD TIME: " +tempTime.format('YYYY-MM-DD >> HH:mm:ss.SSSZZ'));
}, nextload); //define update setInterval timer
}, //end scheduleUpdate function
/*
* build DarkSky API request
*
*/
fetchDarkSky: function() {
var self = this;
this.DSfetcherRunning = true;
var apiMessage = "";
var wulang = this.config.lang.toUpperCase();
if (wulang == "DE") {
wulang = "DL";
}
var DarkSkyURL = encodeURI(this.config.DarkSkyAPIBase + this.config.DarkSkyAPI + "/" + this.config.DarkSkyLat + "," + this.config.DarkSkyLong);
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + " HELPER: DarkSky Data API REQUEST (3): " + DarkSkyURL;
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
request({
url: DarkSkyURL,
method: 'GET'
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
//this.DSPayload = body;
DSPayload = body;
//for some reason, when inside function(error, response, body) we lose the ability to see this.config.debug...
//but with declaration of self = this...we change to self.config.debug et voila.
if (self.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER: DarkSky Data API RESPONSE (4): Received';
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
self.sendSocketNotification('DARKSKY', body);
} else {
if (self.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER: DarkSky Data API ERROR (5): ' + error;
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
}
} // end request(function())
); // end request()
this.DSfetcherRunning = false; // turn our running flag off.
}, //end fetchDarkSky
/*
*
* build NOAA API requests for Tide and Water Temp
*
*/
fetchNOAAData: function() {
var self = this;
this.NOAAfetcherRunning = true;
var apiMessage = "";
var station_id = this.config.station_id;
var noaatz = this.config.noaatz;
var todayString = moment().format('YYYYMMDD');
var tomorrowString = moment().add(1, 'day').format('YYYYMMDD');
//NOAA Water Temperature
//NOAA asks us to send the application name when making API requests
var NOAAWaterTempURL = encodeURI(this.config.NOAAapiBase + "datagetter?product=water_temperature&application=MMM-Surf&begin_date=" + todayString + "&end_date=" + tomorrowString + "&station=" + station_id + "&time_zone=" + noaatz + "&units=english&interval=h&format=json");
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + " HELPER: NOAA Water Temp API REQUEST(3): " + NOAAWaterTempURL;
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
request({
url: NOAAWaterTempURL,
method: 'GET'
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
//this.NOAATempPayload = body;
NOAATempPayload = body;
if (self.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER: NOAA Water Temp API RESULT(4): Received';
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
self.sendSocketNotification('NOAA_WATERTEMP', body);
} else {
if (self.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER: NOAA Water Temp API ERROR (5): ' + error;
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
}
} //end request(function())
); //end request() for Water Temp
//NOAA Tide Data
//NOAA asks us to send the application name when making API requests
var NOAAtideURL = encodeURI(this.config.NOAAapiBase + "datagetter?product=predictions&application=MMM-Surfer&begin_date=" + todayString + "&end_date=" + tomorrowString + "&datum=MLLW&station=" + station_id + "&time_zone=" + noaatz + "&units=english&interval=hilo&format=json");
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + " HELPER: NOAA Tide Data API REQUEST(3): " + NOAAtideURL;
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
request({
url: NOAAtideURL,
method: 'GET'
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
//this.NOAATidePayload = body;
NOAATidePayload = body;
if (self.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER: NOAA Tide Data API RESULT(4): Received';
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
self.sendSocketNotification('NOAA_TIDE_DATA', body);
} else {
if (self.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER: NOAA Tide API ERROR (5): ' + error;
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
}
} //end request(function())
); //end request() for NOAA Tides
this.NOAAfetcherRunning = false;
}, // end fetchNOAA function
fetchMagicseaweedData: function() {
var self = this;
var apiMessage = "";
this.MAGICfetcherRunning = true;
//Magicseaweed URL
var magicseaweedURL = encodeURI(this.config.MagicSeaweedAPIBase + this.config.MagicAPI + this.config.forecastEndpoint + this.config.MagicSeaweedSpotID);
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + " HELPER: Magicseaweed API REQUEST(3): " + magicseaweedURL;
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
request({
url: magicseaweedURL,
method: 'GET'
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
//this.magicseaweed = body;
magicseaweed = body;
if (self.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER: Magicseaweed API RESULT(4): Received';
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
self.sendSocketNotification('MAGICSEAWEED', body);
this.sendCachedTime = "";
sendCachedTime = moment().format('YYYY-MM-DD >> HH:mm:ss.SSSZZ');
self.sendSocketNotification('LAST_UPDATED', sendCachedTime); //send notification back to MMM-Surf.js to set update time
} else {
if (self.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER: Magicseaweed API ERROR(4): ' + error;
self.sendSocketNotification('HELPER_MESSAGE', apiMessage);
}
}
} // end request(function())
); //end Magicseaweed request
this.MAGICfetcherRunning = false;
}, //end Magicseaweed function
// ------------------ SOCKET CONFIGURATION --------------------------
socketNotificationReceived: function(notification, payload) {
var self = this;
var apiMessage = "";
if (notification === 'UPDATE_TIMER') {
this.config = payload;
if (this.started !== true) {
this.started = true;
this.scheduleUpdate();
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDHH:mm:ss.SSSZZ') + ' UPDATE: ' + notification + ': Setting update schedule';
self.sendSocketNotification('HELPER_MESSAGE', apiMessage)
}
} else {
var self = this;
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' UPDATE: scheduleUpdate already set. Next update at: '+ tempTime.format('YYYY-MM-DD >> HH:mm:ss.SSSZZ');
self.sendSocketNotification('HELPER_MESSAGE', apiMessage)
}
}
} //end UPDATE_TIMER Socket Config
if (notification === 'GET_NOAA' && this.started == false) {
// if we haven't started, go fetch data. Otherwise dont & send cached data back
this.config = payload;
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER_SOCKET(RECEIVED FROM MAIN): ' + notification + ': Fetching Data (2)';
self.sendSocketNotification('HELPER_MESSAGE', apiMessage)
}
if (!this.NOAAfetcherRunning) {
this.fetchNOAAData();
} else {
var self = this;
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER_SOCKET(ERROR)(2): ' + self.name + ': NOAAfetcherRunning = ' + this.NOAAfetcherRunning;
self.sendSocketNotification('HELPER_MESSAGE', apiMessage)
}
} // end get live data if clause
} else if (notification === 'GET_NOAA' && this.started == true) {
//send cached NOAA data back to module for new client to load
self.sendSocketNotification('LAST_UPDATED', sendCachedTime); //send notification back to MMM-Surf.js to set update time
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER_SOCKET(RECEIVED FROM MAIN): ' + notification + ': Sending cached data: NOAA_WATERTEMP'};
self.sendSocketNotification('HELPER_MESSAGE', apiMessage)
self.sendSocketNotification('NOAA_WATERTEMP', NOAATempPayload); // send previously fetched data
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER_SOCKET(RECEIVED FROM MAIN): ' + notification + ': Sending cached data: NOAA_TIDE_DATA'};
self.sendSocketNotification('HELPER_MESSAGE', apiMessage)
self.sendSocketNotification('NOAA_TIDE_DATA', NOAATidePayload); // send previously fetched data
} //end NOAA Socket Config
if (notification === 'GET_MAGIC' && this.started == false) {
// if we haven't started, go fetch data. Otherwise dont & send cached data back
this.config = payload;
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER_SOCKET(RECEIVED FROM MAIN): ' + notification + ': Fetching Data (2)';
self.sendSocketNotification('HELPER_MESSAGE', apiMessage)
}
if (!this.MAGICfetcherRunning) {
this.fetchMagicseaweedData();
} else {
var self = this;
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER_SOCKET(ERROR)(2): ' + self.name + ': MAGICfetcherRunning = ' + this.MAGICfetcherRunning;
self.sendSocketNotification('HELPER_MESSAGE', apiMessage)
}
} //end get live data if clause
} else if (notification === 'GET_MAGIC' && this.started == true) {
//send cached Magicseaweed data back to module for new client to load
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER_SOCKET(RECEIVED FROM MAIN): ' + notification + ': Sending cached data';
self.sendSocketNotification('HELPER_MESSAGE', apiMessage)
self.sendSocketNotification('MAGICSEAWEED', magicseaweed); //send previously fetched data
}
} //end Magicseaweed Socket config
if (notification === 'GET_DARKSKY' && this.started == false) {
// if we haven't started, go fetch data. Otherwise dont & send cached data back
this.config = payload;
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER_SOCKET(RECEIVED FROM MAIN): ' + notification + ': Fetching Data (2)';
self.sendSocketNotification('HELPER_MESSAGE', apiMessage)
}
if (!this.DSfetcherRunning) {
this.fetchDarkSky();
} else {
var self = this;
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER_SOCKET(ERROR)(2): ' + self.name + ': DSfetcherRunning = ' + this.DSfetcherRunning;
self.sendSocketNotification('HELPER_MESSAGE', apiMessage)
}
} //end get live data if clause
} else if (notification === 'GET_DARKSKY' && this.started == true) {
//send cached DarkSky data back to module for new client to load
if (this.config.debug === 1) {
apiMessage = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ') + ' HELPER_SOCKET(RECEIVED FROM MAIN): ' + notification + ': Sending cached data';
self.sendSocketNotification('HELPER_MESSAGE', apiMessage)
self.sendSocketNotification('DARKSKY', DSPayload); //send previously fetched data
}
} //end DARKSKY Socket config
} // end socketNotification
}); //end helper module