Vae,
here the part of the code that does
1. preselect devices that have a property that is a number
2. present that list or show existing - already used in previous selections(for editing)
3. confirm device selection button
4. preselect props for selected device
5. present list (8 times)
6. confirm props selection.
then tracking will start of the data from these devices/properties, that will be used later in the plots - defined in a similar way
sounds complicated and I believe it is. may be I have done it really complicated and there is an easy way to do this, but I have not found it.
Karl
- Code: Select all
def getNumber(self,val):
x = ""
try:
x = float(val)
return x
except:
xx = ''.join([c for c in val if c in '1234567890.'])
if len( ''.join([c for c in xx if c in '.']) ) >1: return "" # remove strings that have 2 or more dots " 5.5 6.6"
if len( ''.join([c for c in xx if c in '1234567890']) ) !=0: return xx # remove strings that have letters and dots eg "abc.xyz"
if len(xx) ==0:
val = str(val).upper()
if val== "TRUE" or val =="ON" : x= 1. # true --> 1, false --> 0
if val== "FALSE" or val =="OFF" : x= 0.
return x
########################################
def preSelectDevices(self): # Select only device/properties that are supported: numbers, bool, but not props that have "words"
self.listOfPreselectedDevices=[]
self.devIdToTypeandName={}
self.logMessage(u" listOfPreselectedDevices 1 "+ str(self.listOfPreselectedDevices))
for dev in indigo.devices.iter():
theStates = dev.states.keys()
count =0
for test in theStates:
try:
if "Mode" in test or "All" in test or ".ui" in test:
skip= True
else:
skip= False
except:
skip=False
if not skip:
val= dev.states[test]
x = self.getNumber(val)
if x !="":
count +=1
if count>0: # add to the selection list
try:
self.listOfPreselectedDevices.append((dev.id, dev.name)) ## give all id's and names of devices that have at least one of the keys we can track
self.devIdToTypeandName[dev.id]="Dev-",dev.name
except:
self.logMessage(u" listOfPreselectedDevices 2-1 error id, name "+ str(dev.id) + " " +str(dev.id)+" " + str(self.listOfPreselectedDevices),0)
for theVar in indigo.variables:
val = theVar.value
x = self.getNumber(val)
if x!="":
try:
self.listOfPreselectedDevices.append((theVar.id, "Var-"+str(theVar.name)))
self.devIdToTypeandName[theVar.id]= "Var-",theVar.name
except:
self.logMessage(u" listOfPreselectedDevices 2-2 error id, name "+ str(theVar.id) + " " +str(theVar.id)+" " + str(self.listOfPreselectedDevices),0)
return
########################################
def pickExistingOrNewDevice(self, valuesDict):
self.addNewDevice=False
devNo = valuesDict["selectedExistingOrNewDevice"]
try:
devNo= int(devNo)
except:
devNo=0
if devNo == 0:
self.addNewDevice=True
for ii in range (1,self.numberOfDevices+1):
if self.deviceNumberIsUsed[ii] ==0:
self.sequentialDevNo = ii
break
else:
self.sequentialDevNo = devNo
devID= int(self.deviceId[self.sequentialDevNo])
if devID !=0:
valuesDict["selectedDeviceID"] = devID
else:
valuesDict["selectedDeviceID"] = 0
self.logMessage(u" DefineDevices "+ str(valuesDict["DefineDevices"]),"pickExistingOrNewDevice")
if self.addNewDevice and valuesDict["DefineDevices"]:
valuesDict["DefineDevicesAndNew"] = True
valuesDict["DefineDevicesAndOld"] = False
else:
valuesDict["DefineDevicesAndNew"] = False
valuesDict["DefineDevicesAndOld"] = True
if not self.addNewDevice: self.buttonConfirmDevice(valuesDict, 0, 0)
if not self.addNewDevice: self.buttonConfirmDevice(valuesDict, 0, 0)
self.logMessage(u" sequentialDevNo "+ str(self.sequentialDevNo),"pickExistingOrNewDevice")
return valuesDict
########################################
def filterExistingDevice(self, valuesDict,xxx,yyy):
retList = []
for devNo in range (1, self.numberOfDevices+1):
if self.deviceId[devNo] >0:
retList.append((devNo, self.deviceType[devNo]+self.deviceName[devNo]))
retList.append((0,"Add New Device to Selection List"))
self.logMessage(u" retList "+ str(retList),"filterExistingDevice")
return retList
########################################
def filterDevicesThatQualify(self, valuesDict, xxx, yyy):# Select only device/properties that are supported
if len(self.listOfPreselectedDevices)==0: return [(0,0)] # nothing there yet
retList=self.listOfPreselectedDevices[:] # make a copy
if self.sequentialDevNo !=0: # just making sure
devID= int(self.deviceId[self.sequentialDevNo]) # less typing
if devID != 0: # we have a device id already from indigo use that one as first
defaultName= self.deviceName[self.sequentialDevNo]
retList.append((0,defaultName))
self.logMessage(u" retList "+ str(retList),"filterDevicesThatQualify")
return retList
########################################
def buttonConfirmDevice(self, valuesDict, xxx, yyy):
if self.addNewDevice:
try:
# self.sequentialDevNo = int(valuesDict["sequentialDevNo"])
self.deviceIdNew = int(valuesDict["selectedDeviceID"])
self.deviceNameNew = self.devIdToTypeandName[self.deviceIdNew][1]
self.deviceTypeNew = self.devIdToTypeandName[self.deviceIdNew][0] # "Dev-" or "Var-"
self.deviceId[self.sequentialDevNo] = self.deviceIdNew
self.deviceType[self.sequentialDevNo] = self.deviceTypeNew
self.deviceName[self.sequentialDevNo] = self.deviceNameNew
except:
self.logMessage("bad input, no device selected ",0)
valuesDict["text1-1"] = " no device selected" # restore to the old one
return valuesDict
else:
if valuesDict["DeleteDevice"]:
self.DeleteDevice(self.sequentialDevNo,valuesDict)
valuesDict["text1-1"] = "device deleted"
valuesDict["DeleteDevice"] = False
self.sequentialDevNo =0
return valuesDict
self.deviceIdNew = self.deviceId[self.sequentialDevNo]
self.deviceTypeNew = self.deviceType[self.sequentialDevNo]
self.deviceNameNew = self.deviceName[self.sequentialDevNo]
self.selectablePropertiesInDevice = self.preSelectProps(self.deviceIdNew)
for propNo in range(1,self.numberOfPropertiesPerDevice+1):
if self.DevicePropsIndex[self.sequentialDevNo][propNo]> 0:
theResponseGnu = "... using 1:"+str(int(self.DevicePropsIndex[self.sequentialDevNo][propNo])+2)+" with ..." # +2: data starts at columm 3, col1 = timestamp, col2 =
theResponseDType = self.DevicePropsDType[self.sequentialDevNo][propNo]
theResponseProp = self.DevicePropsName[self.sequentialDevNo][propNo]
else:
theResponseGnu = " " # if empty dont show any text
theResponseDType = "average" # if empty dont show any text
theResponseProp = 0
self.logMessage(u"theResponseProp " + str(theResponseProp) ,"buttonConfirmDevice ")
valuesDict["selDevicePropertiesa"+str(propNo)] = theResponseProp # by putting a 0 here we enable the default to show up later.. tricky
valuesDict["selDevicePropsIndex"+str(propNo)] = theResponseGnu
valuesDict["selDevicePropsDType"+str(propNo)] = theResponseDType
valuesDict["text1-2"] = " and then confirm !" # restore to the old one
valuesDict["text1-1"] = " CONFIRMED/SAVED" # restore to the old one
self.logMessage(u"no, id name " + str(self.sequentialDevNo) + " " + str(self.deviceIdNew) + " " + str(self.deviceNameNew)+ " " +str(self.deviceTypeNew) ,"buttonConfirmDevice ")
return valuesDict
########################################
def preSelectProps(self,devID): # Select only device/properties that are supported
retList=[]
# retList.append((0,"None"))
if self.deviceTypeNew =="Var-":
retList.append((self.deviceNameNew, self.deviceNameNew))
if self.deviceTypeNew =="Dev-":
dev = indigo.devices[devID]
theStates = dev.states.keys()
for test in theStates:
try:
if "Mode" in test or "All" in test or ".ui" in test:
skip= True # reject fanMode etc
else:
skip= False
except:
skip=False
if not skip:
val= dev.states[test]
x = self.getNumber(val)
if x !="": retList.append((test, self.tryNiceProperty(test)))
self.logMessage(u"retList" + str(retList) ,"preSelectProps ")
return retList
########################################
def tryNiceProperty (self, inProp):
try:
return self.propertyNiceWords[inProp] # replace property with nice list if available
except:
return inProp
########################################
def selDeviceProperties1 (self, filter="", valuesDict=None, xxx="", targetId=0):
return self.selectDevicePropertiesAll(valuesDict,1)
def selDeviceProperties2 (self, filter="", valuesDict=None, xxx="", targetId=0):
return self.selectDevicePropertiesAll(valuesDict,2)
def selDeviceProperties3 (self, filter="", valuesDict=None, xxx="", targetId=0):
return self.selectDevicePropertiesAll(valuesDict,3)
def selDeviceProperties4 (self, filter="", valuesDict=None, xxx="", targetId=0):
return self.selectDevicePropertiesAll(valuesDict,4)
def selDeviceProperties5 (self, filter="", valuesDict=None, xxx="", targetId=0):
return self.selectDevicePropertiesAll(valuesDict,5)
def selDeviceProperties6 (self, filter="", valuesDict=None, xxx="", targetId=0):
return self.selectDevicePropertiesAll(valuesDict,6)
def selDeviceProperties7 (self, filter="", valuesDict=None, xxx="", targetId=0):
return self.selectDevicePropertiesAll(valuesDict,7)
def selDeviceProperties8 (self, filter="", valuesDict=None, xxx="", targetId=0):
return self.selectDevicePropertiesAll(valuesDict,8)
########################################
def selectDevicePropertiesAll (self, valuesDict, propNo):
if self.sequentialDevNo ==0: return [(0,0)]
propPreviouslySelected= self.DevicePropsName[self.sequentialDevNo][propNo] # is there a previously selected dev/property, if yes use it
retList = self.selectablePropertiesInDevice[:] # make a copy for this property
if propPreviouslySelected !="None": # use the old one if there
retList.append( (0,self.tryNiceProperty(propPreviouslySelected)) ) # make it the fist to show up (the 0)
self.logMessage(u"retList" + str(retList) ,"selectDevicePropertiesAll ")
return retList
########################################
def buttonConfirmDeviceProps(self, valuesDict, xxx, yyy): # thsi will store the selected dev/props
error = 0
devNo= self.sequentialDevNo
oldlineDataSourceCount = self.lineDataSourceCount
if self.deviceName[devNo] !="":
for propNo in range (1, self.numberOfPropertiesPerDevice+1):
theDeviceProp = valuesDict["selDevicePropertiesa"+str(propNo)]
theDeviceDType = valuesDict["selDevicePropsDType"+str(propNo)]
self.logMessage(u" propNo theDeviceProp theDeviceDType " + str(propNo) +" " + str(theDeviceProp) +" " + str(theDeviceDType),"buttonConfirmDeviceProps")
if theDeviceProp!=0: # if = 0 now change, keep what we have.
if theDeviceProp !="" and theDeviceProp!="None": # anything meaing full returned or not selected
skip = 0
for ii in range(1,self.lineDataSourceCount+1):
devNo2= self.devPropUsedIndex[ii][0] # devNo of already stored device numbers
propNo2= self.devPropUsedIndex[ii][1] # propNo of already stored device numbers
if devNo2 == devNo and propNo ==propNo2 and theDeviceDType == self.DevicePropsDType[devNo2][propNo2]:
skip =2 # this is "me"
else:
if devNo2 == devNo and self.DevicePropsName[devNo2][propNo2] == theDeviceProp and theDeviceDType == self.DevicePropsDType[devNo2][propNo2]: # already in list? then skip it!
skip = 1
valuesDict["selDevicePropertiesa"+str(propNo)] = self.DevicePropsName[devNo][propNo] # restore to the old one
valuesDict["selDevicePropsDType"+str(propNo)] = self.DevicePropsDType[devNo][propNo] # restore to the old one
error = propNo
break
if skip == 0:
self.deviceNumberIsUsed[devNo]=1 # at least one accepcted property
### this is the selction menue touple for the plot lines to choose data from:
self.lineDataSourceCount+=1
self.sqlUpdateList[self.lineDataSourceCount]=True
if self.deviceType[devNo] =="Var-":
self.lineDataSource.append((str(self.lineDataSourceCount),"Var-"+theDeviceDType+"-"+self.deviceName[devNo]))
if self.deviceType[devNo] =="Dev-":
self.lineDataSource.append((str(self.lineDataSourceCount),theDeviceDType+"-"+self.deviceName[devNo]+"-"+self.tryNiceProperty(theDeviceProp)))
self.logMessage(u"lineDataSource" + str(self.lineDataSourceCount)+" " +theDeviceDType+"-"+self.deviceName[devNo]+"-"+self.tryNiceProperty(theDeviceProp),"buttonConfirmDeviceProps")
### this is the list of combinations that are active: dev,prop
self.devPropUsedIndex[self.lineDataSourceCount]= [int(devNo),int(propNo)] # used later by plot/line slection to pick the index
self.DevicePropsName[devNo][propNo]= theDeviceProp # = "property value"
self.DevicePropsDType[devNo][propNo]= theDeviceDType # = "average/sum/count/min/max"
self.DevicePropsIndex[devNo][propNo]= self.lineDataSourceCount # = "line index"
if self.DevicePropsIndex[devNo][propNo]> 0:
theResponse = "... using 1:($"+str(int(self.DevicePropsIndex[devNo][propNo])+2)+"*Multiplier +Offset) with ..." # +2: data starts at columm 3, col1 = timestamp, col2 =
else:
theResponse = " " # if empty dont show any text
valuesDict["selDevicePropsIndex"+str(propNo)] = theResponse
if error > 0:
valuesDict["text1-2"] = "..property "+str(error)+" already used.." # restore to the old one
else:
valuesDict["text1-2"] = "CONFIRMED & SAVED" # restore to the old one
if self.lineDataSourceCount > oldlineDataSourceCount: self.devicesAdded =1
self.logMessage(u"DevicePropsName " + str(self.DevicePropsName) ,"buttonConfirmDeviceProps")
self.logMessage(u"DevicePropsDType" + str(self.DevicePropsDType),"buttonConfirmDeviceProps")
self.logMessage(u"lineDataSource" + str(self.lineDataSource),"buttonConfirmDeviceProps")
return valuesDict