-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigimondoMeter.go
executable file
·102 lines (82 loc) · 2.8 KB
/
DigimondoMeter.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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"time"
//"strconv"
)
type digimondodata struct {
Time time.Time
MeterStatus string
MeterReading float64
}
type decodeddigimondodata struct {
DeviceEui string `json:"deviceEui"`
Seqno uint32 `json:"seqno"`
Port uint8 `json:"port"`
AppEui string `json:"appEui"`
Time string `json:"time"`
DeviceTx devicetx `json:"deviceTx,omitempty"`
GatewayRx []gatewayrx `json:"gatewayRx,omitempty"`
Data []digimondodata `json:"data,omitempty"`
}
func parsedDigimondoMeterData(receivedtime time.Time, port uint8, receiveddata string) []digimondodata {
//Input Validation
//Length should be a multiple of 6
databytes, _ := base64.StdEncoding.DecodeString(receiveddata)
if len(databytes)%4 != 0 {
return nil
}
//fmt.Println("databytes", databytes)
capacity := len(databytes) / 4
parsedvalues := make([]digimondodata, capacity)
for index := 0; index < capacity; index++ {
if int32(databytes[index*3]) == 3 {
parsedvalues[index].MeterStatus = "OK"
} else {
parsedvalues[index].MeterStatus = "Not OK"
}
fmt.Println("receiveddata", receiveddata)
dst := ByteToHex(databytes)
fmt.Println("Dec=", dst)
var sat float64
var final string
first2 := dst[2:8]
fmt.Println("first2", first2)
final = first2
fmt.Println("final", final)
sat = hex2int(final)
// strAbs=fmt.Sprint(uint32(databytes[(index*4)+1]))+fmt.Sprint(uint32(databytes[(index*4)+2]))+fmt.Sprint(uint32(databytes[(index*4)+3]))
// sat, err :=strconv.Atoi(strAbs)
// if err != nil {
// // handle error
// }
parsedvalues[index].MeterReading = sat // uint32(uint32(databytes[(index*4)+1]) << 4 uint32(databytes[(index*4)+2]) << 4 uint32(databytes[(index*4)+3]))
parsedvalues[index].Time = receivedtime.Add(time.Duration((-2)*index) * time.Hour)
}
return parsedvalues
}
func publishDigimondoMeterData(dev device, entry loradata, parsedvalues []digimondodata) {
loradatabytes, err := json.Marshal(entry)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
if !dev.RawData {
var decodeddata decodeddigimondodata
if err := json.Unmarshal([]byte(loradatabytes), &decodeddata); err != nil {
fmt.Println("Failed to encode message", err) //This error is ok as the format of data is different
}
decodeddata.Data = append(decodeddata.Data, parsedvalues...)
loradecodeddatabytes, err := json.Marshal(decodeddata)
if err != nil {
fmt.Println("Failed to encode message", err)
return
}
fmt.Println("Data sent: ", string(loradecodeddatabytes))
transferDatatoEndPoint(loradecodeddatabytes, dev)
} else {
transferDatatoEndPoint(loradatabytes, dev)
}
}