-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
315 lines (264 loc) · 8.52 KB
/
server.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
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
import got from 'got'
import 'dotenv/config'
import express from 'express';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
const app = express();
const port = process.env.PORT || 4000;
// const cors = require('cors')
import Database from 'better-sqlite3';
const env = process.env.NODE_ENV;
const dir = (env === 'dev') ? '' : '/storage/';
const dbPath = dir + 'ambiserve.db';
const buildPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'build');
let corsOptions = {
origin : '*'
};
let stationJson = '';
let forecastJson = '';
let isHotterOutside = false;
const db = new Database(dbPath);
const requestURL = `https://api.ambientweather.net/v1/devices?applicationKey=${process.env.AMBIENT_APP_KEY}&apiKey=${process.env.AMBIENT_API_KEY}`;
// app.use(cors(corsOptions));
//https://medium.com/jeremy-gottfrieds-tech-blog/tutorial-how-to-deploy-a-production-react-app-to-heroku-c4831dfcfa08
app.use(express.static(buildPath));
app.get('/api/current', async (req, res) => {
var endTime = Date.now() - 86400000;
var today = new Date();
today.setHours(0,0,0);
var todayStart = today.getTime() / 1000;
var lastDayData = getHistory(endTime, 288);
var tempDiff = lastDayData[0] ? (stationJson[0].lastData.tempf - lastDayData[0].tempf) : null;
var tempArray = [];
lastDayData.forEach(d => {
if(d.dateutc >= todayStart) {
tempArray.push(d.tempf);
}
})
let maxTemp = null;
let minTemp = null;
let avgTemp = null;
if(tempArray.length > 0) {
maxTemp = Math.max(...tempArray);
minTemp = Math.min(...tempArray);
avgTemp = tempArray.reduce((a, b) => a + b) / tempArray.length;
}
// console.log('todayStart: ' + today.getTime());
// console.log('endDate: ' + endTime);
// console.log(tempArray);
if(forecastJson == '') {
updateForecastData();
}
let response = {
weatherData : stationJson,
forecastData : forecastJson,
lastDay : {
"tempDiff" : tempDiff,
"maxTemp" : maxTemp,
"minTemp" : minTemp,
"avgTemp" : avgTemp
}
}
res.json(response);
});
app.get('/api/history', async (req, res) => {
var endTime = (Date.now()) - 86400000;
let prevData = getHistory(endTime, 288);
let response = {
data : prevData
}
res.json(response);
});
app.get('/', async (req, res) => {
res.sendFile(path.join(buildPath, 'index.html'));
});
async function updateStationData() {
try {
stationJson = await got(
requestURL)
.json();
console.log(stationJson);
} catch (error) {
console.log(error);
}
}
async function updateForecastData() {
forecastJson = [];
// try {
// let darkSkyData = await got(`https://api.darksky.net/forecast/${process.env.FORECAST_API_KEY}/${process.env.FORECAST_LAT_LON}`).json();
// forecastJson = {
// data : darkSkyData,
// updated : Date.now()
// }
// } catch (error) {
// console.log(error);
// }
};
async function backfillHistory()
{
//get last db entry
//compare with current timestamp
//calculate ms between (convert ms to s)
//divide by 288 to determine if multiple requests are needed
let historicalData;
let lastEntry = db.prepare(`SELECT max(dateutc) AS timestamp FROM minutedata`).get();
if(lastEntry.timestamp === null) {
historicalData = await getHistoricalData(Date.now());
insertData(historicalData);
} else {
let currentTime = Date.now();
let lastTimestamp = lastEntry.timestamp * 1000;
let timeDiff = currentTime - lastTimestamp;
if(timeDiff > 300000) {
let missingPoints = timeDiff / 300000;
if(missingPoints < 288) {
let limit = Math.floor(missingPoints);
historicalData = await getHistoricalData(currentTime, limit);
insertData(historicalData);
} else {
// let numCalls = Math.floor(missingPoints / 288);
// let limit = Math.floor(missingPoints);
let endDate = lastTimestamp + (300000 * 288);
// console.log(`Missing points: ${missingPoints}\nNum loops: ${numCalls}\nLast limit: ${lastLimit}`);
historicalData = await getHistoricalData(endDate);
insertData(historicalData);
backfillHistory();
}
}
}
}
async function getHistoricalData(endDate, limit = 288)
{
try {
let prevData = await got(
`https://api.ambientweather.net/v1/devices/${process.env.AMBIENT_DEVICE_MAC}?applicationKey=${process.env.AMBIENT_APP_KEY}&apiKey=${process.env.AMBIENT_API_KEY}&endDate=${endDate}&limit=${limit}`)
.json();
for(let e of prevData) {
let time = e.dateutc;
e.dateutc = time / 1000;
}
return prevData;
} catch (error) {
console.error('Error getting historical data!');
console.log(error);
}
}
function insertData(historicalData)
{
//https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#transactionfunction---function
let query = db.prepare(`INSERT INTO minutedata (
dateutc,
winddir,
windspeedmph,
windgustmph,
maxdailygust,
tempf,
hourlyrainin,
dailyrainin,
weeklyrainin,
monthlyrainin,
totalrainin,
baromrelin,
baromabsin,
humidity,
tempinf,
humidityin,
uv,
solarradiation,
feelslike,
dewpoint,
feelslikein,
dewpointin
) VALUES (
@dateutc,
@winddir,
@windspeedmph,
@windgustmph,
@maxdailygust,
@tempf,
@hourlyrainin,
@dailyrainin,
@weeklyrainin,
@monthlyrainin,
@totalrainin,
@baromrelin,
@baromabsin,
@humidity,
@tempinf,
@humidityin,
@uv,
@solarradiation,
@feelsLike,
@dewPoint,
@feelsLikein,
@dewPointin
)`
);
let insertTransaction = db.transaction((data) => {
for(const entry of data) {
query.run(entry)
}
});
try {
insertTransaction(historicalData);
} catch (error) {
console.log(`****** Caught error: \n ${error}`);
if(historicalData.length > 0) {
historicalData.pop();
insertData(historicalData);
}
}
}
async function updateHistoryData() {
let lastEntry = db.prepare(`SELECT max(dateutc) AS timestamp FROM minutedata`).get();
let lastTimestamp = lastEntry.timestamp * 1000;
let timeDiff = Date.now() - lastTimestamp;
let limit = Math.floor(timeDiff / 300000);
let data = await getHistoricalData(Date.now(), limit);
insertData(data);
compareValues();
}
async function compareValues() {
let entry = db.prepare(`SELECT * FROM minutedata WHERE dateutc=(SELECT max(dateutc) FROM minutedata)`).get();
let message = '';
if(!isHotterOutside && entry.tempf > entry.tempinf) {
message = `It's hotter outside (${entry.tempf}°) than inside (${entry.tempinf}°)!`;
isHotterOutside = true;
} else if (isHotterOutside && entry.tempf < entry.tempinf) {
message = `It's cooler outside (${entry.tempf}°) than inside (${entry.tempinf}°)!`;
isHotterOutside = false;
}
if(message != '') {
let notification = {
"token" : `${process.env.PUSHOVER_APP_TOKEN}`,
"user" : `${process.env.PUSHOVER_USER_KEY}`,
"message" : message
};
try {
let post =
await got.post('https://api.pushover.net/1/messages.json', {
json: notification
});
console.log(post.body);
} catch (error) {
console.log("**********\n" + error);
}
}
}
function getHistory(endTime, limit = 288) {
try {
let query = db.prepare(`SELECT * FROM minutedata WHERE dateutc >= ? LIMIT ?`);
let entries = query.all(endTime / 1000, limit) ;
console.log(entries);
return entries;
} catch (error) {
console.log(error);
}
}
updateStationData();
updateForecastData();
backfillHistory();
setInterval(updateStationData, 60000);
setInterval(updateForecastData, 3600000);
setInterval(updateHistoryData, 300000);
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));