annotate service/bedroomArduino/bedroomArduino.py @ 162:bb70eaa45666

whitespace Ignore-this: 29ddf8925c373bdb1ec8f62a01f0ac4f
author drewp@bigasterisk.com
date Sun, 22 Mar 2015 00:41:55 -0700
parents 1fada3a61c5f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
1 #!bin/python
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
2 """
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
3 talks to bed.pde on an arduino
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
4 """
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
5
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
6 from __future__ import division
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
7
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
8 import cyclone.web, json, traceback, os, sys, time, logging, bitstring
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
9 from twisted.internet import reactor, task
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
10 from twisted.web.client import getPage
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
11 sys.path.append("/my/proj/house/frontdoor")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
12 from loggingserial import LoggingSerial
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
13 sys.path.append("/my/proj/homeauto/lib")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
14 from cycloneerr import PrettyErrorHandler
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
15 from logsetup import log
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
16
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
17 sys.path.append("/my/proj/pixel/shiftweb")
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
18 from shiftweb import hexFromRgb, rgbFromHex
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
19
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
20 sys.path.append("/my/site/magma")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
21 from stategraph import StateGraph
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
22 from rdflib import Namespace
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
23
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
24 ROOM = Namespace("http://projects.bigasterisk.com/room/")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
25 DEV = Namespace("http://projects.bigasterisk.com/device/")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
26
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
27 class ArduinoBedroom(object):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
28 def __init__(self, port='/dev/ttyACM0'):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
29 self.ser = LoggingSerial(port=port, baudrate=115200, timeout=1)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
30 self.ser.flush()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
31
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
32 def ping(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
33 self.ser.write("\x60\x00\x00")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
34 msg = self.ser.readJson()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
35 assert msg == {"ok":True}, msg
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
36
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
37 def poll(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
38 self.ser.write("\x60\x01\x00")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
39 ret = self.ser.readJson()
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
40 ret['motion'] = int(ret['motion'] > 100)
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
41 return ret
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
42
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
43 def setSpeakerChoice(self, pillow):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
44 self.ser.write("\x60\x02" + chr(pillow))
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
45 return self.ser.readJson()
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
46
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
47 def setLeds(self, colors):
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
48 """
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
49 shift out this sequence of (r,g,b) triples of 10-bit ints
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
50 The nearest led gets color[0], etc.
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
51 """
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
52 resetCurrent = "".join(bitstring.pack("0b01, uint:10, uint:10, uint:10",
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
53 127, 127, 127).bytes
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
54 for loop in range(len(colors)))
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
55 out = "".join(bitstring.pack("0b00, uint:10, uint:10, uint:10",
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
56 b, r, g).bytes
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
57 for r,g,b in reversed(colors))
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
58 out = resetCurrent + out
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
59 self.ser.write("\x60\x03" + chr(len(out)) + out)
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
60 msg = self.ser.readJson()
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
61 assert msg == {"ok":1}, msg
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
62
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
63 class Index(PrettyErrorHandler, cyclone.web.RequestHandler):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
64 def get(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
65 """
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
66 this is an acceptable status check since it makes a round-trip
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
67 to the arduino before returning success
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
68 """
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
69 self.settings.arduino.ping()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
70
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
71 self.set_header("Content-Type", "application/xhtml+xml")
117
1fada3a61c5f bedroomarduino updates
drewp@bigasterisk.com
parents: 37
diff changeset
72 self.write(open("index.xhtml").read())
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
73
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
74 class SpeakerChoice(PrettyErrorHandler, cyclone.web.RequestHandler):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
75 def put(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
76 ret = self.settings.arduino.setSpeakerChoice(int(self.get_argument('pillow')))
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
77 self.write(ret)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
78
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
79 class Brite(PrettyErrorHandler, cyclone.web.RequestHandler):
117
1fada3a61c5f bedroomarduino updates
drewp@bigasterisk.com
parents: 37
diff changeset
80 def get(self, which):
1fada3a61c5f bedroomarduino updates
drewp@bigasterisk.com
parents: 37
diff changeset
81 self.set_header("Content-Type", "text/plain")
1fada3a61c5f bedroomarduino updates
drewp@bigasterisk.com
parents: 37
diff changeset
82 self.write(hexFromRgb(self.settings.brites[int(which)]))
1fada3a61c5f bedroomarduino updates
drewp@bigasterisk.com
parents: 37
diff changeset
83
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
84 def put(self, which):
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
85 which = int(which)
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
86 brites = self.settings.brites
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
87 if which + 1 > len(brites):
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
88 brites.extend([(0,0,0)] * (which + 1 - len(brites)))
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
89 brites[which] = rgbFromHex(self.request.body)
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
90
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
91 self.settings.arduino.setLeds(brites)
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
92 self.set_header("Content-Type", "text/plain")
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
93 self.write("ok")
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
94
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
95 class GraphPage(PrettyErrorHandler, cyclone.web.RequestHandler):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
96 def get(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
97 self.set_header("Content-Type", "application/x-trig")
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
98 g = StateGraph(ROOM['bedroomArduino'])
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
99 self.settings.poller.assertIsCurrent()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
100 g.add((DEV['bedroomMotion'], ROOM['state'],
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
101 ROOM['motion'] if self.settings.poller.lastValues['motion'] else
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
102 ROOM['noMotion']))
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
103 self.write(g.asTrig())
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
104
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
105 class Poller(object):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
106 """
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
107 Watches sensor values
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
108 """
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
109 def __init__(self, config, ard, period):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
110 self.config, self.ard = config, ard
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
111 self.period = period
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
112 self.lastValues = None
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
113 self.lastPollTime = 0
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
114 self.lastMotion = None
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
115
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
116 def assertIsCurrent(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
117 """raise an error if the poll data is not fresh"""
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
118 dt = time.time() - self.lastPollTime
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
119 if dt > period * 2:
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
120 raise ValueError("last poll time was too old: %.1f sec ago" % dt)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
121
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
122 def poll(self):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
123 now = time.time()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
124 try:
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
125 try:
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
126 newData = ard.poll()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
127 except ValueError, e:
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
128 print e
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
129 else:
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
130 #print newData # for testing
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
131
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
132 self.lastPollTime = now
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
133 self.lastValues = newData # for other data besides the blinks
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
134 self.processMotion(newData['motion'])
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
135
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
136 except (IOError, OSError):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
137 os.abort()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
138 except Exception, e:
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
139 print "poll error", e
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
140 traceback.print_exc()
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
141
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
142 def processMotion(self, state):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
143 if state == self.lastMotion:
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
144 return
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
145 self.lastMotion = state
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
146 msg = json.dumps(dict(board=self.config['boardName'],
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
147 name="bedroomMotion", state=state))
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
148 getPage('http://bang.bigasterisk.com:9069/inputChange',
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
149 method="POST",
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
150 postdata=msg,
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
151 headers={'Content-Type' : 'application/json'}
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
152 ).addErrback(self.reportError, msg)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
153
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
154 def reportError(self, msg, *args):
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
155 print "post error", msg, args
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
156
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
157 if __name__ == '__main__':
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
158
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
159 config = { # to be read from a file
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
160 'arduinoPort': '/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A4001nIu-if00-port0',
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
161 'servePort' : 9088,
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
162 'pollFrequency' : 6,
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
163 'boardName' : 'bedroom', # gets sent with updates
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
164 }
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
165
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
166 from twisted.python import log as twlog
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
167 #twlog.startLogging(sys.stdout)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
168
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
169 log.setLevel(logging.DEBUG)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
170
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
171 ard = ArduinoBedroom(port=config['arduinoPort'])
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
172
117
1fada3a61c5f bedroomarduino updates
drewp@bigasterisk.com
parents: 37
diff changeset
173 period = 1 / config['pollFrequency']
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
174 p = Poller(config, ard, period)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
175 task.LoopingCall(p.poll).start(period)
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
176
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
177 reactor.listenTCP(config['servePort'], cyclone.web.Application([
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
178 (r"/", Index),
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
179 (r"/graph", GraphPage),
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
180 (r'/speakerChoice', SpeakerChoice),
37
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
181 (r'/brite/(\d+)', Brite),
8e55a6a9c425 add shiftbrites to bedroom arduino
drewp@bigasterisk.com
parents: 29
diff changeset
182 ], arduino=ard, poller=p, brites=[]))
29
5fef0b7db346 bedroomarduino project started. web server isn't ready
drewp@bigasterisk.com
parents:
diff changeset
183 reactor.run()