Пример #1
0
def handle_payload(ev):
    msg = ev.msg

    parser = SafeConfigParser()
    parser.read('data.ini')
    section = parser.sections()

    print "\ttopic : " + msg.topic
    if msg.payload is not None:
        data = msg.payload
        data = str(data)
        print "payload = ", data
        try:
            data2 = json.loads(data)
        except ValueError:
            return

        for name in parser.items('thingspeak'):
            if name[0] == data2['id']:
                field_id = name[1][:7]
                api_key = name[1][8:]
                value = data2['value']
                datastream = data2['id']
                packet = ThingSpeakPacket(api_key, [(field_id, value)])
                print "Thingspeak status = ", packet.push()
                print field_id
                print datastream
                print value
Пример #2
0
    def eval_upload(self):
        """
        Evaluate possible transmissions. To be called once per minute
        """
        if self.tx_config is None:
            return
        
        transmissions = self.tx_config["cloud"]
        
        update = False
        current = datetime.now()

        for name, tx in transmissions.iteritems():
            interval = tx["interval"]         
            if interval == "1m":
                update = True
            elif interval == "5m":
                if current.minute % 5 == 0:
                    update = True
            elif interval == "15m":
                if current.minute % 15 == 0:
                    update = True
            elif interval == "30m":
                if current.minute % 30 == 0:
                    update = True           
            elif interval == "1h":
                if current.minute == 0:
                    update = True
            elif interval == "12h":
                if current.hour % 12 == 0:
                    update = True
            elif interval == "1d":
                if current.hour == 0:
                    update = True
            
            if update:
                update = False                 
                # Fill endpoint data
                endpoint_values = []
                for field_id in tx["endpoints"]:
                    endp_name = tx["endpoints"][field_id]
                    endp = network.get_endpoint(endp_name)
                    if endp is None:
                        raise LagartoException("Unable to find a matching endpoint for " + endp_name + " before updating " + name + " from database")
                    elif endp.value is None:
                        raise LagartoException(endp_name + " has an invalid value. Unable to upload " + name + " to " + tx["service"])
                    else:                        
                        endpoint_values.append((field_id, endp.value))
                        update = True
                
                if update:
                    if tx["service"] == "ThingSpeak":
                        thinkspeak = ThingSpeakPacket(tx["apikey"], endpoint_values)        
                        thinkspeak.push()
Пример #3
0
    def push_thingspeak(endp, api_key, field_id):
        """
        Push data to ThingSpeak

        @param endp: endpoint identification string
        format 1: process.location.name
        format 2: process.id        
        @param api_key: ThingSpeak API key
        @param field_id: ThingSpeak field ID
        
        @return HTTP response from ThingSpeak
        """
        try:
            endpoint = NetworkAPI.get_endpoint(endp)
            if endpoint is not None:
                packet = ThingSpeakPacket(api_key,
                                          [(field_id, endpoint.value)])
                return packet.push()
            return None
        except LagartoException as ex:
            ex.log()