88 lines
1.7 KiB
Python
88 lines
1.7 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():
|
|
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()
|