164
|
1 from rdflib import Namespace, RDF, URIRef
|
|
2
|
|
3 ROOM = Namespace('http://projects.bigasterisk.com/room/')
|
|
4
|
|
5 class BoardInput(object):
|
|
6 """
|
|
7 one device that gives us input. this includes processing to make
|
|
8 statements, but this object doesn't store state
|
|
9 """
|
|
10 def __init__(self, graph, uri):
|
|
11 self.graph, self.uri = graph, uri
|
|
12
|
|
13 def readFromPoll(self, read):
|
|
14 """
|
|
15 read an update message returned as part of a poll bundle. This may
|
|
16 consume a varying number of bytes depending on the type of
|
|
17 input (e.g. IR receiver).
|
|
18 Returns rdf statements.
|
|
19 """
|
|
20 raise NotImplementedError
|
|
21
|
|
22 def generateSetupCode(self):
|
|
23 return ''
|
|
24
|
|
25 def generatePollCode(self):
|
|
26 return ''
|
|
27
|
|
28 def pinNumber(self, pred=ROOM['pin']):
|
|
29 pinUri = self.graph.value(self.uri, pred)
|
|
30 return int(self.graph.value(pinUri, ROOM['pinNumber']))
|
|
31
|
|
32 _inputForType = {}
|
|
33 def registerInput(deviceType):
|
|
34 def newcls(cls):
|
|
35 _inputForType[deviceType] = cls
|
|
36 return cls
|
|
37 return newcls
|
|
38
|
|
39 class PingInput(BoardInput):
|
|
40 def generatePollCode(self):
|
|
41 return "Serial.write('k');"
|
|
42 def readFromPoll(self, read):
|
|
43 if read(1) != 'k':
|
|
44 raise ValueError('invalid ping response')
|
|
45 return [(self.uri, ROOM['ping'], ROOM['ok'])]
|
|
46
|
|
47 @registerInput(deviceType=ROOM['MotionSensor'])
|
|
48 class MotionSensorInput(BoardInput):
|
|
49 def generateSetupCode(self):
|
|
50 return 'pinMode(%(pin)d, INPUT); digitalWrite(%(pin)d, LOW);' % {
|
|
51 'pin': self.pinNumber(),
|
|
52 }
|
|
53
|
|
54 def generatePollCode(self):
|
|
55 return "Serial.write(digitalRead(%(pin)d) ? 'y' : 'n');" % {
|
|
56 'pin': self.pinNumber()
|
|
57 }
|
|
58
|
|
59 def readFromPoll(self, read):
|
|
60 b = read(1)
|
|
61 if b not in 'yn':
|
|
62 raise ValueError('unexpected response %r' % b)
|
|
63 motion = b == 'y'
|
|
64 return [(self.uri, ROOM['sees'],
|
|
65 ROOM['motion'] if motion else ROOM['noMotion'])]
|
|
66
|
|
67 def makeBoardInput(graph, uri):
|
|
68 deviceType = graph.value(uri, RDF.type)
|
|
69 return _inputForType[deviceType](graph, uri)
|
|
70
|