def writeblk(self, blk): print "received message", blk.getdevid(), self._coupleid if BlkType.isrequest(blk.gettype()) is False: self._logger.warn("The message is not request. " + str(blk.gettype())) return False response = Response(blk) cmd = blk.getcommand() nd = self._devinfo.finddevbyid(blk.getnodeid()) dev = self._devinfo.finddevbyid(blk.getdevid()) if blk.getdevid() == self._coupleid: params = blk.getparams() if cmd == CmdCode.DETECT_DEVICE: print "detect device" code = self.startdetection(params, blk.getopid()) elif cmd == CmdCode.CANCEL_DETECT: print "cancel to detect device" code = self.canceldetection(params) else: self._logger.warn("Unknown Error. " + str(blk) + ", " + str(dev)) code = ResCode.FAIL elif dev is None: self._logger.warn("There is no device. " + str(blk.getdevid())) code = ResCode.FAIL_NO_DEVICE elif DevType.ispropercommand(dev['dt'], cmd) is False: self._logger.warn("The request is not proper. " + str(cmd) + " " + str(dev['dt'])) code = ResCode.FAIL_NOT_PROPER_COMMAND elif DevType.isactuator(dev['dt']) or DevType.isnode(dev['dt']): # modbus code = self.processrequest(dev, blk, nd) self._logger.info("Actuator processed : " + str(code)) elif DevType.isgateway(dev['dt']): self._logger.info("Gateway does not receive a request") code = ResCode.FAIL else: self._logger.warn("Unknown Error. " + str(blk) + ", " + str(dev)) code = ResCode.FAIL response.setresult(code) self._logger.info("write response: " + str(response)) self.writecb(response) return True #if code == ResCode.OK else False
def readactnodeinfo(self, node): ret = self.readnodeinfo(node) for dev in node['children']: if DevType.issensor(dev["dt"]) == False: info = self.readinfofromdev(ret["conn"], dev) if info: ret["act"][dev["id"]] = info else: self._logger.warn("fail to read actuator info : " + str(dev) + " however continue to read other device") return ret
def writeobs(self, blk): content = blk.getcontent() tm = content.pop('time', None) for devid, values in blk.getcontent().iteritems(): dev = self._devinfo.finddevbyid(devid) if dev is None: self._logger.warn("There is no device : " + str(devid)) continue if DevType.issensor(dev["dt"]): vdid = self.getdataid(devid, "value") status = self.checkdata(vdid, values[0], values[1]) if status == StatCode.READY: self._writedata(tm, values[0], vdid) self._writedata(tm, status, self.getdataid(devid, "status"))
def sendnoti(self): for gw in self._devinfo: for node in gw["children"]: ret = self.readnodeinfo(node) i = 1 for dev in node['children']: if DevType.issensor(dev["dt"]) == False: info = self.readinfofromdev(ret["conn"], dev) if info: ret["act"][dev["id"]] = info i = i + 1 if i % 3 == 0: self.sendnoticeforactuatorstatus(ret) ret["act"] = {} self.sendnoticeforactuatorstatus(ret)
def readnodeinfo(self, node): ret = {"id" : node["id"], "sen" : {}, "act" : {}} info = self.readinfofromdev(node) if info: ret["nd"] = info else: self._logger.warn("fail to read node info : " + str(node)) #return None for dev in node['children']: info = self.readinfofromdev(dev) if info: if DevType.issensor(dev["dt"]): ret["sen"][dev["id"]] = info else: ret["act"][dev["id"]] = info else: self._logger.warn("fail to read dev info : " + str(dev) + " however continue to read other device") return ret