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.

64 lines
1.6 KiB

  1. import time
  2. import smtplib
  3. from email.mime.text import MIMEText
  4. from alarmdecoder import AlarmDecoder
  5. from alarmdecoder.devices import SerialDevice
  6. # Configuration values
  7. SUBJECT = "AlarmDecoder - ALARM"
  8. FROM_ADDRESS = "root@localhost"
  9. TO_ADDRESS = "root@localhost" # NOTE: Sending an SMS is as easy as looking
  10. # up the email address format for your provider.
  11. SMTP_SERVER = "localhost"
  12. SMTP_USERNAME = None
  13. SMTP_PASSWORD = None
  14. SERIAL_DEVICE = '/dev/ttyUSB0'
  15. BAUDRATE = 115200
  16. def main():
  17. """
  18. Example application that sends an email when an alarm event is
  19. detected.
  20. """
  21. try:
  22. # Retrieve the first USB device
  23. device = AlarmDecoder(SerialDevice(interface=SERIAL_DEVICE))
  24. # Set up an event handler and open the device
  25. device.on_alarm += handle_alarm
  26. with device.open(baudrate=BAUDRATE):
  27. while True:
  28. time.sleep(1)
  29. except Exception as ex:
  30. print('Exception:', ex)
  31. def handle_alarm(sender, **kwargs):
  32. """
  33. Handles alarm events from the AlarmDecoder.
  34. """
  35. zone = kwargs.pop('zone', None)
  36. text = "Alarm: Zone {0}".format(zone)
  37. # Build the email message
  38. msg = MIMEText(text)
  39. msg['Subject'] = SUBJECT
  40. msg['From'] = FROM_ADDRESS
  41. msg['To'] = TO_ADDRESS
  42. s = smtplib.SMTP(SMTP_SERVER)
  43. # Authenticate if needed
  44. if SMTP_USERNAME is not None:
  45. s.login(SMTP_USERNAME, SMTP_PASSWORD)
  46. # Send the email
  47. s.sendmail(FROM_ADDRESS, TO_ADDRESS, msg.as_string())
  48. s.quit()
  49. print('sent alarm email:', text)
  50. if __name__ == '__main__':
  51. main()