pirius

garagedoor.py

May 16th, 2017
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.47 KB | None | 0 0
  1. #garagedoor.py
  2. version = '20170505-dev'
  3. import logging
  4. logging.basicConfig(filename='/var/log/pi/garagedoor.pylog',level=logging.DEBUG)
  5.  
  6. import time
  7. import datetime
  8. import sys
  9. from alarmdecoder import AlarmDecoder
  10. from alarmdecoder.devices import SocketDevice
  11. import subprocess
  12. from pymyq import MyQAPI as pymyq
  13.  
  14. KFUSER1 = 'redacted'
  15. KFID11 = '0000000'
  16. KFID12 = '0000001'
  17. KFUSER2 = 'redacted'
  18. KFID21 = '0000010'
  19. KFID22 = '0000011'
  20. KFUSER3 = 'redacted'
  21. KFID31 = '0000020'
  22. KFID32 = '0000021'
  23. GDOORID = 0
  24. myq = pymyq('redacted', 'redacted', 'redacted')
  25.  
  26. HOSTNAME = 'localhost'
  27. PORT = 10000
  28.  
  29. def main():
  30.     print "{} garagedoor.py version {}".format(ts, version)
  31.     logging.info("{} garagedoor.py version {}".format(ts, version))
  32.  
  33. #    Example application that watches for an event from a specific RF device.
  34.  
  35. #    This feature allows you to watch for events from RF devices if you have
  36. #    an RF receiver.  This is useful in the case of internal sensors, which
  37. #    don't emit a FAULT if the sensor is tripped and the panel is armed STAY.
  38. #    It also will monitor sensors that aren't configured.
  39.  
  40. #    NOTE: You must have an RF receiver installed and enabled in your panel
  41. #          for RFX messages to be seen.
  42.     while True:
  43.         try:
  44.             # Retrieve an AD2 device that has been exposed with ser2sock on localhost:10000.
  45.             device = AlarmDecoder(SocketDevice(interface=(HOSTNAME, PORT)))
  46.  
  47.             # Set up an event handler and open the device
  48.             device.on_rfx_message += handle_rfx
  49.             with device.open():
  50.                 while True:
  51.                     time.sleep(1)
  52.  
  53.         except Exception as e:
  54.             ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  55.             print(ts,'Exception:', e)
  56.             logging.info(ts + 'Exception: ' + e)
  57.             continue
  58.  
  59. def handle_rfx(sender, message):
  60. # Handles RF message events from the AlarmDecoder.
  61. # Check for our target serial number and loop
  62.  
  63.     # LOOP->BUTTON (SERIAL 1):
  64.     # 0 = D: PANIC/STAR
  65.     # 1 = B: DISARM/CIRCLE/OPEN LOCK
  66.     # 2 = A: ARM AWAY/MAN OUTSIDE HOUSE/CLOSED LOCK
  67.     # 3 = C: ARM STAY/MAN INSIDE HOUSE
  68.     # LOOP->BUTTON (SERIAL 2):
  69.     # 0 = AB: TOP 2
  70.     # 1 = CD: BOTTOM 2
  71.     # 2 = AC: LEFT 2
  72.     # 3 = BD: RIGHT 2
  73.  
  74.     if message.serial_number == KFID11 and message.loop[0] == True:
  75.  
  76.         ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  77.         print "{} - {}'s keyfob attempted to open the garage door.".format(ts, KFUSER1)
  78.         logging.info(ts + " - " + KFUSER1 + "'s keyfob attempted to open the garage door.")
  79.  
  80.         if garagedoor(0):
  81.             ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  82.             print "{} - {}'s keyfob successfully opened the garage door.".format(ts, KFUSER1)
  83.             logging.info(ts + " - " + KFUSER1 + "'s keyfob successfully opened the garage door.")
  84.  
  85.         else:
  86.             ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  87.             print "{} - {}'s keyfob failed to open the garage door.".format(ts, KFUSER1)
  88.             logging.info(ts + " - " + KFUSER1 + "'s keyfob failed to open the garage door.")
  89.  
  90.     elif message.serial_number == KFID21 and message.loop[0] == True:
  91.  
  92.         ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  93.         print "{} - {}'s keyfob attempted to open the garage door".format(ts, KFUSER2)
  94.         logging.info(ts + " - " + KFUSER2 + "'s keyfob attempted to open the garage door.")
  95.        
  96.         if garagedoor(0):
  97.            ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  98.            print "{} - {}'s keyfob successfully opened the garage door.".format(ts, KFUSER2)
  99.            logging.info(ts + " - " + KFUSER2 + "'s keyfob successfully opened the garage door.")
  100.  
  101.         else:
  102.            ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  103.            print "{} - {}'s keyfob failed to open the garage door.".format(ts, KFUSER2)
  104.            logging.info(ts + " - " + KFUSER2 + "'s keyfob failed to open the garage door.")
  105.  
  106.     elif message.serial_number == KFID31 and message.loop[0] == True:
  107.         ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  108.         print "{} - {}'s keyfob attempted to open the garage door.".format(ts, KFUSER3)
  109.         logging.info(ts + " - " + KFUSER3 + "'s keyfob attempted to open the garage door.")
  110.  
  111.         if garagedoor(0):
  112.             ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  113.             print "{} - {}'s keyfob successfully opened the garage door.".format(ts, KFUSER3)
  114.             logging.info(ts + " - " + KFUSER3 + "'s keyfob successfully opened the garage door.")
  115.  
  116.         else:
  117.             ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  118.             print "{} - {}'s keyfob failed to open the garage door.".format(ts, KFUSER3)
  119.             logging.info(ts + " - " + KFUSER3 + "'s keyfob failed to open the garage door.")
  120.  
  121.     elif message.serial_number == KFID12 and message.loop[1] == True:
  122.         ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  123.         print "{} - {}'s keyfob attempted to close the garage door.".format(ts, KFUSER1)
  124.         logging.info(ts + " - " + KFUSER1 + "'s keyfob attempted to open the garage door.")
  125.  
  126.         if garagedoor(1):
  127.             ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  128.             print "{} - {}'s keyfob sent the close command.".format(ts, KFUSER1)
  129.             logging.info(ts + " - " + KFUSER1 + "'s keyfob sent the close command.")
  130.         else:
  131.             ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  132.             print "{} - {}'s keyfob failed to close the garage door.".format(ts, KFUSER1)
  133.             logging.info(ts + " - " + KFUSER1 + "'s keyfob failed to close the garage door.")
  134.  
  135.     elif message.serial_number == KFID22 and message.loop[1] == True:
  136.         ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  137.         print "{} - {}'s keyfob attempted to close the garage door.".format(ts, KFUSER2)
  138.         logging.info(ts + " - " + KFUSER2 + "'s keyfob attempted to close the garage door.")
  139.  
  140.         if garagedoor(1):
  141.             ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  142.             print "{} - {}'s keyfob sent the close command.".format(ts, KFUSER2)
  143.             logging.info(ts + " - " + KFUSER2 + "'s keyfob sent the close command.")
  144.         else:
  145.             ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  146.             print "{} - {}'s keyfob failed to close the garage door.".format(ts, KFUSER2)
  147.             logging.info(ts + " - " + KFUSER2 + "'s keyfob failed to close the garage door.")
  148.  
  149.     elif message.serial_number == KFID32 and message.loop[1] == True:
  150.         ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  151.         print "{} - {}'s keyfob attempted to close the garage door.".format(ts, KFUSER3)
  152.         logging.info(ts + " - " + KFUSER3 + "'s keyfob attempted to close the garage door.")
  153.  
  154.         if garagedoor(1):
  155.             ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  156.             print "{} - {}'s keyfob successfully close the garage door.".format(ts, KFUSER3)
  157.             logging.info(ts + " - " + KFUSER3 + "'s keyfob sent the close command.")
  158.         else:
  159.             ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  160.             print "{} - {}'s keyfob failed to close the garage door.".format(ts, KFUSER3)
  161.             logging.info(ts + " - " + KFUSER3 + "'s keyfob failed to close the garage door.")
  162.  
  163. def garagedoor(action):
  164.     try:
  165.         if myq.is_login_valid():
  166.             if action == 0:
  167.                 if myq.open_device(GDOORID):
  168.                     return True
  169.                 else:
  170.                     ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  171.                     print "{} - myq.opendevice(GDOORID) is not!".format.ts
  172.                     logging.warning(ts + " - " + "myq.opendevice(GDOORID) is not!")
  173.                     return False
  174.             elif action == 1:
  175.                 if myq.close_device(GDOORID):
  176.                     return True
  177.                 else:
  178.                     ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  179.                     print "{} - myq.closedevice(GDOORID) is not!".format(ts)
  180.                     logging.warning(ts + " - " + "myq.closedevice(GDOORID) is not!")
  181.                     return False
  182.             else:
  183.                 ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  184.                 print '{} - myq.is_login_valid() is not!'.format(ts)
  185.                 logging.warning(ts + " - " + "myq.is_login_valid() is not!")
  186.                 return False
  187.         else:
  188.              ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  189.              print "{} - myq.is_login_valid() is not!".format(ts)
  190.              logging.warning(ts + " - " + "myq.is_login_valid() is not!")
  191.              return False
  192.  
  193.     except Exception as e:
  194.             ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
  195.             print(ts,'Exception:', e)
  196.             logging.info(ts + 'Exception: ' + e)
  197.             return False
  198.  
  199.  
  200. if __name__ == '__main__':
  201.     main()
Advertisement
Add Comment
Please, Sign In to add comment