A clone of: https://github.com/nutechsoftware/alarmdecoder This is requires as they dropped support for older firmware releases w/o building in backward compatibility code, and they had previously hardcoded pyserial to a python2 only version.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.1 KiB

  1. import time
  2. from alarmdecoder import AlarmDecoder
  3. from alarmdecoder.devices import USBDevice
  4. __devices = {}
  5. def main():
  6. """
  7. Example application that shows how to handle attach/detach events generated
  8. by the USB devices.
  9. In this case we open the device and listen for messages when it is attached.
  10. And when it is detached we remove it from our list of monitored devices.
  11. """
  12. try:
  13. # Start up the detection thread such that handle_attached and handle_detached will
  14. # be called when devices are attached and detached, respectively.
  15. USBDevice.start_detection(on_attached=handle_attached, on_detached=handle_detached)
  16. # Wait for events.
  17. while True:
  18. time.sleep(1)
  19. except Exception as ex:
  20. print('Exception:', ex)
  21. finally:
  22. # Close all devices and stop detection.
  23. for sn, device in __devices.items():
  24. device.close()
  25. USBDevice.stop_detection()
  26. def create_device(device_args):
  27. """
  28. Creates an AlarmDecoder from the specified USB device arguments.
  29. :param device_args: Tuple containing information on the USB device to open.
  30. :type device_args: Tuple (vid, pid, serialnumber, interface_count, description)
  31. """
  32. device = AlarmDecoder(USBDevice.find(device_args))
  33. device.on_message += handle_message
  34. device.open()
  35. return device
  36. def handle_message(sender, message):
  37. """
  38. Handles message events from the AlarmDecoder.
  39. """
  40. print(sender, message.raw)
  41. def handle_attached(sender, device):
  42. """
  43. Handles attached events from USBDevice.start_detection().
  44. """
  45. # Create the device from the specified device arguments.
  46. dev = create_device(device)
  47. __devices[dev.id] = dev
  48. print('attached', dev.id)
  49. def handle_detached(sender, device):
  50. """
  51. Handles detached events from USBDevice.start_detection().
  52. """
  53. vendor, product, sernum, ifcount, description = device
  54. # Close and remove the device from our list.
  55. if sernum in list(__devices.keys()):
  56. __devices[sernum].close()
  57. del __devices[sernum]
  58. print('detached', sernum)
  59. if __name__ == '__main__':
  60. main()