Esempio n. 1
0
def publish_data( keyvals, msg_dbg=''):    
    if keyvals != None  and 'driver' in keyvals and 'device_id' in keyvals:
        driver = keyvals['driver']
        device_id = keyvals['device_id']
        devdef = utils.read_device(driver)
        if devdef == None:
            print "Device definition for %s not found"%driver
            return

        ## Custom device drivers here
        if driver == 'custom!@#!@$!$#':
            pass
        else: # generic driver: try to match keys to feeds
            if 'timestamp' in keyvals:
                dev = publisher.find_device( device_id, create_new=True, device_type=driver, devdef=devdef )
                
                ts = utils.date_to_unix(utils.str_to_date(keyvals['timestamp']))
                datapoints = []
                feednums = []
                
                for key,val in keyvals.iteritems():
                    if key in ['driver','device_id','timestamp']:
                        continue
                        
                    try:
                        f = float(val);
                    except:
                        print "Skipping Key-Value pair",key,"/",val,"as it is non-numeric"
                        continue;
                        
                    if key in dev.feed_names:
                        idx = dev.feed_names.index(key)
                        feednums.append(idx)
                        datapoints.append(f)
                    else:
                        feednums.append(len(dev.feed_names))
                        datapoints.append(f)
                        dev.feed_names.append(key)
                try:
                    if (len(datapoints) > 0):
                        publisher.publish_data( device_id, ts, datapoints, feednum=feednums, devdef=devdef, device_type=driver, dev=dev)
                except:
                    import traceback
                    traceback.print_exc();
            else:
                print "Data Line '%s' did not have a timestamp field (for generic driver)"%msg_dbg
        
        
    else:
        print "Data Line '%s' did not contain a key for driver and/or device_id"%msg_dbg
    #0000004F056B5200
    return '\x00\x00'+struct.pack('<I',time.time())
    
def publish(source, data):
    import publisher
    #print "Publish from %s data %s"%(source,utils.hexify(data))
    try:
        SPF_result = read_packet(data)
    except struct.error, emsg:
            print "Error parsing SPF packet from %s device (%s): %s"%(source,str(emsg),utils.hexify(data))

    if SPF_result != None:
        if SPF_result[1] == MT_SENSOR_DATA:
            (devname,packet_type,time,feedidxfound,feedvalsfound) = SPF_result
            devdef = utils.read_device(devname);
            dev = publisher.find_device(source, create_new=True, device_type=devname, devdef=devdef )
            dev.feed_names = devdef['feeds']
            publisher.publish_data(source, time, feedvalsfound, feednum=feedidxfound, device_type=devname, dev=dev)
        elif SPF_result[1] == MT_RFID_TAG_DETECTED:
            (devname,packet_type,time,uidstr) = SPF_result
            devdef = utils.read_device(devname);
            dev = publisher.find_device(uidstr, create_new=True, device_type=devname, devdef=devdef )
            sourcestr = "RFID Reader %s"%source
            if ( sourcestr in dev.feed_names ):
                idx = dev.feed_names.index(sourcestr)
                feedidxfound = idx
            else:
                dev.feed_names.append(sourcestr)
                feedidxfound = len(dev.feed_names)-1
                
            feedvalfound = 1.0;