157
|
1 from __future__ import division
|
|
2 import serial, struct, json, sys
|
|
3
|
|
4 from rdflib import Namespace, Literal
|
|
5 sys.path.append("/my/site/magma")
|
|
6 from stategraph import StateGraph
|
|
7 import klein
|
|
8 from twisted.internet import task
|
|
9 import random, time
|
|
10
|
|
11 import restkit
|
|
12 reasoning = restkit.Resource("http://bang:9071/", timeout=1)
|
|
13 ROOM = Namespace("http://projects.bigasterisk.com/room/")
|
|
14
|
|
15 def sendOneShot(stmt):
|
|
16 try:
|
|
17 t1 = time.time()
|
|
18 print "post to reasoning", stmt
|
|
19 p = reasoning.post("oneShot",
|
|
20 headers={"content-type": "text/n3"},
|
|
21 payload=("%s %s %s ." %
|
|
22 tuple(n.n3() for n in stmt)))
|
|
23 except restkit.errors.RequestFailed:
|
|
24 print "oneShot failed"
|
|
25 return
|
|
26 print "posted in %.04f sec. %r" % (time.time() - t1,
|
|
27 p.body_string())
|
|
28
|
|
29
|
|
30 class Busybox(object):
|
|
31 def __init__(self, port='/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A4001lVK-if00-port0'):
|
|
32 self.serial = serial.Serial(port, baudrate=115200, timeout=.2)
|
|
33
|
|
34 def poll(self):
|
|
35 for tries in range(5):
|
|
36 self.serial.write('\x60\x00')
|
|
37 line = self.serial.readline()
|
|
38 if not line.startswith('{'):
|
|
39 continue
|
|
40 if tries > 0:
|
|
41 print "after %s tries" % tries
|
|
42 return json.loads(line)
|
|
43 return {'error': 'invalid response'}
|
|
44
|
|
45 def writeMessage(self, row, col, text):
|
|
46 msg = struct.pack('BBBBB', 0x60, 0x01, row, col, len(text)) + text
|
|
47 self.serial.write(msg)
|
|
48
|
|
49 def setBacklight(self, level):
|
|
50 self.serial.write(struct.pack('BBB', 0x60, 0x02, level))
|
|
51
|
159
|
52 def sendIr(self, remote, command):
|
|
53 code = {
|
|
54 'led_22_key': {
|
|
55 # LED618 remote command byte (addr is ff)
|
|
56 # see https://github.com/alistairallan/RgbIrLed/blob/master/RgbIrLed.cpp#L44
|
|
57 'ON': 0xE0,
|
|
58 'OFF': 0x60,
|
|
59 'BRIGHTNESS_UP': 0xA0,
|
|
60 'BRIGHTNESS_DOWN': 0x20,
|
|
61 'FLASH': 0xF0,
|
|
62 'STROBE': 0xE8,
|
|
63 'FADE': 0xD8,
|
|
64 'SMOOTH': 0xC8,
|
|
65 'RED': 0x90, 'GREEN': 0x10, 'BLUE': 0x50, 'WHITE': 0xD0,
|
|
66 'ORANGE': 0xB0, 'YELLOW_DARK': 0xA8, 'YELLOW_MEDIUM': 0x98, 'YELLOW_LIGHT': 0x88,
|
|
67 'GREEN_LIGHT': 0x30, 'GREEN_BLUE1': 0x28, 'GREEN_BLUE2': 0x18, 'GREEN_BLUE3': 0x08,
|
|
68 'BLUE_RED': 0x70, 'PURPLE_DARK': 0x68, 'PURPLE_LIGHT': 0x58, 'PINK': 0x48,
|
|
69 },
|
|
70 'led_44_key': {
|
|
71 # 44 key remote. command chart: http://blog.allgaiershops.com/2012/05/
|
|
72 'up': 0x3A, 'down': 0xBA, 'play': 0x82, 'power': 0x02,
|
|
73 'red0': 0x1A, 'grn0': 0x9A, 'blu0': 0xA2, 'wht0': 0x22,
|
|
74 'red1': 0x2A, 'grn1': 0xAA, 'blu1': 0x92, 'wht1': 0x12,
|
|
75 'red2': 0x0A, 'grn2': 0x8A, 'blu2': 0xB2, 'wht2': 0x32,
|
|
76 'red3': 0x38, 'grn3': 0xB8, 'blu3': 0x78, 'wht3': 0xF8,
|
|
77 'red4': 0x18, 'grn4': 0x98, 'blu4': 0x58, 'wht4': 0xD8,
|
|
78 'RUp': 0x28, 'GUp': 0xA8, 'BUp': 0x68,
|
|
79 'RDn': 0x08, 'GDn': 0x88, 'BDn': 0x48,
|
|
80 'Quick': 0xE8, 'Slow': 0xC8,
|
|
81 'DIY1': 0x30, 'DIY2': 0xB0, 'DIY3': 0x70, 'DIY4': 0x10, 'DIY5': 0x90, 'DIY6': 0x50,
|
|
82 'AUTO': 0xF0,
|
|
83 'Flash': 0xD0,
|
|
84 'JMP3': 0x20, 'JMP7': 0xA0,
|
|
85 'Fade': 0x60, 'Fade7': 0xE0,
|
|
86 }
|
|
87 }
|
|
88
|
|
89 address = {
|
|
90 'led_22_key': 0x00,
|
|
91 'led_44_key': 0x00,
|
|
92 }[remote]
|
|
93 self.serial.write(struct.pack('BBBB', 0x60, 0x03, address, code[remote][command]))
|
|
94 time.sleep(.2)
|
|
95
|
157
|
96
|
|
97 bb = Busybox()
|
|
98 words = open('/usr/share/dict/words').readlines()
|
|
99
|
163
|
100 while 1:
|
|
101 bb.sendIr('led_22_key', 'RED')
|
|
102 bb.sendIr('led_22_key', 'BRIGHTNESS_DOWN')
|
|
103 bb.sendIr('led_22_key', 'BRIGHTNESS_DOWN')
|
|
104 bb.sendIr('led_22_key', 'BRIGHTNESS_DOWN')
|
|
105 bb.sendIr('led_22_key', 'BLUE')
|
|
106 bb.sendIr('led_22_key', 'BRIGHTNESS_DOWN')
|
|
107 bb.sendIr('led_22_key', 'BRIGHTNESS_DOWN')
|
|
108 bb.sendIr('led_22_key', 'BRIGHTNESS_DOWN')
|
|
109
|
|
110
|
|
111
|
159
|
112 class Poller(object):
|
|
113 def __init__(self):
|
|
114 self.lastWordTime = 0
|
|
115 self.last = None
|
|
116 self.s1 = []
|
157
|
117
|
159
|
118 def poll(self):
|
|
119 try:
|
|
120 self.tryPoll()
|
|
121 except Exception as e:
|
|
122 print "poll failed: %r" % e
|
|
123
|
|
124 def tryPoll(self):
|
|
125 now = time.time()
|
|
126 if now - self.lastWordTime > 1:
|
|
127 msg = '%15s' % random.choice(words).strip()[:15]
|
|
128 bb.writeMessage(1, 1, msg)
|
|
129 self.lastWordTime = now
|
|
130 self.last = bb.poll()
|
|
131 print self.last
|
|
132 if 'slider1' in self.last:
|
|
133 self.s1 = self.s1[-5:] + [self.last['slider1']]
|
|
134 if len(self.s1) > 4:
|
|
135 median = sorted(self.s1)[2]
|
|
136 bb.setBacklight(min(255, median // 4))
|
|
137 if 'keyDown' in self.last:
|
|
138 keyNum = self.last['keyDown']
|
|
139 sendOneShot((ROOM['ariBed/button%s' % keyNum],
|
|
140 ROOM['change'],
|
|
141 ROOM['down']))
|
|
142 if self.last['motion']:
|
163
|
143 bb.sendIr('led_22_key', 'ON')
|
159
|
144 else:
|
163
|
145 bb.sendIr('led_22_key', 'OFF')
|
157
|
146
|
|
147
|
|
148 @klein.route('/graph', methods=['GET'])
|
|
149 def getGraph(request):
|
|
150 g = StateGraph(ROOM.busybox)
|
|
151 g.add((ROOM.busybox, ROOM.localHour, Literal('x')))
|
|
152 for attr in ['slider1', 'slider2', 'slider3', 'slider4']:
|
|
153 # needs: smoothing, exp curve correction
|
159
|
154 g.add((ROOM['busybox/%s' % attr], ROOM.value, Literal(round(poller.last[attr] / 1021, 3))))
|
|
155 g.add((ROOM['busybox/motion'], ROOM.value, Literal(poller.last['motion'])))
|
157
|
156 request.setHeader('Content-type', 'application/x-trig')
|
|
157 return g.asTrig()
|
|
158
|
159
|
159 poller = Poller()
|
|
160 task.LoopingCall(poller.poll).start(.05)
|
|
161
|
|
162 # todo: watch reasoning graph. put lines on display. send updated ir codes.
|
|
163
|
157
|
164 klein.run('0.0.0.0', port=9056)
|
|
165
|