def procReqToRsp(self, reqPkt): isError = False if wsp.ReqPacket.getMagicPacketType() != reqPkt.getPacketType(): rspParams = [wsp.ParameterLine("ErrorDesc", "Not a ReqPacket")] trace("Not a ReqPacket") return wsp.RspPacket(reqPkt.getCtag(), wsp.ERROR, rspParams) try: commandName = reqPkt.getCmdName() except AttributeError: rspParams = [ wsp.ParameterLine("ErrorDesc", "Unable to get ReqPacket command name") ] trace("Unable to get ReqPacket command name") return wsp.RspPacket(reqPkt.getCtag(), wsp.ERROR, rspParams) rspParams = [] if commandName == wsp.READ_TEMPERATURE: isError, rspParams = self.onReadTemp(reqPkt) elif commandName == "Calibrate": isError, rspParams = self.onCalibrate(reqPkt) elif commandName == "GetSensors": isError, rspParams = self.onGetSensors(reqPkt) elif commandName == "GetCache": isError, rspParams = self.onGetCache(reqPkt) elif commandName == "ClearCache": SensorCache.clearCache() elif commandName == "GetAllWeather": isError, rspParams = self.onGetAllWeather(reqPkt) elif commandName in [wsp.MEASURE_WEATHER, wsp.READ_WEATHER]: isError, rspParams = self.onReadWeather(reqPkt) elif commandName == wsp.IDENTIFY: isError, rspParams = self.onIdentify(reqPkt) else: isError = True rspParams = [ wsp.ParameterLine("ErrorDesc", "Unknown request: %s" % commandName) ] trace("Unknown request: %s" % commandName) if isError: rspKind = wsp.ERROR else: rspKind = wsp.COMPLETE return wsp.RspPacket(reqPkt.getCtag(), rspKind, rspParams)
def procReqToRsp(self, reqPkt): isError = False if wsp.ReqPacket.getMagicPacketType() != reqPkt.getPacketType(): rspParams = [wsp.ParameterLine( "ErrorDesc", "Not a ReqPacket")] trace ("Not a ReqPacket") return wsp.RspPacket(reqPkt.getCtag(), wsp.ERROR, rspParams) try: commandName = reqPkt.getCmdName() except AttributeError: rspParams = [wsp.ParameterLine( "ErrorDesc", "Unable to get ReqPacket command name")] trace ("Unable to get ReqPacket command name") return wsp.RspPacket(reqPkt.getCtag(), wsp.ERROR, rspParams) rspParams = [] if commandName == wsp.READ_TEMPERATURE: isError, rspParams = self.onReadTemp (reqPkt) elif commandName == "Calibrate": isError, rspParams = self.onCalibrate (reqPkt) elif commandName == "GetSensors": isError, rspParams = self.onGetSensors (reqPkt) elif commandName == "GetCache": isError, rspParams = self.onGetCache (reqPkt) elif commandName == "ClearCache": SensorCache.clearCache() elif commandName == "GetAllWeather": isError, rspParams = self.onGetAllWeather (reqPkt) elif commandName in [wsp.MEASURE_WEATHER, wsp.READ_WEATHER]: isError, rspParams = self.onReadWeather (reqPkt) elif commandName == wsp.IDENTIFY: isError, rspParams = self.onIdentify (reqPkt) else: isError = True rspParams = [wsp.ParameterLine( "ErrorDesc", "Unknown request: %s" % commandName)] trace ("Unknown request: %s" % commandName) if isError: rspKind = wsp.ERROR else: rspKind = wsp.COMPLETE return wsp.RspPacket(reqPkt.getCtag(), rspKind, rspParams)
def procRxRow(self, rowStr): # format of rowStr is #<boardNum>,<unknown>,<date>,<time>,<val>,...,<val>,<calSerial> self._dbgMsg("Got: %s" % rowStr) self.DataRowTmr.startTmr( self.DataRowSecs) # print rowStr els = rowStr.strip().split(',') # row_idx1 = els[0] try: row_brdIdx = int(els[0]) except: print "Error converting board index --%s--" % els[0] logging.error ("Error converting board index --%s--" % els[0]) return(0) row_date = els[2] # %m-%d-%y format row_time = els[3] # %H:%M:%S format niTime = time.strptime ("%s %s" % (row_date, row_time), "%m-%d-%y %H:%M:%S") # row_reads = els[4:] if els [-1].strip()[0] == '-': calSerial = els[-1].strip() row_reads = els[4:-1] else: calSerial = None row_reads = els[4:] # nowStr = time.strftime (dbTimestampFormat) # print row_date, row_time, time.strftime (dbTimestampFormat) ## nowStr = time.strftime (dbTimestampFormat, niTime) # nowStr = DbHandler.getTimeStr (niTime) nowStr = DbHandler.getTimeStr () listOfParams = [] for chanIdx in range(len(row_reads)): rdStr = row_reads[chanIdx].strip() if len(rdStr)==0: continue self._dbgMsg("CHAN=%d, VAL=%s" % (chanIdx, rdStr)) sName = "%s.%d.%d" % (self.niboxName, row_brdIdx, chanIdx) try: listOfParams.append ((sName, nowStr, float(rdStr))) SensorCache.updateSensor("T." +sName, rdStr, calSerial = calSerial) except ValueError: listOfParams.append ((sName, nowStr, 0.0)) SensorCache.updateSensor("T." +sName, "ValueError: %s" % rdStr, calSerial = calSerial, isError = True) print ("Bad value for %s: %s" % (sName, rdStr)) print (" " +str(rowStr)) DbHandler.insertValues (listOfParams)
def onGetSensors(self, reqPkt): isError = False rspParams = [] for key in self.probeMap.keys(): niBoxProbeName = self.probeMap.get(key.lower()) if niBoxProbeName == None: isError = True rspParams.append( wsp.ParameterLine("ErrorDesc." + key, "No such sensor")) trace("Cannot find %s in %s" % (key, str(self.probeMap))) else: sensorEnt = SensorCache.findSensor("T." + niBoxProbeName) if sensorEnt is None: isError = True rspParams.append( wsp.ParameterLine("ErrorDesc." + key, "No such sensor")) trace("Cannot find %s in Cache" % niBoxProbeName) elif sensorEnt[3]: isError = True rspParams.append( wsp.ParameterLine("ErrorDesc." + key, sensorEnt[0])) else: paramValue = (str(sensorEnt[0]) + ' ' + str(time.time() - sensorEnt[1]) + ' ' + str(sensorEnt[2])) rspParams.append(wsp.ParameterLine(key, paramValue)) return (isError, rspParams)
def onReadTemp(self, reqPkt): isError = False rspParams = [] sensorParams = reqPkt.getParamByName("Sensor") for param in sensorParams: sensorName = param.getValue() trace("Got query for sensor %s" % sensorName) niBoxProbeName = self.probeMap.get(sensorName.lower()) if niBoxProbeName == None: niBoxProbeName = sensorName sensorEnt = SensorCache.findSensor("T." + niBoxProbeName) if sensorEnt is None: # if not found isError = True rspParams.append( wsp.ParameterLine("ErrorDesc." + sensorName, "No such sensor")) trace("Cannot find %s in Cache" % niBoxProbeName) elif sensorEnt[3]: # if an error paramName = "ErrorDesc." + sensorName rspParams.append(wsp.ParameterLine(paramName, sensorEnt[0])) else: paramName = "Sensor." + sensorName paramValue = (str(sensorEnt[0]) + ' ' + str(time.time() - sensorEnt[1])) rspParams.append(wsp.ParameterLine(paramName, paramValue)) return (isError, rspParams)
def averageProbes(probeList): """ averageProbes(probeList) -> float or None returns the average the list of probes passed in. This routine is not intended to be called outside the Station class """ ret = None if len(probeList): val = 0.0 count = 0 for probe in probeList: probeTuple = SensorCache.findSensor(probe.getCacheName()) if probeTuple is not None and not probeTuple [3]: try: val += float(probeTuple [0]) count += 1 except ValueError, err: print ("Value Error on float() call in Station::averageProbes") print ("Probe name: %s" % probe.getName()) print ("Probe list: %s" % str(probeList)) print ("Sensor Cache: %s" % str(SensorCache.sensorTbl)) print (err) raise err else: print ("Bad probe tuple for probe %s: %s" % (probe.getName(), probeTuple)) if count > 0: ret = val/float(count) else: print ("no valid values in Station's probe list")
def onGetSensors(self, reqPkt): isError = False rspParams = [] for key in self.probeMap.keys(): niBoxProbeName=self.probeMap.get(key.lower()) if niBoxProbeName == None: isError = True rspParams.append(wsp.ParameterLine( "ErrorDesc." +key, "No such sensor")) trace ("Cannot find %s in %s" %(key,str(self.probeMap))) else: sensorEnt = SensorCache.findSensor("T." +niBoxProbeName) if sensorEnt is None: isError = True rspParams.append(wsp.ParameterLine( "ErrorDesc." +key, "No such sensor")) trace ("Cannot find %s in Cache" % niBoxProbeName) elif sensorEnt[3]: isError = True rspParams.append(wsp.ParameterLine( "ErrorDesc." +key, sensorEnt[0])) else: paramValue = (str(sensorEnt[0]) + ' ' +str(time.time() -sensorEnt[1]) + ' ' +str (sensorEnt[2])) rspParams.append (wsp.ParameterLine (key, paramValue)) return (isError, rspParams)
def onReadTemp(self, reqPkt): isError = False rspParams = [] sensorParams = reqPkt.getParamByName("Sensor") for param in sensorParams: sensorName = param.getValue() trace ("Got query for sensor %s" % sensorName) niBoxProbeName=self.probeMap.get(sensorName.lower()) if niBoxProbeName == None: niBoxProbeName = sensorName sensorEnt = SensorCache.findSensor ("T." +niBoxProbeName) if sensorEnt is None: # if not found isError = True rspParams.append (wsp.ParameterLine ("ErrorDesc." +sensorName, "No such sensor")) trace ("Cannot find %s in Cache" % niBoxProbeName) elif sensorEnt[3]: # if an error paramName = "ErrorDesc." +sensorName rspParams.append (wsp.ParameterLine (paramName, sensorEnt[0])) else: paramName = "Sensor." +sensorName paramValue = (str(sensorEnt[0]) + ' ' +str(time.time() -sensorEnt[1])) rspParams.append (wsp.ParameterLine (paramName, paramValue)) return (isError, rspParams)
def handle_timer(self,tmrName,tmrObj): # print "ProbePoller:handle_timer()" listOfParams = [] nowStr = DbHandler.getTimeStr () probes = Config.getProbes() # print " handle_timer(): %s" % probes for probe in probes: name = probe.getName () try: value = probe.getValue() # is this value from one of the niboxes? # if yes then ignore if value is not None: SensorCache.updateSensor(name, str(value)) listOfParams.append ((name, nowStr, value)) except Probes.ProbeError, err: print " %s: Probe error: %s" % (name, str(err)) print " %s" % err.message SensorCache.updateSensor(name, str(err), isError=True)
def handle_timer(self, tmrName, tmrObj): # print "ProbePoller:handle_timer()" listOfParams = [] nowStr = DbHandler.getTimeStr() probes = Config.getProbes() # print " handle_timer(): %s" % probes for probe in probes: name = probe.getName() try: value = probe.getValue() # is this value from one of the niboxes? # if yes then ignore if value is not None: SensorCache.updateSensor(name, str(value)) listOfParams.append((name, nowStr, value)) except Probes.ProbeError, err: print " %s: Probe error: %s" % (name, str(err)) print " %s" % err.message SensorCache.updateSensor(name, str(err), isError=True)
def onGetAllWeather(self, reqPkt): isError = False rspParams = [] for key in SensorCache.getKeys(): sensorEnt = SensorCache.findSensor(key) if sensorEnt is None: # this should never happen isError = True rspParams.append( wsp.ParameterLine( "ErrorDesc." +key.capitalize(), "No such sensor")) trace ("Cannot find %s in Cache" % key) elif sensorEnt[3]: isError = True rspParams.append(wsp.ParameterLine( "ErrorDesc." +key.capitalize(), sensorEnt[0])) else: paramName = key.capitalize() paramValue = str(sensorEnt[0]) rspParams.append (wsp.ParameterLine (paramName, paramValue)) return (isError, rspParams)
def onGetAllWeather(self, reqPkt): isError = False rspParams = [] for key in SensorCache.getKeys(): sensorEnt = SensorCache.findSensor(key) if sensorEnt is None: # this should never happen isError = True rspParams.append( wsp.ParameterLine("ErrorDesc." + key.capitalize(), "No such sensor")) trace("Cannot find %s in Cache" % key) elif sensorEnt[3]: isError = True rspParams.append( wsp.ParameterLine("ErrorDesc." + key.capitalize(), sensorEnt[0])) else: paramName = key.capitalize() paramValue = str(sensorEnt[0]) rspParams.append(wsp.ParameterLine(paramName, paramValue)) return (isError, rspParams)
def getMaxAge(self): """ getMaxAge() -->float or None returns the maximum age of all the probes """ maxAge = 0 for probe in (self.temperatureProbes +self.pressureProbes +self.humidityProbes): probeTuple = SensorCache.findSensor(probe.getCacheName()) if probeTuple is not None and not probeTuple [3]: age = time.time() -probeTuple[1] if age > maxAge: maxAge = age return float(maxAge)
else: oldCalSerial = None config.set(setupInfoSection, "CalSerial", str(-nowInt)) # for each sensor in packet # calc new delta from currentTemperature, sensor value # & old delta # reset delta in ini file # set rspParams as we go # add entry to calibration history for param in reqPkt.getParamByName("Sensor"): sensorName = param.getValue() niBoxProbeName = self.probeMap.get(sensorName.lower()) if niBoxProbeName == None: niBoxProbeName = sensorName sensorEnt = SensorCache.findSensor("T." + niBoxProbeName) if sensorEnt is None: isError[0] = True rspParams.append( wsp.ParameterLine("ErrorDesc." + sensorName, "No such sensor")) trace("No such sensor: %s" % sensorName) continue elif sensorEnt[3]: isError[0] = True rspParams.append( wsp.ParameterLine("ErrorDesc." + sensorName, sensorEnt[0])) continue if sensorEnt[2] not in (None, oldCalSerial): isError[0] = True rspParams.append(
else: oldCalSerial = None config.set (setupInfoSection, "CalSerial", str (-nowInt)) # for each sensor in packet # calc new delta from currentTemperature, sensor value # & old delta # reset delta in ini file # set rspParams as we go # add entry to calibration history for param in reqPkt.getParamByName("Sensor"): sensorName = param.getValue () niBoxProbeName=self.probeMap.get(sensorName.lower()) if niBoxProbeName == None: niBoxProbeName=sensorName sensorEnt = SensorCache.findSensor ("T." +niBoxProbeName) if sensorEnt is None: isError[0] = True rspParams.append (wsp.ParameterLine ("ErrorDesc." +sensorName, "No such sensor")) trace ("No such sensor: %s" % sensorName) continue elif sensorEnt[3]: isError[0] = True rspParams.append (wsp.ParameterLine ("ErrorDesc." +sensorName, sensorEnt[0])) continue if sensorEnt[2] not in (None, oldCalSerial): isError[0] = True rspParams.append (wsp.ParameterLine ("ErrorDesc." +sensorName, "Calibration timestamp not current"))