annotate service/starArduino/starArduino.py @ 66:4e34fd9aee00

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