-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
629 lines (488 loc) · 21.2 KB
/
Program.cs
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
using System.Text.Json;
using WeatherAPI;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var server = Environment.GetEnvironmentVariable("DB_ENDPOINT");
var userID = Environment.GetEnvironmentVariable("DB_USER");
var portStr = Environment.GetEnvironmentVariable("DB_PORT");
uint port = 3306; // Default mariaDB port
if (portStr != null)
{
port = uint.Parse(portStr);
}
var password = Environment.GetEnvironmentVariable("DB_PASSWORD");
var database = Environment.GetEnvironmentVariable("DB_DB");
var geocodeAPIKey = Environment.GetEnvironmentVariable("GCAPIKEY");
if (geocodeAPIKey == null)
{
Console.WriteLine("Geocode API key not provided, location endpoints will not work.");
}
var dbBuilder = new MySqlConnector.MySqlConnectionStringBuilder
{
Server = server,
UserID = userID,
Port = port,
Password = password,
Database = database,
};
app.MapGet("/", async () =>
{
/// <summary>
/// This is our main endpoint at the root of our API
/// This endpoint retrieves weather info from all devices from the last 2 hours
/// </summary>
return await QueryParser.Parse(dbBuilder, @"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE timestamp BETWEEN DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 2 HOUR) AND CURRENT_TIMESTAMP
ORDER BY timestamp ASC"
);
});
app.MapGet("/hour", async () =>
{
/// <summary>
/// This endpoint returns all weather data from the last hour
/// </summary>
return await QueryParser.Parse(dbBuilder, @"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE timestamp BETWEEN DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 HOUR) AND CURRENT_TIMESTAMP
ORDER BY timestamp ASC"
);
});
app.MapGet("/today", async () =>
{
/// <summary>
/// This endpoint returns all weather data from today (the current date)
/// </summary>
return await QueryParser.Parse(dbBuilder, @"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE cast(timestamp as date) = CURDATE()
ORDER BY timestamp ASC"
// We use CURDATE() instead CURRENT_TIMESTAMP because we only want the date and not time related stuff (timekampf <- Max made me)
);
});
app.MapGet("/yesterday", async () =>
{
/// <summary>
/// This endpoint returns all weather data from yesterday
/// </summary>
return await QueryParser.Parse(dbBuilder, @"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE cast(timestamp as date) = DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 DAY)
ORDER BY timestamp ASC"
);
});
app.MapGet("/week", async () =>
{
/// <summary>
/// This endpoint returns all weather data from the last week
/// </summary>
return await QueryParser.Parse(dbBuilder, @"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE timestamp BETWEEN DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 WEEK) AND CURRENT_TIMESTAMP
ORDER BY timestamp ASC"
);
});
app.MapGet("/fortnight", async () =>
{
/// <summary>
/// This endpoint returns all weather data from the last two weeks (also called a fortnight)
/// </summary>
return await QueryParser.Parse(dbBuilder, @"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE timestamp BETWEEN DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 2 WEEK) AND CURRENT_TIMESTAMP
ORDER BY timestamp ASC"
);
});
app.MapGet("/month", async () =>
{
/// <summary>
/// This endpoint returns all weather data from this month
/// </summary>
return await QueryParser.Parse(dbBuilder, @"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE timestamp BETWEEN DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 MONTH) AND CURRENT_TIMESTAMP
ORDER BY timestamp ASC"
);
});
app.MapGet("/year", async () =>
{
/// <summary>
/// This endpoint returns all weather data from the last year
/// </summary>
return await QueryParser.Parse(dbBuilder, @"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE timestamp BETWEEN DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 YEAR) AND CURRENT_TIMESTAMP
ORDER BY timestamp ASC"
);
});
app.MapGet("/on-date/{date}", async (DateTime date) =>
{
/// <summary>
/// This endpoint returns all weather data from a specific date
/// </summary>
/// <param name="date"></param>
var formattedDate = date.ToString("yyyy-MM-dd");
var query = string.Format(@"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE cast(timestamp as date) = '{0}' ORDER BY timestamp ASC", formattedDate);
return await QueryParser.Parse(dbBuilder, query);
});
app.MapGet("/on-timestamp/{timestamp}", async (DateTime timestamp) =>
{
/// <summary>
/// This endpoint returns all weather data from a specific timestamp
/// </summary>
/// <param name="timestamp"></param>
var formattedDate = timestamp.ToString("yyyy-MM-dd HH:mm:ss");
var query = string.Format(@"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE timestamp = '{0}'", formattedDate);
return await QueryParser.Parse(dbBuilder, query);
});
app.MapGet("/since/{timestamp}", async (DateTime timestamp) =>
{
/// <summary>
/// This endpoint returns all weather data since a specific timestamp
/// </summary>
/// <param name="timestamp"></param>
var formattedDate = timestamp.ToString("yyyy-MM-dd HH:mm:ss");
var query = string.Format(@"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE metadata.timestamp BETWEEN '{0}' AND CURRENT_TIMESTAMP ORDER BY timestamp ASC", formattedDate);
return await QueryParser.Parse(dbBuilder, query);
});
app.MapGet("/devices", async () =>
{
/// <summary>
/// This endpoint returns all devices with their id
/// This id is used for the /device endpoints
/// </summary>
return await QueryParser.GetDistinctStringColumn(dbBuilder, @"SELECT DISTINCT device FROM metadata ORDER BY device DESC");
});
app.MapGet("/gateways", async () =>
{
/// <summary>
/// This endpoint returns all gateways
/// </summary>
return await QueryParser.GetDistinctStringColumn(dbBuilder, @"SELECT DISTINCT gateway FROM metadata");
});
app.MapGet("/applications", async () =>
{
/// <summary>
/// This endpoint returns all the things network applications
/// </summary>
return await QueryParser.GetDistinctStringColumn(dbBuilder, @"SELECT DISTINCT application FROM metadata");
});
app.MapGet("/locations", async () =>
{
/// <summary>
/// This endpoint returns cities with sensors in them in the following format:
/// city_name: {
/// device_id : device_name
/// }
/// </summary>
if (geocodeAPIKey == null)
{
var errorDict = new Dictionary<string, Dictionary<string, string>>();
var errorMsgs = new Dictionary<string, string>();
errorMsgs.Add("0", "Missing Google Maps Geocode API Key");
errorDict.Add("error", errorMsgs);
return errorDict;
}
// We use an id mapped to the response the SQL query from /devices would give us
Dictionary<int, string> allDevices = await QueryParser.GetDistinctStringColumn(dbBuilder, @"SELECT DISTINCT device FROM metadata ORDER BY device DESC");
// Get all devices with their latitude and longitude
/*
The returned data will look like this:
{
"device-name" : {
"latitude": 23.23...,
"longitude": 25.323,
}
}
*/
Dictionary<string, Dictionary<string, double>> deviceLocations = await QueryParser.GetDevicesLocations(
dbBuilder, @"SELECT DISTINCT device,
latitude, longitude FROM positional
INNER JOIN metadata ON metadata.id = positional.id
ORDER BY metadata.device DESC");
// This wil store our device: city entries
Dictionary<string, Dictionary<string, string>> citiesWithDevices = new();
int deviceIndex = 0;
foreach (var deviceAndLocational in deviceLocations)
{
// Get location data from google maps api
string apiURL = string.Format("https://maps.googleapis.com/maps/api/geocode/json?latlng={0},{1}&key={2}",
deviceAndLocational.Value["latitude"], deviceAndLocational.Value["longitude"],
geocodeAPIKey);
HttpClient locationRequest = new();
Stream responseBody = await locationRequest.GetStreamAsync(apiURL);
GeocodeResponse.Root geoAPIResponse = await JsonSerializer.DeserializeAsync<GeocodeResponse.Root>(responseBody);
string cityName = geoAPIResponse.results[0].address_components[3].short_name.Split(" ")[0];
if (!citiesWithDevices.ContainsKey(cityName))
{
Dictionary<string, string> deviceInfo = new();
deviceInfo.Add(Convert.ToString(deviceIndex), deviceAndLocational.Key);
citiesWithDevices.Add(cityName, deviceInfo);
} else
{
Dictionary<string, string> deviceList = citiesWithDevices[cityName];
deviceList.Add(Convert.ToString(deviceIndex), deviceAndLocational.Key);
}
deviceIndex++;
}
return citiesWithDevices;
});
app.MapGet("/device/{deviceID}", async(int deviceID, DateTime? since) =>
{
/// <summary>
/// This endpoint returns device data from the last 24 hours by default
/// </summary>
/// <param name="timestamp"></param>
// We use an id mapped to the response the SQL query from /devices would give us
Dictionary<int, string> all_devices = await QueryParser.GetDistinctStringColumn(dbBuilder, @"SELECT DISTINCT device FROM metadata ORDER BY device DESC");
if (deviceID > all_devices.Count()) {
// deviceID is out of bounds for our list, return an empty weatherpoint list
return new List<WeatherPoint>();
}
var device = all_devices[deviceID];
// since might be null, this makes sure if since is not passed, we take the current UTC time minus two hours
DateTime assuredSince = since ?? DateTime.UtcNow.AddHours(-24);
var formattedTimestamp = assuredSince.ToString("yyyy-MM-dd HH:mm:ss");
var query = string.Format(@"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE device = '{0}' AND metadata.timestamp BETWEEN '{1}' AND CURRENT_TIMESTAMP ORDER BY timestamp ASC", device, formattedTimestamp);
// return query;
Console.WriteLine("Tried device Query: ");
Console.WriteLine(query);
return await QueryParser.Parse(dbBuilder, query);
});
app.MapGet("/initDevice/{deviceID}", async(int deviceID, DateTime? since, DateTime? till) =>
{
// We use an id mapped to the response the SQL query from /devices would give us
Dictionary<int, string> all_devices = await QueryParser.GetDistinctStringColumn(dbBuilder, @"SELECT DISTINCT device FROM metadata ORDER BY device DESC");
if (deviceID > all_devices.Count()) {
// deviceID is out of bounds for our list, return an empty weatherpoint list
return new List<WeatherPoint>();
}
var device = all_devices[deviceID];
// string[] times = span.Split('\t');
// string since = times[0];
// string till = times[1];
// in case since parameter is null, return last 2 hours
DateTime assuredSince = since ?? DateTime.UtcNow.AddHours(-2);
var formattedSince = assuredSince.ToString("yyyy-MM-dd HH:mm:ss");
// in case till parameter is null, return now
DateTime assuredtill = till ?? DateTime.UtcNow;
var formattedTill = assuredtill.ToString("yyyy-MM-dd HH:mm:ss");
var query = string.Format(
@"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE metadata.device = '{0}' AND metadata.timestamp BETWEEN '{1}' AND '{2}' ORDER BY timestamp ASC", device, formattedSince, formattedTill);
// var query = string.Format(
// @"SELECT metadata.timestamp, metadata.device, transmissional_data.snr, sensor_data.temperature, sensor_data.humidity, sensor_data.pressure, sensor_data.light_lux, sensor_data.light_log_scale, sensor_data.battery_voltage FROM metadata
// INNER JOIN sensor_data ON metadata.id = sensor_data.id
// INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
// WHERE metadata.device = '{0}' AND metadata.timestamp BETWEEN '{1}' AND '{2}' ORDER BY timestamp ASC", device, formattedSince, formattedTill);
Console.WriteLine("Tried initDevice Query: ");
Console.WriteLine(query);
return await QueryParser.Parse(dbBuilder, query);
});
app.MapGet("/device/{deviceID}/location", async (int deviceID) =>
{
if (geocodeAPIKey == null)
{
var errorDict = new Dictionary<string, string>();
errorDict.Add("error", "Missing Google Maps Geocode API Key");
return errorDict;
}
// We use an id mapped to the response the SQL query from /devices would give us
Dictionary<int, string> allDevices = await QueryParser.GetDistinctStringColumn(dbBuilder, @"SELECT DISTINCT device FROM metadata ORDER BY device DESC");
if (deviceID > allDevices.Count() || allDevices.Count() == 0) {
// deviceID is out of bounds for our list, return an empty weatherpoint list
return new Dictionary<string, string>();
}
// Figure out which device name the user specified
var device = allDevices[deviceID];
var query = string.Format(@"SELECT DISTINCT device,
latitude, longitude FROM positional
INNER JOIN metadata ON metadata.id = positional.id
WHERE device = '{0}' ORDER BY metadata.timestamp DESC", device);
Dictionary<string, Dictionary<string, double>> locations = await QueryParser.GetDevicesLocations(dbBuilder, query);
Dictionary<string, double> deviceLocation = locations[device];
string apiURL = string.Format("https://maps.googleapis.com/maps/api/geocode/json?latlng={0},{1}&key={2}", deviceLocation["latitude"], deviceLocation["longitude"], geocodeAPIKey);
HttpClient locationRequest = new();
Stream responseBody = await locationRequest.GetStreamAsync(apiURL);
GeocodeResponse.Root geoAPIResponse = await JsonSerializer.DeserializeAsync<GeocodeResponse.Root>(responseBody);
string cityName = geoAPIResponse.results[0].address_components[3].short_name.Split(" ")[0];
Dictionary<string, string> returnData = new();
returnData.Add(device, cityName);
return returnData;
});
app.MapGet("/device/{deviceID}/average-temp", async (int deviceID, DateTime? since, DateTime? until) =>
{
// We use an id mapped to the response from /devices here
// You can get the id: device list from /devices, then pass the id for the device you want
var all_devices = await QueryParser.GetDistinctStringColumn(dbBuilder, @"SELECT DISTINCT device FROM metadata ORDER BY device DESC");
if (deviceID > all_devices.Count())
{
// User requested from device that is out of bounds, just send an empty list back
return new Dictionary<string, float>();
}
var device = all_devices[deviceID];
// Return the average temp from the last hour
DateTime sinceEnsured = since ?? DateTime.UtcNow.AddHours(-12);
DateTime untilEnsured = until ?? DateTime.UtcNow;
var sinceFormatted = sinceEnsured.ToString("yyyy-MM-dd HH:mm:ss");
var untilFormatted = untilEnsured.ToString("yyyy-MM-dd HH:mm:ss");
var query = string.Format(@"SELECT SUM(temperature)/COUNT(temperature) as avr FROM metadata
INNER JOIN sensor_data ON metadata.id = sensor_data.id
WHERE device = '{0}'
AND timestamp BETWEEN '{1}' AND '{2}'", device, sinceFormatted, untilFormatted);
float averageTemp = await QueryParser.GetSingleFloatColumn(dbBuilder, query);
Dictionary<string, float> returnData = new();
returnData.Add(device, averageTemp);
return returnData;
});
app.MapGet("/devices/locations", async () =>
{
/// <summary>
/// This endpoint returns cities with sensors in them in the following format:
/// city_name: {
/// device_id : device_name
/// }
/// </summary>
if (geocodeAPIKey == null)
{
var errorDict = new List<Dictionary<string, object>>();
var errorMsgs = new Dictionary<string, string>();
errorMsgs.Add("error", "missing Google Maps Geocode API Key");
return errorDict;
}
// We use an id mapped to the response the SQL query from /devices would give us
Dictionary<int, string> allDevices = await QueryParser.GetDistinctStringColumn(dbBuilder, @"SELECT DISTINCT device FROM metadata ORDER BY device DESC");
// Get all devices with their latitude and longitude
/*
The returned data will look like this:
{
"device-name" : {
"latitude": 23.23...,
"longitude": 25.323,
}
}
*/
Dictionary<string, Dictionary<string, double>> deviceLocations = await QueryParser.GetDevicesLocations(
dbBuilder, @"SELECT DISTINCT device,
latitude, longitude FROM positional
INNER JOIN metadata ON metadata.id = positional.id
ORDER BY metadata.device DESC");
// This wil store our device: city entries
List<Dictionary<string, object>> citiesWithDevices = new();
/*
[
{
"City" : "Enschede",
"deviceID: py-saxion,
"deviceIndex" : 1
},
...
]
*/
int deviceIndex = 0;
foreach (var deviceAndLocational in deviceLocations)
{
// Get location data from google maps api
string apiURL = string.Format("https://maps.googleapis.com/maps/api/geocode/json?latlng={0},{1}&key={2}", deviceAndLocational.Value["latitude"], deviceAndLocational.Value["longitude"], geocodeAPIKey);
HttpClient locationRequest = new();
Stream responseBody = await locationRequest.GetStreamAsync(apiURL);
GeocodeResponse.Root geoAPIResponse = await JsonSerializer.DeserializeAsync<GeocodeResponse.Root>(responseBody);
string cityName = geoAPIResponse.results[0].address_components[3].short_name.Split(" ")[0];
Dictionary<string, object> cityAndDevice = new();
cityAndDevice.Add("City", cityName);
cityAndDevice.Add("deviceID", deviceAndLocational.Key);
cityAndDevice.Add("deviceNumber", deviceIndex);
citiesWithDevices.Add(cityAndDevice);
// if (!citiesWithDevices.ContainsKey(cityName))
// {
// Dictionary<string, string> deviceInfo = new();
// deviceInfo.Add(Convert.ToString(deviceIndex), deviceAndLocational.Key);
// citiesWithDevices.Add(cityName, deviceInfo);
// } else
// {
// Dictionary<string, string> deviceList = citiesWithDevices[cityName];
// deviceList.Add(Convert.ToString(deviceIndex), deviceAndLocational.Key);
// }
deviceIndex++;
}
return citiesWithDevices;
});
app.MapGet("/device/{deviceID}/latest", async (int deviceID) =>
{
var all_devices = await QueryParser.GetDistinctStringColumn(dbBuilder, @"SELECT DISTINCT device FROM metadata ORDER BY device DESC");
if (deviceID > all_devices.Count())
{
// User requested from device that is out of bounds, just send an empty list back
return new List<WeatherPoint>();
}
string device = all_devices[deviceID];
string query = string.Format(@"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE device = '{0}' and metadata.id = (SELECT max(id) FROM metadata where device = '{1}')", device, device);
return await QueryParser.Parse(dbBuilder, query);
});
app.MapGet("/latest", async () =>
{
var latest = await QueryParser.Parse(dbBuilder, @"SELECT * FROM metadata
INNER JOIN positional ON metadata.id = positional.id
INNER JOIN sensor_data ON metadata.id = sensor_data.id
INNER JOIN transmissional_data ON metadata.id = transmissional_data.id
WHERE metadata.id = (SELECT max(id) FROM metadata)");
return latest;
});
app.MapGet("/trans", async () =>
{
var trans = await QueryParser.Parse(dbBuilder,
@"SELECT device, snr, rssi, MAX(metadata.id) as id, MAX(timestamp) as collected
FROM transmissional_data, metadata
WHERE metadata.id = transmissional_data.id
GROUP BY device");
return trans;
});
app.MapGet("/battery", async () =>
{
var bat = await QueryParser.Parse(dbBuilder,
@"SELECT device, battery_voltage, MAX(metadata.id) as id, MAX(timestamp) as collected
FROM sensor_data, metadata
WHERE metadata.id = sensor_data.id
GROUP BY device");
return bat;
});
app.Run("http://0.0.0.0:5000");