Beispiel #1
0
    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
Beispiel #2
0
    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
Beispiel #3
0
    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