forked from switchdoclabs/BeaconAirPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BeaconAir.py
252 lines (191 loc) · 6.96 KB
/
BeaconAir.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/python
# BeaconAir - Reads iBeacons and controls HUE lights
# JCS 6/7/14
#
#
import sys
import time
import utils
sys.path.append('./ble')
sys.path.append('./config')
# if conflocal.py is not found, import default conf.py
# Check for user imports
try:
import conflocal as conf
except ImportError:
import conf
import bleThread
import lights
import webmap
import bubblelog
import iBeaconChart
from threading import Thread
from Queue import Queue
# State Variables
currentiBeaconRSSI=[]
rollingiBeaconRSSI=[]
currentiBeaconTimeStamp=[]
# Light State Variables
currentLightState= []
LIGHT_BRIGHTNESS_SENSITIVITY = 2.0
LIGHT_DISTANCE_SENSITIVITY = 2.0
BEACON_ON = True
DISPLAY_BEACON_ON = True
DISPLAY_LIGHTS_ON = True
# init state variables
for beacon in conf.BeaconList:
currentiBeaconRSSI.append(0)
rollingiBeaconRSSI.append(0)
currentiBeaconTimeStamp.append(time.time())
# init light state variables
for light in conf.LightList:
currentLightState.append(0)
lights.initializeHue('192.168.1.6')
lights.setInitialLightState(currentLightState)
# recieve commands from RasPiConnect Execution Code
def completeCommand():
f = open("/home/pi/BeaconAir/state/BeaconAirCommand.txt", "w")
f.write("DONE")
f.close()
def processCommand():
global LIGHT_BRIGHTNESS_SENSITIVITY
global LIGHT_DISTANCE_SENSITIVITY
global BEACON_ON
global DISPLAY_BEACON_ON
global DISPLAY_LIGHTS_ON
global currentLightState
f = open("/home/pi/BeaconAir/state/BeaconAirCommand.txt", "r")
command = f.read()
f.close()
if (command == "") or (command == "DONE"):
# Nothing to do
return False
# Check for our commands
print "Processing Command: ", command
if (command == "BEACONON"):
BEACON_ON = True
completeCommand()
return True
if (command == "BEACONOFF"):
BEACON_ON = False
completeCommand()
return True
if (command == "ALLLIGHTSON"):
lights.allLights(True, currentLightState )
completeCommand()
return True
if (command == "ALLLIGHTSOFF"):
lights.allLights(False, currentLightState)
completeCommand()
return True
if (command == "BEACONON"):
BEACON_ON = True
completeCommand()
return True
if (command == "BEACONOFF"):
BEACON_ON = False
completeCommand()
return True
if (command == "DISPLAYBEACONON"):
DISPLAY_BEACON_ON = True
completeCommand()
return True
if (command == "DISPLAYBEACONOFF"):
DISPLAY_BEACON_ON = False
completeCommand()
return True
if (command == "DISPLAYLIGHTSON"):
DISPLAY_LIGHTS_ON = True
completeCommand()
return True
if (command == "DISPLAYLIGHTSOFF"):
DISPLAY_LIGHTS_ON = False
completeCommand()
return True
if (command == "UPDATESENSITIVITIES"):
try:
f = open("/home/pi/BeaconAir/state/distanceSensitivity.txt", "r")
commandresponse = f.read()
LIGHT_DISTANCE_SENSITIVITY = float(commandresponse)
f.close()
except:
LIGHT_DISTANCE_SENSITIVITY = 2.0
try:
f = open("/home/pi/BeaconAir/state/brightnessSensitivity.txt", "r")
commandresponse = f.read()
f.close()
LIGHT_BRIGHTNESS_SENSITIVITY = float(commandresponse)
except:
LIGHT_BRIGHTNESS_SENSITIVITY = 2.0
print "LIGHT_DISTANCE_SENSITIVITY, LIGHT_BRIGHTNESS_SENSITIVITY= ", LIGHT_DISTANCE_SENSITIVITY, LIGHT_BRIGHTNESS_SENSITIVITY
completeCommand()
return True
completeCommand()
return True
# build configuration Table
# set up BLE thread
# set up a communication queue
queueBLE = Queue()
BLEThread = Thread(target=bleThread.bleDetect, args=(__name__,10,queueBLE,))
BLEThread.daemon = True
BLEThread.start()
bubblelog.writeToBubbleLog("BeaconAir Started")
# the main loop of BeaconAir
myPosition = [0,0]
lastPosition = [1,1]
beacons = []
while True:
if (BEACON_ON == True):
# check for iBeacon Updates
print "Queue Length =", queueBLE.qsize()
if (queueBLE.empty() == False):
result = queueBLE.get(False)
print "------"
utils.processiBeaconList(result,currentiBeaconRSSI, currentiBeaconTimeStamp,rollingiBeaconRSSI)
utils.clearOldValues(10,currentiBeaconRSSI, currentiBeaconTimeStamp,rollingiBeaconRSSI)
for beacon in conf.BeaconList:
utils.printBeaconDistance(beacon, currentiBeaconRSSI, currentiBeaconTimeStamp,rollingiBeaconRSSI)
# update position
if (utils.haveThreeGoodBeacons(rollingiBeaconRSSI) >= 3):
oldbeacons = beacons
beacons = utils.get3ClosestBeacons(rollingiBeaconRSSI)
print "beacons=", beacons
if (cmp(oldbeacons, beacons) != 0):
bubblelog.writeToBubbleLog("closebeacons:%i,%i,%i" % (beacons[0], beacons[1], beacons[2]))
# setup for Kludge
#rollingiBeaconRSSI[7] = rollingiBeaconRSSI[6]
myPosition = utils.getXYFrom3Beacons(beacons[0],beacons[1],beacons[2], rollingiBeaconRSSI)
print "myPosition1 = %3.2f,%3.2f" % (myPosition[0], myPosition[1])
#bubblelog.writeToBubbleLog("position updated:%3.2f,%3.2f" % (myPosition[0], myPosition[1]))
# calculate jitter in position
jitter = (((lastPosition[0] - myPosition[0])/lastPosition[0]) + ((lastPosition[1] - myPosition[1])/lastPosition[1]))/2.0
jitter = jitter * 100.0 # to get to percent
lastPosition = myPosition
print "jitter=", jitter
f = open("/home/pi/BeaconAir/state/distancejitter.txt", "w")
f.write(str(jitter))
f.close()
for light in conf.LightList:
lightdistance = utils.distanceBetweenTwoPoints([light[2],light[3]], myPosition)
print "distance to light %i : %3.2f" % (light[0], lightdistance)
print "LIGHT_DISTANCE_SENSITIVITY, LIGHT_BRIGHTNESS_SENSITIVITY= ", LIGHT_DISTANCE_SENSITIVITY, LIGHT_BRIGHTNESS_SENSITIVITY
lights.checkForLightTrigger(myPosition, LIGHT_DISTANCE_SENSITIVITY, LIGHT_BRIGHTNESS_SENSITIVITY, currentLightState)
print "DISPLAY_BEACON_ON, DISPLAY_LIGHTS_ON", DISPLAY_BEACON_ON, DISPLAY_LIGHTS_ON
# build webpage
webmap.buildWebMapToFile(myPosition, rollingiBeaconRSSI, currentLightState, DISPLAY_BEACON_ON, DISPLAY_LIGHTS_ON)
# build beacon count graph
iBeaconChart.iBeacondetect(rollingiBeaconRSSI)
else:
# lost position
myPosition = [-myPosition[0], -myPosition[1]]
#print currentiBeaconRSSI
#print currentiBeaconTimeStamp
# end of BEACON_ON - always process commands
else:
if (queueBLE.empty() == False):
result = queueBLE.get(False)
print "------"
print "Beacon Disabled"
# process commands from RasPiConnect
processCommand()
time.sleep(0.25)