76 lines
1.5 KiB
Python
76 lines
1.5 KiB
Python
import json
|
|
import sys
|
|
|
|
from configparser import ConfigParser
|
|
from gpiozero import Button
|
|
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)
|
|
|
|
eastButton.when_pressed = eastDoorClosed
|
|
eastButton.when_released = eastDoorOpened
|
|
westButton.when_pressed = westDoorClosed
|
|
westButton.when_released = westDoorOpened
|
|
|
|
pause()
|
|
|
|
|
|
def eastDoorOpened():
|
|
with threadLock:
|
|
state["east-door"] = "opened"
|
|
persistState()
|
|
|
|
|
|
def eastDoorClosed():
|
|
with threadLock:
|
|
state["east-door"] = "closed"
|
|
persistState()
|
|
|
|
|
|
def westDoorOpened():
|
|
with threadLock:
|
|
state["west-door"] = "opened"
|
|
persistState()
|
|
|
|
|
|
def westDoorClosed():
|
|
with threadLock:
|
|
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()
|