-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
160 lines (137 loc) · 4.01 KB
/
handler.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"use strict";
let config = require('./config.json');
let fetch = require('node-fetch');
let Particle = require('particle-api-js');
let cheerio = require('cheerio');
let particle = new Particle();
/**
* Splits dutch direction string and translates to english (ZZO => "SSE")
* @param direction {string}
* @returns {string}
*/
function translateDirection(direction) {
const englishDirections = {
"N" : "N",
"O" : "E",
"Z" : "S",
"W" : "W"
};
return direction.split('').map(c => englishDirections[c]).join('');
}
/**
*
* Translates windspeed from M/S -> Beaufort
* @param ms {string}
* @returns {string}
*/
function getBeaufortForMS(ms) {
const speeds = {
"0": {
"speed_interval": [0.0, 0.3]
},
"1": {
"speed_interval": [0.3, 1.6]
},
"2":{
"speed_interval": [1.6, 3.3]
},
"3":{
"speed_interval": [3.4, 5.5]
},
"4":{
"speed_interval": [5.5, 8.0]
},
"5":{
"speed_interval": [8.0, 10.8]
},
"6":{
"speed_interval": [10.8, 13.9]
},
"7":{
"speed_interval": [13.9, 17.2]
},
"8":{
"speed_interval": [17.2, 20.7]
}
};
// Default to 0 beaufort
let output = "0";
Object.keys(speeds).forEach((key)=>{
let speed = speeds[key];
if (ms >= speed.speed_interval[0] && ms < speed.speed_interval[1]) {
output = key;
}
});
return output;
}
/**
* Pushes data to Particle returns with a promise which will always resolve with status-message
* @param key
* @param value
* @returns {Promise.<String>}
*/
function pushToParticle(key, value){
return particle.callFunction({ deviceId: config.DEVICE_ID, name:key, argument: value, auth: config.ACCESS_TOKEN }).then(() => {
return "Pushed " + key;
}).catch(()=>{
return 'Could not push '+ key;
});
}
/**
*
* Get's data from dutch KNMI (weather institute), and pushes to particle
*
* @param station
* @returns {Promise.<String/Err>}
*/
function getAndPushWeatherData(station){
return fetch('http://www.knmi.nl/nederland-nu/weer/waarnemingen').then(response => response.text()).then(body => {
let $ = cheerio.load(body);
// Set defaults
let dutchDirection = ""
let direction = "N";
let speed = "1";
let beaufort = "1";
// Loop through all tr's which represent the weather-stations
$("#weather table tbody tr").each( (index, tr) => {
// Get the cell's in this row
let tds = $(tr).find('td');
// The first cell (index 0) corresponds with the name of the station.
let rowStation = $(tds[0]).html();
// If it's the station we're interested in....
if (station.toUpperCase() == rowStation.toUpperCase()) {
if (tds.length == 8) {
// SUMMERTIME!
// then get the winddirection from the 5th cell (index 4)
dutchDirection = $(tds[4]).html();
// and get the speed from the 6th cell (index 5)
speed = $(tds[5]).html();
} else {
// then get the winddirection from the 6th cell (index 5)
dutchDirection = $(tds[5]).html();
// and get the speed from the 7th cell (index 6)
speed = $(tds[6]).html();
}
direction = translateDirection(dutchDirection);
beaufort = getBeaufortForMS(speed);
}
});
// Try to push our windspeed and winddirection to particle
let speedp = pushToParticle('windSpeed', beaufort);
let dirp = pushToParticle('windDir', direction);
// Return a promise which will resolve when both pushes are handled.
return Promise.all([speedp, dirp]).then(
responses => direction + ", " + beaufort + " BFT, " + speed + " m/s - " + responses[0] + ", " + responses[1]
);
});
}
module.exports.update = (event, context, callback) => {
getAndPushWeatherData(config.WEATHER_STATION).then((response)=>{
// If it succeeded, log this, and call callback with 'success' param
console.log(response);
callback(null, response);
}, (err)=>{
// If it didn't succeed, call callback with err
callback(err);
})
};