|
Post by nolasafari on Jun 27, 2014 7:30:39 GMT -8
Do you have any examples of uisng the LIVE objects in Local.py?
|
|
|
Post by SDL on Jun 27, 2014 12:36:07 GMT -8
nolasafari, You bet! We are planning to post an example by each of the controls, but it just hasn't been done yet. Here's a couple: For the Bubble Log, this reads a file and sends it to the bubble log, unless the file doesn't exist, in which case no update is sent (a "" is returned). Look at Faraday's BeaconAir screen to see the bubble log in action:  # # BTL-1 Bubble Log if (objectServerID == "BTL-1"):
#check for validate request # validate allows RasPiConnect to verify this object is here
if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
responseData ="" # not validate request, so execute
# note that python is in the main directory for this call, not the local directory try: f = open("/home/pi/BeaconAir/state/bubblelog.txt", "r") tempString = f.read() f.close() os.remove("/home/pi/BeaconAir/state/bubblelog.txt") except IOError as e: tempString = ""
responseData = tempString
print "responseData =", responseData
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
For the Dynamic Spark Line - Event Driven, you put one value in a file and then read it with the Local.py code. If there is no file, then return "" which tells the control on RasPiConnect that no event has happened. The timer driven Dynamic Spark line looks very similar. # DSPL-1 Dynamic Spark Line event driven if (objectServerID == "DSPL-1"):
#check for validate request # validate allows RasPiConnect to verify this object is here
if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
responseData ="" # not validate request, so execute
# note that python is in the main directory for this call, not the local directory try: f = open("/home/pi/BeaconAir/state/distancejitter.txt", "r") tempString = f.read() f.close() os.remove("/home/pi/BeaconAir/state/distancejitter.txt") except IOError as e: tempString = ""
responseData = tempString
print "responseData =", responseData
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
Now for the Sliders. We are using a command / response method for communicating from your application to the RasPiConnectServer process. See the July issue of MagPi magazine for details (it will be out the first week of July). Faraday has written an article about building a control panel using RasPiConnect. # SL-1 - Sensitivity in meters if (objectServerID == "SL-1"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# normal response requested commandresponse = objectAction
if (float(commandresponse) < 0.15): try: f = open("/home/pi/BeaconAir/state/distanceSensitivity.txt", "r") commandresponse = f.read() f.close() except: commandresponse = "2.0"
f = open("/home/pi/BeaconAir/state/distanceSensitivity.txt", "w") f.write(commandresponse) f.close() status = sendCommandToBeaconAirAndWait("UPDATESENSITIVITIES") # Not LIVE values, just send "" back
commandresponse = ""
outgoingXMLData += BuildResponse.buildResponse(commandresponse)
outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
Finally, here is an example hunk of code that runs a Simple Graph LIVE. Note that there are two interesting parts to this. In Local.py, we are reading a file containing the formatted data information. The code generating the formatted data is given under the Local.py code. # SLGL-1 - Beacon count if (objectServerID == "SLGL-1"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# normal response requested try: f = open("/home/pi/BeaconAir/state/iBeaconCountGraph.txt", "r") commandresponse = f.read() f.close() except IOError as e: commandresponse = "0^^0||No Data from BeaconAir^^"
outgoingXMLData += BuildResponse.buildResponse(commandresponse)
outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
Finally, here is the user application code (from BeaconAir.py - Faraday's iBeacon app. See switchdoc.com) to generate the formatted data and write it to a file. # # # Builds iBeaconCount graph string # filename: detection.py # Version 1.0 06/19/2014 # #
import sys import time
import utils
sys.path.append('/home/pi/BeaconAir/config')
#global variables
datalist = []; # if conflocal.py is not found, import default conf.py
# Check for user imports try: import conflocal as conf except ImportError: import conf
def buildGraphString():
global datalist
response = "" valuecount = "" f = open("/home/pi/BeaconAir/state/iBeaconCountGraph.txt", "w") for i in range(len(datalist)): response += str(datalist[i]) valuecount += str(i) if (i < len(datalist)-1): response +="^^" valuecount +="^^"
if (len(response) != 0): fullresponse = response + "||" + valuecount else: fullresponse = ""
f.write(fullresponse)
f.close()
def iBeacondetect(RSSIArray):
global datalist
count = 0 for beacon in RSSIArray: if (beacon < 0): count += 1
if (len(datalist) == 10): if (len(datalist) > 0): datalist.pop(0) datalist.append(count)
buildGraphString() f = open("/home/pi/BeaconAir/state/beaconCount.txt", "w") f.write(str(count)) f.close()
return count
Here is the code that calls the above from the main BeaconAir loop. rollingBEaconRSSI is an array that contains the current distance information for iBeacons. If an element of the array is < 0, then it is a valid RSSI. If not, then it isn't a valid iBeacon. # build beacon count graph iBeaconChart.iBeacondetect(rollingiBeaconRSSI)
Hoep this helps! Best regards, BP
|
|
|
Post by nolasafari on Jun 28, 2014 18:16:46 GMT -8
Thanks that is exactly what I was looking for.
Jim
|
|