This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
182 lines (169 loc) · 4.74 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
const nmea = require('@drivetech/node-nmea')
const { buildService } = require('@digicatapult/wasp-payload-processor')
const { WASP_SENSOR_TYPE } = require('./env')
const payloadProcessor =
({ logger }) =>
({ thingId, timestamp, payload }) => {
if (payload.messageType !== 'DATA') {
throw new Error(`Unexpected messageType: "${payload.messageType}"`)
}
switch (payload.appId) {
case 'BUTTON':
if (!['0', '1'].includes(payload.data)) {
throw new Error(`Unexpected button data: "${payload.data}"`)
}
return formatEvent({
thingId,
timestamp,
type: payload.data === '1' ? 'button_pressed' : 'button_released',
})
case 'FLIP':
if (!['NORMAL', 'UPSIDE_DOWN'].includes(payload.data)) {
throw new Error(`Unexpected flip data: "${payload.data}"`)
}
return formatEvent({
thingId,
timestamp,
type: payload.data === 'NORMAL' ? 'flipped_normal' : 'flipped_upside_down',
})
case 'HUMID':
return formatReading({
thingId,
timestamp,
type: 'humidity',
label: 'relative_humidity',
value: parseFloat(payload.data), // relative humidity as %
unit: '%',
})
case 'TEMP':
return formatReading({
thingId,
timestamp,
type: 'temperature',
label: 'air_temperature',
value: parseFloat(payload.data), // celsius
unit: '°C',
})
case 'AIR_PRESS':
return formatReading({
thingId,
timestamp,
type: 'pressure',
label: 'air_pressure',
value: parseFloat(payload.data) * 1000, //kPa->Pa
unit: 'Pa',
})
case 'GPS':
return formatGps({ thingId, timestamp, payload: payload.data }) // latitude, longitude, altitude
case 'LIGHT':
logger.info(`Ignoring LIGHT ${payload.appId}`)
break
case 'AIR_QUAL':
logger.info(`Ignoring AIR_QUAL ${payload.appId}`)
break
case 'RSRP':
logger.info(`Ignoring RSRP ${payload.appId}`)
break
default:
throw new Error(`Unexpected appId: ${payload.appId}`)
}
}
const formatEvent = ({ thingId, timestamp, type, details = {} }) => {
return {
events: [
{
thingId,
timestamp,
type,
details,
},
],
}
}
const formatReading = ({ thingId, timestamp, type, label, unit, value }) => {
return {
readings: [
{
dataset: {
thingId,
type,
label,
unit,
},
timestamp,
value,
},
],
}
}
const formatGps = ({ thingId, timestamp, payload: payloadData }) => {
let parsedPayload = null
// remove any whitespace as parser will invalidate payload, and line endings that will cause parsing issues
const payload = payloadData.replace(/\s/g, '')
// Thingy91 GPGGA value is not in the correct format as they do not support the geiod model, only the ellipsoid model
// so if this is the case, pad out missing value with ' ' before the checksum value
const payloadSplit = payload.split(',')
const payloadSplitLastIndex = payloadSplit.length - 1
if (payloadSplitLastIndex === 13) {
const payloadSplitLastElement = payloadSplit[payloadSplitLastIndex]
payloadSplit[payloadSplitLastIndex] = ''
payloadSplit.push(payloadSplitLastElement)
parsedPayload = nmea.parse(payloadSplit.join())
} else {
parsedPayload = nmea.parse(payload)
}
return {
readings: [
{
dataset: {
thingId,
type: 'gps',
label: 'gps_latitude',
unit: '°',
},
timestamp,
value: parsedPayload.loc.geojson.coordinates[0],
},
{
dataset: {
thingId,
type: 'gps',
label: 'gps_longitude',
unit: '°',
},
timestamp,
value: parsedPayload.loc.geojson.coordinates[1],
},
{
dataset: {
thingId,
type: 'gps',
label: 'gps_altitude',
unit: 'm',
},
timestamp,
value: parsedPayload.altitude,
},
],
events: [
{
thingId,
timestamp: timestamp,
type: 'location',
details: {
latitude: parsedPayload.loc.geojson.coordinates[0],
latitudeUnit: '°',
longitude: parsedPayload.loc.geojson.coordinates[1],
longitudeUnit: '°',
altitude: parsedPayload.altitude,
altitudeUnit: 'm',
},
},
],
}
}
const { startServer, createHttpServer } = buildService({
sensorType: WASP_SENSOR_TYPE,
payloadProcessor,
})
module.exports = { startServer, createHttpServer }