protobin 0.0.33__tar.gz → 0.0.35__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.33 → protobin-0.0.35}/PKG-INFO +1 -1
- {protobin-0.0.33 → protobin-0.0.35}/protobin/fields.py +1 -1
- {protobin-0.0.33 → protobin-0.0.35}/protobin/protocol.py +16 -2
- {protobin-0.0.33 → protobin-0.0.35}/pyproject.toml +1 -1
- {protobin-0.0.33 → protobin-0.0.35}/tests/teltonika.json +1 -0
- {protobin-0.0.33 → protobin-0.0.35}/tests/tests.py +20 -2
- {protobin-0.0.33 → protobin-0.0.35}/.gitignore +0 -0
- {protobin-0.0.33 → protobin-0.0.35}/LICENSE.txt +0 -0
- {protobin-0.0.33 → protobin-0.0.35}/README.md +0 -0
- {protobin-0.0.33 → protobin-0.0.35}/protobin/__init__.py +0 -0
- {protobin-0.0.33 → protobin-0.0.35}/protobin/errors.py +0 -0
- {protobin-0.0.33 → protobin-0.0.35}/protobin/interfaces.py +0 -0
- {protobin-0.0.33 → protobin-0.0.35}/protobin/singleton.py +0 -0
- {protobin-0.0.33 → protobin-0.0.35}/tests/codec8.json +0 -0
- {protobin-0.0.33 → protobin-0.0.35}/tests/demo.json +0 -0
- {protobin-0.0.33 → protobin-0.0.35}/tests/demo.yaml +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: protobin
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.35
|
|
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
|
|
@@ -215,7 +215,7 @@ class CharField(FieldBase):
|
|
|
215
215
|
def to_binary(self, val):
|
|
216
216
|
if val is None:
|
|
217
217
|
return bytes([0])
|
|
218
|
-
return val.encode('utf')
|
|
218
|
+
return (val + ' '*self.bytes)[:self.bytes].encode('utf')
|
|
219
219
|
|
|
220
220
|
def from_binary(self, binary):
|
|
221
221
|
val = int.from_bytes(binary[:self.bytes], 'big', signed=False)
|
|
@@ -67,6 +67,7 @@ class Protocol:
|
|
|
67
67
|
crc16 = None
|
|
68
68
|
crc_byteorder = None
|
|
69
69
|
crc_size = 2
|
|
70
|
+
fake_prefix = None
|
|
70
71
|
|
|
71
72
|
def __init__(self, server: bool = None, file=None, js=None):
|
|
72
73
|
self.server = server
|
|
@@ -87,6 +88,7 @@ class Protocol:
|
|
|
87
88
|
if 'crc' in js:
|
|
88
89
|
self.config_crc(js['crc'])
|
|
89
90
|
self.length = js['length']
|
|
91
|
+
self.fake_prefix = js.get('fake_prefix')
|
|
90
92
|
formats = js['formats']
|
|
91
93
|
self.formats = {}
|
|
92
94
|
for name in formats.keys():
|
|
@@ -112,22 +114,33 @@ class Protocol:
|
|
|
112
114
|
raise InputError(f'{format_key} is not available format, these are the all availables formats {self.formats.keys()}')
|
|
113
115
|
format = self.formats[format_key]
|
|
114
116
|
binary = format.encode(data)
|
|
115
|
-
if
|
|
117
|
+
if self.fake_prefix:
|
|
118
|
+
binary = bytes(self.fake_prefix) + binary + self.get_crc(binary)
|
|
119
|
+
elif format.crc and self.crc16:
|
|
116
120
|
binary = len(binary).to_bytes(self.length, 'big', signed=False) + binary + self.get_crc(binary)
|
|
121
|
+
|
|
117
122
|
return binary
|
|
118
123
|
|
|
119
124
|
def get_header(self, binary):
|
|
120
125
|
n = binary.find(b'=')
|
|
121
126
|
if n == -1:
|
|
127
|
+
# posiciones sin =, calcular la longitud de la trama con sus primero bytes
|
|
122
128
|
binary = self.check_crc(binary)
|
|
123
129
|
h = binary[:1]
|
|
124
130
|
binary = binary[1:]
|
|
125
131
|
format = self.get_codec(h)
|
|
126
132
|
else:
|
|
127
133
|
if n > 4:
|
|
128
|
-
|
|
134
|
+
# comandos de pantalla con = o que tengan un igual más adelante por casualidad
|
|
135
|
+
if self.fake_prefix and binary[:4] == bytes(self.fake_prefix):
|
|
136
|
+
# comandos
|
|
137
|
+
binary = binary[4:]
|
|
138
|
+
else:
|
|
139
|
+
# codec8
|
|
140
|
+
binary = self.check_crc(binary)
|
|
129
141
|
n = binary.find(b'=')
|
|
130
142
|
if n > 4 or n == -1:
|
|
143
|
+
# si el igual está adelante no es parte del comando
|
|
131
144
|
h = binary[:1]
|
|
132
145
|
binary = binary[1:]
|
|
133
146
|
format = self.get_codec(h)
|
|
@@ -141,6 +154,7 @@ class Protocol:
|
|
|
141
154
|
if codec is None:
|
|
142
155
|
format, binary = self.get_header(binary)
|
|
143
156
|
else:
|
|
157
|
+
# codec 8 u otro ya se sabe el codec
|
|
144
158
|
format = self.formats[codec]
|
|
145
159
|
if format.crc and self.crc16:
|
|
146
160
|
binary = self.check_crc(binary)
|
|
@@ -93,7 +93,10 @@ class BasicTest(unittest.TestCase):
|
|
|
93
93
|
},
|
|
94
94
|
'char2': {
|
|
95
95
|
"header": "C2",
|
|
96
|
-
"fields": {
|
|
96
|
+
"fields": {
|
|
97
|
+
'test': {'bytes': 2, 'type': 'char'},
|
|
98
|
+
'prueba': {'bytes': 2, 'type': 'char'}
|
|
99
|
+
}
|
|
97
100
|
},
|
|
98
101
|
'date': {
|
|
99
102
|
"header": "D",
|
|
@@ -196,10 +199,17 @@ class BasicTest(unittest.TestCase):
|
|
|
196
199
|
self.assertEqual(data, recv)
|
|
197
200
|
|
|
198
201
|
def test_char2(self):
|
|
199
|
-
data = {'test': 'FA'}
|
|
202
|
+
data = {'test': 'FA', 'prueba': 'DO'}
|
|
200
203
|
binary = self.protocol.encode(data, 'char2')
|
|
201
204
|
h, recv = self.protocol.decode(binary)
|
|
202
205
|
self.assertEqual(data, recv)
|
|
206
|
+
data = {'test': '', 'prueba': 'F'}
|
|
207
|
+
binary = self.protocol.encode(data, 'char2')
|
|
208
|
+
print(binary)
|
|
209
|
+
h, recv = self.protocol.decode(binary)
|
|
210
|
+
data_corregida = {'test': ' ', 'prueba': 'F '}
|
|
211
|
+
self.assertEqual(data_corregida, recv)
|
|
212
|
+
print(recv)
|
|
203
213
|
|
|
204
214
|
def test_date(self):
|
|
205
215
|
data = {'test': datetime.date(2024, 9, 13)}
|
|
@@ -701,3 +711,11 @@ report:
|
|
|
701
711
|
header, recv = client.decode(binary)
|
|
702
712
|
print(recv)
|
|
703
713
|
|
|
714
|
+
def test_status_teltonika(self):
|
|
715
|
+
pesquero = b'\x00\x00\x00\x01S=RA\x07IQUITOS14:05\x06LOBATO\x04\x07IGLESIA\x00f\x08\x00n\x05\x00\x96\x03\x01m\x08\x00\xb5\r\x01w\t\x00\x00o\xb4'
|
|
716
|
+
client = Protocol(file='teltonika.json', server=None)
|
|
717
|
+
header, recv = client.decode(pesquero)
|
|
718
|
+
print(recv)
|
|
719
|
+
binary = client.encode(recv, header)
|
|
720
|
+
print(binary)
|
|
721
|
+
self.assertEqual(pesquero, binary)
|
|
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
|