Mercurial > code > home > repos > homeauto
annotate service/starArduino/starArduino.py @ 462:7ec5945752b7
some old Measurement and Locator edits
Ignore-this: 188ecdf63f49b7a58f99a5961c6c23a6
author | drewp@bigasterisk.com |
---|---|
date | Sat, 20 Apr 2019 23:35:46 -0700 |
parents | ca380454d176 |
children |
rev | line source |
---|---|
4
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
1 """ |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
2 arduino driver for the nightlight+buttons+temp setup running on star |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
3 |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
4 """ |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
5 from __future__ import division |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
6 |
36 | 7 import sys,json |
4
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
8 from twisted.internet import reactor, task |
200 | 9 from rdflib import Namespace |
10 import cyclone.web | |
11 from cyclone.httpclient import fetch | |
4
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
12 |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
13 sys.path.append("/my/proj/pixel/shiftweb") |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
14 from drvarduino import ShiftbriteArduino |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
15 from shiftweb import hexFromRgb, rgbFromHex |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
16 |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
17 sys.path.append("/my/proj/homeauto/lib") |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
18 from cycloneerr import PrettyErrorHandler |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
19 from logsetup import log |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
20 |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
21 sys.path.append("/my/proj/ariremote") |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
22 from oscserver import ArduinoWatcher |
200 | 23 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
24 | |
4
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
25 |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
26 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
27 def get(self): |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
28 self.settings.arduino.ping() |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
29 |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
30 self.set_header("Content-Type", "application/xhtml+xml") |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
31 self.write(open("index.html").read()) |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
32 |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
33 class Temperature(PrettyErrorHandler, cyclone.web.RequestHandler): |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
34 def get(self): |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
35 f = self.settings.arduino.getTemperature() |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
36 self.set_header("Content-Type", "application/json") |
36 | 37 self.write(json.dumps({"temp" : f})) |
4
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
38 |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
39 class Brite(PrettyErrorHandler, cyclone.web.RequestHandler): |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
40 def get(self, pos): |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
41 self.set_header("Content-Type", "text/plain") |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
42 self.write(hexFromRgb(self.settings.colors[int(pos)])) |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
43 |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
44 def put(self, pos): |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
45 channel = int(pos) |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
46 colors = self.settings.colors |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
47 colors[channel] = rgbFromHex(self.request.body) |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
48 self.settings.arduino.update(colors) |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
49 self.set_header("Content-Type", "text/plain") |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
50 self.write("updated %r" % colors) |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
51 |
23
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
52 class Barcode(PrettyErrorHandler, cyclone.web.RequestHandler): |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
53 def get(self): |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
54 self.set_header("Content-Type", "text/plain") |
90 | 55 ard = self.settings.arduino |
56 ard.ser.write("\x60\x02") | |
57 self.write(str(ard.readJson())) | |
23
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
58 |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
59 class BarcodeBeep(PrettyErrorHandler, cyclone.web.RequestHandler): |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
60 def put(self): |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
61 self.set_header("Content-Type", "text/plain") |
90 | 62 ard = self.settings.arduino |
63 ard.ser.write("\x60\x03") | |
64 self.write(str(ard.readJson())) | |
23
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
65 |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
66 def barcodeWatch(arduino, postBarcode): |
66 | 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 | |
23
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
73 |
66 | 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'): | |
200 | 81 log.warn("couldn't read %r", code) |
66 | 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] | |
200 | 92 body = "%s %s %s ." % ( |
93 ROOM['barcodeScan'].n3(), | |
94 ROOM['read'].n3(), | |
95 ROOM['barcode/%s/%s' % (codeType, code)].n3()) | |
96 body = body.encode('utf8') | |
97 print "body: %r" % body | |
98 fetch("http://bang:9071/oneShot", | |
99 method='POST', | |
100 timeout=1, | |
101 postdata=body, | |
102 headers={"content-type" : ["text/n3"]}, | |
103 ).addErrback(log.error) | |
23
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
104 |
4
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
105 class Graph(PrettyErrorHandler, cyclone.web.RequestHandler): |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
106 def get(self): |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
107 raise NotImplementedError |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
108 |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
109 if __name__ == '__main__': |
23
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
110 class A(ShiftbriteArduino): |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
111 # from loggingserial.py |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
112 def readJson(self): |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
113 line = '' |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
114 while True: |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
115 c = self.ser.read(1) |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
116 #print "gotchar", repr(c) |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
117 if c: |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
118 line += c |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
119 if c == "\n": |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
120 break |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
121 else: |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
122 raise ValueError("timed out waiting for chars") |
36 | 123 return json.loads(line) |
23
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
124 |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
125 sb = A(numChannels=3) |
4
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
126 |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
127 colors = [(0,0,0)] * sb.numChannels |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
128 |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
129 aw = ArduinoWatcher(sb) |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
130 task.LoopingCall(aw.poll).start(1.0/20) |
23
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
131 |
200 | 132 postBarcode = 'http://star:9011/barcodeScan' |
23
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
133 task.LoopingCall(barcodeWatch, sb, postBarcode).start(interval=.5) |
4
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
134 |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
135 reactor.listenTCP(9014, cyclone.web.Application([ |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
136 (r'/', Index), |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
137 (r'/temperature', Temperature), |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
138 (r'/brite/(\d+)', Brite), |
23
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
139 (r'/barcode', Barcode), |
4bf2be19c6c4
barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents:
4
diff
changeset
|
140 (r'/barcode/beep', BarcodeBeep), |
4
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
141 (r'/graph', Graph), |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
142 ], arduino=sb, colors=colors)) |
be855a111619
move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff
changeset
|
143 reactor.run() |