annotate service/starArduino/starArduino.py @ 895:84d415fd454e

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