protobin 0.0.25__tar.gz → 0.0.26__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: protobin
3
- Version: 0.0.25
3
+ Version: 0.0.26
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
@@ -114,21 +114,30 @@ class Protocol:
114
114
  binary = len(binary).to_bytes(self.length, 'big', signed=False) + binary + self.get_crc(binary)
115
115
  return binary
116
116
 
117
- def decode(self, binary, codec=None):
118
- if codec is None:
119
- n = binary.find(b'=')
120
- if n == -1:
117
+ def get_header(self, binary):
118
+ n = binary.find(b'=')
119
+ if n == -1:
120
+ binary = self.check_crc(binary)
121
+ h = binary[:1]
122
+ binary = binary[1:]
123
+ format = self.get_codec(h)
124
+ else:
125
+ if n > 4:
121
126
  binary = self.check_crc(binary)
122
- h = binary[:1]
123
- binary = binary[1:]
124
- format = self.get_codec(h)
125
- else:
127
+ n = binary.find(b'=')
126
128
  if n > 4:
127
- binary = self.check_crc(binary)
128
- n = binary.find(b'=')
129
- h = binary[0:n]
130
- binary = binary[n + 1:]
131
- format = self.get_format(h)
129
+ h = binary[:1]
130
+ binary = binary[1:]
131
+ format = self.get_codec(h)
132
+ return format, binary
133
+ h = binary[0:n]
134
+ binary = binary[n + 1:]
135
+ format = self.get_format(h)
136
+ return format, binary
137
+
138
+ def decode(self, binary, codec=None):
139
+ if codec is None:
140
+ format, binary = self.get_header(binary)
132
141
  else:
133
142
  format = self.formats[codec]
134
143
  if format.crc and self.crc16:
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "protobin"
7
- version = "0.0.25"
7
+ version = "0.0.26"
8
8
  authors = [
9
9
  { name="Daniel Robles", email="drm.electronic@gmail.com" },
10
10
  ]
@@ -45,9 +45,9 @@
45
45
  "events8b": {"type": "array", "array": {
46
46
  "id": {"type": "id", "bytes": 1},
47
47
  "value": {"type": "unsigned", "bytes": 8}
48
- }},
49
- "#reports": {"type": "unsigned", "bytes": 1}
50
- }}
48
+ }}
49
+ }},
50
+ "#reports": {"type": "unsigned", "bytes": 1}
51
51
  }
52
52
  },
53
53
  "status3": {
@@ -570,31 +570,32 @@ report:
570
570
  header, recv = client.decode(binary)
571
571
  self.assertEqual(recv, {
572
572
  'positions': [{
573
- 'time': datetime.datetime(2019, 6, 10, 5, 4, 46),
574
- 'priority': 1,
575
- 'lng': 0.0,
576
- 'lat': 0.0,
577
- 'alt': 0,
578
- 'angle': 0,
579
- 'satellites': 0,
580
- 'speed': 0,
581
- 'event_io': 1,
582
- '#events': 5,
583
- 'events1b': [
584
- {'id': 21, 'value': 3},
585
- {'id': 1, 'value': 1}
573
+ 'time': datetime.datetime(2019, 6, 10, 5, 4, 46),
574
+ 'priority': 1,
575
+ 'lng': 0.0,
576
+ 'lat': 0.0,
577
+ 'alt': 0,
578
+ 'angle': 0,
579
+ 'satellites': 0,
580
+ 'speed': 0,
581
+ 'event_io': 1,
582
+ '#events': 5,
583
+ 'events1b': [
584
+ {'id': 21, 'value': 3},
585
+ {'id': 1, 'value': 1}
586
+ ],
587
+ 'events2b': [
588
+ {'id': 66, 'value': 24079}
589
+ ],
590
+ 'events4b': [
591
+ {'id': 241, 'value': 24602}
592
+ ],
593
+ 'events8b': [
594
+ {'id': 78, 'value': 0}
595
+ ]}
586
596
  ],
587
- 'events2b': [
588
- {'id': 66, 'value': 24079}
589
- ],
590
- 'events4b': [
591
- {'id': 241, 'value': 24602}
592
- ],
593
- 'events8b': [
594
- {'id': 78, 'value': 0}
595
- ],
596
- '#reports': 1,
597
- }]})
597
+ '#reports': 1
598
+ })
598
599
 
599
600
  def test_login_keyerror(self):
600
601
  client = Protocol(file='demo.json', server=False)
@@ -611,7 +612,7 @@ report:
611
612
  self.assertEqual(data, recv)
612
613
 
613
614
  def test_crc(self):
614
- data = {'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}]}
615
+ data = {'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}
615
616
  client = Protocol(file='codec8.json')
616
617
  binary = client.encode(data, 'report')
617
618
  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)
@@ -634,3 +635,10 @@ report:
634
635
  data = {'positions': 1}
635
636
  binary = protocol.encode(data, 'report_ack')
636
637
  self.assertEqual(binary, b'\x00\x00\x00\x01')
638
+
639
+ def test_decode_codec8(self):
640
+ # binary = b'\xfe\x0f350424068858891\x00\x00\x01\x06CBF541\x04\x00\x02R\x00\x00\x00\x00\x01\x00\x00\x1e\n\x04\x00)\x11$\x100\x00R\xff\x00\x00\x00\x02'
641
+ binary = b'\x00\x00\x00\x00\x00\x00\x01\x11\x08\t\x00\x00\x01\x93x\xd74\xf0\x00\xd2\x15\x19\xe6\xf8\xc6\xe8\x9e\x00Z\x01b\x0e\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x01\x93x\xd7_\xe8\x00\xd2\x15\x1b#\xf8\xc6\xfau\x00W\x00\x06\x0f\x00\x0e\x00\x00\x00\x00\x00\x00\x00\x00\x01\x93x\xd7\x86\xf8\x00\xd2\x15\x19\xf7\xf8\xc7\x0c~\x00S\x01e\x10\x00\x14\x00\x00\x00\x00\x00\x00\x00\x00\x01\x93x\xd7\xd5\x18\x00\xd2\x15\x19\xf7\xf8\xc7\x14=\x00U\x00\x02\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x93x\xd8#8\x00\xd2\x15\x19\xf7\xf8\xc7\x14=\x00U\x00\x02\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x93x\xd8qX\x00\xd2\x15\x19\xf7\xf8\xc7\x14=\x00U\x00\x02\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x93x\xd8\xb3\xc0\x00\xd2\x15\x1bU\xf8\xc7%\xe2\x00R\x01g\x10\x00\x19\x00\x00\x00\x00\x00\x00\x00\x00\x01\x93x\xd8\xcf\x18\x00\xd2\x15\x1a\xe0\xf8\xc79\x8b\x00T\x01g\x0f\x00\x17\x00\x00\x00\x00\x00\x00\x00\x00\x01\x93x\xd8\xeeX\x00\xd2\x15\x1al\xf8\xc7K\x1f\x00Y\x00\x01\x0e\x00\x17\x00\x00\x00\x00\x00\x00\t\x00\x00\x13x'
642
+ client = Protocol(file='codec8.json')
643
+ header, recv = client.decode(binary)
644
+ print(header, recv)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes