protobin 0.0.49__tar.gz → 0.1.0__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.49 → protobin-0.1.0}/PKG-INFO +1 -1
- {protobin-0.0.49 → protobin-0.1.0}/protobin/fields.py +4 -3
- {protobin-0.0.49 → protobin-0.1.0}/protobin/protocol.py +6 -2
- {protobin-0.0.49 → protobin-0.1.0}/pyproject.toml +1 -1
- {protobin-0.0.49 → protobin-0.1.0}/tests/codec8.json +1 -1
- {protobin-0.0.49 → protobin-0.1.0}/tests/tests.py +10 -7
- {protobin-0.0.49 → protobin-0.1.0}/.gitignore +0 -0
- {protobin-0.0.49 → protobin-0.1.0}/LICENSE.txt +0 -0
- {protobin-0.0.49 → protobin-0.1.0}/README.md +0 -0
- {protobin-0.0.49 → protobin-0.1.0}/protobin/__init__.py +0 -0
- {protobin-0.0.49 → protobin-0.1.0}/protobin/errors.py +0 -0
- {protobin-0.0.49 → protobin-0.1.0}/protobin/interfaces.py +0 -0
- {protobin-0.0.49 → protobin-0.1.0}/protobin/singleton.py +0 -0
- {protobin-0.0.49 → protobin-0.1.0}/tests/demo.json +0 -0
- {protobin-0.0.49 → protobin-0.1.0}/tests/demo.yaml +0 -0
- {protobin-0.0.49 → protobin-0.1.0}/tests/teltonika.json +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: protobin
|
|
3
|
-
Version: 0.0
|
|
3
|
+
Version: 0.1.0
|
|
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
|
|
@@ -243,7 +243,7 @@ class CharField(FieldBase):
|
|
|
243
243
|
|
|
244
244
|
def to_binary(self, val):
|
|
245
245
|
if val is None:
|
|
246
|
-
val = ''
|
|
246
|
+
val = '\x00' * self.bytes
|
|
247
247
|
encoded = val[:self.bytes].encode('utf')
|
|
248
248
|
if self.bytes == 1 and len(encoded) > 1:
|
|
249
249
|
raise ValueError(f'{self} of one byte is possible to encode a utf-8 character')
|
|
@@ -506,9 +506,10 @@ class TimestampField(FieldBase):
|
|
|
506
506
|
super().__init__(k, js)
|
|
507
507
|
self.bytes = js.get('bytes', 6)
|
|
508
508
|
if self.bytes == 6:
|
|
509
|
-
self.decimals = 3
|
|
509
|
+
self.decimals = js.get('decimals', 3)
|
|
510
510
|
elif self.bytes == 8:
|
|
511
|
-
self.decimals = 6
|
|
511
|
+
self.decimals = js.get('decimals', 6)
|
|
512
|
+
|
|
512
513
|
else:
|
|
513
514
|
raise ValueError(f'Error in field TimestampField<{self.key}>, bytes must be 6 or 8 but "{self.bytes}" is received')
|
|
514
515
|
|
|
@@ -74,6 +74,7 @@ class Protocol:
|
|
|
74
74
|
self.server = server
|
|
75
75
|
self.headers = {}
|
|
76
76
|
self.codecs = {}
|
|
77
|
+
self.file = file
|
|
77
78
|
if file:
|
|
78
79
|
with open(file, 'r') as f:
|
|
79
80
|
if 'json' in file:
|
|
@@ -85,6 +86,9 @@ class Protocol:
|
|
|
85
86
|
else:
|
|
86
87
|
self.load_format(js)
|
|
87
88
|
|
|
89
|
+
def __str__(self):
|
|
90
|
+
return self.file
|
|
91
|
+
|
|
88
92
|
def load_format(self, js):
|
|
89
93
|
if 'crc' in js:
|
|
90
94
|
self.config_crc(js['crc'])
|
|
@@ -128,7 +132,7 @@ class Protocol:
|
|
|
128
132
|
n = binary.find(b'=')
|
|
129
133
|
if n == -1:
|
|
130
134
|
# posiciones sin =, calcular la longitud de la trama con sus primero bytes
|
|
131
|
-
binary = self.check_crc(binary)
|
|
135
|
+
# binary = self.check_crc(binary)
|
|
132
136
|
h = binary[:1]
|
|
133
137
|
binary = binary[1:]
|
|
134
138
|
format = self.get_codec(h)
|
|
@@ -140,7 +144,7 @@ class Protocol:
|
|
|
140
144
|
# binary = binary[4:]
|
|
141
145
|
# else:
|
|
142
146
|
# # codec8
|
|
143
|
-
binary = self.check_crc(binary)
|
|
147
|
+
# binary = self.check_crc(binary)
|
|
144
148
|
n = binary.find(b'=')
|
|
145
149
|
if n > 4 or n == -1:
|
|
146
150
|
# si el igual está adelante no es parte del comando
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"codec": 8,
|
|
24
24
|
"fields": {
|
|
25
25
|
"positions": {"type": "array", "array": {
|
|
26
|
-
"time": {"type": "timestamp", "decimals": 3},
|
|
26
|
+
"time": {"type": "timestamp", "decimals": 3, "bytes": 8},
|
|
27
27
|
"priority": {"type": "unsigned", "bytes": 1},
|
|
28
28
|
"lng": {"type": "float", "bytes": 4, "decimals": 7},
|
|
29
29
|
"lat": {"type": "float", "bytes": 4, "decimals": 7},
|
|
@@ -277,7 +277,7 @@ class BasicTest(unittest.TestCase):
|
|
|
277
277
|
data = {'test': None}
|
|
278
278
|
binary = self.protocol.encode(data, 'char')
|
|
279
279
|
h, recv = self.protocol.decode(binary)
|
|
280
|
-
self.assertEqual({'test':
|
|
280
|
+
self.assertEqual({'test': None}, recv)
|
|
281
281
|
|
|
282
282
|
def test_char_utf(self):
|
|
283
283
|
data = {'test': 'ñ'}
|
|
@@ -457,10 +457,11 @@ class BasicTest(unittest.TestCase):
|
|
|
457
457
|
self.assertEqual(data, recv)
|
|
458
458
|
|
|
459
459
|
def test_timestamp(self):
|
|
460
|
-
data = {'test': datetime.datetime(
|
|
460
|
+
data = {'test': datetime.datetime(2025, 9, 2, 12, 9, 50, 732000)}
|
|
461
461
|
binary = self.protocol.encode(data, 'timestamp')
|
|
462
462
|
print(binary)
|
|
463
463
|
h, recv = self.protocol.decode(binary)
|
|
464
|
+
print('recv', recv)
|
|
464
465
|
self.assertEqual(data, recv)
|
|
465
466
|
|
|
466
467
|
def test_timestamp6(self):
|
|
@@ -477,12 +478,14 @@ class BasicTest(unittest.TestCase):
|
|
|
477
478
|
self.assertEqual(data, recv)
|
|
478
479
|
|
|
479
480
|
def test_timestamp_default(self):
|
|
480
|
-
data = {'test': datetime.datetime
|
|
481
|
+
data = {'test': datetime.datetime(2025, 9, 2, 12, 9, 50, 732000)}
|
|
482
|
+
# data = {'test': datetime.datetime.now()}
|
|
481
483
|
print(data)
|
|
482
484
|
binary = self.protocol.encode(data, 'timestamp_default')
|
|
483
|
-
print(binary)
|
|
485
|
+
print('mio', binary)
|
|
486
|
+
binary = b'\x54\x44\x3d\x01\x99\x0A\x55\x5D\x8C'
|
|
484
487
|
h, recv = self.protocol.decode(binary)
|
|
485
|
-
print(recv)
|
|
488
|
+
print('edu', recv)
|
|
486
489
|
self.assertEqual(data['test'].replace(microsecond=(data['test'].microsecond // 1000) * 1000), recv['test'])
|
|
487
490
|
|
|
488
491
|
class AdvancedTest(unittest.TestCase):
|
|
@@ -753,7 +756,7 @@ report:
|
|
|
753
756
|
}
|
|
754
757
|
client = Protocol(file='demo.json', server=False)
|
|
755
758
|
binary = client.encode(data, 'report')
|
|
756
|
-
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'
|
|
759
|
+
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\xa2\x00'
|
|
757
760
|
self.assertEqual(binary, binary2)
|
|
758
761
|
server = Protocol(file='demo.json', server=True)
|
|
759
762
|
header, recv = server.decode(binary)
|
|
@@ -1018,7 +1021,7 @@ report:
|
|
|
1018
1021
|
data = {'id': 52723, 'ruta': 1, 'lado': True, 'inicio': datetime.datetime(2025, 7, 22, 16, 40), 'estado': 'R', 'frecuencia': 6, 'conductor': 36, 'geocerca': 14, 'marcados': [True, True, True, True, True, True, True, True, True, True, True, True, True, True, False, False, False, False], 'controles': [{'geocerca': 3, 'tiempo': 0, 'volada': -10}, {'geocerca': 11, 'tiempo': 3, 'volada': -11}, {'geocerca': 28, 'tiempo': 3, 'volada': -4}, {'geocerca': 29, 'tiempo': 6, 'volada': -2}, {'geocerca': 43, 'tiempo': 0, 'volada': -2}, {'geocerca': 30, 'tiempo': 3, 'volada': 2}, {'geocerca': 12, 'tiempo': 3, 'volada': 2}, {'geocerca': 31, 'tiempo': 8, 'volada': 2}, {'geocerca': 32, 'tiempo': 2, 'volada': 3}, {'geocerca': 13, 'tiempo': 2, 'volada': 3}, {'geocerca': 33, 'tiempo': 2, 'volada': 6}, {'geocerca': 34, 'tiempo': 6, 'volada': 4}, {'geocerca': 35, 'tiempo': 4, 'volada': 4}, {'geocerca': 14, 'tiempo': 6, 'volada': 3}, {'geocerca': 36, 'tiempo': 11, 'volada': 0}, {'geocerca': 37, 'tiempo': 11, 'volada': 0}, {'geocerca': 38, 'tiempo': 11, 'volada': 0}, {'geocerca': 4, 'tiempo': 11, 'volada': 0}]}
|
|
1019
1022
|
binary = server.encode(data, 'salida')
|
|
1020
1023
|
print(binary)
|
|
1021
|
-
self.assertEqual(b'S=\x00\x00\xcd\xf3\x01\x01"\x07%\x16@\x00R\x06\x00\x00\x00$\x00\x0e\x12\xff\xfc\x00\x12\x00\x03\x00\x00\xff\xf6\x00\x0b\x00\x03\xff\xf5\x00\x1c\x00\x03\xff\xfc\x00\x1d\x00\x06\xff\xfe\x00+\x00\x00\xff\xfe\x00\x1e\x00\x03\x00\x02\x00\x0c\x00\x03\x00\x02\x00\x1f\x00\x08\x00\x02\x00 \x00\x02\x00\x03\x00\r\x00\x02\x00\x03\x00!\x00\x02\x00\x06\x00"\x00\x06\x00\x04\x00#\x00\x04\x00\x04\x00\x0e\x00\x06\x00\x03\x00$\x00\x0b\x00\x00\x00%\x00\x0b\x00\x00\x00&\x00\x0b\x00\x00\x00\x04\x00\x0b\x00\x00', binary)
|
|
1024
|
+
self.assertEqual(b'S=\x00\x00\xcd\xf3\x01\x01"\x07%\x16@\x00R\x06\x00\x00\x00$\x00\x0e\x12\xff\xfc\x00\x12\x00\x03\x00\x00\xff\xf6\x00\x0b\x00\x03\xff\xf5\x00\x1c\x00\x03\xff\xfc\x00\x1d\x00\x06\xff\xfe\x00+\x00\x00\xff\xfe\x00\x1e\x00\x03\x00\x02\x00\x0c\x00\x03\x00\x02\x00\x1f\x00\x08\x00\x02\x00 \x00\x02\x00\x03\x00\r\x00\x02\x00\x03\x00!\x00\x02\x00\x06\x00"\x00\x06\x00\x04\x00#\x00\x04\x00\x04\x00\x0e\x00\x06\x00\x03\x00$\x00\x0b\x00\x00\x00%\x00\x0b\x00\x00\x00&\x00\x0b\x00\x00\x00\x04\x00\x0b\x00\x00\x9e\xce', binary)
|
|
1022
1025
|
h, recv = client.decode(binary)
|
|
1023
1026
|
print(recv)
|
|
1024
1027
|
self.assertEqual(h, 'salida')
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|