logging and JSON

This commit is contained in:
2021-05-13 15:51:08 +02:00
parent 12ef30fe2b
commit 57bdbf85ee
+22 -16
View File
@@ -1,16 +1,22 @@
import RPi.GPIO as GPIO
import logging
import logging.handlers as handlers
import time
from threading import Thread
import cherrypy # https://docs.cherrypy.org/en/latest/tutorials.html#tutorial-1-a-basic-web-application
import urllib.request, json
cherrypy.config.update({
'server.socket_host' : '192.168.178.49',
'server.socket_port' : 8081,
})
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s - %(message)s')
logger = logging.getLogger('Rotating Log')
logger.setLevel(logging.INFO)
logHandler = handlers.RotatingFileHandler('garage.log', maxBytes=1000000, backupCount=2)
logHandler.setLevel(logging.INFO)
logHandler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s - %(message)s'))
logger.addHandler(logHandler)
PIN_GARAGE_BUTTON = 23
@@ -23,7 +29,6 @@ PIN_DOOR_CLOSED = 13
PIN_SENSOR_GATE= 19
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_GARAGE_BUTTON, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(PIN_GATE_BUTTON, GPIO.IN, GPIO.PUD_UP)
@@ -35,29 +40,30 @@ GPIO.setup(PIN_DOOR_CLOSED, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(PIN_SENSOR_GATE, GPIO.IN, GPIO.PUD_UP)
def garage_button(channel):
time.sleep(0.01) # el cheapo relay module creates lots of interference, so we check again after a waiting period (see https://electronics.stackexchange.com/a/361793/253533)
if channel == 0 or GPIO.input(PIN_GARAGE_BUTTON) == False:
logging.info("trigger motor")
logger.info("trigger motor")
GPIO.output(PIN_GARAGE, GPIO.LOW)
GPIO.output(PIN_LED, GPIO.HIGH)
time.sleep(0.4)
GPIO.output(PIN_GARAGE, GPIO.HIGH)
GPIO.output(PIN_LED, GPIO.LOW)
else:
logging.info("flare (motor button)")
logger.info("flare (motor button)")
def gate_button(channel):
time.sleep(0.01)
if channel == 0 or GPIO.input(PIN_GATE_BUTTON) == False:
logging.info("trigger remote")
logger.info("trigger remote")
GPIO.output(PIN_GATE, GPIO.HIGH)
GPIO.output(PIN_LED, GPIO.HIGH)
time.sleep(0.4)
GPIO.output(PIN_GATE, GPIO.LOW)
GPIO.output(PIN_LED, GPIO.LOW)
else:
logging.info("flare (remote button)")
logger.info("flare (remote button)")
GPIO.add_event_detect(PIN_GARAGE_BUTTON, GPIO.FALLING, callback=garage_button, bouncetime=250)
GPIO.add_event_detect(PIN_GATE_BUTTON, GPIO.FALLING, callback=gate_button, bouncetime=250)
@@ -88,29 +94,29 @@ class GarageService(object):
def garage(self, action=None):
if (action == "open" and GPIO.input(PIN_DOOR_CLOSED) and not GPIO.input(PIN_DOOR_OPEN)):
Thread(target=garage_button,args=[0]).start()
return "{ action: open }"
return '{ "action": "open" }'
elif (action == "close" and GPIO.input(PIN_DOOR_OPEN) and not GPIO.input(PIN_DOOR_CLOSED)):
Thread(target=garage_button,args=[0]).start()
return "{ action: close }"
return '{ "action": "close" }'
if (GPIO.input(PIN_DOOR_OPEN) and not GPIO.input(PIN_DOOR_CLOSED)):
status = "true"
else:
status = "false"
return "{ status: " + status + " }"
return '{ "status": "' + status + '" }'
@cherrypy.expose
def gate(self, action=None):
if (action == "open" and not GPIO.input(PIN_SENSOR_GATE)):
Thread(target=gate_button,args=[0]).start()
return "{ action: open }"
return '{ "action": "open" }'
elif (action == "close" and GPIO.input(PIN_SENSOR_GATE)):
Thread(target=gate_button,args=[0]).start()
return "{ action: close }"
return '{ "action": "close" }'
if GPIO.input(PIN_SENSOR_GATE) == 0:
status = "false"
else:
status = "true"
return "{ status: " + status + " }"
return '{ "status": "' + status + '" }'
@cherrypy.expose
def all(self):
@@ -120,12 +126,12 @@ class GarageService(object):
@cherrypy.expose
def status(self):
return "{ open: " + str(GPIO.input(PIN_DOOR_OPEN)) + ", closed: " + str(GPIO.input(PIN_DOOR_CLOSED)) + ", gate: " + str(GPIO.input(PIN_SENSOR_GATE)) + " }"
return '{ "open": ' + str(GPIO.input(PIN_DOOR_OPEN)) + ', "closed": ' + str(GPIO.input(PIN_DOOR_CLOSED)) + ', "gate": ' + str(GPIO.input(PIN_SENSOR_GATE)) + ' }'
cherrypy.quickstart(GarageService())
logging.info('Shutdown')
logging.info('Reset GPIO configuration and close')
logger.info('Shutdown')
logger.info('Reset GPIO configuration and close')
GPIO.cleanup()