Mercurial > code > home > repos > homeauto
annotate service/starArduino/starArduino.py @ 1005:48a5ce00a881
old updates to barcode player
Ignore-this: d547867bff8b659bd81f58f08ce4a6f9
darcs-hash:20151004111712-312f9-c0b3191d292205c5d7667dfd0161df87d6e59df1
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sun, 04 Oct 2015 04:17:12 -0700 |
parents | 84d415fd454e |
children |
rev | line source |
---|---|
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
1 """ |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
2 arduino driver for the nightlight+buttons+temp setup running on star |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
3 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
4 """ |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
5 from __future__ import division |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
6 |
841
ddc2cdbfde50
rdflib and jsonlib api updates
drewp <drewp@bigasterisk.com>
parents:
828
diff
changeset
|
7 import sys,json |
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
8 from twisted.internet import reactor, task |
1005
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
9 from rdflib import Namespace |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
10 import cyclone.web |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
11 from cyclone.httpclient import fetch |
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
12 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
13 sys.path.append("/my/proj/pixel/shiftweb") |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
14 from drvarduino import ShiftbriteArduino |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
15 from shiftweb import hexFromRgb, rgbFromHex |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
16 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
17 sys.path.append("/my/proj/homeauto/lib") |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
18 from cycloneerr import PrettyErrorHandler |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
19 from logsetup import log |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
20 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
21 sys.path.append("/my/proj/ariremote") |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
22 from oscserver import ArduinoWatcher |
1005
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
23 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
24 |
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
25 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
26 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
27 def get(self): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
28 self.settings.arduino.ping() |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
29 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
30 self.set_header("Content-Type", "application/xhtml+xml") |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
31 self.write(open("index.html").read()) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
32 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
33 class Temperature(PrettyErrorHandler, cyclone.web.RequestHandler): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
34 def get(self): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
35 f = self.settings.arduino.getTemperature() |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
36 self.set_header("Content-Type", "application/json") |
841
ddc2cdbfde50
rdflib and jsonlib api updates
drewp <drewp@bigasterisk.com>
parents:
828
diff
changeset
|
37 self.write(json.dumps({"temp" : f})) |
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
38 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
39 class Brite(PrettyErrorHandler, cyclone.web.RequestHandler): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
40 def get(self, pos): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
41 self.set_header("Content-Type", "text/plain") |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
42 self.write(hexFromRgb(self.settings.colors[int(pos)])) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
43 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
44 def put(self, pos): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
45 channel = int(pos) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
46 colors = self.settings.colors |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
47 colors[channel] = rgbFromHex(self.request.body) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
48 self.settings.arduino.update(colors) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
49 self.set_header("Content-Type", "text/plain") |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
50 self.write("updated %r" % colors) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
51 |
828
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
52 class Barcode(PrettyErrorHandler, cyclone.web.RequestHandler): |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
53 def get(self): |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
54 self.set_header("Content-Type", "text/plain") |
895 | 55 ard = self.settings.arduino |
56 ard.ser.write("\x60\x02") | |
57 self.write(str(ard.readJson())) | |
828
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
58 |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
59 class BarcodeBeep(PrettyErrorHandler, cyclone.web.RequestHandler): |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
60 def put(self): |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
61 self.set_header("Content-Type", "text/plain") |
895 | 62 ard = self.settings.arduino |
63 ard.ser.write("\x60\x03") | |
64 self.write(str(ard.readJson())) | |
828
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
65 |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
66 def barcodeWatch(arduino, postBarcode): |
871 | 67 arduino.ser.write("\xff\xfb") |
68 ret = arduino.readJson() | |
69 if not ret['barcode']: | |
70 return | |
71 if ret['barcode'] == "27 80 48 13 ": | |
72 return # scanner's beep response | |
828
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
73 |
871 | 74 arduino.ser.write("\xff\xfc") |
75 arduino.readJson() # my beep response | |
76 s = ''.join(chr(int(x)) for x in ret['barcode'].split()) | |
77 for code in s.split('\x02'): | |
78 if not code: | |
79 continue | |
80 if not code.endswith('\x03'): | |
1005
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
81 log.warn("couldn't read %r", code) |
871 | 82 return |
83 codeType = {'A':'UPC-A', | |
84 'B':'JAN-8', | |
85 'E':'UPC-E', | |
86 'N':'NW-7', | |
87 'C':'CODE39', | |
88 'I':'ITF', | |
89 'K':'CODE128', | |
90 }[code[0]] | |
91 code = code[1:-1] | |
1005
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
92 body = "%s %s %s ." % ( |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
93 ROOM['barcodeScan'].n3(), |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
94 ROOM['read'].n3(), |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
95 ROOM['barcode/%s/%s' % (codeType, code)].n3()) |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
96 body = body.encode('utf8') |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
97 print "body: %r" % body |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
98 fetch("http://bang:9071/oneShot", |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
99 method='POST', |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
100 timeout=1, |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
101 postdata=body, |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
102 headers={"content-type" : ["text/n3"]}, |
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
103 ).addErrback(log.error) |
828
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
104 |
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
105 class Graph(PrettyErrorHandler, cyclone.web.RequestHandler): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
106 def get(self): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
107 raise NotImplementedError |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
108 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
109 if __name__ == '__main__': |
828
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
110 class A(ShiftbriteArduino): |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
111 # from loggingserial.py |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
112 def readJson(self): |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
113 line = '' |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
114 while True: |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
115 c = self.ser.read(1) |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
116 #print "gotchar", repr(c) |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
117 if c: |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
118 line += c |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
119 if c == "\n": |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
120 break |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
121 else: |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
122 raise ValueError("timed out waiting for chars") |
841
ddc2cdbfde50
rdflib and jsonlib api updates
drewp <drewp@bigasterisk.com>
parents:
828
diff
changeset
|
123 return json.loads(line) |
828
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
124 |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
125 sb = A(numChannels=3) |
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
126 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
127 colors = [(0,0,0)] * sb.numChannels |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
128 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
129 aw = ArduinoWatcher(sb) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
130 task.LoopingCall(aw.poll).start(1.0/20) |
828
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
131 |
1005
48a5ce00a881
old updates to barcode player
drewp <drewp@bigasterisk.com>
parents:
895
diff
changeset
|
132 postBarcode = 'http://star:9011/barcodeScan' |
828
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
133 task.LoopingCall(barcodeWatch, sb, postBarcode).start(interval=.5) |
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
134 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
135 reactor.listenTCP(9014, cyclone.web.Application([ |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
136 (r'/', Index), |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
137 (r'/temperature', Temperature), |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
138 (r'/brite/(\d+)', Brite), |
828
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
139 (r'/barcode', Barcode), |
a422d875d94d
barcode support on star. triggers mpd song
drewp <drewp@bigasterisk.com>
parents:
809
diff
changeset
|
140 (r'/barcode/beep', BarcodeBeep), |
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
141 (r'/graph', Graph), |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
142 ], arduino=sb, colors=colors)) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
143 reactor.run() |