Beispiel #1
0
 def __init__(self,
              raw="",
              vertical=0,
              horizontal=0,
              draw_style=0,
              font_style=0,
              text=""):
     if len(raw) > 0:
         self._raw = raw
         self.length = hexstring_to_int(self._raw[0:2],
                                        little_endian=False) * 2
         if len(self._raw) != self.length + 2:
             raise Exception(
                 f'Answer package is always a 23 byte string, this string is {len(self._raw)} Packet: {self._raw}'
             )
         self.vertical = hexstring_to_int(self._raw[2:5],
                                          little_endian=True)
         self.horizontal = hexstring_to_int(self._raw[5:8],
                                            little_endian=False)
         self.draw_style = hexstring_to_int(self._raw[8:10],
                                            little_endian=False)
         self.font_style = hexstring_to_int(self._raw[10:12],
                                            little_endian=False)
         self.text = utf16hexstring_to_utf8(self._raw[12:])
     else:
         self.length = 0
         self.vertical = vertical
         self.horizontal = horizontal
         self.draw_style = draw_style
         self.font_style = font_style
         self.text = text
Beispiel #2
0
 def __init__(self,
              raw="",
              service_code=0,
              display_tag_id="",
              rssi=0,
              tag_status=0,
              volt=0.0,
              temperature=0):
     if len(raw) > 0:
         self._raw = raw
         if len(self._raw) != 23:
             raise Exception(
                 f'Answer package is always a 23 byte string, this string is {len(self._raw)} Packet: {self._raw}'
             )
         self.length = hexstring_to_int(self._raw[1:5], little_endian=False)
         self.service_code = hexstring_to_int(self._raw[5:9],
                                              little_endian=False)
         self.display_tag_id = self._raw[9:15]
         self.rssi = hexstring_to_int(self._raw[15:17],
                                      little_endian=False) - 254
         self.tag_status = hexstring_to_int(self._raw[17:19],
                                            little_endian=False)
         self.volt = float(
             hexstring_to_int(self._raw[19:21], little_endian=False)) / 10
         self.temperature = hexstring_to_int(self._raw[21:23],
                                             little_endian=False)
     else:
         self.length = 0
         self.service_code = service_code
         self.display_tag_id = display_tag_id
         self.rssi = rssi
         self.tag_status = tag_status
         self.volt = volt
         self.temperature = temperature
Beispiel #3
0
 def __init__(self,
              raw="",
              color_red=False,
              color_green=True,
              color_blue=False,
              service_code=0,
              flash_times=1):
     if len(raw) > 0:
         self._raw = raw
         if len(self._raw) != 16:
             raise Exception(
                 f'LED Data package is always a 16 byte string, this string is {len(self._raw)} Packet: {self._raw}'
             )
         self.length = hexstring_to_int(self._raw[0:2], little_endian=False)
         flash_color = hexstring_to_int(self._raw[2:4], little_endian=False)
         if (flash_color & 1) == 1:
             self.color_red = True
         else:
             self.color_red = False
         if (flash_color & 2) == 2:
             self.color_green = True
         else:
             self.color_green = False
         if (flash_color & 4) == 4:
             self.color_blue = True
         else:
             self.color_blue = False
         self.service_code = hexstring_to_int(self._raw[4:8],
                                              little_endian=False)
         self.flash_times = hexstring_to_int(self._raw[12:16],
                                             little_endian=False)
     else:
         self.length = 0
         self.color_red = color_red
         self.color_green = color_green
         self.color_blue = color_blue
         self.service_code = service_code
         self.flash_times = flash_times
Beispiel #4
0
 def test_hexstring_to_int_little(self):
     self.assertEqual(
         1, hexstring_to_int(inputstring="010", little_endian=True))
     self.assertEqual(
         241, hexstring_to_int(inputstring="F1", little_endian=True))
     self.assertEqual(
         255, hexstring_to_int(inputstring="FF", little_endian=True))
     self.assertEqual(
         256, hexstring_to_int(inputstring="001", little_endian=True))
     self.assertEqual(
         20017, hexstring_to_int(inputstring="314E", little_endian=True))
     self.assertEqual(
         0x0c0d, hexstring_to_int(inputstring="0D0C", little_endian=True))
     self.assertEqual(
         0x0a0b0c0d,
         hexstring_to_int(inputstring="0D0C0B0A", little_endian=True))
Beispiel #5
0
 def test_hexstring_to_int_big(self):
     self.assertEqual(
         1, hexstring_to_int(inputstring="001", little_endian=False))
     self.assertEqual(
         241, hexstring_to_int(inputstring="F1", little_endian=False))
     self.assertEqual(
         255, hexstring_to_int(inputstring="FF", little_endian=False))
     self.assertEqual(
         256, hexstring_to_int(inputstring="100", little_endian=False))
     self.assertEqual(
         20017, hexstring_to_int(inputstring="4E31", little_endian=False))
     self.assertEqual(
         0x0c0d, hexstring_to_int(inputstring="0C0D", little_endian=False))
     self.assertEqual(
         0x0a0b0c0d,
         hexstring_to_int(inputstring="0A0B0C0D", little_endian=False))
Beispiel #6
0
 def __init__(self,
              raw="",
              vertical=0,
              horizontal=0,
              draw_style=0,
              height=0,
              width=0,
              border=0):
     if len(raw) > 0:
         self._raw = raw
         self.length = hexstring_to_int(self._raw[0:2],
                                        little_endian=False) * 2
         if self.length != 22:
             raise Exception(f'Rectangle package is always 2+22 byte.')
         self.vertical = hexstring_to_int(self._raw[2:5],
                                          little_endian=True)
         self.horizontal = hexstring_to_int(self._raw[5:8],
                                            little_endian=False)
         self.font_style = hexstring_to_int(self._raw[8:10],
                                            little_endian=False)
         if self.font_style != FontStylesInv['Rectangle']:
             raise Exception(
                 f'Font style must be 0x64(Rectangle) in a Rectangle package.'
             )
         self.draw_style = hexstring_to_int(self._raw[10:12],
                                            little_endian=False)
         self.height = hexstring_to_int(self._raw[12:16],
                                        little_endian=False)
         self.width = hexstring_to_int(self._raw[16:20],
                                       little_endian=False)
         self.border = hexstring_to_int(self._raw[20:24],
                                        little_endian=False)
     else:
         self.length = 0
         self.vertical = vertical
         self.horizontal = horizontal
         self.font_style = FontStylesInv['Rectangle']
         self.draw_style = draw_style
         self.height = height
         self.width = width
         self.border = border
Beispiel #7
0
 def __init__(self,
              raw="",
              vertical=0,
              horizontal=0,
              draw_style=0,
              font_style=0,
              border=0):
     if len(raw) > 0:
         self._raw = raw
         self.length = hexstring_to_int(self._raw[0:2],
                                        little_endian=False) * 2
         if self.length != 14:
             raise Exception(f'Rectangle package is always 2+14 byte.')
         self.vertical = hexstring_to_int(self._raw[2:5],
                                          little_endian=True)
         self.horizontal = hexstring_to_int(self._raw[5:8],
                                            little_endian=False)
         self.draw_style = hexstring_to_int(self._raw[8:10],
                                            little_endian=False)
         self.font_style = hexstring_to_int(self._raw[10:12],
                                            little_endian=False)
         self.border = hexstring_to_int(self._raw[12:16],
                                        little_endian=False)
     else:
         self.length = 0
         self.vertical = vertical
         self.horizontal = horizontal
         self.draw_style = draw_style
         self.font_style = font_style
         self.border = border
     if self.font_style not in [
             FontStylesInv["Horizontal Line"],
             FontStylesInv["Vertical Line"]
     ]:
         raise Exception(
             "Draw style for line must be %02X (Horizontal Line) or %02X (Vertical Line)."
             % (FontStylesInv["Horizontal Line"],
                FontStylesInv["Vertical Line"]))
Beispiel #8
0
 def __init__(self,
              raw="",
              x: int = 0,
              y: int = 0,
              image: Image = None,
              colored_image: bool = False):
     if len(raw) > 0:
         self._raw = raw
         if self._raw[0:2] != "FC":
             raise Exception("Expected image to start with FC")
         self.x = hexstring_to_int(self._raw[2:6], little_endian=False)
         self.y = hexstring_to_int(self._raw[6:10], little_endian=False)
         self.height = hexstring_to_int(self._raw[10:14],
                                        little_endian=False) + 1
         self.width = hexstring_to_int(self._raw[14:18],
                                       little_endian=False) + 1
         image_black_size = hexstring_to_int(self._raw[18:26],
                                             little_endian=False) * 2
         image_black_end = 26 + image_black_size
         image_black_compressed = self._raw[26:image_black_end]
         image_black_raw = uncompress_pixel_array(image_black_compressed)
         self.image = Image.new('RGB', (self.width, self.height),
                                "white")  # Init blank image
         pixel_string_to_image(self.image, image_black_raw)
         # Check if there is more data, that would inicate color data
         if (len(self._raw) - 26) > image_black_size:
             self.colored_image = True
             color_part = self._raw[image_black_end:]
             if color_part[0:3] != "FC8":
                 raise Exception("Expected color image to start with FC8")
             color_x = hexstring_to_int(color_part[3:6],
                                        little_endian=False)
             color_y = hexstring_to_int(color_part[6:10],
                                        little_endian=False)
             color_height = hexstring_to_int(color_part[11:14],
                                             little_endian=False) + 1
             color_width = hexstring_to_int(color_part[14:18],
                                            little_endian=False) + 1
             if color_x != self.x:
                 raise Exception(
                     f"Expected color x parameter to be {self.x}, but it is {color_x}"
                 )
             if color_y != self.y:
                 raise Exception(
                     f"Expected color y parameter to be {self.y}, but it is {color_y}"
                 )
             if color_height != self.height:
                 raise Exception(
                     f"Expected color height parameter to be {self.height}, but it is {color_height}"
                 )
             if color_width != self.width:
                 raise Exception(
                     f"Expected color width parameter to be {self.width}, but it is {color_width}"
                 )
             image_color_size = hexstring_to_int(color_part[18:26],
                                                 little_endian=False) * 2
             image_color_compressed = color_part[26:]
             if len(image_color_compressed) != image_color_size:
                 raise Exception(
                     f"Expected color image to be {image_color_size} long, but it is {len(image_color_compressed)}"
                 )
             image_color_raw = uncompress_pixel_array(
                 image_color_compressed)
             pixel_string_to_image(self.image, image_color_raw, (255, 0, 0))
         else:
             self.colored_image = False
     else:
         self.x = x
         self.y = y
         self.image = image
         self.colored_image = colored_image
         self.width = self.image.width
         self.height = self.image.height
Beispiel #9
0
 def __init__(self,
              raw="",
              vertical=0,
              horizontal=0,
              draw_style=0,
              font_style=0,
              text=""):
     # Barcodes that is supported now, some other check digits etc. might be needed
     self.supported_barcodes = [
         FontStylesInv['Barcode EAN13'], FontStylesInv['Barcode 128'],
         FontStylesInv['Barcode EAN13 Double Size'],
         FontStylesInv['Barcode 128 Double Size']
     ]
     if len(raw) > 0:
         self._raw = raw
         self.length = hexstring_to_int(self._raw[0:2],
                                        little_endian=False) * 2
         if len(self._raw) != self.length + 2:
             raise Exception(
                 f'Barcode packet length field does not match up with actual size, this string is {len(self._raw)} Packet: {self._raw}'
             )
         self.vertical = hexstring_to_int(self._raw[2:5],
                                          little_endian=True)
         self.horizontal = hexstring_to_int(self._raw[5:8],
                                            little_endian=False)
         self.draw_style = hexstring_to_int(self._raw[8:10],
                                            little_endian=False)
         self.font_style = hexstring_to_int(self._raw[10:12],
                                            little_endian=False)
         rawtext = self._raw[12:]
         if self.font_style in [
                 FontStylesInv['Barcode 128'],
                 FontStylesInv['Barcode 128 Double Size']
         ]:
             self.text = rawtext[
                 4:
                 -8]  # Skip first start code, and last check digit and stop
         if self.font_style in [
                 FontStylesInv['Barcode EAN13'],
                 FontStylesInv['Barcode EAN13 Double Size']
         ]:
             if len(rawtext) != (1 * 4) + (13 * 4) + (2 * 4):
                 raise Exception(
                     "Can't read EAN 13, expected length to be 1 start + 13 digits + 2 stop."
                 )
             self.text = rawtext[
                 4:
                 -12]  # Skip first start code, and last check digit and stop
         self.text = utf16hexstring_to_utf8(self.text)
     else:
         self.length = 0
         self.vertical = vertical
         self.horizontal = horizontal
         self.draw_style = draw_style
         self.font_style = font_style
         self.text = text
     self.prefix = "0088"  # Start Code B 0088(UTF-16 HTS), also used by EAN 13
     if self.font_style not in self.supported_barcodes:
         raise Exception("Only Barcode 128 and EAN13 is supported.")
     # Barcode 128
     if self.font_style in [
             FontStylesInv['Barcode 128'],
             FontStylesInv['Barcode 128 Double Size']
     ]:
         if len(self.text) < 1:
             raise Exception("Barcode 128 musbe more than 0 cahracters.")
         if not validate_barcode128b_characters(self.text):
             raise Exception(
                 "Only characters belonging to Barcode 128 B is valid.")
         self.suffix = utf8_to_utf16hexstring(
             calculate_barcode128b_check_digit(self.text))
         self.suffix += "008A"  # Stop 008A(UTF-16 VTS)
     # EAN 13
     if self.font_style in [
             FontStylesInv['Barcode EAN13'],
             FontStylesInv['Barcode EAN13 Double Size']
     ]:
         if len(self.text) != 12:
             raise Exception(
                 "EAN 13 must bra 12 digits, the 13th is check digit and it will be calculated automatically."
             )
         if not validate_ean13_characters(self.text):
             raise Exception("EAN 13 can only contain digits.")
         self.suffix = utf8_to_utf16hexstring(
             calculate_ean13_check_digit(self.text))
         self.suffix += "003D"  # Hardcoded for EAN13?
         self.suffix += "008A"  # Stop 008A(UTF-16 VTS)