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.

alarm_email.py 1.6 KiB

11 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import time
  2. import smtplib
  3. from email.mime.text import MIMEText
  4. from pyad2 import AD2
  5. from pyad2.devices import USBDevice
  6. # Configuration values
  7. SUBJECT = "Alarm Decoder - 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 = AD2(USBDevice.find())
  22. # Set up an event handler and open the device
  23. device.on_alarm += handle_alarm
  24. device.open()
  25. # Retrieve the device configuration
  26. device.get_config()
  27. # Wait for events
  28. while True:
  29. time.sleep(1)
  30. except Exception, ex:
  31. print 'Exception:', ex
  32. finally:
  33. device.close()
  34. def handle_alarm(sender, *args, **kwargs):
  35. """
  36. Handles alarm events from the AD2.
  37. """
  38. status = kwargs['status']
  39. text = "Alarm status: {0}".format(status)
  40. # Build the email message
  41. msg = MIMEText(text)
  42. msg['Subject'] = SUBJECT
  43. msg['From'] = FROM_ADDRESS
  44. msg['To'] = TO_ADDRESS
  45. s = smtplib.SMTP(SMTP_SERVER)
  46. # Authenticate if needed
  47. if SMTP_USERNAME is not None:
  48. s.login(SMTP_USERNAME, SMTP_PASSWORD)
  49. # Send the email
  50. s.sendmail(FROM_ADDRESS, TO_ADDRESS, msg.as_string())
  51. s.quit()
  52. print 'sent alarm email:', text
  53. if __name__ == '__main__':
  54. main()