Mercurial > code > home > repos > homeauto
comparison service/starArduino/starArduino.py @ 200:ca380454d176
old updates to barcode player
Ignore-this: d547867bff8b659bd81f58f08ce4a6f9
author | drewp@bigasterisk.com |
---|---|
date | Sun, 04 Oct 2015 04:17:12 -0700 |
parents | 80efeab5c02f |
children |
comparison
equal
deleted
inserted
replaced
199:7d897fe34306 | 200:ca380454d176 |
---|---|
4 """ | 4 """ |
5 from __future__ import division | 5 from __future__ import division |
6 | 6 |
7 import sys,json | 7 import sys,json |
8 from twisted.internet import reactor, task | 8 from twisted.internet import reactor, task |
9 import cyclone.web, restkit | 9 from rdflib import Namespace |
10 import cyclone.web | |
11 from cyclone.httpclient import fetch | |
10 | 12 |
11 sys.path.append("/my/proj/pixel/shiftweb") | 13 sys.path.append("/my/proj/pixel/shiftweb") |
12 from drvarduino import ShiftbriteArduino | 14 from drvarduino import ShiftbriteArduino |
13 from shiftweb import hexFromRgb, rgbFromHex | 15 from shiftweb import hexFromRgb, rgbFromHex |
14 | 16 |
16 from cycloneerr import PrettyErrorHandler | 18 from cycloneerr import PrettyErrorHandler |
17 from logsetup import log | 19 from logsetup import log |
18 | 20 |
19 sys.path.append("/my/proj/ariremote") | 21 sys.path.append("/my/proj/ariremote") |
20 from oscserver import ArduinoWatcher | 22 from oscserver import ArduinoWatcher |
23 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
24 | |
21 | 25 |
22 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): | 26 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): |
23 def get(self): | 27 def get(self): |
24 self.settings.arduino.ping() | 28 self.settings.arduino.ping() |
25 | 29 |
72 s = ''.join(chr(int(x)) for x in ret['barcode'].split()) | 76 s = ''.join(chr(int(x)) for x in ret['barcode'].split()) |
73 for code in s.split('\x02'): | 77 for code in s.split('\x02'): |
74 if not code: | 78 if not code: |
75 continue | 79 continue |
76 if not code.endswith('\x03'): | 80 if not code.endswith('\x03'): |
77 print "couldn't read %r" % code | 81 log.warn("couldn't read %r", code) |
78 return | 82 return |
79 codeType = {'A':'UPC-A', | 83 codeType = {'A':'UPC-A', |
80 'B':'JAN-8', | 84 'B':'JAN-8', |
81 'E':'UPC-E', | 85 'E':'UPC-E', |
82 'N':'NW-7', | 86 'N':'NW-7', |
83 'C':'CODE39', | 87 'C':'CODE39', |
84 'I':'ITF', | 88 'I':'ITF', |
85 'K':'CODE128', | 89 'K':'CODE128', |
86 }[code[0]] | 90 }[code[0]] |
87 code = code[1:-1] | 91 code = code[1:-1] |
88 try: | 92 body = "%s %s %s ." % ( |
89 postBarcode.post(payload=json.dumps( | 93 ROOM['barcodeScan'].n3(), |
90 {'barcodeType':codeType, 'code':code}), | 94 ROOM['read'].n3(), |
91 headers={"content-type" : | 95 ROOM['barcode/%s/%s' % (codeType, code)].n3()) |
92 "application/json"}) | 96 body = body.encode('utf8') |
93 except Exception, e: | 97 print "body: %r" % body |
94 print "post err", e | 98 fetch("http://bang:9071/oneShot", |
95 | 99 method='POST', |
100 timeout=1, | |
101 postdata=body, | |
102 headers={"content-type" : ["text/n3"]}, | |
103 ).addErrback(log.error) | |
96 | 104 |
97 class Graph(PrettyErrorHandler, cyclone.web.RequestHandler): | 105 class Graph(PrettyErrorHandler, cyclone.web.RequestHandler): |
98 def get(self): | 106 def get(self): |
99 raise NotImplementedError | 107 raise NotImplementedError |
100 | 108 |
119 colors = [(0,0,0)] * sb.numChannels | 127 colors = [(0,0,0)] * sb.numChannels |
120 | 128 |
121 aw = ArduinoWatcher(sb) | 129 aw = ArduinoWatcher(sb) |
122 task.LoopingCall(aw.poll).start(1.0/20) | 130 task.LoopingCall(aw.poll).start(1.0/20) |
123 | 131 |
124 postBarcode = restkit.Resource('http://star:9011/barcodeScan') | 132 postBarcode = 'http://star:9011/barcodeScan' |
125 task.LoopingCall(barcodeWatch, sb, postBarcode).start(interval=.5) | 133 task.LoopingCall(barcodeWatch, sb, postBarcode).start(interval=.5) |
126 | 134 |
127 reactor.listenTCP(9014, cyclone.web.Application([ | 135 reactor.listenTCP(9014, cyclone.web.Application([ |
128 (r'/', Index), | 136 (r'/', Index), |
129 (r'/temperature', Temperature), | 137 (r'/temperature', Temperature), |