예제 #1
0
def find_device(id_str, create_new=False, device_type=None, source=None, devdef=None, dev=None ):
    if not devicedb.connected():
        devicedb.connect()
    
    if id_str in device_cache and time.time() < device_cache[id_str].birth + CACHE_TIMEOUT:
        dev = device_cache[id_str]
    else:
        if not dev:
            devfind = devicedb.get_devices(where="idstr='%s'"%id_str,limit=1)
            if (len(devfind) == 0):
                if CREATE_NEW_DEVICE and create_new:
                    dev = devicedb.new_device()
                    dev.IDstr = id_str
                    dev.device_type = device_type if device_type else DEFAULT_NEW_DEVICE
                    dev.source_name = source if source else DEFAULT_NEW_SOURCE

                    # generate some random places to dump data
                    dev.source_ids = gen_source_ids(dev,devdef)
                    devicedb.insert_device(dev)
                else:
                    print "Error publishing data : %s is not in devicedb"%id_str
                    return None
            else:
                dev = devfind[0]
                
                    
            
        dev.birth = time.time()
        device_cache[id_str] = dev        
    
    if create_new:
        check_source_ids(dev)
        
    return dev
예제 #2
0
def publish_data(id_str, time, data, feednum=None, devdef=None, device_type=None, source=None, dev=None):    
    # Usage 1: time and data are scalars - one data point, feed # = feednum or 0 if feednum=None
    # Usage 2: time and data are lists of scalars (time could also be scalar) - one data point per feed, feed #s = feednum (list) or range(total feeds) if feednum=None
    # Usage 3: time and data are lists of scalars, feednum is a scalar - multiple data points for one feed
    # Usage 4: time and data are lists of lists of scalars (time could also be list of scalar) - multiple data points per feed, feed #s = feednum(list) or range(total feeds) if feednum=None
    if not isinstance(data, list): # Usage 1
        data = [data]
        if feednum == None:
            feednum = [0]
        else:
            feednum = [feednum]
    else:  # Usage 2,3,4
        if feednum == None: # Usage 2,4
            feednum = range(len(data))
        elif not isinstance(feednum,list): # usage 3
            feednum = [feednum]
            time = [time]
            data = [data]
    
    if not isinstance(time,list) or (not isinstance(time[0],list) and isinstance(data[0],list)): # Usage 1,2,4
        time = [time]*len(feednum)
        
    if not devicedb.connected():
        devicedb.connect()
    
    id_str = id_str.replace('/','_');
    postgresops.check_evil(id_str);
    
    dev = find_device(id_str, create_new = True, device_type=device_type, source=source, devdef=devdef, dev=dev)
    
    if dev == None:
        return;
    
    source_struct = utils.read_source(dev.source_name)
    if devdef == None:
        devdef = utils.read_device(dev.device_type)
        
    driver = source_struct['driver']
    for i in range(len(feednum)):
        if feednum[i] >= len(dev.feed_names):
            print "ERROR cannot publish data for feed %d because it is not defined in the definition for %s"%(feednum[i],dev.device_type)
        elif feednum[i] >= len(dev.source_ids) or dev.source_ids[feednum[i]] == None or dev.source_ids[feednum[i]] == '':
            print "ERROR cannot publish data for feed %d of device %s because it is not defined"%(feednum[i],dev.IDstr)
        else:
            source_id = dev.source_ids[feednum[i]]
            if driver == 'SMAP':
                publish_smap(dev.source_name,source_struct,dev,devdef,feednum[i],source_id,time[i],data[i])
            elif driver == 'CSV':
                fname = source_id
                if fname[0] != '/':
                    fname = source_struct['path'] + '/' + fname
                try:
                    parentdir = fname[:fname.rfind('/')]
                    
                    try:
                        os.makedirs(parentdir)
                    except: pass
                    
                    csvfile = open(fname, "ab")
                    #print "\t",time[i],data[i]
                    if isinstance(time[i],list):
                        for j in range(len(time[i])):
                            csvfile.write("%.12f,%.12f\n"%(time[i][j],data[i][j]))
                    else:
                        csvfile.write("%.12f,%.12f\n"%(time[i],data[i]))
                    csvfile.close()
                except OSError,e:
                    print "ERROR Cannot publish data to %s because "%(fname),e
                    
            else:
                print "ERROR Cannot publish data for %s b/c no %s driver"%(dev.source_name,driver)
                return []