Exemplo n.º 1
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
Exemplo n.º 2
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
Exemplo n.º 3
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