This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
235 lines (205 loc) · 6.69 KB
/
index.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
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
/* Let's set up the environment */
const configResults = require('dotenv').config();
const env = require('./src/env');
/* Error management for .ENV */
if (configResults.error) {
throw configResults.error;
}
/* Get DarkSky as a weather provider on board */
const DarkSky = require('dark-sky');
const darksky = new DarkSky(process.env.DARK_SKY_API_KEY);
/* Calculate the request interval, we don't have to be 100% precise, always err on the side of caution */
const requestInterval = Math.ceil((24 * 60 * 60 * 1000) / env('CALLS_PER_DAY', 1000));
/* Let's setup the WebThings, based on the Mozilla docs */
const {
Property,
Thing,
Value,
WebThingServer,
MultipleThings
} = require('webthing');
//const uuidv4 = require('uuid/v4');
class DSTemperature extends Thing {
constructor() {
super('Dark Sky Temperature', ['MultiLevelSensor'], 'A Multi level sensor based on the Dark Sky API');
this.level = new Value(0.0);
this.createProperties();
this.createSensorPolling();
}
createProperties() {
this.addProperty(new Property(
this,
'level',
this.level, {
'@type': 'LevelProperty',
label: 'Temperature',
type: 'number',
description: 'The current temperature in °C',
minimum: -100,
maximum: 100,
unit: '°C',
}
));
}
createSensorPolling() {
setInterval(() => {
const newLevel = DSResultCollector.currently.temperature;
this.level.notifyOfExternalUpdate(newLevel);
}, 3000);
}
}
class DSWindSpeed extends Thing {
constructor() {
super('Dark Sky Wind Speed', ['MultiLevelSensor'], 'A Multi level sensor based on the Dark Sky API');
this.level = new Value(0.0);
this.createProperties();
this.createSensorPolling();
}
createProperties() {
this.addProperty(new Property(
this,
'level',
this.level, {
'@type': 'LevelProperty',
label: 'Temperature',
type: 'number',
description: 'The current wind speed in Km/h',
minimum: 0,
maximum: 100,
unit: 'Km/h',
}
));
}
createSensorPolling() {
setInterval(() => {
const newLevel = DSResultCollector.currently.windSpeed;
this.level.notifyOfExternalUpdate(newLevel);
}, 3000);
}
}
class DSHumidity extends Thing {
constructor() {
super('Dark Sky Humidity', ['MultiLevelSensor'], 'A Multi level sensor based on the Dark Sky API');
this.level = new Value(0.0);
this.createProperties();
this.createSensorPolling();
}
createProperties() {
this.addProperty(new Property(
this,
'level',
this.level, {
'@type': 'LevelProperty',
label: 'Humidity',
type: 'number',
description: 'The current humidity in %',
minimum: 0,
maximum: 100,
unit: '%',
}
));
}
createSensorPolling() {
setInterval(() => {
const newLevel = DSResultCollector.currently.humidity * 100;
this.level.notifyOfExternalUpdate(newLevel);
}, 3000);
}
}
class DSUVIndex extends Thing {
constructor() {
super('Dark Sky UV Index', ['MultiLevelSensor'], 'A Multi level sensor based on the Dark Sky API');
this.level = new Value(0.0);
this.createProperties();
this.createSensorPolling();
}
createProperties() {
this.addProperty(new Property(
this,
'level',
this.level, {
'@type': 'LevelProperty',
label: 'UV Index',
type: 'number',
description: 'The current UV index',
minimum: 0,
maximum: 100,
unit: '',
}
));
}
createSensorPolling() {
setInterval(() => {
const newLevel = DSResultCollector.currently.uvIndex;
this.level.notifyOfExternalUpdate(newLevel);
}, 3000);
}
}
class DSPrecipitationProbability extends Thing {
constructor() {
super('Dark Sky Precipitation Probability', ['MultiLevelSensor'], 'A Multi level sensor based on the Dark Sky API');
this.level = new Value(0.0);
this.createProperties();
this.createSensorPolling();
}
createProperties() {
this.addProperty(new Property(
this,
'level',
this.level, {
'@type': 'LevelProperty',
label: 'Precipitation',
type: 'number',
description: 'The current probabilty of Precipitation in %',
minimum: 0,
maximum: 100,
unit: '%',
}
));
}
createSensorPolling() {
setInterval(() => {
const newLevel = DSResultCollector.currently.precipProbability * 100;
this.level.notifyOfExternalUpdate(newLevel);
}, 3000);
}
}
let DSResultCollector = {};
let DarkSkyRequest = () => {
darksky
.latitude(process.env.LAT) // required: latitude, string || float.
.longitude(process.env.LONG) // required: longitude, string || float.
.units(env('UNTI_TYPE', 'auto'))
.language(env('LANGUAGE', 'en')) // optional: language, string, refer to API documentation.
.exclude('hourly,minutely,daily') // optional: exclude, string || array, refer to API documentation.
.extendHourly(true) // optional: extend, boolean, refer to API documentation.
.get() // execute your get request.
.then((response) => {
DSResultCollector = response;
})
.catch((errorMsg) => {
throw errorMsg;
}); // handle your error response.
};
function runServer() {
const temp = new DSTemperature();
const wind = new DSWindSpeed();
const hum = new DSHumidity();
const uv = new DSUVIndex();
const precip = new DSPrecipitationProbability();
// If adding more than one thing, use MultipleThings() with a name.
// In the single thing case, the thing's name will be broadcast.
const server = new WebThingServer(new MultipleThings([temp, wind, precip, hum, uv],
'DarkSkyWeatherStation'),
8888);
DarkSkyRequest();
setInterval(() => {
DarkSkyRequest();
}, requestInterval);
process.on('SIGINT', () => {
server.stop();
process.exit();
});
server.start();
}
runServer();