Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #garagedoor.py
- version = '20170505-dev'
- import logging
- logging.basicConfig(filename='/var/log/pi/garagedoor.pylog',level=logging.DEBUG)
- import time
- import datetime
- import sys
- from alarmdecoder import AlarmDecoder
- from alarmdecoder.devices import SocketDevice
- import subprocess
- from pymyq import MyQAPI as pymyq
- KFUSER1 = 'redacted'
- KFID11 = '0000000'
- KFID12 = '0000001'
- KFUSER2 = 'redacted'
- KFID21 = '0000010'
- KFID22 = '0000011'
- KFUSER3 = 'redacted'
- KFID31 = '0000020'
- KFID32 = '0000021'
- GDOORID = 0
- myq = pymyq('redacted', 'redacted', 'redacted')
- HOSTNAME = 'localhost'
- PORT = 10000
- def main():
- print "{} garagedoor.py version {}".format(ts, version)
- logging.info("{} garagedoor.py version {}".format(ts, version))
- # Example application that watches for an event from a specific RF device.
- # This feature allows you to watch for events from RF devices if you have
- # an RF receiver. This is useful in the case of internal sensors, which
- # don't emit a FAULT if the sensor is tripped and the panel is armed STAY.
- # It also will monitor sensors that aren't configured.
- # NOTE: You must have an RF receiver installed and enabled in your panel
- # for RFX messages to be seen.
- while True:
- try:
- # Retrieve an AD2 device that has been exposed with ser2sock on localhost:10000.
- device = AlarmDecoder(SocketDevice(interface=(HOSTNAME, PORT)))
- # Set up an event handler and open the device
- device.on_rfx_message += handle_rfx
- with device.open():
- while True:
- time.sleep(1)
- except Exception as e:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print(ts,'Exception:', e)
- logging.info(ts + 'Exception: ' + e)
- continue
- def handle_rfx(sender, message):
- # Handles RF message events from the AlarmDecoder.
- # Check for our target serial number and loop
- # LOOP->BUTTON (SERIAL 1):
- # 0 = D: PANIC/STAR
- # 1 = B: DISARM/CIRCLE/OPEN LOCK
- # 2 = A: ARM AWAY/MAN OUTSIDE HOUSE/CLOSED LOCK
- # 3 = C: ARM STAY/MAN INSIDE HOUSE
- # LOOP->BUTTON (SERIAL 2):
- # 0 = AB: TOP 2
- # 1 = CD: BOTTOM 2
- # 2 = AC: LEFT 2
- # 3 = BD: RIGHT 2
- if message.serial_number == KFID11 and message.loop[0] == True:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob attempted to open the garage door.".format(ts, KFUSER1)
- logging.info(ts + " - " + KFUSER1 + "'s keyfob attempted to open the garage door.")
- if garagedoor(0):
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob successfully opened the garage door.".format(ts, KFUSER1)
- logging.info(ts + " - " + KFUSER1 + "'s keyfob successfully opened the garage door.")
- else:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob failed to open the garage door.".format(ts, KFUSER1)
- logging.info(ts + " - " + KFUSER1 + "'s keyfob failed to open the garage door.")
- elif message.serial_number == KFID21 and message.loop[0] == True:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob attempted to open the garage door".format(ts, KFUSER2)
- logging.info(ts + " - " + KFUSER2 + "'s keyfob attempted to open the garage door.")
- if garagedoor(0):
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob successfully opened the garage door.".format(ts, KFUSER2)
- logging.info(ts + " - " + KFUSER2 + "'s keyfob successfully opened the garage door.")
- else:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob failed to open the garage door.".format(ts, KFUSER2)
- logging.info(ts + " - " + KFUSER2 + "'s keyfob failed to open the garage door.")
- elif message.serial_number == KFID31 and message.loop[0] == True:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob attempted to open the garage door.".format(ts, KFUSER3)
- logging.info(ts + " - " + KFUSER3 + "'s keyfob attempted to open the garage door.")
- if garagedoor(0):
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob successfully opened the garage door.".format(ts, KFUSER3)
- logging.info(ts + " - " + KFUSER3 + "'s keyfob successfully opened the garage door.")
- else:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob failed to open the garage door.".format(ts, KFUSER3)
- logging.info(ts + " - " + KFUSER3 + "'s keyfob failed to open the garage door.")
- elif message.serial_number == KFID12 and message.loop[1] == True:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob attempted to close the garage door.".format(ts, KFUSER1)
- logging.info(ts + " - " + KFUSER1 + "'s keyfob attempted to open the garage door.")
- if garagedoor(1):
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob sent the close command.".format(ts, KFUSER1)
- logging.info(ts + " - " + KFUSER1 + "'s keyfob sent the close command.")
- else:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob failed to close the garage door.".format(ts, KFUSER1)
- logging.info(ts + " - " + KFUSER1 + "'s keyfob failed to close the garage door.")
- elif message.serial_number == KFID22 and message.loop[1] == True:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob attempted to close the garage door.".format(ts, KFUSER2)
- logging.info(ts + " - " + KFUSER2 + "'s keyfob attempted to close the garage door.")
- if garagedoor(1):
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob sent the close command.".format(ts, KFUSER2)
- logging.info(ts + " - " + KFUSER2 + "'s keyfob sent the close command.")
- else:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob failed to close the garage door.".format(ts, KFUSER2)
- logging.info(ts + " - " + KFUSER2 + "'s keyfob failed to close the garage door.")
- elif message.serial_number == KFID32 and message.loop[1] == True:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob attempted to close the garage door.".format(ts, KFUSER3)
- logging.info(ts + " - " + KFUSER3 + "'s keyfob attempted to close the garage door.")
- if garagedoor(1):
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob successfully close the garage door.".format(ts, KFUSER3)
- logging.info(ts + " - " + KFUSER3 + "'s keyfob sent the close command.")
- else:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - {}'s keyfob failed to close the garage door.".format(ts, KFUSER3)
- logging.info(ts + " - " + KFUSER3 + "'s keyfob failed to close the garage door.")
- def garagedoor(action):
- try:
- if myq.is_login_valid():
- if action == 0:
- if myq.open_device(GDOORID):
- return True
- else:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - myq.opendevice(GDOORID) is not!".format.ts
- logging.warning(ts + " - " + "myq.opendevice(GDOORID) is not!")
- return False
- elif action == 1:
- if myq.close_device(GDOORID):
- return True
- else:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - myq.closedevice(GDOORID) is not!".format(ts)
- logging.warning(ts + " - " + "myq.closedevice(GDOORID) is not!")
- return False
- else:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print '{} - myq.is_login_valid() is not!'.format(ts)
- logging.warning(ts + " - " + "myq.is_login_valid() is not!")
- return False
- else:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print "{} - myq.is_login_valid() is not!".format(ts)
- logging.warning(ts + " - " + "myq.is_login_valid() is not!")
- return False
- except Exception as e:
- ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
- print(ts,'Exception:', e)
- logging.info(ts + 'Exception: ' + e)
- return False
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment