protobin 0.0.16__tar.gz → 0.0.18__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {protobin-0.0.16 → protobin-0.0.18}/PKG-INFO +1 -1
- {protobin-0.0.16 → protobin-0.0.18}/protobin/errors.py +4 -0
- {protobin-0.0.16 → protobin-0.0.18}/protobin/fields.py +4 -1
- {protobin-0.0.16 → protobin-0.0.18}/protobin/protocol.py +31 -9
- {protobin-0.0.16 → protobin-0.0.18}/pyproject.toml +1 -1
- {protobin-0.0.16 → protobin-0.0.18}/tests/codec8.json +6 -3
- protobin-0.0.18/tests/demo.json +75 -0
- {protobin-0.0.16 → protobin-0.0.18}/tests/tests.py +40 -12
- protobin-0.0.16/tests/demo.json +0 -160
- {protobin-0.0.16 → protobin-0.0.18}/.gitignore +0 -0
- {protobin-0.0.16 → protobin-0.0.18}/LICENSE.txt +0 -0
- {protobin-0.0.16 → protobin-0.0.18}/README.md +0 -0
- {protobin-0.0.16 → protobin-0.0.18}/protobin/__init__.py +0 -0
- {protobin-0.0.16 → protobin-0.0.18}/protobin/interfaces.py +0 -0
- {protobin-0.0.16 → protobin-0.0.18}/tests/demo.yaml +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: protobin
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.18
|
|
4
4
|
Summary: Python library for encode and decode data in protobin format
|
|
5
5
|
Project-URL: Homepage, https://github.com/drmelectronic/protobin-python
|
|
6
6
|
Project-URL: Bug Tracker, https://github.com/drmelectronic/protobin-python/issues
|
|
@@ -210,10 +210,13 @@ class CharField(FieldBase):
|
|
|
210
210
|
|
|
211
211
|
def to_binary(self, val):
|
|
212
212
|
if val is None:
|
|
213
|
-
return
|
|
213
|
+
return bytes([0])
|
|
214
214
|
return val.encode('utf')
|
|
215
215
|
|
|
216
216
|
def from_binary(self, binary):
|
|
217
|
+
val = int.from_bytes(binary[:self.bytes], 'big', signed=False)
|
|
218
|
+
if val == 0:
|
|
219
|
+
return None
|
|
217
220
|
return binary[:self.bytes].decode('utf')
|
|
218
221
|
|
|
219
222
|
|
|
@@ -2,7 +2,7 @@ import json
|
|
|
2
2
|
import yaml
|
|
3
3
|
import crcmod
|
|
4
4
|
|
|
5
|
-
from protobin.errors import InputError, FormatError
|
|
5
|
+
from protobin.errors import InputError, FormatError, CRCError
|
|
6
6
|
from protobin.fields import FieldBase, FIELD_MAP
|
|
7
7
|
|
|
8
8
|
|
|
@@ -16,6 +16,7 @@ class Format:
|
|
|
16
16
|
self.input_fields = []
|
|
17
17
|
self.output_fields = []
|
|
18
18
|
self.header = format.get('header')
|
|
19
|
+
self.crc = format.get('crc', True)
|
|
19
20
|
if server is None:
|
|
20
21
|
input_mode = 'fields'
|
|
21
22
|
output_mode = 'fields'
|
|
@@ -55,6 +56,7 @@ class Format:
|
|
|
55
56
|
class Protocol:
|
|
56
57
|
crc16 = None
|
|
57
58
|
crc_byteorder = None
|
|
59
|
+
crc_size = 2
|
|
58
60
|
|
|
59
61
|
def __init__(self, server: bool = None, file=None, js=None):
|
|
60
62
|
self.server = server
|
|
@@ -73,6 +75,7 @@ class Protocol:
|
|
|
73
75
|
def load_format(self, js):
|
|
74
76
|
if 'crc' in js:
|
|
75
77
|
self.config_crc(js['crc'])
|
|
78
|
+
self.length = js['length']
|
|
76
79
|
formats = js['formats']
|
|
77
80
|
self.formats = {}
|
|
78
81
|
for name in formats.keys():
|
|
@@ -86,10 +89,14 @@ class Protocol:
|
|
|
86
89
|
def get_format(self, h):
|
|
87
90
|
return self.formats[self.headers[h.decode('utf')]]
|
|
88
91
|
|
|
89
|
-
def encode(self, data,
|
|
90
|
-
if
|
|
91
|
-
raise InputError(f'{
|
|
92
|
-
|
|
92
|
+
def encode(self, data, format_key):
|
|
93
|
+
if format_key not in self.formats:
|
|
94
|
+
raise InputError(f'{format_key} is not available format, these are the all availables formats {self.formats.keys()}')
|
|
95
|
+
format = self.formats[format_key]
|
|
96
|
+
binary = format.encode(data)
|
|
97
|
+
if format.crc and self.crc16:
|
|
98
|
+
binary = len(binary).to_bytes(self.length, 'big', signed=False) + binary + self.get_crc(binary)
|
|
99
|
+
return binary
|
|
93
100
|
|
|
94
101
|
def decode(self, binary, codec=None):
|
|
95
102
|
if codec is None:
|
|
@@ -97,15 +104,30 @@ class Protocol:
|
|
|
97
104
|
h = binary[0:n]
|
|
98
105
|
binary = binary[n + 1:]
|
|
99
106
|
format = self.get_format(h)
|
|
100
|
-
|
|
101
|
-
|
|
107
|
+
else:
|
|
108
|
+
format = self.formats[codec]
|
|
109
|
+
|
|
110
|
+
if format.crc and self.crc16:
|
|
111
|
+
length = int.from_bytes(binary[:self.length], 'big', signed=False)
|
|
112
|
+
clean_binary = binary[self.length:length + self.length]
|
|
113
|
+
crc = binary[length + self.length: length + self.length + self.crc_size]
|
|
114
|
+
crc_value = int.from_bytes(crc, self.crc_byteorder, signed=False)
|
|
115
|
+
print('trama', clean_binary, 'crc', crc)
|
|
116
|
+
if not crc:
|
|
117
|
+
raise CRCError(f'There is no CRC')
|
|
118
|
+
elif crc_value != self.crc16(clean_binary):
|
|
119
|
+
raise CRCError(f'CRC no coincide {crc} != {self.get_crc(clean_binary)}')
|
|
120
|
+
binary = clean_binary
|
|
102
121
|
data = format.decode(binary)
|
|
122
|
+
if codec is None:
|
|
123
|
+
return format.name, data
|
|
103
124
|
return data
|
|
104
125
|
|
|
105
126
|
def config_crc(self, js):
|
|
106
127
|
self.crc16 = crcmod.mkCrcFun(int(js['poly'], 16), int(js['init'], 16), js['reverse'])
|
|
107
128
|
self.crc_byteorder = js['byte_order']
|
|
129
|
+
if 'size' in js:
|
|
130
|
+
self.crc_size = js['size']
|
|
108
131
|
|
|
109
132
|
def get_crc(self, binary):
|
|
110
|
-
|
|
111
|
-
return self.crc16(binary)
|
|
133
|
+
return self.crc16(binary).to_bytes(self.length, byteorder=self.crc_byteorder)
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
2
|
+
"length": 4,
|
|
3
|
+
"crc": {"poly": "0x18005", "init": "0x0000", "reverse": true, "byte_order": "big", "size": 4},
|
|
3
4
|
"formats": {
|
|
4
5
|
"login": {
|
|
6
|
+
"crc": false,
|
|
5
7
|
"fields": {
|
|
6
8
|
"serial": {"type": "string", "length_size": 2}
|
|
7
9
|
}
|
|
@@ -13,9 +15,10 @@
|
|
|
13
15
|
"route": {"type": "string"}
|
|
14
16
|
}
|
|
15
17
|
},
|
|
16
|
-
"
|
|
18
|
+
"report": {
|
|
17
19
|
"fields": {
|
|
18
|
-
"
|
|
20
|
+
"codec": {"type": "unsigned", "bytes": 1},
|
|
21
|
+
"positions": {"type": "array", "array": {
|
|
19
22
|
"time": {"type": "timestamp", "decimals": 3},
|
|
20
23
|
"priority": {"type": "unsigned", "bytes": 1},
|
|
21
24
|
"lng": {"type": "float", "bytes": 4, "decimals": 7},
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"formats": {
|
|
3
|
+
"login": {
|
|
4
|
+
"header": "T",
|
|
5
|
+
"client": {
|
|
6
|
+
"serial": {"type": "string"}
|
|
7
|
+
},
|
|
8
|
+
"server": {
|
|
9
|
+
"id": {"bytes": 2, "type": "id"},
|
|
10
|
+
"bus_number": {"bytes": 2, "type": "id"},
|
|
11
|
+
"route": {"bytes": 1, "type": "id"},
|
|
12
|
+
"direction": {"bytes": 1, "type": "bool"},
|
|
13
|
+
"geofence": {"bytes": 2, "type": "id"},
|
|
14
|
+
"state": {"bytes": 1, "type": "char"},
|
|
15
|
+
"trip": {"bytes": 4, "type": "id"},
|
|
16
|
+
"driver": {"bytes": 4, "type": "id"},
|
|
17
|
+
"order": {"bytes": 1, "type": "unsigned"},
|
|
18
|
+
"logged": {"type": "bool"},
|
|
19
|
+
"data": {"type": "datetime"},
|
|
20
|
+
"schedule": {"type": "time"}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"report": {
|
|
24
|
+
"header": "PV",
|
|
25
|
+
"client": {
|
|
26
|
+
"positions": {"type": "array", "array": {
|
|
27
|
+
"time": {"type": "datetime"},
|
|
28
|
+
"lng": {"bytes": 4,"type": "float", "decimals": 6},
|
|
29
|
+
"lat": {"bytes": 4, "type": "float", "decimals": 6},
|
|
30
|
+
"speed": {"bytes": 1, "type": "unsigned"},
|
|
31
|
+
"mark": {"bytes": 2, "type": "id"},
|
|
32
|
+
"busstop": {"bytes": 2, "type": "id"}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"trip": {"bytes": 4, "type": "id"},
|
|
36
|
+
"route": {"bytes": 1, "type": "id"},
|
|
37
|
+
"direction": {"type": "bool"},
|
|
38
|
+
"state": {"type": "char"},
|
|
39
|
+
"sales": {"bytes": 3, "type": "unsigned"},
|
|
40
|
+
"events": {"type": "array", "array": {
|
|
41
|
+
"id": {"bytes": 2, "type": "unsigned"},
|
|
42
|
+
"value": {"bytes": 1, "type": "unsigned"}
|
|
43
|
+
}}
|
|
44
|
+
},
|
|
45
|
+
"server": {
|
|
46
|
+
"positions": {"bytes": 1, "type": "unsigned"}
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"comando": {
|
|
50
|
+
"header": "$",
|
|
51
|
+
"client": {
|
|
52
|
+
"comando": {
|
|
53
|
+
"type": "char"
|
|
54
|
+
},
|
|
55
|
+
"id": {
|
|
56
|
+
"type": "string"
|
|
57
|
+
},
|
|
58
|
+
"value": {
|
|
59
|
+
"type": "string"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"server": {
|
|
63
|
+
"comando": {
|
|
64
|
+
"type": "char"
|
|
65
|
+
},
|
|
66
|
+
"id": {
|
|
67
|
+
"type": "string"
|
|
68
|
+
},
|
|
69
|
+
"value": {
|
|
70
|
+
"type": "string"
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -455,7 +455,7 @@ report:
|
|
|
455
455
|
|
|
456
456
|
def test_login_binary(self):
|
|
457
457
|
client = Protocol(file='demo.json', server=False)
|
|
458
|
-
binary = b'T=\x00\x00\x00\x00\x00\
|
|
458
|
+
binary = b'T=\x00\n\x00\x03\x03\x00\x00\x00T\x00\x00\x0b\x8a\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00'
|
|
459
459
|
header, recv = client.decode(binary)
|
|
460
460
|
self.assertEqual(header, 'login')
|
|
461
461
|
print('recv', recv)
|
|
@@ -535,7 +535,7 @@ report:
|
|
|
535
535
|
}}
|
|
536
536
|
})
|
|
537
537
|
|
|
538
|
-
def
|
|
538
|
+
def test_teltonika_login_hex(self):
|
|
539
539
|
hexdata = '000F333536333037303432343431303133'
|
|
540
540
|
binary = bytes.fromhex(hexdata)
|
|
541
541
|
client = Protocol(file='codec8.json', server=None)
|
|
@@ -544,18 +544,29 @@ report:
|
|
|
544
544
|
binary2 = client.encode(recv, 'login')
|
|
545
545
|
self.assertEqual(binary, binary2)
|
|
546
546
|
|
|
547
|
+
def test_position_binary(self):
|
|
548
|
+
data = {'positions':
|
|
549
|
+
[{'time': datetime.datetime(2024, 10, 18, 15, 57, 38),
|
|
550
|
+
'lng': -77.015533, 'lat': -12.061365, 'speed': 105, 'mark': None, 'busstop': None}],
|
|
551
|
+
'sales': 0, 'events': [], 'direction': None, 'route': None, 'state': None, 'trip': None
|
|
552
|
+
}
|
|
553
|
+
client = Protocol(file='demo.json', server=False)
|
|
554
|
+
binary = client.encode(data, 'report')
|
|
555
|
+
binary2 = b'PV=\x01\x18\x10$\x15W8\xfbh\xd6\x13\xffG\xf5Ki\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00'
|
|
556
|
+
self.assertEqual(binary, binary2)
|
|
557
|
+
server = Protocol(file='demo.json', server=True)
|
|
558
|
+
header, recv = server.decode(binary)
|
|
559
|
+
self.assertEqual(recv, data)
|
|
560
|
+
|
|
547
561
|
|
|
548
|
-
def
|
|
549
|
-
hexdata = '
|
|
562
|
+
def test_teltonika_position_hex(self):
|
|
563
|
+
hexdata = '0000003608010000016B40D8EA30010000000000000000000000000000000105021503010101425E0F01F10000601A014E0000000000000000010000C7CF'
|
|
550
564
|
binary = bytes.fromhex(hexdata)
|
|
551
565
|
client = Protocol(file='codec8.json', server=None)
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
self.assertEqual(crc16, client.get_crc(binary[8:length + 8]))
|
|
557
|
-
recv = client.decode(clean_binary, 'reports')
|
|
558
|
-
self.assertEqual(recv, {'reports': [{
|
|
566
|
+
recv = client.decode(binary, 'report')
|
|
567
|
+
self.assertEqual(recv, {
|
|
568
|
+
'codec': 8,
|
|
569
|
+
'positions': [{
|
|
559
570
|
'time': datetime.datetime(2019, 6, 10, 5, 4, 46),
|
|
560
571
|
'priority': 1,
|
|
561
572
|
'lng': 0.0,
|
|
@@ -585,4 +596,21 @@ report:
|
|
|
585
596
|
def test_login_keyerror(self):
|
|
586
597
|
client = Protocol(file='demo.json', server=False)
|
|
587
598
|
data = {}
|
|
588
|
-
|
|
599
|
+
with self.assertRaises(InputError):
|
|
600
|
+
client.encode(data, 'not_exist')
|
|
601
|
+
|
|
602
|
+
def test_no_crc(self):
|
|
603
|
+
data = {'serial': '359769037211486'}
|
|
604
|
+
client = Protocol(file='codec8.json')
|
|
605
|
+
binary = client.encode(data, 'login')
|
|
606
|
+
self.assertEqual(b'\x00\x0f359769037211486', binary)
|
|
607
|
+
recv = client.decode(binary, 'login')
|
|
608
|
+
self.assertEqual(data, recv)
|
|
609
|
+
|
|
610
|
+
def test_crc(self):
|
|
611
|
+
data = {'codec': 8, 'positions': [{'time': datetime.datetime(2024, 10, 19, 9, 37, 57, 555000), 'priority': 1, 'lng': -77.0155334, 'lat': -12.0613651, 'alt': 0, 'angle': 0, 'satellites': 3, 'speed': 105, 'event_io': 0, '#events': 0, 'events1b': [], 'events2b': [], 'events4b': [], 'events8b': [], '#reports': 1}]}
|
|
612
|
+
client = Protocol(file='codec8.json')
|
|
613
|
+
binary = client.encode(data, 'report')
|
|
614
|
+
self.assertEqual(b'\x00\x00\x00!\x08\x01\x00\x00\x01\x92\xa56\xaf\xb3\x01\xd2\x18\\\xba\xf8\xcf\x94\xed\x00\x00\x00\x00\x03\x00i\x00\x00\x00\x00\x00\x00\x01\x00\x00\xa3\xe4', binary)
|
|
615
|
+
recv = client.decode(binary, 'report')
|
|
616
|
+
self.assertEqual(data, recv)
|
protobin-0.0.16/tests/demo.json
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"formats": {
|
|
3
|
-
"login": {
|
|
4
|
-
"header": "T",
|
|
5
|
-
"client": {
|
|
6
|
-
"serial": {
|
|
7
|
-
"type": "string"
|
|
8
|
-
}
|
|
9
|
-
},
|
|
10
|
-
"server": {
|
|
11
|
-
"id": {
|
|
12
|
-
"bytes": 2,
|
|
13
|
-
"type": "id"
|
|
14
|
-
},
|
|
15
|
-
"bus_number": {
|
|
16
|
-
"bytes": 2,
|
|
17
|
-
"type": "id"
|
|
18
|
-
},
|
|
19
|
-
"route": {
|
|
20
|
-
"bytes": 1,
|
|
21
|
-
"type": "id"
|
|
22
|
-
},
|
|
23
|
-
"direction": {
|
|
24
|
-
"bytes": 1,
|
|
25
|
-
"type": "bool"
|
|
26
|
-
},
|
|
27
|
-
"geofence": {
|
|
28
|
-
"bytes": 2,
|
|
29
|
-
"type": "id"
|
|
30
|
-
},
|
|
31
|
-
"state": {
|
|
32
|
-
"bytes": 1,
|
|
33
|
-
"type": "char"
|
|
34
|
-
},
|
|
35
|
-
"trip": {
|
|
36
|
-
"bytes": 4,
|
|
37
|
-
"type": "id"
|
|
38
|
-
},
|
|
39
|
-
"driver": {
|
|
40
|
-
"bytes": 4,
|
|
41
|
-
"type": "id"
|
|
42
|
-
},
|
|
43
|
-
"order": {
|
|
44
|
-
"bytes": 1,
|
|
45
|
-
"type": "unsigned"
|
|
46
|
-
},
|
|
47
|
-
"logged": {
|
|
48
|
-
"type": "bool"
|
|
49
|
-
},
|
|
50
|
-
"data": {
|
|
51
|
-
"type": "datetime"
|
|
52
|
-
},
|
|
53
|
-
"schedule": {
|
|
54
|
-
"type": "time"
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
"report": {
|
|
59
|
-
"header": "P",
|
|
60
|
-
"client": {
|
|
61
|
-
"positions": {
|
|
62
|
-
"bytes": 19,
|
|
63
|
-
"type": "array",
|
|
64
|
-
"array": {
|
|
65
|
-
"time": {
|
|
66
|
-
"bytes": 6,
|
|
67
|
-
"type": "datetime"
|
|
68
|
-
},
|
|
69
|
-
"lng": {
|
|
70
|
-
"bytes": 4,
|
|
71
|
-
"type": "float",
|
|
72
|
-
"decimals": 6
|
|
73
|
-
},
|
|
74
|
-
"lat": {
|
|
75
|
-
"bytes": 4,
|
|
76
|
-
"type": "float",
|
|
77
|
-
"decimals": 6
|
|
78
|
-
},
|
|
79
|
-
"speed": {
|
|
80
|
-
"bytes": 1,
|
|
81
|
-
"type": "unsigned"
|
|
82
|
-
},
|
|
83
|
-
"mark": {
|
|
84
|
-
"bytes": 2,
|
|
85
|
-
"type": "id"
|
|
86
|
-
},
|
|
87
|
-
"busstop": {
|
|
88
|
-
"bytes": 2,
|
|
89
|
-
"type": "id"
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
"trip": {
|
|
94
|
-
"bytes": 4,
|
|
95
|
-
"type": "unsigned"
|
|
96
|
-
},
|
|
97
|
-
"route": {
|
|
98
|
-
"bytes": 1,
|
|
99
|
-
"type": "unsigned"
|
|
100
|
-
},
|
|
101
|
-
"direction": {
|
|
102
|
-
"bytes": 1,
|
|
103
|
-
"type": "bool"
|
|
104
|
-
},
|
|
105
|
-
"state": {
|
|
106
|
-
"bytes": 1,
|
|
107
|
-
"type": "char"
|
|
108
|
-
},
|
|
109
|
-
"sales": {
|
|
110
|
-
"bytes": 3,
|
|
111
|
-
"type": "unsigned"
|
|
112
|
-
},
|
|
113
|
-
"events": {
|
|
114
|
-
"type": "array",
|
|
115
|
-
"array": {
|
|
116
|
-
"id": {
|
|
117
|
-
"bytes": 2,
|
|
118
|
-
"type": "unsigned"
|
|
119
|
-
},
|
|
120
|
-
"value": {
|
|
121
|
-
"bytes": 1,
|
|
122
|
-
"type": "unsigned"
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
},
|
|
127
|
-
"server": {
|
|
128
|
-
"positions": {
|
|
129
|
-
"bytes": 1,
|
|
130
|
-
"type": "unsigned"
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
},
|
|
134
|
-
"comando": {
|
|
135
|
-
"header": "$",
|
|
136
|
-
"client": {
|
|
137
|
-
"comando": {
|
|
138
|
-
"type": "char"
|
|
139
|
-
},
|
|
140
|
-
"id": {
|
|
141
|
-
"type": "string"
|
|
142
|
-
},
|
|
143
|
-
"value": {
|
|
144
|
-
"type": "string"
|
|
145
|
-
}
|
|
146
|
-
},
|
|
147
|
-
"server": {
|
|
148
|
-
"comando": {
|
|
149
|
-
"type": "char"
|
|
150
|
-
},
|
|
151
|
-
"id": {
|
|
152
|
-
"type": "string"
|
|
153
|
-
},
|
|
154
|
-
"value": {
|
|
155
|
-
"type": "string"
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|