-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-note.go
546 lines (496 loc) · 13.2 KB
/
http-note.go
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
// Copyright 2017 Inca Roads LLC. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
// Inbound support for the routing from a note
package main
import (
"encoding/json"
"fmt"
"hash/crc32"
"io/ioutil"
"net/http"
"strings"
"time"
ttdata "github.com/Safecast/safecast-go"
"github.com/blues/note-go/note"
)
// Schemas for the different file types
type sensorTRACKER struct {
Model string `json:"sensor,omitempty"`
CPM float64 `json:"cpm,omitempty"`
USV float64 `json:"usv,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
Humidity float64 `json:"humidity,omitempty"`
Pressure float64 `json:"pressure,omitempty"`
Voltage float64 `json:"voltage,omitempty"`
Distance float64 `json:"distance,omitempty"`
Seconds float64 `json:"seconds,omitempty"`
Velocity float64 `json:"velocity,omitempty"`
Bearing float64 `json:"bearing,omitempty"`
}
type sensorBAT struct {
Voltage float64 `json:"voltage,omitempty"`
}
type sensorINA struct {
Voltage float64 `json:"voltage,omitempty"`
Current float64 `json:"current,omitempty"`
}
type sensorBME struct {
Temperature float64 `json:"temp,omitempty"`
Humidity float64 `json:"humid,omitempty"`
Pressure float64 `json:"press,omitempty"`
}
type sensorRAD struct {
CPM float64 `json:"cpm,omitempty"`
Seconds float64 `json:"secs,omitempty"`
}
type sensorAIR struct {
Pm01_0 float64 `json:"pm01_0,omitempty"`
Pm02_5 float64 `json:"pm02_5,omitempty"`
Pm10_0 float64 `json:"pm10_0,omitempty"`
Count00_30 uint32 `json:"c00_30,omitempty"`
Count00_50 uint32 `json:"c00_50,omitempty"`
Count01_00 uint32 `json:"c01_00,omitempty"`
Count02_50 uint32 `json:"c02_50,omitempty"`
Count05_00 uint32 `json:"c05_00,omitempty"`
Count10_00 uint32 `json:"c10_00,omitempty"`
CountSecs uint32 `json:"csecs,omitempty"`
Samples uint32 `json:"csamples,omitempty"`
Pm01_0cf1 *float64 `json:"pm01_0cf1,omitempty"`
Pm02_5cf1 *float64 `json:"pm02_5cf1,omitempty"`
Pm10_0cf1 *float64 `json:"pm10_0cf1,omitempty"`
Model string `json:"sensor,omitempty"`
Voltage *float64 `json:"voltage,omitempty"`
TempOLD *float64 `json:"temp,omitempty"`
HumidOLD *float64 `json:"humid,omitempty"`
PressOLD *float64 `json:"press,omitempty"`
Temp *float64 `json:"temperature,omitempty"`
Humid *float64 `json:"humidity,omitempty"`
Press *float64 `json:"pressure,omitempty"`
Charging *bool `json:"charging,omitempty"`
USB *bool `json:"usb,omitempty"`
Indoors *bool `json:"indoors,omitempty"`
CPM float64 `json:"cpm,omitempty"`
USV float64 `json:"usv,omitempty"`
}
type sensorTRACK struct {
Lat float64 `json:"lat,omitempty"`
Lon float64 `json:"lon,omitempty"`
Distance float64 `json:"distance,omitempty"`
Seconds float64 `json:"seconds,omitempty"`
Velocity float64 `json:"velocity,omitempty"`
Bearing float64 `json:"bearing,omitempty"`
}
// Handle inbound HTTP requests from individual data Notes, in test mode
func inboundWebNoteHandlerTest(rw http.ResponseWriter, req *http.Request) {
noteHandler(rw, req, true)
}
// Handle inbound HTTP requests from individual data Notes, in production mode
func inboundWebNoteHandler(rw http.ResponseWriter, req *http.Request) {
noteHandler(rw, req, false)
}
// Handle inbound HTTP requests from individual data Notes
func noteHandler(rw http.ResponseWriter, req *http.Request, testMode bool) {
var body []byte
var err error
// Count the request
stats.Count.HTTP++
// Get the remote address, and only add this to the count if it's likely from
// the internal HTTP load balancer.
remoteAddr, isReal, abusive := getRequestorIPv4(req)
if abusive {
return
}
if !isReal {
remoteAddr = "internal address"
}
transportStr := "notehub:" + remoteAddr
// Read the body as a byte array
body, err = ioutil.ReadAll(req.Body)
if err != nil {
return
}
// Exit if it's there's nothing there
if len(body) == 0 {
return
}
// Unmarshal into a notehub Event structure, and exit if badly formatted
e := note.Event{}
err = json.Unmarshal(body, &e)
if err != nil {
return
}
// Convert to Safecast data, and exit if failure
sd, upload, log, err := noteToSD(e, transportStr, testMode)
if err != nil {
fmt.Printf("NOTE ignored: %s\n%s\n", err, body)
return
}
// Display info about it
fmt.Printf("\n%s Received payload for %s from %s in %s\n", LogTime(), sd.DeviceUID, transportStr,
e.TowerLocation+" "+e.TowerCountry)
// If this is an air reading, annotate it with AQI if possible
aqiCalculate(&sd)
// Send it to the Ingest service
if upload {
go SafecastUpload(sd)
}
// Add native event data and log it
if log {
native := map[string]interface{}{}
err = json.Unmarshal(body, &native)
if err == nil {
sd.Native = &native
go SafecastLog(sd)
}
}
}
// Deterministic way to convert a Notecard DeviceUID to a Safecast DeviceID, in a way that
// reserves the low 2^20 addresses for fixed allocation as per Rob agreement (see ttnode/src/io.c)
func notecardDeviceUIDToSafecastDeviceID(notecardDeviceUID string) (safecastDeviceURN string, safecastDeviceID uint32) {
safecastDeviceURN = "note:" + notecardDeviceUID
safecastDeviceID = crc32.ChecksumIEEE([]byte(safecastDeviceURN))
if safecastDeviceID < 1048576 {
safecastDeviceID = ^safecastDeviceID
}
return
}
// ReformatFromNote reformats to our standard normalized data format
func noteToSD(e note.Event, transport string, testMode bool) (sd ttdata.SafecastData, upload bool, log bool, err error) {
// Mark it as to whether or not it is a test measurement
isTest := testMode
var dev ttdata.Dev
sd.Dev = &dev
if isTest {
sd.Dev.Test = &isTest
}
// Device movement
if e.Moved != 0 {
deviceMovedAt := time.Unix(e.Moved, 0).Format("2006-01-02T15:04:05Z")
dev.Moved = &deviceMovedAt
}
if e.Orientation != "" {
dev.Orientation = &e.Orientation
}
// Device temp & voltage
if e.Temp != 0.0 {
dev.Temp = &e.Temp
}
if e.Voltage != 0.0 {
var bat ttdata.Bat
bat.Voltage = &e.Voltage
sd.Bat = &bat
}
// Cell info
if e.Rat != "" {
dev.Rat = &e.Rat
}
if e.Bars != 0 {
dev.Bars = &e.Bars
}
// Convert to safecast device ID
sd.DeviceUID, sd.DeviceID = notecardDeviceUIDToSafecastDeviceID(e.DeviceUID)
// Serial number
sd.DeviceSN = e.DeviceSN
// Product UID is REQUIRED of anything which passed through from notehub
if e.ProductUID == "" {
err = fmt.Errorf("note: event has no product UID")
return
}
sd.DeviceClass = e.ProductUID
// Optional device contact info, for accountability
if e.DeviceContact != nil {
sd.DeviceContactName = e.DeviceContact.Name
sd.DeviceContactOrg = e.DeviceContact.Affiliation
sd.DeviceContactRole = e.DeviceContact.Role
sd.DeviceContactEmail = e.DeviceContact.Email
}
// When captured on the device
if e.When != 0 {
capturedAt := time.Unix(e.When, 0).Format("2006-01-02T15:04:05Z")
sd.CapturedAt = &capturedAt
}
// Service-related
var svc ttdata.Service
sd.Service = &svc
svc.Transport = &transport
if e.Routed != 0 {
uploadedAt := time.Unix(e.Routed, 0).Format("2006-01-02T15:04:05Z")
sd.UploadedAt = &uploadedAt
} else {
uploadedAt := NowInUTC()
svc.UploadedAt = &uploadedAt
}
// Where captured
if e.Where != "" {
var loc ttdata.Loc
sd.Loc = &loc
var lat, lon float64
lat = float64(e.WhereLat)
lon = float64(e.WhereLon)
sd.Loc.Lat = &lat
sd.Loc.Lon = &lon
sd.Loc.Olc = &e.Where
if e.WhereLocation != "" {
sd.Loc.LocName = &e.WhereLocation
}
if e.WhereCountry != "" {
sd.Loc.LocCountry = &e.WhereCountry
}
if e.WhereTimeZone != "" {
sd.Loc.LocZone = &e.WhereTimeZone
}
}
// If there's no body, bail
if e.Body == nil {
err = fmt.Errorf("note: no recognizable sensor data")
return
}
var sensorJSON []byte
sensorJSON, err = json.Marshal(e.Body)
if err != nil {
return
}
// Decompose the body with a per-notefile schema
upload = true
log = true
switch e.NotefileID {
case "_session.qo":
upload = false
// Everything in session has been captured above
return
case "_air.qo":
s := sensorAIR{}
err = json.Unmarshal(sensorJSON, &s)
if err != nil {
return
}
switch s.Model {
case "lnd712": // Airnote Radiation
var lnd ttdata.Lnd
lnd.U712 = &s.CPM
lnd.USv = &s.USV
sd.Lnd = &lnd
case "lnd7317": // Radnote, which is covered/shielded by default
var lnd ttdata.Lnd
lnd.C7318 = &s.CPM
lnd.USv = &s.USV
sd.Lnd = &lnd
default:
var pms ttdata.Pms
pms.Pm01_0 = &s.Pm01_0
pms.Pm02_5 = &s.Pm02_5
pms.Pm10_0 = &s.Pm10_0
pms.Count00_30 = &s.Count00_30
pms.Count00_50 = &s.Count00_50
pms.Count01_00 = &s.Count01_00
pms.Count02_50 = &s.Count02_50
pms.Count05_00 = &s.Count05_00
pms.Count10_00 = &s.Count10_00
pms.CountSecs = &s.CountSecs
pms.Samples = &s.Samples
pms.Pm01_0cf1 = s.Pm01_0cf1
pms.Pm02_5cf1 = s.Pm02_5cf1
pms.Pm10_0cf1 = s.Pm10_0cf1
pms.Model = &s.Model
sd.Pms = &pms
}
if s.Indoors != nil {
indoors := true
sd.Dev.Indoors = &indoors
}
if s.Voltage != nil {
var bat ttdata.Bat
bat.Voltage = s.Voltage
bat.Charging = s.Charging
bat.Line = s.USB
sd.Bat = &bat
}
if s.TempOLD != nil {
var env ttdata.Env
env.Temp = s.TempOLD
env.Humid = s.HumidOLD
env.Press = s.PressOLD
sd.Env = &env
}
if s.Temp != nil {
var env ttdata.Env
env.Temp = s.Temp
env.Humid = s.Humid
env.Press = s.Press
sd.Env = &env
}
case "_track.qo":
s := sensorTRACKER{}
err = json.Unmarshal(sensorJSON, &s)
if err != nil {
return
}
var bat ttdata.Bat
bat.Voltage = &s.Voltage
sd.Bat = &bat
var env ttdata.Env
env.Temp = &s.Temperature
if s.Humidity != 0 {
env.Humid = &s.Humidity
}
if s.Pressure != 0 {
env.Press = &s.Pressure
}
sd.Env = &env
if s.CPM > 0 {
switch s.Model {
default: // Airnote Radiation had no model field
fallthrough
case "lnd712": // Airnote Radiation
var lnd ttdata.Lnd
lnd.U712 = &s.CPM
lnd.USv = &s.USV
sd.Lnd = &lnd
case "lnd7317": // Radnote, which is covered/shielded by default
var lnd ttdata.Lnd
lnd.C7318 = &s.CPM
lnd.USv = &s.USV
sd.Lnd = &lnd
}
}
var track ttdata.Track
track.Distance = &s.Distance
var secs = uint32(s.Seconds)
track.Seconds = &secs
track.Velocity = &s.Velocity
track.Bearing = &s.Bearing
sd.Track = &track
case "bat.qo":
s := sensorBAT{}
err = json.Unmarshal(sensorJSON, &s)
if err != nil {
return
}
var bat ttdata.Bat
bat.Voltage = &s.Voltage
sd.Bat = &bat
case "bat-ina219.qo":
s := sensorINA{}
err = json.Unmarshal(sensorJSON, &s)
if err != nil {
return
}
var bat ttdata.Bat
bat.Voltage = &s.Voltage
bat.Current = &s.Current
sd.Bat = &bat
case "air-bme280.qo":
s := sensorBME{}
err = json.Unmarshal(sensorJSON, &s)
if err != nil {
return
}
var env ttdata.Env
env.Temp = &s.Temperature
env.Humid = &s.Humidity
env.Press = &s.Pressure
sd.Env = &env
case "rad1-lnd7318u.qo":
fallthrough
case "rad0-lnd7318u.qo":
s := sensorRAD{}
err = json.Unmarshal(sensorJSON, &s)
if err != nil {
return
}
var lnd ttdata.Lnd
lnd.U7318 = &s.CPM
sd.Lnd = &lnd
case "rad1-lnd7318c.qo":
fallthrough
case "rad0-lnd7318c.qo":
s := sensorRAD{}
err = json.Unmarshal(sensorJSON, &s)
if err != nil {
return
}
var lnd ttdata.Lnd
lnd.C7318 = &s.CPM
sd.Lnd = &lnd
case "rad1-lnd7128ec.qo":
fallthrough
case "rad0-lnd7128ec.qo":
s := sensorRAD{}
err = json.Unmarshal(sensorJSON, &s)
if err != nil {
return
}
var lnd ttdata.Lnd
lnd.EC7128 = &s.CPM
sd.Lnd = &lnd
case "aq0-pms7003.qo":
fallthrough
case "aq0-pms5003.qo":
s := sensorAIR{}
err = json.Unmarshal(sensorJSON, &s)
if err != nil {
return
}
var pms ttdata.Pms
pms.Pm01_0 = &s.Pm01_0
pms.Pm02_5 = &s.Pm02_5
pms.Pm10_0 = &s.Pm10_0
pms.Count00_30 = &s.Count00_30
pms.Count00_50 = &s.Count00_50
pms.Count01_00 = &s.Count01_00
pms.Count02_50 = &s.Count02_50
pms.Count05_00 = &s.Count05_00
pms.Count10_00 = &s.Count10_00
pms.CountSecs = &s.CountSecs
pms.Samples = &s.Samples
model := strings.Split(strings.Split(e.NotefileID, "-")[1], ".")[0]
pms.Model = &model
sd.Pms = &pms
case "aq1-pms7003.qo":
fallthrough
case "aq1-pms5003.qo":
s := sensorAIR{}
err = json.Unmarshal(sensorJSON, &s)
if err != nil {
return
}
var pms ttdata.Pms2
pms.Pm01_0 = &s.Pm01_0
pms.Pm02_5 = &s.Pm02_5
pms.Pm10_0 = &s.Pm10_0
pms.Count00_30 = &s.Count00_30
pms.Count00_50 = &s.Count00_50
pms.Count01_00 = &s.Count01_00
pms.Count02_50 = &s.Count02_50
pms.Count05_00 = &s.Count05_00
pms.Count10_00 = &s.Count10_00
pms.CountSecs = &s.CountSecs
pms.Samples = &s.Samples
model := strings.Split(strings.Split(e.NotefileID, "-")[1], ".")[0]
pms.Model = &model
sd.Pms2 = &pms
case "track.qo":
s := sensorTRACK{}
err = json.Unmarshal(sensorJSON, &s)
if err != nil {
return
}
var track ttdata.Track
track.Lat = &s.Lat
track.Lon = &s.Lon
track.Distance = &s.Distance
var secs = uint32(s.Seconds)
track.Seconds = &secs
track.Velocity = &s.Velocity
track.Bearing = &s.Bearing
sd.Track = &track
default:
upload = false
log = false
fmt.Printf("*** note-go: no sensor data in file %s", e.NotefileID)
return
}
// Done
return
}