-
Notifications
You must be signed in to change notification settings - Fork 0
/
newParserVerion.go
176 lines (146 loc) · 6.31 KB
/
newParserVerion.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
package main
import(
"bytes"
"fmt"
//"reflect"
cbugorji "github.com/ugorji/go/codec"
)
type Logger struct {
Vin string
State uint64
VehicleID string
DeviceID string
LoggerID uint64 // change to primitive.ObjectID
IntegratorAccountID string
Timestamp uint64
Items map[string][]LoggerItem
Model string
}
type LoggerItem struct {
ItemValue interface{}
ItemTime uint64
}
type Location struct {
Latitude int64
Longitude int64
}
var itemIdToString = map[string]uint64 {
"VehicleSpeed": 288,
"BatteryLevel": 1600,
"GNSS": 2048,
}
func decodeCbor(in []byte) (interface{}, error) {
var returnedValue interface{}
bufferCBOR := bytes.NewBuffer(in)
err := cbugorji.NewDecoderBytes(bufferCBOR.Bytes(), new(cbugorji.CborHandle)).Decode(&returnedValue)
return returnedValue, err
}
func getValue(data interface{}) (int64) {
var intValue int64
switch v := data.(type) {
case int64:
intValue = v
case uint64:
diff := v
intValue = int64(diff)
}
return intValue
}
func GetItemNameFromItemID(value uint64) (name string) {
for k, v := range itemIdToString {
if v == value {
name = k
return
}
}
return
}
func parsingLoggerDataCbor(stream interface{}) {
// Getting records
for _, recordData := range stream.([]interface{}) {
record := recordData.([]interface{})
header, diff := record[0], record[1:]
//Parsing Header
headerValues := header.([]interface{})
state := headerValues[0].(uint64)
indexes := headerValues[1]
//timestamp := headerValues[2]
initialValues := headerValues[3:]
recordDataLogger := Logger {
Vin: "",
State: state,
VehicleID: "",
DeviceID: "",
LoggerID: 85,
IntegratorAccountID: "",
Timestamp: 001,
Items: make(map[string][]LoggerItem),
Model: "",
}
// Getting the initial values
for idx, id := range indexes.([]interface{}) {
key := GetItemNameFromItemID(id.(uint64))
loggerData := LoggerItem {
ItemTime: headerValues[2].(uint64),
}
if id.(uint64) == 2048 {
loggerData.ItemValue = Location {
Longitude: getValue(initialValues[idx].([]interface{})[0]),
Latitude: getValue(initialValues[idx].([]interface{})[1]),
}
} else {
loggerData.ItemValue = getValue(initialValues[idx])
}
recordDataLogger.Items[key] = append(recordDataLogger.Items[key], loggerData)
}
// Calculating current values
for _, sample := range diff {
itemSample := sample.([]interface{})
Id := indexes.([]interface{})[itemSample[1].(uint64)].(uint64)
itemKey := GetItemNameFromItemID(Id)
previousData := recordDataLogger.Items[itemKey][len(recordDataLogger.Items[itemKey]) - 1]
itemData := LoggerItem {
ItemTime: previousData.ItemTime + itemSample[0].(uint64),
}
if Id == 2048 {
itemData.ItemValue = Location {
Longitude: previousData.ItemValue.(Location).Longitude + getValue(itemSample[2].([]interface{})[0]),
Latitude: previousData.ItemValue.(Location).Latitude + getValue(itemSample[2].([]interface{})[1]),
}
} else {
itemData.ItemValue = previousData.ItemValue.(int64) + getValue(itemSample[2])
}
recordDataLogger.Items[itemKey] = append(recordDataLogger.Items[itemKey], itemData)
}
fmt.Println(recordDataLogger)
}
}
func main() {
cbor := []byte{
0x81, 0x97, 0x86, 0x03, 0x83, 0x19, 0x08, 0x00, 0x19, 0x01, 0x20, 0x19, 0x06, 0x40, 0x1A, 0x62, 0xD8, 0xF0,
0x0B, 0x82, 0x19, 0xCB, 0x8C, 0x19, 0xC8, 0xB0, 0x18, 0x82, 0x0C, 0x83, 0x01, 0x00, 0x82, 0x0A, 0x18, 0x64,
0x83, 0x01, 0x01, 0x24, 0x83, 0x01, 0x00, 0x82, 0x39, 0x01, 0x17, 0x19, 0x02, 0x39, 0x83, 0x01, 0x01, 0x02,
0x83, 0x01, 0x00, 0x82, 0x04, 0x29, 0x83, 0x01, 0x01, 0x0A, 0x83, 0x01, 0x00, 0x82, 0x00, 0x00, 0x83, 0x01,
0x01, 0x33, 0x83, 0x01, 0x00, 0x82, 0x39, 0x07, 0x73, 0x39, 0x06, 0x20, 0x83, 0x01, 0x01, 0x04, 0x83, 0x01,
0x02, 0x20, 0x83, 0x01, 0x00, 0x82, 0x0A, 0x18, 0x64, 0x83, 0x01, 0x01, 0x24, 0x83, 0x01, 0x00, 0x82, 0x39,
0x01, 0x17, 0x19, 0x02, 0x39, 0x83, 0x01, 0x01, 0x02, 0x83, 0x01, 0x00, 0x82, 0x04, 0x29, 0x83, 0x01, 0x01,
0x0A, 0x83, 0x01, 0x00, 0x82, 0x00, 0x00, 0x83, 0x01, 0x01, 0x33, 0x83, 0x01, 0x00, 0x82, 0x39, 0x07, 0x73,
0x39, 0x06, 0x20, 0x83, 0x01, 0x01, 0x04, 0x83, 0x01, 0x02, 0x20,
}
/*cbor := []byte{
0x82, 0x8C, 0x86, 0x03, 0x83, 0x19, 0x08, 0x00, 0x19, 0x01, 0x20, 0x19, 0x06, 0x40, 0x1A, 0x62, 0xD8, 0xF0,
0x0B, 0x82, 0x19, 0xCB, 0x8C, 0x19, 0xC8, 0xB0, 0x18, 0x82, 0x0C, 0x83, 0x01, 0x00, 0x82, 0x0A, 0x18, 0x64,
0x83, 0x01, 0x01, 0x24, 0x83, 0x01, 0x00, 0x82, 0x39, 0x01, 0x17, 0x19, 0x02, 0x39, 0x83, 0x01, 0x01, 0x02,
0x83, 0x01, 0x00, 0x82, 0x04, 0x29, 0x83, 0x01, 0x01, 0x0A, 0x83, 0x01, 0x00, 0x82, 0x00, 0x00, 0x83, 0x01,
0x01, 0x33, 0x83, 0x01, 0x00, 0x82, 0x39, 0x07, 0x73, 0x39, 0x06, 0x20, 0x83, 0x01, 0x01, 0x04, 0x83, 0x01,
0x02, 0x20, 0x87, 0x85, 0x04, 0x82, 0x19, 0x08, 0x00, 0x19, 0x06, 0x40, 0x1A, 0x62, 0xD8, 0xF0, 0x10, 0x82,
0x19, 0xCB, 0x8C, 0x19, 0xC8, 0xB0, 0x0C, 0x83, 0x01, 0x00, 0x82, 0x0A, 0x18, 0x64, 0x83, 0x01, 0x00, 0x82,
0x39, 0x01, 0x17, 0x19, 0x02, 0x39, 0x83, 0x01, 0x00, 0x82, 0x04, 0x29, 0x83, 0x01, 0x00, 0x82, 0x00, 0x00,
0x83, 0x01, 0x00, 0x82, 0x39, 0x07, 0x73, 0x39, 0x06, 0x20, 0x83, 0x01, 0x01, 0x20,
}*/
decodedCbor, err := decodeCbor(cbor)
if err != nil {
fmt.Println(err)
}
parsingLoggerDataCbor(decodedCbor)
}