-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-ttn.go
57 lines (48 loc) · 1.47 KB
/
http-ttn.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
// 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 "/ttn" HTTP topic
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
// Handle inbound HTTP requests from TTN
func inboundWebTTNHandler(rw http.ResponseWriter, req *http.Request) {
var AppReq IncomingAppReq
var ttn UplinkMessage
stats.Count.HTTP++
body, err := ioutil.ReadAll(req.Body)
if err != nil {
fmt.Printf("Error reading HTTP request body: \n%v\n", req)
return
}
// Unmarshal the payload and extract the base64 data
err = json.Unmarshal(body, &ttn)
if err != nil {
fmt.Printf("\n*** Web TTN payload doesn't have TTN data *** %v\n%s\n\n", err, body)
return
}
// Copy fields to the app request structure
AppReq.TTNDevID = ttn.DevID
tt := time.Time(ttn.Metadata.Time)
ts := tt.UTC().Format("2006-01-02T15:04:05Z")
AppReq.GwReceivedAt = &ts
if ttn.Metadata.Longitude != 0 {
AppReq.GwLongitude = &ttn.Metadata.Longitude
AppReq.GwLatitude = &ttn.Metadata.Latitude
alt := float64(ttn.Metadata.Altitude)
AppReq.GwAltitude = &alt
}
if len(ttn.Metadata.Gateways) >= 1 {
AppReq.GwSnr = &ttn.Metadata.Gateways[0].SNR
AppReq.GwLocation = &ttn.Metadata.Gateways[0].GtwID
}
AppReq.SvTransport = "ttn-http:" + ttn.DevID
// Push it to be processed
go AppReqPushPayload(AppReq, ttn.PayloadRaw, "TTN")
stats.Count.HTTPTTN++
}