-
Notifications
You must be signed in to change notification settings - Fork 0
/
liftbot.py
168 lines (142 loc) · 5.88 KB
/
liftbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from twisted.words.xish import domish
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
from wokkel.xmppim import MessageProtocol, AvailablePresence
from rainbot import RainBotProtocol
import u3
BIG_DOOR_BUTTON = u3.FIO4
BIG_DOOR_SENSOR = u3.FIO7
LITTLE_DOOR_BUTTON = u3.FIO5
LITTLE_DOOR_SENSOR = u3.FIO6
SAMPLE_PERIOD = 3
PUSH_TIME = 3 # Seconds to hold the button down for
# Set buttons for digital output low and sensors for digital input
# Also set DAC0 to 0 V
INIT_IO_COMMAND = [u3.BitDirWrite(BIG_DOOR_SENSOR, 0),
u3.BitDirWrite(LITTLE_DOOR_SENSOR, 0),
u3.DAC0_16(0)
]
# Set buttons for analog input (Hi-Z) plus the INIT_IO_COMMAND
def initU3(d):
d.configAnalog(BIG_DOOR_BUTTON, LITTLE_DOOR_BUTTON)
d.getFeedback(INIT_IO_COMMAND)
def powerOnOpener(d):
"""Provide power to the opener."""
commandList = [u3.DAC0_16(int(3.3 / 5 * 65535))]
d.getFeedback(commandList)
def powerOffOpener(d):
"""Cut power to the opener."""
commandList = [u3.DAC0_16(0)]
d.getFeedback(commandList)
def pressBigDoorButton(d):
"""Set BIG_DOOR_BUTTON digital output high."""
d.configDigital(BIG_DOOR_BUTTON)
commandList = [u3.BitDirWrite(BIG_DOOR_BUTTON, 1),
u3.BitStateWrite(BIG_DOOR_BUTTON, 1)]
d.getFeedback(commandList)
def releaseBigDoorButton(d):
"""Set BIG_DOOR_BUTTON analog input."""
d.configAnalog(BIG_DOOR_BUTTON)
def pressLittleDoorButton(d):
"""Set LITTLE_DOOR_BUTTON digital output high."""
d.configDigital(LITTLE_DOOR_BUTTON)
commandList = [u3.BitDirWrite(LITTLE_DOOR_BUTTON, 1),
u3.BitStateWrite(LITTLE_DOOR_BUTTON, 1)]
d.getFeedback(commandList)
def releaseLittleDoorButton(d):
"""Set LITTLE_DOOR_BUTTON analog input."""
d.configAnalog(LITTLE_DOOR_BUTTON)
class DoorState(object):
"""Track which doors are open"""
def __init__(self, bigDoorUp, littleDoorUp):
self.bigDoorUp = bigDoorUp
self.littleDoorUp = littleDoorUp
def __str__(self):
if self.bigDoorUp:
if self.littleDoorUp:
return "Both doors open"
else:
return "Big door open"
else:
if self.littleDoorUp:
return "Little door open"
else:
return "Doors closed"
def __eq__(self, b):
return self.bigDoorUp == b.bigDoorUp and self.littleDoorUp == b.littleDoorUp
def getDoorState(d):
"""Read the state of BIG_DOOR_SENSOR and LITTLE_DOOR_SENSOR
"""
commandList = [u3.BitStateRead(BIG_DOOR_SENSOR),
u3.BitStateRead(LITTLE_DOOR_SENSOR)]
try:
bigDoorUp, littleDoorUp = d.getFeedback(commandList)
except:
print "LiftBot got exception while reading door state. Returning None."
return None
else:
return DoorState(bigDoorUp, littleDoorUp)
class LiftBotProtocol(RainBotProtocol):
def connectionMade(self):
print "LiftBot connected"
initU3(self.d)
powerOnOpener(self.d)
self.doorState = getDoorState(self.d)
if self.doorState:
self.setStatus(str(self.doorState))
self.updateLoop = LoopingCall(self.updateDoorState)
self.updateLoop.start(SAMPLE_PERIOD, now=False)
def connectionLost(self, reason):
print "LiftBot disconnected"
print "LiftBot shutting down reactor. That's it for me you've been great."
reactor.stop()
def updateDoorState(self):
newDoorState = getDoorState(self.d)
if newDoorState and self.doorState != newDoorState:
self.doorState = newDoorState
self.setStatus(str(self.doorState))
def onMessage(self, msg):
#print "Got a message", msg.toXml()
self.lastFrom = msg["from"] # Save this to send a reply.
if msg["type"] == 'chat' and hasattr(msg, "body") and msg.body != None:
msgTokens = str(msg.body).split()
msgCommand = msgTokens[0].lower()
if len(msgCommand) > 0:
if msgCommand in ["1", "big", "b"]:
self.handleBig(msgTokens)
elif msgCommand in ["2", "little", "l"]:
self.handleLittle(msgTokens)
elif msgCommand == "help" or msgCommand == "h" or msgCommand == "?":
self.handleHelp(msgTokens)
elif msgCommand == "quit" or msgCommand == "q":
self.handleQuit(msgTokens)
else:
self.handleUnknownCommand(msgTokens)
def handleBig(self, msgTokens):
self.sendText("Pushing big button")
self.pushAButton(pressBigDoorButton, releaseBigDoorButton)
def handleLittle(self, msgTokens):
self.sendText("Pushing little button")
self.pushAButton(pressLittleDoorButton, releaseLittleDoorButton)
def pushAButton(self, pressFunction, releaseFunction):
if self.updateLoop.running:
self.updateLoop.stop()
# reactor.callLater(0, powerOnOpener, self.d)
reactor.callLater(0, pressFunction, self.d)
reactor.callLater(PUSH_TIME, releaseFunction, self.d)
# reactor.callLater(PUSH_TIME + 2, powerOffOpener, self.d)
if not self.updateLoop.running:
reactor.callLater(PUSH_TIME + 3, self.updateLoop.start, SAMPLE_PERIOD)
def handleHelp(self, msgTokens):
responseText = "Commands:\n"
responseText += "big (alias 'b' or '1'): Press big button\n"
responseText += "little (alias 'l' or '2'): Press little button\n"
responseText += "quit (alias 'q'): Quit the application\n"
self.sendText(responseText)
def handleQuit(self, msgTokens):
"""
Exit. Hopefully you've got monit or something to start you back up.
"""
responseText = "Quitting"
self.sendText(responseText)
reactor.callLater(1, reactor.stop)