Exemplo n.º 1
0
 def runLoop(self):
     self.running = True
     reconnect = True
     try:
         self.pt = participation.participationThread(self.api, self.res, self.swarms,
             onPresence=self.presence, onMessage=self.message, onError=self.error)
         self.running = self.pt.connected
         while(self.running):
             line = self.ser.readline()
             if len(line) > 5:
                 self.parseSerial(line)
             if time.time() > self.lastCapabilities + 5.0:
                 self.sendCapabilities()
                 self.lastCapabilities = time.time()
             if time.time() > self.lastLCDUpdate + 1.0 and self.running:
                 self.lcd.clear()
                 self.lcd.message("sats:"+str(self.sats)+" Air:"+str(self.airquality)+"\n")
                 if not self.warmedup:
                     self.lcd.message("Sensor WarmingUp\n")
                 elif not self.gpslock:
                     self.lcd.message("No GPS Fix\n")
                 else:
                     self.lcd.message("tx:"+str(self.tx)+" rx:"+str(self.rx)+"\n")
                 self.lastLCDUpdate = time.time()
             if self.lcd.buttonPressed(self.lcd.SELECT):
                 reconnect = False
                 self.running = False
     except Exception as e:
         reconnect = True
         print e
     finally:
         print "Loop quit"
         return reconnect
                return
            sendGPIOStatus()

        if "name" in obj['payload'] and obj['payload']['name'] == "gpio_read":
            #TODO - Do this automatically, via edge detection or a loop.
            for pin in gpio_list:
                if gpio_direction[pin] == "in":
                    gpio_value = GPIO.input(pin)
            sendGPIOStatus()
    except:
        return

def error(obj):
    logging.info("error "+str(obj['errors']))

pt = participation.participationThread(api,res, swarms,
        onPresence=presence, onMessage=message, onError=error)
def sendGPIOStatus():
    global gpio_list
    global gpio_value
    global gpio_direction
    message = '{"name":"gpio","feed":['
    for pin in gpio_list:
        message += '{"pin":'+str(pin)+',"direction":"'+gpio_direction[pin]+'","value":"'+gpio_value[pin]+'"},'
    message = message[:-1]
    message += ']}'
    pt.produce(message)


try:
    idx = 0
    sendGPIOStatus()