def test_resource(self): #Confirm we can create a resource test_resource = resource.resource.create(self.api, "DELETEMEname", "DELETEMEdesc") self.assertTrue(test_resource != None) self.assertTrue(resource.getResourceByName(self.api, "DELETEMEname") != None) #Confirm at least one resource is listed on the acct res_list = resource.getResources(self.api) self.assertTrue(len(res_list) > 0) #Double check that the server returns data on the created resource init_resource = resource.resource(self.api, test_resource.id) self.assertTrue(init_resource.name == "DELETEMEname") self.assertTrue(init_resource.description == "DELETEMEdesc") self.assertTrue(init_resource.created_at != None) self.assertTrue(init_resource.created_at != False) #Confirm we can actually update resource data self.assertTrue(test_resource.update("DELETEME", "DELETEMEnewdesc")) init_resource = resource.resource(self.api, test_resource.id) self.assertTrue(init_resource.name == "DELETEME") self.assertTrue(init_resource.description == "DELETEMEnewdesc") #Confirm we can destroy swarm objects self.assertTrue(test_resource.destroy()) init_resource = resource.resource(self.api, test_resource.id) self.assertTrue(init_resource.name != "DELETEME") self.assertTrue(init_resource.description != "DELETEMEnewdesc")
def test_swarm(self): #Create a resource for our testing test_resource = resource.resource.create(self.api, "DELETEMEname", "DELETEMEdesc") self.assertTrue(test_resource != None) self.assertTrue(resource.getResourceByName(self.api, "DELETEMEname") != None) test_resource.permission = resource.PERM_PROSUMER #Confirm we can create a swarm test_swarm = swarm.swarm.create(self.api, "DELETEME", "DELETEMEdesc", False, [test_resource]) self.assertTrue(test_swarm != None) self.assertTrue(swarm.getSwarmByName(self.api,"DELETEMEname") != None)
def __init__(self, resource_name, device="/dev/ttyUSB0", lcd=Adafruit_CharLCDPlate()): logging.basicConfig(level=logging.INFO) self.api = apikey.apikey("demo", "buglabs55", port=8080) self.res = resource.getResourceByName(self.api, resource_name) self.swarms = self.res.getSwarms() self.airquality = 1024 self.sats = 0 self.tx = 0 self.rx = 0 self.locationupdates = 0 self.airupdates = 0 self.lcd = lcd self.lcd.clear() self.lastCapabilities = time.time() self.lastLCDUpdate = time.time() self.ser = serial.Serial(device, 9600, timeout=1) self.warmedup = False self.gpslock = False getoutput('mkdir /home/pi/bikelogs/') self.f = open(time.strftime("/home/pi/bikelogs/bikelog_%m%d%y_%H%M%S.csv", "w")) self.f.write('time(min),latitude,longitude,airquality\n') self.f.flush() self.starttime = time.now()
gpio_value = {} gpio_direction = {} #TODO - Do a better job of option parsing, this is heavy handed... if len(sys.argv) != 2: logging.error('usage: '+sys.argv[0]+' <resource name>') sys.exit(1) GPIO.setmode(GPIO.BCM) for i in gpio_list: GPIO.setup(i, GPIO.OUT) GPIO.output(i, GPIO.LOW) gpio_value[i] = "low" gpio_direction[i] = "out" api = apikey.apikey(USER,PASS) res = resource.getResourceByName(api,sys.argv[1]) swarms = res.getSwarms() print "Press Control-C to quit\r\n" def presence(obj): logging.info("presence from "+obj['from']['resource']) #TODO - This needs to be badly refactored, lots of duplicated effort def message(obj): try: #TODO - Eek, global variables HACK, global variables BAD global gpio_list global gpio_value global gpio_direction if "name" in obj['payload'] and obj['payload']['name'] == "gpio_setup":
#!/usr/bin/python from BUGswarm import apikey from BUGswarm import resource from BUGswarm import participation import logging import time logging.basicConfig(level=logging.INFO) api = apikey.apikey("demo","buglabs55",port=8080) res = resource.getResourceByName(api,"Bike01") swarms = res.getSwarms() print "Press Control-C to quit\r\n" def presence(obj): print "presence from "+obj['from']['resource'] def message(obj): print "message "+str(obj['payload']) def error(obj): print "error "+str(obj['errors']) pt = participation.participationThread(api,res, swarms, onPresence=presence, onMessage=message, onError=error) try: while(True): pt.produce('{"data":"Hello world!"}') time.sleep(2) except KeyboardInterrupt: pass pt.stop()
#!/usr/bin/python from BUGswarm import apikey from BUGswarm import resource from BUGswarm import participation import logging import time import sys logging.basicConfig(level=logging.INFO) api = apikey.apikey("demo","buglabs55") res = resource.getResourceByName(api,"OrangePortal") swarms = res.getSwarms() airqual = 0 #Don't do this here, do this in response to a "start" request #Also store the "f" object somewhere that would allow multiple files to be open #at once. For example: # files = {} # resourceid = '498871cd3e8246b9f466d37968f6ef1a1e0c464e' # files[resourceid] = open('Bike04-050813.csv', 'w') # # don't forget at some point later to do # files[resourceid].close() # del files[resourceid] # ^^ so that the entry is removed when recording is to "stop" f = open('airlog.csv','a') print "Press Control-C to quit\r\n" def presence(obj): print "presence from "+obj['from']['resource'] def message(obj):