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.

65 lines
1.7 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, ex:
  30. print 'Exception:', ex
  31. def handle_alarm(sender, **kwargs):
  32. """
  33. Handles alarm events from the AlarmDecoder.
  34. """
  35. status = kwargs.pop('status', None)
  36. zone = kwargs.pop('zone', None)
  37. text = "Alarm status: {0} - Zone {1}".format(status, zone)
  38. # Build the email message
  39. msg = MIMEText(text)
  40. msg['Subject'] = SUBJECT
  41. msg['From'] = FROM_ADDRESS
  42. msg['To'] = TO_ADDRESS
  43. s = smtplib.SMTP(SMTP_SERVER)
  44. # Authenticate if needed
  45. if SMTP_USERNAME is not None:
  46. s.login(SMTP_USERNAME, SMTP_PASSWORD)
  47. # Send the email
  48. s.sendmail(FROM_ADDRESS, TO_ADDRESS, msg.as_string())
  49. s.quit()
  50. print 'sent alarm email:', text
  51. if __name__ == '__main__':
  52. main()