__init__.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import binascii
  2. import requests
  3. import struct
  4. class Helper():
  5. @staticmethod
  6. def downloader(source, dest):
  7. req = requests.get(source, stream=True, timeout=10)
  8. if req.status_code == 200:
  9. with open(dest, 'wb') as fil:
  10. for chunk in req.iter_content(1024):
  11. fil.write(chunk)
  12. else:
  13. raise Exception('File not found')
  14. @staticmethod
  15. def pronto2lirc(pronto):
  16. codes = [int(binascii.hexlify(pronto[i:i+2]), 16) for i in range(0, len(pronto), 2)]
  17. if codes[0]:
  18. raise ValueError('Pronto code should start with 0000')
  19. if len(codes) != 4 + 2 * (codes[2] + codes[3]):
  20. raise ValueError('Number of pulse widths does not match the preamble')
  21. frequency = 1 / (codes[1] * 0.241246)
  22. return [int(round(code / frequency)) for code in codes[4:]]
  23. @staticmethod
  24. def lirc2broadlink(pulses):
  25. array = bytearray()
  26. for pulse in pulses:
  27. pulse = int(pulse * 269 / 8192)
  28. if pulse < 256:
  29. array += bytearray(struct.pack('>B', pulse))
  30. else:
  31. array += bytearray([0x00])
  32. array += bytearray(struct.pack('>H', pulse))
  33. packet = bytearray([0x26, 0x00])
  34. packet += bytearray(struct.pack('<H', len(array)))
  35. packet += array
  36. packet += bytearray([0x0d, 0x05])
  37. # Add 0s to make ultimate packet size a multiple of 16 for 128-bit AES encryption.
  38. remainder = (len(packet) + 4) % 16
  39. if remainder:
  40. packet += bytearray(16 - remainder)
  41. return packet