204 lines
5.8 KiB
Python
Executable File
204 lines
5.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Very simple HTTP server in python for logging requests
|
|
Usage::
|
|
./server.py [<port>]
|
|
"""
|
|
import RPi.GPIO as GPIO
|
|
import http.server
|
|
import socketserver
|
|
import logging
|
|
import sys
|
|
import os
|
|
import subprocess
|
|
import cgi
|
|
import time
|
|
GPIO.setwarnings(False)
|
|
os.system('pkill -9 httpd')
|
|
GPIO.setmode(GPIO.BCM)
|
|
pinList = [2, 3, 4, 17]
|
|
SleepTimeL=10
|
|
|
|
class S(http.server.SimpleHTTPRequestHandler):
|
|
|
|
def do_GET(self):
|
|
|
|
GPIO.setup(pinList[0], GPIO.OUT)
|
|
status0=int(GPIO.input(pinList[0]))
|
|
GPIO.setup(pinList[1], GPIO.OUT)
|
|
status1=int(GPIO.input(pinList[1]))
|
|
GPIO.setup(pinList[2], GPIO.OUT)
|
|
status2=int(GPIO.input(pinList[2]))
|
|
GPIO.setup(pinList[3], GPIO.OUT)
|
|
status3=int(GPIO.input(pinList[3]))
|
|
#logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
|
|
|
|
self._set_response()
|
|
pass
|
|
|
|
def do_POST(self):
|
|
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
|
|
post_data = self.rfile.read(content_length) # <--- Gets the data itself
|
|
logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
|
|
str(self.path), str(self.headers), post_data.decode('utf-8'))
|
|
|
|
self._set_response()
|
|
self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
|
|
|
|
def _set_response(self):
|
|
#time.sleep(5)
|
|
GPIO.setup(pinList[0], GPIO.OUT)
|
|
status0=int(GPIO.input(pinList[0]))
|
|
GPIO.setup(pinList[1], GPIO.OUT)
|
|
status1=int(GPIO.input(pinList[1]))
|
|
GPIO.setup(pinList[2], GPIO.OUT)
|
|
status2=int(GPIO.input(pinList[2]))
|
|
GPIO.setup(pinList[3], GPIO.OUT)
|
|
status3=int(GPIO.input(pinList[3]))
|
|
|
|
objParsedPath = str(format(self.path).encode('utf-8'))
|
|
print ('objParsedPath="'+objParsedPath+'"')
|
|
|
|
if objParsedPath == "b'/R0'":
|
|
print ('R0 run')
|
|
if status0 == 1:
|
|
os.system('./relay.py open 0')
|
|
else:
|
|
os.system('./relay.py close 0')
|
|
time.sleep(int(SleepTimeL));
|
|
os.system('./relay.py open 0')
|
|
|
|
if objParsedPath == "b'/R0O'":
|
|
print ('R0 run0')
|
|
if status0 == 1:
|
|
os.system('./relay.py open 0')
|
|
else:
|
|
os.system('./relay.py close 0')
|
|
|
|
|
|
|
|
if objParsedPath == "b'/R1'":
|
|
print ('R1 run')
|
|
if status1 == 1:
|
|
os.system('./relay.py open 1')
|
|
else:
|
|
os.system('./relay.py close 1')
|
|
time.sleep(int(SleepTimeL));
|
|
os.system('./relay.py open 1')
|
|
|
|
if objParsedPath == "b'/R1O'":
|
|
print ('R1 run0')
|
|
if status1 == 1:
|
|
os.system('./relay.py open 1')
|
|
else:
|
|
os.system('./relay.py close 1')
|
|
|
|
|
|
if objParsedPath == "b'/R2'":
|
|
print ('R2 run')
|
|
if status2 == 1:
|
|
os.system('./relay.py open 2')
|
|
else:
|
|
os.system('./relay.py close 2')
|
|
time.sleep(int(SleepTimeL));
|
|
os.system('./relay.py open 2')
|
|
|
|
if objParsedPath == "b'/R2O'":
|
|
print ('R2 run0')
|
|
if status2 == 1:
|
|
os.system('./relay.py open 2')
|
|
else:
|
|
os.system('./relay.py close 2')
|
|
|
|
|
|
if objParsedPath == "b'/R3'":
|
|
print ('R3 run')
|
|
if status3 == 1:
|
|
os.system('./relay.py open 3')
|
|
else:
|
|
os.system('./relay.py close 3')
|
|
time.sleep(int(SleepTimeL));
|
|
os.system('./relay.py open 3')
|
|
|
|
if objParsedPath == "b'/R3O'":
|
|
print ('R3 run0')
|
|
if status3 == 1:
|
|
os.system('./relay.py open 3')
|
|
else:
|
|
os.system('./relay.py close 3')
|
|
|
|
GPIO.setup(pinList[0], GPIO.OUT)
|
|
status0=int(GPIO.input(pinList[0]))
|
|
GPIO.setup(pinList[1], GPIO.OUT)
|
|
status1=int(GPIO.input(pinList[1]))
|
|
GPIO.setup(pinList[2], GPIO.OUT)
|
|
status2=int(GPIO.input(pinList[2]))
|
|
GPIO.setup(pinList[3], GPIO.OUT)
|
|
status3=int(GPIO.input(pinList[3]))
|
|
|
|
|
|
|
|
file = read_html_template("index.html")
|
|
self.send_response(200, "OK")
|
|
self.end_headers()
|
|
print ('Relay0 = '+str(status0))
|
|
if status0 == 0:
|
|
file = file.replace("CHECKED1", "checked")
|
|
else:
|
|
file = file.replace("CHECKED1", "")
|
|
|
|
print ('Relay1 = '+str(status1))
|
|
if status1 == 0:
|
|
file = file.replace("CHECKED2", "checked")
|
|
else:
|
|
file = file.replace("CHECKED2", "")
|
|
|
|
print ('Relay2 = '+str(status2))
|
|
if status2 == 0:
|
|
file = file.replace("CHECKED3", "checked")
|
|
else:
|
|
file = file.replace("CHECKED3", "")
|
|
|
|
print ('Relay3 = '+str(status3))
|
|
if status3 == 0:
|
|
file = file.replace("CHECKED4", "checked")
|
|
else:
|
|
file = file.replace("CHECKED4", "")
|
|
|
|
self.wfile.write(bytes(file.encode("utf-8")))
|
|
|
|
|
|
|
|
|
|
def read_html_template(path):
|
|
"""function to read HTML file"""
|
|
try:
|
|
with open(path) as f:
|
|
file = f.read()
|
|
except Exception as e:
|
|
file = e
|
|
return file
|
|
|
|
def run(handler_class=S, port=8000):
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
server_address = ('', port)
|
|
#httpd = server_class(server_address, handler_class)
|
|
logging.info('Starting httpd '+str(port)+' ...\n')
|
|
try:
|
|
Handler = http.server.SimpleHTTPRequestHandler
|
|
with socketserver.TCPServer(server_address, handler_class) as httpd:
|
|
httpd.serve_forever()
|
|
except KeyboardInterrupt:
|
|
pass
|
|
httpd.server_close()
|
|
logging.info('Stopping httpd...\n')
|
|
|
|
if __name__ == '__main__':
|
|
from sys import argv
|
|
|
|
if len(argv) == 2:
|
|
run(port=int(argv[1]))
|
|
else:
|
|
run()
|