-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuplinkeventadapter.mjs
85 lines (82 loc) · 2.65 KB
/
uplinkeventadapter.mjs
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
import { assert } from 'console'
// TODO: put it in a dedicated class
export class UplinkEventAdapter {
/**
* Allow the transformation of JSon object coming from Chirpstack to GeoJSON
* Allow to write to mongo
* The constructor doesn't do anything except initialization variables
* @param {object} gatewayMap gateway map (i.e stations)
*/
constructor (gatewayMap) {
this.GWMap = gatewayMap
}
/**
*
* @param {object} jsChirpEvent decoded from the protobuf
* @returns ${Array} of geoJSON object
*/
getGeoJSONFeatures (jsChirpEvent) {
// get location to check if the gateway is registered
const gwEuid = jsChirpEvent.rxInfo[0].gatewayId
if (!(gwEuid in this.GWMap)) {
application.logger.error(`Unknown gateway ${gwEuid}.`)
return null
}
const lat = this.GWMap[gwEuid].lat
const lon = this.GWMap[gwEuid].lon
// read measures
const nsTimeMs = jsChirpEvent.rxInfo[0].nsTime.seconds * 1000 + jsChirpEvent.rxInfo[0].nsTime.nanos / 1000
const gatewayId = jsChirpEvent.rxInfo[0].gatewayId
const observationDatetime = new Date(nsTimeMs)
// create the geoJson, Kano require one feature per sensor
const geoJSONArray = []
for (const key in jsChirpEvent.object.fields) {
const geoJSON = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [lon, lat]
},
properties: {
euid: jsChirpEvent.deviceInfo.devEui,
name: jsChirpEvent.deviceInfo.deviceName,
app_id: jsChirpEvent.deviceInfo.applicationId,
app_name: jsChirpEvent.deviceInfo.applicationName,
tenant_id: jsChirpEvent.deviceInfo.tenantId,
tenant_name: jsChirpEvent.deviceInfo.tenantName,
gw_euid: gatewayId
},
time: observationDatetime
}
geoJSON.properties[key] = this.getKindValue(jsChirpEvent.object.fields[key])
geoJSONArray.push(geoJSON)
}
return geoJSONArray
}
/**
* extract value from Value message (cf protobug struct.proto from google)
* @param {*} protoMessage
* @returns
*/
getKindValue (protoMessage) {
const properties = Object.getOwnPropertyNames(protoMessage)
assert(properties.length === 1)
const kind = properties[0]
switch (kind) {
case 'numberValue':
case 'stringValue':
case 'boolValue':
case 'nullValue':
return protoMessage[kind]
case 'structValue':
case 'listValue':
application.logger.warn('Not currently supported')
assert(false)
break
default:
application.logger.warn('Unsupported type:' + kind)
assert(false)
}
return null
}
}