annotate service/starArduino/starArduino.py @ 871:78a418407eb9

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