-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
87 lines (73 loc) · 2.52 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
http server for controlling rpi GPIOs
[email protected], 2015
"""
import SocketServer
import SimpleHTTPServer
import urllib
import os, json, logging, time
from threading import Thread
import utils
from config import CMD_PORT_MAP, PORT
# Logging Configuration
FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
# use MOCK if not running on rpio
if utils.isRPI():
import RPi.GPIO as GPIO
else:
import mock as GPIO
# init GPIO
GPIO.setmode(GPIO.BCM)
# init output ports
for (cmd, port) in CMD_PORT_MAP.iteritems():
logging.debug("setup port %2d for cmd %s" % (port, cmd))
GPIO.setup(port, GPIO.OUT)
#return curren pin status
def status():
r = dict(map(lambda (cmd, port): (cmd, GPIO.input(port)), CMD_PORT_MAP.iteritems() ))
#logging.debug(r)
return r
def blink(port):
for i in range(3):
GPIO.output(port, 1)
time.sleep(0.05)
GPIO.output(port, 0)
time.sleep(0.2)
class ServerRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
# called for direction cmds (blink)
def cmd_direction(self, direction):
logging.debug("cmd %s" % direction)
if CMD_PORT_MAP.has_key(direction):
# run blink in background
Thread(target=blink, args=(CMD_PORT_MAP[direction],)).start()
# called for function cmds (toggle)
def cmd_fn(self, fn):
logging.debug("cmd %s" % fn)
if CMD_PORT_MAP.has_key(fn):
port = CMD_PORT_MAP[fn]
GPIO.output(port, ~GPIO.input(port) & 0x1 ) # toggle
# webserver handler routine
def do_GET(self):
if self.path.startswith("/cmd"):
cmd = self.path[4:]
if cmd in ("/up", "/down", "/left", "/right"):
self.cmd_direction(cmd[1:])
elif cmd in ("/fn1", "/fn2", "/fn3"):
self.cmd_fn(cmd[1:])
# respond with current status
json.dump(status(), self.wfile)
else:
pos = self.path.find("?") # clean path, remove parameters
if pos != -1:
self.path=self.path[:pos-1]
if self.path == "/": # fallback to index.html
self.path += "index.html"
path = "www" + self.path
if os.path.exists(path):
self.copyfile(urllib.urlopen("www" + self.path), self.wfile)
else:
self.send_response(404)
httpd = SocketServer.ThreadingTCPServer(('', PORT), ServerRequestHandler)
logging.info("starting on port %d" % PORT)
httpd.serve_forever()