-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
80 lines (67 loc) · 2.53 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
const express = require('express')
const app = express()
const port = process.env.PORT || 5000
const fetch = require('node-fetch')
const cors = require('cors')
app.use(cors())
app.listen(port, () => console.log(`Listening on port ${port}`))
app.get('/api/get-location', async (req, res) => {
const locations = await Promise.all([
fetch(
'https://iot.hamburg.de/v1.0/Things?$filter=Datastreams/properties/serviceName%20eq%20%27HH_STA_StadtRad%27&$orderby=id&$count=true&$expand=Locations'
),
fetch(
'https://iot.hamburg.de/v1.0/Things?$skip=100&$filter=%28Datastreams%2Fproperties%2FserviceName+eq+%27HH_STA_StadtRad%27%29&$expand=Locations&$orderby=id+asc&$count=true'
),
fetch(
'https://iot.hamburg.de/v1.0/Things?$skip=200&$filter=%28Datastreams%2Fproperties%2FserviceName+eq+%27HH_STA_StadtRad%27%29&$expand=Locations&$orderby=id+asc&$count=true'
),
]).then(function (responses) {
// Get a JSON object from each of the responses
return Promise.all(
responses.map(function (response) {
return response.json()
})
)
})
const flattenLocations = locations.map((location) => location.value).flat()
const response = flattenLocations.map((location) => {
return {
station_description: location.description,
thing_id: location['@iot.id'],
}
})
res.status(200).json(response)
})
app.get('/api/get-thing/:thing_id', async (req, res) => {
const { thing_id } = req.params
const thing = await fetch(
`https://iot.hamburg.de/v1.0/Things(${thing_id})/Datastreams`
).then((res) => res.json())
const dataStream_id = thing.value[0]['@iot.id']
const getThingName = await fetch(
thing.value[0]['[email protected]']
).then((res) => res.json())
const getAvailablesBikes = await fetch(
`https://iot.hamburg.de/v1.0/Datastreams(${dataStream_id})/Observations?$top=2&$select=phenomenonTime,result&$orderby=phenomenonTime+desc`
).then((res) => res.json())
const availableBikes = getAvailablesBikes.value[0].result
const lastUpdated = getAvailablesBikes.value[0].phenomenonTime
const [lat, long] = getLocationPoints(thing)
const response = {
station_description: getThingName.description,
coordinates: [long, lat],
dataStream_id,
availableBikes,
lastUpdated,
thing_id,
}
res.status(200).json(response)
})
function getLocationPoints(thing) {
const coordinates = thing.value[0].observedArea.coordinates
const isArray = Array.isArray(coordinates[1])
if (isArray) {
return coordinates[1]
} else return coordinates
}