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.

60 lines
1.5 KiB

  1. import time
  2. import smtplib
  3. from email.mime.text import MIMEText
  4. from alarmdecoder import AlarmDecoder
  5. from alarmdecoder.devices import USBDevice
  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. def main():
  15. """
  16. Example application that sends an email when an alarm event is
  17. detected.
  18. """
  19. try:
  20. # Retrieve the first USB device
  21. device = AlarmDecoder(USBDevice.find())
  22. # Set up an event handler and open the device
  23. device.on_alarm += handle_alarm
  24. with device.open():
  25. while True:
  26. time.sleep(1)
  27. except Exception, ex:
  28. print 'Exception:', ex
  29. def handle_alarm(sender, status):
  30. """
  31. Handles alarm events from the AlarmDecoder.
  32. """
  33. text = "Alarm status: {0}".format(status)
  34. # Build the email message
  35. msg = MIMEText(text)
  36. msg['Subject'] = SUBJECT
  37. msg['From'] = FROM_ADDRESS
  38. msg['To'] = TO_ADDRESS
  39. s = smtplib.SMTP(SMTP_SERVER)
  40. # Authenticate if needed
  41. if SMTP_USERNAME is not None:
  42. s.login(SMTP_USERNAME, SMTP_PASSWORD)
  43. # Send the email
  44. s.sendmail(FROM_ADDRESS, TO_ADDRESS, msg.as_string())
  45. s.quit()
  46. print 'sent alarm email:', text
  47. if __name__ == '__main__':
  48. main()