def onmsg(self, client, obj, blk): print("JNMQTTMate Received mblock '" + str(blk.payload) + "' on topic '" + blk.topic + "' with QoS " + str(blk.qos)) #mrchoi87 self._logger.info("JNMQTTMate Received mblock '" + str(blk.payload) + "' on topic '" + blk.topic + "' with QoS " + str(blk.qos)) msg = self._jnmqtt.getpropermsg(blk) if msg is None: self._logger.warn("The message is not proper " + str(blk)) return None # try: tmp = blk.topic.split('/') if JNMQTT._SVR == tmp[2] and JNMQTT._STAT == tmp[3]: if blk.payload == _STAT_ON: self._logger.info("The server is OK now.") else: self._logger.warn("The server might have some problems.") if BlkType.isrequest(msg.gettype()): ret = self.writecb(msg) else: self._logger.warn("Wrong message : " + blk.payload)
def getpropermsg(self, blk): tmp = blk.topic.split('/') msg = MBlock.load(blk.payload) if msg is None: self._logger.info("Fail to parse a message. " + str(blk.payload)) return msg if tmp[3] == JNMQTT._REQ and BlkType.isrequest(msg.gettype()): return msg if tmp[3] == JNMQTT._RES and BlkType.isresponse(msg.gettype()): return msg if tmp[3] == JNMQTT._NOTI and BlkType.isnotice(msg.gettype()): return msg if tmp[3] == JNMQTT._OBS and BlkType.isobservation(msg.gettype()): return msg self._logger.info("Topic is not matched. Check [3]. " + str(tmp)) return None
def writeblk(self, blk): print "**jnmqtt writeblk", blk.stringify() value = blk.stringify() inter = JNMQTT._SELF if blk.getnodeid() is None else "" node = "" if blk.getnodeid() is None else str(blk.getnodeid()) if blk.getnodeid() is None: gwid = blk.getextra("gwid") if gwid is None: idx = 2 lst = [self._svc, JNMQTT._SELF, ""] else: idx = 3 lst = [self._svc, str(gwid), JNMQTT._SELF, ""] else: idx = 2 gwid = self._devinfo.findgateway(blk.getnodeid())["id"] lst = [self._svc, str(gwid), "", str(blk.getnodeid())] if BlkType.isobservation(blk.gettype()): lst[idx] = JNMQTT._OBS qos = 0 elif BlkType.isnotice(blk.gettype()): lst[idx] = JNMQTT._NOTI qos = 1 elif BlkType.isrequest(blk.gettype()): lst[idx] = JNMQTT._REQ qos = 2 elif BlkType.isresponse(blk.gettype()): lst[idx] = JNMQTT._RES qos = 2 else: # error log return False with self._lock: #ret = self._client.publish("/".join(lst), value, qos) ret = publish.single("/".join(lst), payload=value, qos=qos, hostname=self._option["conn"]["host"]) print "publish : ", "/".join(lst), ret, qos, value
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 writeblk(self, blk): print "farmosdb writeblk", blk.getcontent() if BlkType.isobservation(blk.gettype()): print "write obs" self.writeobs(blk) elif BlkType.isrequest(blk.gettype()): print "write req" content = blk.getcontent() if content["cmd"] >= CmdCode.DETECT_DEVICE: print "detect device" else: if "param" not in content: self._logger.warn("Request needs 'param' " + blk.stringify()) return False params = [ content["opid"], content["id"], content["cmd"], json.dumps(content["param"]) ] print "insert req", FarmosDB._REQINS_QUERY, params with self._lock: try: did = int(content["id"]) if did in self._lastopid: print FarmosDB._REQFIN_QUERY, [ did, self._lastopid[did] ] self._cur.execute(FarmosDB._REQFIN_QUERY, [did, self._lastopid[did]]) del self._lastopid[did] self._lastopid[did] = content["opid"] self._cur.execute(FarmosDB._REQINS_QUERY, params) self._conn.commit() except Exception as ex: self._logger.warn("DB exception : " + str(ex)) self.close() self.connect() elif BlkType.isresponse(blk.gettype()): print "write res" content = blk.getcontent() uquery = "update requests set status = %s, exectime = now() where opid = %s " params = [content["res"], content["opid"]] self._lastopid[int(content["id"])] = {content["opid"]} print FarmosDB._REQUPS_QUERY, params with self._lock: try: self._cur.execute(FarmosDB._REQUPS_QUERY, params) self._conn.commit() except Exception as ex: self._logger.warn("DB exception : " + str(ex)) self.close() self.connect() elif BlkType.isnotice(blk.gettype()): print "write noti" self.writenoti(blk) else: # error log self._logger.warn("wrong type messsage : " + blk.stringify()) return False print "finish writing block." print "finish writing block." print "finish writing block." return True