示例#1
0
    def process_msg(self, data):
        if (data['action'] == "register"):
            # adding fresh routingids to modemlist
            modemlist = []
            for modem in data['modemlist']:
                modem['routingid'] = str(uuid.uuid1())
                modemlist.append(modem)

            if WIS.register(modemlist):
                PID.addclientinfo(self.peer_address, data['pidid'], modemlist)

                data['status'] = "registered"
                # replace modemlist to have routingids in it
                data['modemlist'] = modemlist
                smsgwglobals.pislogger.debug("/ws: reply registered - " +
                                             str(data))
                # respond registation status
                PID.sendtopid(str(self.peer_address), data)
            else:
                closingreason = 'Unable to register to any WIS!'
                # tell PID to close and retry initialisation
                self.close(1011, closingreason)

        if data['action'] == "status":
            # set sms status to globals for handling in /sendsms
            PID.setclientsmsstatus(self.peer_address, data['smsid'],
                                   data['status'], data['status_code'])

        if data['action'] == "heartbeat":
            # forward heartbeat to WIS
            httpcode = WIS.request_heartbeat(data)
            data['status'] = httpcode
            PID.sendtopid(str(self.peer_address), data)
示例#2
0
    def process_msg(self, data):
        if (data['action'] == "register"):
            # adding fresh routingids to modemlist
            modemlist = []
            for modem in data['modemlist']:
                modem['routingid'] = str(uuid.uuid1())
                modemlist.append(modem)

            if WIS.register(modemlist):
                PID.addclientinfo(self.peer_address, data['pidid'],
                                  modemlist)

                data['status'] = "registered"
                # replace modemlist to have routingids in it
                data['modemlist'] = modemlist
                smsgwglobals.pislogger.debug("/ws: reply registered - " +
                                             str(data))
                # respond registation status
                PID.sendtopid(str(self.peer_address), data)
            else:
                closingreason = 'Unable to register to any WIS!'
                # tell PID to close and retry initialisation
                self.close(1011, closingreason)

        if data['action'] == "status":
            # set sms status to globals for handling in /sendsms
            PID.setclientsmsstatus(self.peer_address,
                                   data['smsid'],
                                   data['status'])

        if data['action'] == "heartbeat":
            # forward heartbeat to WIS
            httpcode = WIS.request_heartbeat(data)
            data['status'] = httpcode
            PID.sendtopid(str(self.peer_address), data)
示例#3
0
    def restartmodem(self, **params):
        """ json before encoding
        {
        "modemid":"00436762222222",
        """
        # for receiving restartmodem from WIS
        cl = cherrypy.request.headers['Content-Length']
        rawbody = cherrypy.request.body.read(int(cl))
        # smsgwglobals.pislogger.debug("/sendsms: rawbody: " + str(rawbody))
        plaintext = GlobalHelper.decodeAES(rawbody)

        try:
            data = json.loads(plaintext)
            # adding action switch for message to PID
            data['action'] = "restartmodem"
            smsgwglobals.pislogger.debug("/restartmodem: dictionary: " +
                                         str(data))

        except Exception as e:
            smsgwglobals.pislogger.warning("/restartmodem: Invalid data received! "
                                           + str(e))
            cherrypy.response.status = 400  # Bad Request
            return

        try:
            address = PID.getclientaddress(data['modemid'])

            if address:
                # sending SMS to Pid
                PID.sendtopid(address, data)
            else:
                smsgwglobals.pislogger.warning("/restartmodem: No PID for " +
                                               "modem " +
                                               data['modemid'] +
                                               " found!")
                # If no modem endpoint to send - set own status code
                cherrypy.response.status = 404
                return

        except Exception as e:
            smsgwglobals.pislogger.debug("/restartmodem: Internal Server "
                                         "Error! " +
                                         str(e))
            cherrypy.response.status = 500  # Internal Server Error
            return
示例#4
0
    def sendsms(self, **params):
        """ json before encoding
        {
        "smsid":"uuid.uuid1()",
        "modemid":"00436762222222",
        "targetnr":"+43200200200",
        "content":"test_sendsms200 ♠ ♣ ♥ ♦ ↙ ↺ ↻ ⇒ ä"}
        """
        # for receiving sms from WIS
        cl = cherrypy.request.headers['Content-Length']
        rawbody = cherrypy.request.body.read(int(cl))
        # smsgwglobals.pislogger.debug("/sendsms: rawbody: " + str(rawbody))
        plaintext = GlobalHelper.decodeAES(rawbody)

        try:
            # smsgwglobals.pislogger.debug("/sendsms: plaintext: " + plaintext)
            data = json.loads(plaintext)
            # adding action switch for message to PID
            data['action'] = "sendsms"
            smsgwglobals.pislogger.debug("/sendsms: dictionary: " +
                                         str(data))

        except Exception as e:
            smsgwglobals.pislogger.warning("/sendsms: Invalid data received! "
                                           + str(e))
            cherrypy.response.status = 400  # Bad Request
            return

        try:
            address = PID.getclientaddress(data['modemid'])

            if address:
                # sending SMS to Pid
                PID.sendtopid(address, data)
                PID.addclientsms(address, data['smsid'])

                # Poll PIDsmstatus every 0.20 second till maxwait
                maxwaitpid = pisglobals.maxwaitpid
                now = datetime.utcnow()
                until = now + timedelta(seconds=maxwaitpid)

                while now < until:
                    status = PID.getclientsmsstatus(address, data['smsid'])

                    if status == 'SUCCESS':
                        cherrypy.response.status = 200
                        PID.removeclientsms(address, data['smsid'])
                        return

                    if status == 'ERROR':
                        cherrypy.response.status = 500
                        PID.removeclientsms(address, data['smsid'])
                        return

                    # wait for next run
                    time.sleep(0.50)
                    now = datetime.utcnow()

                # maxwaitpid reached so raise an error
                smsgwglobals.pislogger.warning("/sendsms: maxwaitpid " +
                                               "of " + str(maxwaitpid) +
                                               " seconds reached!")
                cherrypy.response.status = 500
                PID.removeclientsms(address, data['smsid'])
                return

            else:
                smsgwglobals.pislogger.warning("/sendsms: No PID for " +
                                               "modem " +
                                               data['modemid'] +
                                               " found!")
                cherrypy.response.status = 500  # Internal Server Error
                return

        except Exception as e:
            smsgwglobals.pislogger.debug("/sendsms: Internal Server "
                                         "Error! " +
                                         str(e))
            cherrypy.response.status = 500  # Internal Server Error
            return
示例#5
0
 def stop(self):
     # Unregister all Modems at WIS
     modemlist = PID.getclientmodemlist()
     PID.delclient()
     WIS.unregister(modemlist)
示例#6
0
 def closed(self, code, reason=None):
     modemlist = PID.getclientmodemlist(self.peer_address)
     PID.delclient(self.peer_address, code, reason)
     # Try to unregister
     WIS.unregister(modemlist)
示例#7
0
 def opened(self):
     PID.addclient(self.peer_address, self)
示例#8
0
    def sendsms(self, **params):
        """ json before encoding
        {
        "smsid":"uuid.uuid1()",
        "modemid":"00436762222222",
        "targetnr":"+43200200200",
        "content":"test_sendsms200 ♠ ♣ ♥ ♦ ↙ ↺ ↻ ⇒ ä"}
        """
        # for receiving sms from WIS
        cl = cherrypy.request.headers['Content-Length']
        rawbody = cherrypy.request.body.read(int(cl))
        # smsgwglobals.pislogger.debug("/sendsms: rawbody: " + str(rawbody))
        plaintext = GlobalHelper.decodeAES(rawbody)

        try:
            # smsgwglobals.pislogger.debug("/sendsms: plaintext: " + plaintext)
            data = json.loads(plaintext)
            # adding action switch for message to PID
            data['action'] = "sendsms"
            smsgwglobals.pislogger.debug("/sendsms: dictionary: " + str(data))

        except Exception as e:
            smsgwglobals.pislogger.warning(
                "/sendsms: Invalid data received! " + str(e))
            cherrypy.response.status = 400  # Bad Request
            return

        try:
            address = PID.getclientaddress(data['modemid'])

            if address:
                # sending SMS to Pid
                PID.sendtopid(address, data)
                PID.addclientsms(address, data['smsid'])

                # Poll PIDsmstatus every 0.20 second till maxwait
                maxwaitpid = pisglobals.maxwaitpid
                now = datetime.utcnow()
                until = now + timedelta(seconds=maxwaitpid)

                while now < until:
                    status, status_code = PID.getclientsmsstatus(
                        address, data['smsid'])

                    if status == 'SUCCESS' or status == "ERROR":
                        cherrypy.response.status = 200
                        cherrypy.response.body = status_code
                        PID.removeclientsms(address, data['smsid'])
                        return str(status_code)

                    # wait for next run
                    time.sleep(0.50)
                    now = datetime.utcnow()

                # maxwaitpid reached so raise an error
                smsgwglobals.pislogger.warning("/sendsms: maxwaitpid " +
                                               "of " + str(maxwaitpid) +
                                               " seconds reached!")
                # If timeout occured - set own status code
                status_code = 1000
                cherrypy.response.status = 200
                cherrypy.response.body = status_code
                PID.removeclientsms(address, data['smsid'])
                return str(status_code)

            else:
                smsgwglobals.pislogger.warning("/sendsms: No PID for " +
                                               "modem " + data['modemid'] +
                                               " found!")
                # If no modem endpoint to send - set own status code
                status_code = 2000
                cherrypy.response.status = 200
                cherrypy.response.body = status_code
                return str(status_code)

        except Exception as e:
            smsgwglobals.pislogger.debug("/sendsms: Internal Server "
                                         "Error! " + str(e))
            cherrypy.response.status = 500  # Internal Server Error
            return
示例#9
0
 def stop(self):
     # Unregister all Modems at WIS
     modemlist = PID.getclientmodemlist()
     PID.delclient()
     WIS.unregister(modemlist)
示例#10
0
 def closed(self, code, reason=None):
     modemlist = PID.getclientmodemlist(self.peer_address)
     PID.delclient(self.peer_address, code, reason)
     # Try to unregister
     WIS.unregister(modemlist)
示例#11
0
 def opened(self):
     PID.addclient(self.peer_address, self)