Пример #1
0
    def handleJSON(self, json_obj):
        # handle scanner rover sensing message calls
        if srv.SCAN_SENSE in json_obj:
            scan_sense = json_obj[srv.SCAN_SENSE]
            # update quota status
            if srv.ZONE in scan_sense:
                if scan_sense[srv.ZONE] is 0:
                    self.client.send(('{ "SEQ": 0, "ZONE": 1, "ACTION": 1 }').encode())
                
                if srv.RED and srv.GREEN and srv.BLUE in scan_sense:
                    text = srv.INFO_DB_STORE_ATT + " Data being stored: " + json.dumps(json_obj)
                    text = re.sub('\"','',text)
                    msg = status.StatusMsg(self.name, "STORE", "COLOR", text)
                    self.sendToStatusThread(msg)

                    text = srv.store(json.loads('{ "SCAN_SENSE.ZONE": ' + str(scan_sense[srv.ZONE]) + ' }'), json_obj, self.col)
                    # text = re.sub('\"','',text)
                    msg = status.StatusMsg(self.name, "SUCCESS", "COLOR", text)
                    self.sendToStatusThread(msg)

                if json_obj[srv.SEQ_FIELD] <= 100 and json_obj[srv.SEQ_FIELD] >= 0:
                    self.client.send(str('{ "SEQ": 0, "ZONE": ' +  str(1) + ' , "ACTION": 1 }').encode())

                elif json_obj[srv.SEQ_FIELD] <= 200 and json_obj[srv.SEQ_FIELD] > 100:
                    self.client.send(str('{ "SEQ": 0, "ZONE": ' +  str(2) + ' , "ACTION": 1 }').encode())

                elif json_obj[srv.SEQ_FIELD] <= 300 and json_obj[srv.SEQ_FIELD] > 200:
                    self.client.send(str('{ "SEQ": 0, "ZONE": ' +  str(3) + ' , "ACTION": 1 }').encode())

                elif json_obj[srv.SEQ_FIELD] >= 301:
                    self.client.send(str('{ "SEQ": 0, "ZONE": 1 , "ACTION": 1 }').encode())
Пример #2
0
    def run(self):
        self.srv.db_init()
        text = re.sub('\"', '', "on port %s" % self.srv.port)
        msg = status.StatusMsg(self.name, "SERVER", "NONE", text)
        self.sendToStatusThread(msg)
        # Main server processing
        while True:
            msg = status.StatusMsg(self.name, "SERVER", "NONE",
                                   "awaiting connection...")
            self.sendToStatusThread(msg)
            # Initialize and start server listening for clients
            # run this on dropped connections
            self.initClient()
            msg = status.StatusMsg(self.name, "SERVER", "NONE", "connected!")
            self.sendToStatusThread(msg)
            # Handle messages
            while True:
                try:
                    # blocking read call
                    buf = self.readFromClient()
                    json_obj = None
                    # Attempt to parse the JSON
                    if defs.isjson(buf):
                        json_obj = json.loads(buf)
                        # handle the valid JSON object
                        self.handleJSON(json_obj)
                    else:
                        msg = status.StatusMsg(
                            self.name, "ERROR", "BAD_JSON",
                            "Message is not json object type")
                        self.sendToStatusThread(msg)

                except (ValueError, ConnectionError, KeyboardInterrupt) as err:
                    self.srv.reset()
                    self.srv.db.clean_db()
                    text = re.sub('\"', '', "Caught Exception " + str(err))
                    msg = status.StatusMsg(self.name, "ERROR", "EXCEPTION",
                                           text)
                    self.sendToStatusThread(msg)
                    break
Пример #3
0
    def handleJSON(self, json_obj):
        # do whatever with the incoming data...
        if not DELIV_NAV in json_obj:
            return
        deliv_nav = json_obj[DELIV_NAV]
        # update rover status
        if STATUS and MESSAGE in deliv_nav:
            text = "Data being stored: " + json.dumps(json_obj)
            text = re.sub('\"', '', text)
            msg = status.StatusMsg(self.srv.db.deliv_nav, "STORE", "COLOR",
                                   text)
            self.sendToStatusThread(msg)
            text = self.srv.store(
                self.srv.db.deliv_nav_col,
                json.loads('{ "DELIV_NAV.STATUS": { "$exists": true } }'),
                json_obj, self.srv.db.deliv_nav)
            text = re.sub('\"', '', text)
            msg = status.StatusMsg(self.name, "SUCCESS", "STATUS", text)
            self.sendToStatusThread(msg)

        # update position
        elif srv.X and srv.Y in deliv_nav:
            text = srv.INFO_DB_STORE_ATT + " Data being stored: " + json.dumps(
                json_obj)
            text = re.sub('\"', '', text)
            msg = status.StatusMsg(self.name, "STORE", "COLOR", text)
            self.sendToStatusThread(msg)
            text = self.srv.store(
                json.loads('{ "DELIV_NAV.X": { "$exists": true } }'), json_obj)
            text = re.sub('\"', '', text)
            msg = status.StatusMsg(self.name, "SUCCESS", "LOC", text)
            self.sendToStatusThread(msg)

        # update spped and direction
        elif srv.RIGHT_DIR and srv.LEFT_DIR and srv.RIGHT_SPEED and srv.LEFT_SPEED in deliv_nav:
            text = srv.INFO_DB_STORE_ATT + " Data being stored: " + json.dumps(
                json_obj)
            text = re.sub('\"', '', text)
            msg = status.StatusMsg(self.name, "STORE", "COLOR", text)
            self.sendToStatusThread(msg)
            text = srv.store(
                json.loads('{ "DELIV_NAV.RIGHT_DIR": { "$exists": true } }'),
                json_obj, self.col)
            text = re.sub('\"', '', text)
            msg = status.StatusMsg(self.name, "SUCCESS", "SPEED", text)
            self.sendToStatusThread(msg)

        if srv.STATUS in deliv_nav:
            if deliv_nav[srv.STATUS] is 0:
                # send message for next action (FWD, BACKWARD, etc)
                #deliv_nav_rtrn_msg = srv.retrieve(self.seq_num, self.col)
                srv.send_msg(self.client, srv.DELIV_NAV_DEFAULT_ACTION)
                self.seq_num += 1
        return
Пример #4
0
    def readFromClient(self):
        buf = ""
        # read data until we see the '!' delimeter
        while delim not in buf:
            try:
                buf += self.srv.recvmsg()
            except ConnectionError as err:
                raise ConnectionError(err)
            # if *HELLO* seen scrap the data
            if buf is WIFLY_INIT_MSG:
                buf = ""
                break

        buf = buf[:-1]
        msg = status.StatusMsg(self.name, "RECVD", "NONE",
                               "Received msg: %s" % buf)
        self.sendToStatusThread(msg)
        return buf
Пример #5
0
    def readFromClient(self):
        buf = ""
        # read data until we see the '!' delimeter
        while srv.DELIM not in buf:
            self.msg_length = len(buf)
            data = srv.recv_msg(self.client, self.msg_length)
            if not data:
                raise ConnectionError('Client not connected, Reconnecting...')
            buf += data

            # if *HELLO* seen scrap the data
            if srv.init_msg(buf) is True:
                buf = ""
                break
        buf = buf[:-1]
        text = re.sub('\"', '', srv.get_msg(srv.RECV, buf))
        msg = status.StatusMsg(self.name, "RECVD", "NONE", text)
        self.sendToStatusThread(msg)
        # remove delimeter to create JSON object
        return buf