garage/garage.py

76 lines
1.5 KiB
Python
Raw Normal View History

2022-12-26 17:03:35 -05:00
import json
import sys
from configparser import ConfigParser
2022-12-26 17:10:16 -05:00
from gpiozero import Button
2022-12-26 17:03:35 -05:00
from signal import pause
from signal import signal
from signal import SIGTERM
from threading import Lock
state = {"east-door": "opened", "west-door": "opened"}
threadLock = Lock()
stateFile = 'www/garage.json'
def main():
signal(SIGTERM, cleanExit())
config = ConfigParser()
config.read('garage.cfg')
eastPin = config['garage'].getint('east-pin')
westPin = config['garage'].getint('west-pin')
# eastUri = config['push'].getint('east-uri')
# westUri = config['push'].getint('west-uri')
persistState()
eastButton = Button(eastPin)
westButton = Button(westPin)
2022-12-26 17:19:02 -05:00
eastButton.when_pressed = eastDoorClosed
eastButton.when_released = eastDoorOpened
westButton.when_pressed = westDoorClosed
westButton.when_released = westDoorOpened
2022-12-26 17:14:02 -05:00
2022-12-26 17:03:35 -05:00
pause()
def eastDoorOpened():
2022-12-26 17:22:51 -05:00
with threadLock:
2022-12-26 17:03:35 -05:00
state["east-door"] = "opened"
persistState()
def eastDoorClosed():
2022-12-26 17:22:51 -05:00
with threadLock:
2022-12-26 17:03:35 -05:00
state["east-door"] = "closed"
persistState()
def westDoorOpened():
2022-12-26 17:22:51 -05:00
with threadLock:
2022-12-26 17:03:35 -05:00
state["west-door"] = "opened"
persistState()
def westDoorClosed():
2022-12-26 17:22:51 -05:00
with threadLock:
2022-12-26 17:03:35 -05:00
state["west-door"] = "closed"
persistState()
def persistState():
with open(stateFile, 'w') as file:
json.dump(state, file)
def cleanExit():
def _exit_(signum, frame):
sys.exit(0)
return _exit_
if __name__ == '__main__':
main()