annotate service/starArduino/starArduino.py @ 90:80efeab5c02f

fix broken request paths Ignore-this: dbfef5f7358cf2b89dc58dd3c377041c
author drewp@bigasterisk.com
date Fri, 16 Aug 2013 22:54:53 -0700
parents 4e34fd9aee00
children ca380454d176
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
0ab069867c64 rdflib and jsonlib api updates
drewp@bigasterisk.com
parents: 23
diff changeset
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
23
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
9 import cyclone.web, restkit
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
10
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
11 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
12 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
13 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
14
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
15 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
16 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
17 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
18
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
19 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
20 from oscserver import ArduinoWatcher
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
21
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
22 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
23 def get(self):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
24 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
25
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
26 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
27 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
28
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
29 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
30 def get(self):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
31 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
32 self.set_header("Content-Type", "application/json")
36
0ab069867c64 rdflib and jsonlib api updates
drewp@bigasterisk.com
parents: 23
diff changeset
33 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
34
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
35 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
36 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
37 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
38 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
39
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
40 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
41 channel = int(pos)
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
42 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
43 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
44 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
45 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
46 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
47
23
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
48 class Barcode(PrettyErrorHandler, cyclone.web.RequestHandler):
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
49 def get(self):
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
50 self.set_header("Content-Type", "text/plain")
90
80efeab5c02f fix broken request paths
drewp@bigasterisk.com
parents: 66
diff changeset
51 ard = self.settings.arduino
80efeab5c02f fix broken request paths
drewp@bigasterisk.com
parents: 66
diff changeset
52 ard.ser.write("\x60\x02")
80efeab5c02f fix broken request paths
drewp@bigasterisk.com
parents: 66
diff changeset
53 self.write(str(ard.readJson()))
23
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
54
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
55 class BarcodeBeep(PrettyErrorHandler, cyclone.web.RequestHandler):
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
56 def put(self):
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
57 self.set_header("Content-Type", "text/plain")
90
80efeab5c02f fix broken request paths
drewp@bigasterisk.com
parents: 66
diff changeset
58 ard = self.settings.arduino
80efeab5c02f fix broken request paths
drewp@bigasterisk.com
parents: 66
diff changeset
59 ard.ser.write("\x60\x03")
80efeab5c02f fix broken request paths
drewp@bigasterisk.com
parents: 66
diff changeset
60 self.write(str(ard.readJson()))
23
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
61
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
62 def barcodeWatch(arduino, postBarcode):
66
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
63 arduino.ser.write("\xff\xfb")
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
64 ret = arduino.readJson()
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
65 if not ret['barcode']:
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
66 return
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
67 if ret['barcode'] == "27 80 48 13 ":
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
68 return # scanner's beep response
23
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
69
66
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
70 arduino.ser.write("\xff\xfc")
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
71 arduino.readJson() # my beep response
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
72 s = ''.join(chr(int(x)) for x in ret['barcode'].split())
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
73 for code in s.split('\x02'):
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
74 if not code:
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
75 continue
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
76 if not code.endswith('\x03'):
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
77 print "couldn't read %r" % code
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
78 return
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
79 codeType = {'A':'UPC-A',
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
80 'B':'JAN-8',
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
81 'E':'UPC-E',
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
82 'N':'NW-7',
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
83 'C':'CODE39',
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
84 'I':'ITF',
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
85 'K':'CODE128',
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
86 }[code[0]]
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
87 code = code[1:-1]
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
88 try:
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
89 postBarcode.post(payload=json.dumps(
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
90 {'barcodeType':codeType, 'code':code}),
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
91 headers={"content-type" :
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
92 "application/json"})
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
93 except Exception, e:
4e34fd9aee00 refactor barcode part
drewp@bigasterisk.com
parents: 36
diff changeset
94 print "post err", e
23
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
95
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
96
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
97 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
98 def get(self):
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
99 raise NotImplementedError
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
100
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
101 if __name__ == '__main__':
23
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
102 class A(ShiftbriteArduino):
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
103 # from loggingserial.py
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
104 def readJson(self):
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
105 line = ''
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
106 while True:
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
107 c = self.ser.read(1)
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
108 #print "gotchar", repr(c)
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
109 if c:
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
110 line += c
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
111 if c == "\n":
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
112 break
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
113 else:
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
114 raise ValueError("timed out waiting for chars")
36
0ab069867c64 rdflib and jsonlib api updates
drewp@bigasterisk.com
parents: 23
diff changeset
115 return json.loads(line)
23
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
116
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
117 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
118
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
119 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
120
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
121 aw = ArduinoWatcher(sb)
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
122 task.LoopingCall(aw.poll).start(1.0/20)
23
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
123
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
124 postBarcode = restkit.Resource('http://star:9011/barcodeScan')
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
125 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
126
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
127 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
128 (r'/', Index),
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
129 (r'/temperature', Temperature),
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
130 (r'/brite/(\d+)', Brite),
23
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
131 (r'/barcode', Barcode),
4bf2be19c6c4 barcode support on star. triggers mpd song
drewp@bigasterisk.com
parents: 4
diff changeset
132 (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
133 (r'/graph', Graph),
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
134 ], 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
135 reactor.run()