protobin 0.0.18__tar.gz → 0.0.20__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.18 → protobin-0.0.20}/PKG-INFO +1 -2
- {protobin-0.0.18 → protobin-0.0.20}/protobin/fields.py +6 -1
- {protobin-0.0.18 → protobin-0.0.20}/protobin/protocol.py +17 -13
- {protobin-0.0.18 → protobin-0.0.20}/pyproject.toml +1 -1
- {protobin-0.0.18 → protobin-0.0.20}/tests/codec8.json +31 -1
- {protobin-0.0.18 → protobin-0.0.20}/tests/tests.py +25 -3
- {protobin-0.0.18 → protobin-0.0.20}/.gitignore +0 -0
- {protobin-0.0.18 → protobin-0.0.20}/LICENSE.txt +0 -0
- {protobin-0.0.18 → protobin-0.0.20}/README.md +0 -0
- {protobin-0.0.18 → protobin-0.0.20}/protobin/__init__.py +0 -0
- {protobin-0.0.18 → protobin-0.0.20}/protobin/errors.py +0 -0
- {protobin-0.0.18 → protobin-0.0.20}/protobin/interfaces.py +0 -0
- {protobin-0.0.18 → protobin-0.0.20}/tests/demo.json +0 -0
- {protobin-0.0.18 → protobin-0.0.20}/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.20
|
|
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
|
|
@@ -28,7 +28,6 @@ License: MIT License
|
|
|
28
28
|
SOFTWARE. 1
|
|
29
29
|
1. en.wikipedia.org
|
|
30
30
|
en.wikipedia.org
|
|
31
|
-
License-File: LICENSE.txt
|
|
32
31
|
Classifier: License :: OSI Approved :: MIT License
|
|
33
32
|
Classifier: Operating System :: OS Independent
|
|
34
33
|
Classifier: Programming Language :: Python :: 3
|
|
@@ -103,6 +103,8 @@ class ArrayField(FieldBase):
|
|
|
103
103
|
return val, binary
|
|
104
104
|
|
|
105
105
|
def to_binary(self, val):
|
|
106
|
+
if not isinstance(val, (list, tuple)):
|
|
107
|
+
raise ValueError(f'Error in field "{self.key}", a array is expected but "{val}" is received, {type(val)}')
|
|
106
108
|
binary = b''
|
|
107
109
|
length = len(val)
|
|
108
110
|
binary += length.to_bytes(1, 'big')
|
|
@@ -402,7 +404,10 @@ class StringField(FieldBase):
|
|
|
402
404
|
if self.bytes:
|
|
403
405
|
utf = str(val).encode('utf')[:self.bytes]
|
|
404
406
|
return utf
|
|
405
|
-
|
|
407
|
+
if val is None:
|
|
408
|
+
utf = b''
|
|
409
|
+
else:
|
|
410
|
+
utf = str(val).encode('utf')[:255]
|
|
406
411
|
return len(utf).to_bytes(self.length_size, 'big') + utf
|
|
407
412
|
|
|
408
413
|
|
|
@@ -101,28 +101,32 @@ class Protocol:
|
|
|
101
101
|
def decode(self, binary, codec=None):
|
|
102
102
|
if codec is None:
|
|
103
103
|
n = binary.find(b'=')
|
|
104
|
+
if n > 4:
|
|
105
|
+
binary = self.check_crc(binary)
|
|
106
|
+
n = binary.find(b'=')
|
|
104
107
|
h = binary[0:n]
|
|
105
108
|
binary = binary[n + 1:]
|
|
106
109
|
format = self.get_format(h)
|
|
107
110
|
else:
|
|
108
111
|
format = self.formats[codec]
|
|
109
|
-
|
|
110
|
-
|
|
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
|
|
112
|
+
if format.crc and self.crc16:
|
|
113
|
+
binary = self.check_crc(binary)
|
|
121
114
|
data = format.decode(binary)
|
|
122
115
|
if codec is None:
|
|
123
116
|
return format.name, data
|
|
124
117
|
return data
|
|
125
118
|
|
|
119
|
+
def check_crc(self, binary):
|
|
120
|
+
length = int.from_bytes(binary[:self.length], 'big', signed=False)
|
|
121
|
+
clean_binary = binary[self.length:length + self.length]
|
|
122
|
+
crc = binary[length + self.length: length + self.length + self.crc_size]
|
|
123
|
+
crc_value = int.from_bytes(crc, self.crc_byteorder, signed=False)
|
|
124
|
+
if not crc:
|
|
125
|
+
raise CRCError(f'There is no CRC')
|
|
126
|
+
elif crc_value != self.crc16(clean_binary):
|
|
127
|
+
raise CRCError(f'CRC no coincide {crc} != {self.get_crc(clean_binary)}')
|
|
128
|
+
return clean_binary
|
|
129
|
+
|
|
126
130
|
def config_crc(self, js):
|
|
127
131
|
self.crc16 = crcmod.mkCrcFun(int(js['poly'], 16), int(js['init'], 16), js['reverse'])
|
|
128
132
|
self.crc_byteorder = js['byte_order']
|
|
@@ -130,4 +134,4 @@ class Protocol:
|
|
|
130
134
|
self.crc_size = js['size']
|
|
131
135
|
|
|
132
136
|
def get_crc(self, binary):
|
|
133
|
-
return self.crc16(binary).to_bytes(self.
|
|
137
|
+
return self.crc16(binary).to_bytes(self.crc_size, byteorder=self.crc_byteorder)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"length":
|
|
2
|
+
"length": 8,
|
|
3
3
|
"crc": {"poly": "0x18005", "init": "0x0000", "reverse": true, "byte_order": "big", "size": 4},
|
|
4
4
|
"formats": {
|
|
5
5
|
"login": {
|
|
@@ -49,6 +49,36 @@
|
|
|
49
49
|
"#reports": {"type": "unsigned", "bytes": 1}
|
|
50
50
|
}}
|
|
51
51
|
}
|
|
52
|
+
},
|
|
53
|
+
"status3": {
|
|
54
|
+
"header": "s",
|
|
55
|
+
"fields": {
|
|
56
|
+
"status": {"type": "char"},
|
|
57
|
+
"direction": {"type": "char"},
|
|
58
|
+
"next_next_control": {"type": "string"},
|
|
59
|
+
"next_next_time": {"type": "string", "bytes": 5},
|
|
60
|
+
"next_control": {"type": "string"},
|
|
61
|
+
"next_time": {"type": "string", "bytes": 5},
|
|
62
|
+
"previous_control": {"type": "string"},
|
|
63
|
+
"delay": {"type": "signed", "bytes": 1},
|
|
64
|
+
"front_control": {"type": "string"},
|
|
65
|
+
"back_control": {"type": "string"},
|
|
66
|
+
"back_back_control": {"type": "string"},
|
|
67
|
+
"datero_bus_-1": {"type": "unsigned", "bytes": 2},
|
|
68
|
+
"datero_dif_-1": {"type": "unsigned", "bytes": 1},
|
|
69
|
+
"datero_bus_0": {"type": "unsigned", "bytes": 2},
|
|
70
|
+
"datero_dif_0": {"type": "unsigned", "bytes": 1},
|
|
71
|
+
"datero_bus_1": {"type": "unsigned", "bytes": 2},
|
|
72
|
+
"datero_dif_1": {"type": "unsigned", "bytes": 1},
|
|
73
|
+
"datero_bus_2": {"type": "unsigned", "bytes": 2},
|
|
74
|
+
"datero_dif_2": {"type": "unsigned", "bytes": 1},
|
|
75
|
+
"datero_bus_3": {"type": "unsigned", "bytes": 2},
|
|
76
|
+
"datero_dif_3": {"type": "unsigned", "bytes": 1},
|
|
77
|
+
"datero_bus_4": {"type": "unsigned", "bytes": 2},
|
|
78
|
+
"datero_dif_4": {"type": "unsigned", "bytes": 1},
|
|
79
|
+
"datero_bus_5": {"type": "unsigned", "bytes": 2},
|
|
80
|
+
"datero_dif_5": {"type": "unsigned", "bytes": 1}
|
|
81
|
+
}
|
|
52
82
|
}
|
|
53
83
|
}
|
|
54
84
|
}
|
|
@@ -151,6 +151,11 @@ class BasicTest(unittest.TestCase):
|
|
|
151
151
|
h, recv = self.protocol.decode(binary)
|
|
152
152
|
self.assertEqual(data, recv)
|
|
153
153
|
|
|
154
|
+
def test_array_none(self):
|
|
155
|
+
data = {}
|
|
156
|
+
with self.assertRaises(ValueError):
|
|
157
|
+
self.protocol.encode(data, 'array')
|
|
158
|
+
|
|
154
159
|
def test_bits(self):
|
|
155
160
|
data = {'test': [True, False, True, False, False] * 3}
|
|
156
161
|
binary = self.protocol.encode(data, 'bits')
|
|
@@ -560,7 +565,7 @@ report:
|
|
|
560
565
|
|
|
561
566
|
|
|
562
567
|
def test_teltonika_position_hex(self):
|
|
563
|
-
hexdata = '
|
|
568
|
+
hexdata = '000000000000003608010000016B40D8EA30010000000000000000000000000000000105021503010101425E0F01F10000601A014E0000000000000000010000C7CF'
|
|
564
569
|
binary = bytes.fromhex(hexdata)
|
|
565
570
|
client = Protocol(file='codec8.json', server=None)
|
|
566
571
|
recv = client.decode(binary, 'report')
|
|
@@ -611,6 +616,23 @@ report:
|
|
|
611
616
|
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
617
|
client = Protocol(file='codec8.json')
|
|
613
618
|
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)
|
|
619
|
+
self.assertEqual(b'\x00\x00\x00\x00\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
620
|
recv = client.decode(binary, 'report')
|
|
616
|
-
self.assertEqual(data, recv)
|
|
621
|
+
self.assertEqual(data, recv)
|
|
622
|
+
|
|
623
|
+
def test_status_crc(self):
|
|
624
|
+
data = {'status': 'E', 'direction': 'A', 'next_next_control': '', 'next_next_time': ' ', 'next_control': '', 'next_time': ' ', 'previous_control': '', 'delay': 0, 'front_control': '', 'back_control': '', 'back_back_control': '', 'datero_bus_-1': 0, 'datero_dif_-1': 0, 'datero_bus_0': 0, 'datero_dif_0': 0, 'datero_bus_1': 0, 'datero_dif_1': 0, 'datero_bus_2': 0, 'datero_dif_2': 0, 'datero_bus_3': 256, 'datero_dif_3': 0, 'datero_bus_4': 0, 'datero_dif_4': 0, 'datero_bus_5': 0, 'datero_dif_5': 0}
|
|
625
|
+
client = Protocol(file='codec8.json')
|
|
626
|
+
binary = client.encode(data, 'status3')
|
|
627
|
+
self.assertEqual(b'\x00\x00\x00\x00\x00\x00\x00*s=EA\x00 \x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb6\xd6', binary)
|
|
628
|
+
header, recv = client.decode(binary)
|
|
629
|
+
self.assertEqual(data, recv)
|
|
630
|
+
|
|
631
|
+
def test_unheader(self):
|
|
632
|
+
protocol = Protocol(js={'formats': {'report_ack': {
|
|
633
|
+
'fields': {
|
|
634
|
+
'positions': {'bytes': 4, 'type': 'unsigned'}
|
|
635
|
+
}}}})
|
|
636
|
+
data = {'positions': 1}
|
|
637
|
+
binary = protocol.encode(data, 'report_ack')
|
|
638
|
+
self.assertEqual(binary, b'\x00\x00\x00\x01')
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|