Browse Source

Add 'server.py'

master
kiichan 2 years ago
parent
commit
ef1e88d80c
  1. 54
      server.py

54
server.py

@ -0,0 +1,54 @@
vacation_mode = True
DEBUG = False
def dbg_set():
if DEBUG:
return print
def nop(*args, **kwargs):
return None
return nop
dbg = dbg_set()
import socketserver
import threading
from gpiozero import LED
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
tx = self.request.recv(1024)
power = eval(str(tx, 'utf-8'))
dbg(tx,power)
dbg(power)
if (power <= 30 and not vacation_mode) or (power >= 80 and vacation_mode):
# Brings power to the "normally off" outlets
onoff("on")
elif (power >= 80 and not vacation_mode) or (power <= 30 and vacation_mode):
# Brings power to the "normally on" outlets
onoff("off")
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
def onoff(O):
if O == "on":
dbg("going on!!!")
led.on()
else:
dbg("Off time")
led.off()
if __name__ == "__main__":
HOST, PORT = "", 12894
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
led = LED(16) # Pin #36 (with USB facing down, two above bottom right - ground directly above)
server_thread = threading.Thread(target=server.serve_forever)
try:
server_thread.start()
except Exception as e:
server.shutdown()
server.server_close()
dbg("Exiting with error:")
Loading…
Cancel
Save