示例#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 lookupstuff():
    global lookedstuffup, CODE_LINES
    if lookedstuffup:
        return 
        
    import config
    import utils
    import devicedb
    import postgresops
    devicedb.connect()
    
    for code,dat in CODES.iteritems():
        if dat[0] == "P":
            dev = devicedb.get_devices(where="idstr='%s'"%dat[1],limit=1);
            if len(dev) != 1:
                CODE_LINES[code] = lambda:"ERROR: Can't find device";
                return;
            dev = dev[0]
            # get plug load names
            metas = devicedb.get_devicemetas(where="key like 'PLUGLOAD%d' and %d=any(devices)"%(dat[2],dev.ID),limit=1);
            if len(metas) != 1 or metas[0].parent == 0:
                loadstr = "UNKNOWN";
            else:
                meta = metas[0]
                if meta.parent == 1:
                    loads = [int(s) for s in meta.value.split(',')]
                else:
                    loads = [meta.parent]
                
                loadstr = ""
                for lid in loads:
                    loadnamemeta = devicedb.get_devicemetas(where="id=%d"%lid,limit=1)[0]
                    loadstr += loadnamemeta.value;
                    if lid != loads[-1]:
                        loadstr += " + "
                        
            # get uuid of power and current
            devdef = utils.read_device(dev.device_type)
            poweruuid = None
            currentuuid = None
            for feednameidx in range(len(dev.feed_names)):
                feedname = dev.feed_names[feednameidx]
                if 'power %d'%(dat[2]+1) in feedname.lower():
                    poweruuid = dev.source_ids[feednameidx]
                elif 'current %d'%(dat[2]+1) in feedname.lower():
                    currentuuid = dev.source_ids[feednameidx]
        
            CODE_LINES[code] = lambda loadstr=loadstr,poweruuid=poweruuid,currentuuid=currentuuid,dat=dat:  ("http://sensezilla.berkeley.edu:7500/showliveplot?title=%s&source=sensezilla&sourceid=%s"%(urllib.quote("Wattage of Device(s): %s (Instrument:%s Port:%d)"%(loadstr,dat[1],dat[2]+1)),poweruuid)+";Device(s): "+loadstr + "\n" +
                                        "Wattage: %.2f W"%(get_smap_data(poweruuid)) + "\n" + 
                                        "Amperage: %.2f A"%(get_smap_data(currentuuid)));
                
        elif dat[0] == "R":
            devs = {}
            for t,v in dat[1].iteritems():
                dev = devicedb.get_devices(where="idstr='%s'"%v,limit=1);
                if len(dev) != 1:
                    CODE_LINES[code] = lambda:"ERROR: Can't find device %s"%v;
                    return;
                devs[t] = dev[0]
            
            occuuid   = devs['occupancy'].source_ids[2];
            humiduuid = devs['humidity'].source_ids[0];
            tempuuid  = devs['temperature'].source_ids[1];
            lightuuid = devs['light'].source_ids[3];
            
            CODE_LINES[code] = lambda occuuid=occuuid,humiduuid=humiduuid,tempuuid=tempuuid,lightuuid=lightuuid:  ("http://sensezilla.berkeley.edu:7500/showliveplot?title=%s&source=sensezilla&sourceid=%s"%(urllib.quote("Occupancy of room"),occuuid)+";"+
                                        "Occupancy: %.1f %%"%(get_smap_data(occuuid)) + "\n" +
                                        "Humidity: %.1f %%RH"%(get_smap_data(humiduuid)) + "\n" + 
                                        "Temperature: %.1f C"%(get_smap_data(tempuuid))  + "\n" +
                                        "Light Level: %.1f ulux"%(1.0e6*get_smap_data(lightuuid)));
                
        else:
            CODE_LINES[code] = lambda:"NOT IMPLEMENTED"
    
    lookedstuffup = True