garage/garage.py

83 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)
pause()
def eastDoorOpened():
try:
threadLock.aquire()
state["east-door"] = "opened"
persistState()
finally:
threadLock.release()
def eastDoorClosed():
try:
threadLock.aquire()
state["east-door"] = "closed"
persistState()
finally:
threadLock.release()
def westDoorOpened():
try:
threadLock.aquire()
state["west-door"] = "opened"
persistState()
finally:
threadLock.release()
def westDoorClosed():
try:
threadLock.aquire()
state["west-door"] = "closed"
persistState()
finally:
threadLock.release()
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()