REST service with CherryPy

This commit is contained in:
2020-08-05 09:14:26 +00:00
parent c32502eac5
commit 94a1c7b621
+39 -13
View File
@@ -3,6 +3,13 @@ import logging
import time
from threading import Thread
import cherrypy # https://docs.cherrypy.org/en/latest/tutorials.html#tutorial-1-a-basic-web-application
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')
@@ -20,35 +27,54 @@ GPIO.setup(PIN_REMOTE, GPIO.OUT)
GPIO.setup(PIN_LED, GPIO.OUT)
def check_motor_button(channel):
logging.info("Motor pressed")
def motor_button(channel):
logging.info("trigger motor")
GPIO.output(PIN_MOTOR, GPIO.LOW)
GPIO.output(PIN_LED, GPIO.HIGH)
time.sleep(0.8)
GPIO.output(PIN_MOTOR, GPIO.HIGH)
GPIO.output(PIN_LED, GPIO.LOW)
time.sleep(1)
def check_remote_button(channel):
logging.info("Remote pressed")
def remote_button(channel):
logging.info("trigger remote")
GPIO.output(PIN_REMOTE, GPIO.HIGH)
GPIO.output(PIN_LED, GPIO.HIGH)
time.sleep(0.8)
GPIO.output(PIN_REMOTE, GPIO.LOW)
GPIO.output(PIN_LED, GPIO.LOW)
time.sleep(1)
GPIO.add_event_detect(PIN_MOTOR_BUTTON, GPIO.FALLING, callback=check_motor_button, bouncetime=200)
GPIO.add_event_detect(PIN_REMOTE_BUTTON, GPIO.FALLING, callback=check_remote_button, bouncetime=200)
GPIO.add_event_detect(PIN_MOTOR_BUTTON, GPIO.FALLING, callback=motor_button, bouncetime=200)
GPIO.add_event_detect(PIN_REMOTE_BUTTON, GPIO.FALLING, callback=remote_button, bouncetime=200)
try:
GPIO.output(PIN_MOTOR, GPIO.HIGH)
GPIO.output(PIN_REMOTE, GPIO.LOW)
GPIO.output(PIN_LED, GPIO.LOW)
while True:
time.sleep(0.01)
except KeyboardInterrupt:
class GarageService(object):
@cherrypy.expose
def index(self):
return "Hello World!"
@cherrypy.expose
def motor(self):
Thread(target=motor_button,args=[0]).start()
return "trigger motor"
@cherrypy.expose
def remote(self):
Thread(target=remote_button,args=[0]).start()
return "trigger remote"
@cherrypy.expose
def all(self):
Thread(target=motor_button,args=[0]).start()
Thread(target=remote_button,args=[0]).start()
return "trigger all"
cherrypy.quickstart(GarageService())
logging.info('Shutdown')
finally:
logging.info('Reset GPIO configuration and close')
GPIO.cleanup()