-
Notifications
You must be signed in to change notification settings - Fork 0
/
sigfox.go
301 lines (256 loc) · 7.73 KB
/
sigfox.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
package controllers
import (
"googlemaps.github.io/maps"
"net/http"
"context"
"fmt"
"github.com/IoThingsDev/api/config"
"github.com/IoThingsDev/api/helpers"
"github.com/IoThingsDev/api/models"
"github.com/IoThingsDev/api/store"
"gopkg.in/gin-gonic/gin.v1"
"log"
"strconv"
)
type SigfoxController struct {
}
func NewSigfoxController() SigfoxController {
return SigfoxController{}
}
func resolveWifiPosition(contxt *gin.Context, msg models.SigfoxMessage) (bool, models.Location) {
fmt.Print("WiFi frame: \t\t\t")
var wifiLoc models.Location
if len(msg.Data) <= 12 {
fmt.Println("Only one WiFi, frame don't resolve for privacy issues")
return false, wifiLoc
}
ssid1 := ""
for i := 0; i <= 10; i += 2 {
if i == 10 {
ssid1 += fmt.Sprint(string(msg.Data[i:i+2]))
} else {
ssid1 += fmt.Sprint(string(msg.Data[i:i+2]), ":")
}
}
ssid2 := ""
for i := 12; i <= 22; i += 2 {
if i == 22 {
ssid2 += fmt.Sprint(string(msg.Data[i:i+2]))
} else {
ssid2 += fmt.Sprint(string(msg.Data[i:i+2]), ":")
}
}
fmt.Print("SSID1: ", ssid1, "\t SSID2:", ssid2, "\t\t\t")
googleApiKey := config.GetString(contxt, "google_api_key")
c, err := maps.NewClient(maps.WithAPIKey(googleApiKey))
if err != nil {
log.Fatalf("API connection fatal error: %s", err)
}
r := &maps.GeolocationRequest{
ConsiderIP: false,
WiFiAccessPoints: []maps.WiFiAccessPoint{{
MACAddress: ssid1,
}, {
MACAddress: ssid2,
}},
}
resp, err := c.Geolocate(context.Background(), r)
if err != nil {
fmt.Println("Google Maps Geolocation Request, Position:", err)
return false, wifiLoc
}
//Else, position is resolved
fmt.Println("Google Maps Geolocation resolved")
wifiLoc.Latitude = resp.Location.Lat
wifiLoc.Longitude = resp.Location.Lng
wifiLoc.Radius = resp.Accuracy
wifiLoc.FrameNumber = msg.FrameNumber
wifiLoc.SpotIt = false
wifiLoc.GPS = false
wifiLoc.WiFi = true
fmt.Println(resp)
fmt.Println(wifiLoc)
return true, wifiLoc
}
func decodeGPSFrame(msg models.SigfoxMessage) (models.Location, float64, bool) {
fmt.Print("GPS frame: \t\t\t")
var gpsLoc models.Location
var temperature float64
var status bool
var latitude, longitude float64
var latDeg, latMin, latSec float64
var lngDeg, lngMin, lngSec float64
isNorth, isEast := false, false
if string(msg.Data[0:2]) == "4e" {
isNorth = true
}
if string(msg.Data[10:12]) == "45" {
isEast = true
}
if isNorth {
fmt.Print("N:")
} else {
fmt.Print("S:")
}
valLatDeg, _ := strconv.ParseInt(msg.Data[2:4], 16, 8)
latDeg = float64(valLatDeg)
valLatMin, _ := strconv.ParseInt(msg.Data[4:6], 16, 8)
latMin = float64(valLatMin)
valLatSec, _ := strconv.ParseInt(msg.Data[6:8], 16, 8)
latSec = float64(valLatSec)
fmt.Print(latDeg, "° ", latMin, "m ", latSec, "s\t")
latitude = float64(latDeg) + float64(latMin/60) + float64(latSec/3600)
if isEast {
fmt.Print("E:")
} else {
fmt.Print("W:")
}
valLngDeg, _ := strconv.ParseInt(msg.Data[10:12], 16, 8)
lngDeg = float64(valLngDeg)
valLngMin, _ := strconv.ParseInt(msg.Data[12:14], 16, 8)
lngMin = float64(valLngMin)
valLngSec, _ := strconv.ParseInt(msg.Data[14:16], 16, 8)
lngSec = float64(valLngSec)
fmt.Print(lngDeg, "° ", lngMin, "m ", lngSec, "s")
longitude = float64(lngDeg) + float64(lngMin/60) + float64(lngSec/3600)
fmt.Print("\t\t\t Lat: ", latitude, "\t Lng:", longitude)
// Populating returned location
gpsLoc.Latitude = latitude
gpsLoc.Longitude = longitude
gpsLoc.FrameNumber = msg.FrameNumber
gpsLoc.SpotIt = false
gpsLoc.GPS = true
gpsLoc.WiFi = false
if msg.Data[18:20] == "41" {
status = true
} else if msg.Data[18:20] == "56" {
status = false
}
temperature, err := strconv.ParseFloat(msg.Data[20:22], 64)
if err != nil {
fmt.Println("Error while converting temperature main")
}
dec, err := strconv.ParseFloat(msg.Data[22:24], 64)
if err != nil {
fmt.Println("Error while converting temperature decimal")
}
temperature += dec * 0.01
fmt.Println("\t\t", gpsLoc, "\t", temperature, '\t', status)
return gpsLoc, temperature, status
}
func (sc SigfoxController) CreateMessage(c *gin.Context) {
sigfoxMessage := &models.SigfoxMessage{}
err := c.BindJSON(sigfoxMessage)
if err != nil {
c.AbortWithError(http.StatusBadRequest, helpers.ErrorWithCode("invalid_input", "Failed to bind the body data"))
return
}
if sigfoxMessage.MesType == 3 {
computedLocation := &models.Location{}
//var computedLocation models.Location
if (string(sigfoxMessage.Data[0:2]) == "4e") || (string(sigfoxMessage.Data[0:2]) == "53") {
if string(sigfoxMessage.Data[2:4]) != "00" {
decodedGPSFrame, decodedTemperature, status := decodeGPSFrame(*sigfoxMessage)
sigfoxMessage.Data4 = decodedTemperature
if status {
sigfoxMessage.Data5 = 1
} else {
sigfoxMessage.Data5 = 0
}
computedLocation = &decodedGPSFrame
fmt.Println("Wisol GPS Frame, contaning: ", computedLocation)
computedLocation.SigfoxId = sigfoxMessage.SigfoxId
//err = store.CreateLocationWithMessage(c, computedLocation, sigfoxMessage)
err = store.CreateLocation(c, computedLocation)
fmt.Println("Computed location created")
if err != nil {
fmt.Println("Error while creating GPS computed location")
c.Error(err)
c.Abort()
return
}
} else { //No GPS, frame is empty
sigfoxMessage.Data = "No GPS: " + sigfoxMessage.Data
fmt.Println("Wisol No GPS Frame")
}
} else {
res, decodedWifiFrame := resolveWifiPosition(c, *sigfoxMessage)
if res == false {
fmt.Println("Error while resolving WiFi computed location")
return
}
computedLocation = &decodedWifiFrame
fmt.Println("Wisol WiFi Frame, contaning: ", computedLocation)
//store.CreateLocation(context.Background(), &wifiLoc)
computedLocation.SigfoxId = sigfoxMessage.SigfoxId
//err = store.CreateLocationWithMessage(c, computedLocation, sigfoxMessage)
err = store.CreateLocation(c, computedLocation)
fmt.Println("Computed location created")
if err != nil {
fmt.Println("Error while creating WiFi computed location")
c.Error(err)
c.Abort()
return
}
}
sigfoxMessage.Data1 = computedLocation.Latitude
sigfoxMessage.Data2 = computedLocation.Longitude
sigfoxMessage.Data3 = computedLocation.Radius
if computedLocation.SpotIt {
sigfoxMessage.Data4 = 1
} else if computedLocation.GPS {
sigfoxMessage.Data5 = 1
} else if computedLocation.WiFi {
sigfoxMessage.Data6 = 1
}
}
// Create message in all cases
err = store.CreateSigfoxMessage(c, sigfoxMessage)
if err != nil {
c.Error(err)
c.Abort()
return
}
c.JSON(http.StatusCreated, sigfoxMessage)
}
// Route for Sigfox Message creation without any rate limit (to import from Sigfox API quickly)
func (sc SigfoxController) ImportMessage(c *gin.Context) {
sigfoxMessage := &models.SigfoxMessage{}
err := c.BindJSON(sigfoxMessage)
if err != nil {
c.AbortWithError(http.StatusBadRequest, helpers.ErrorWithCode("invalid_input", "Failed to bind the body data"))
return
}
err = store.CreateSigfoxMessage(c, sigfoxMessage)
if err != nil {
c.Error(err)
c.Abort()
return
}
c.JSON(http.StatusCreated, sigfoxMessage)
}
// Route for Sigfox Location creation without any rate limit (to import from Sigfox API quickly)
func (lc SigfoxController) ImportLocation(c *gin.Context) {
location := &models.Location{}
err := c.BindJSON(location)
if err != nil {
c.AbortWithError(http.StatusBadRequest, helpers.ErrorWithCode("invalid_input", "Failed to bind the body data"))
return
}
err = store.CreateLocation(c, location)
if err != nil {
c.Error(err)
c.Abort()
return
}
c.JSON(http.StatusCreated, location)
}
func (sc SigfoxController) GetLastDevicesSigfoxMessages(c *gin.Context) {
messages, err := store.GetLastDevicesSigfoxMessages(c)
if err != nil {
c.Error(err)
c.Abort()
return
}
c.JSON(http.StatusOK, messages)
}