Exemple #1
0
    def on_message(self, message):
        #print "Received message", message
        if len(message) == 0: return

        decodedMsg = rscpLib.rscpDecode(message)[0]

        #print "Decoded received message", decodedMsg
        if decodedMsg[0] == 'SERVER_REQ_PING':
            pingFrame = rscpLib.rscpFrame(
                rscpLib.rscpEncode("SERVER_PING", "None", None))
            self.ws.send(pingFrame, websocket.ABNF.OPCODE_BINARY)
            return
        elif decodedMsg[0] == 'SERVER_REGISTER_CONNECTION':
            self.registerConnectionHandler(decodedMsg)
        elif decodedMsg[0] == 'SERVER_UNREGISTER_CONNECTION':
            # this signifies some error
            self.disconnect()
        elif decodedMsg[0] == 'SERVER_REQ_RSCP_CMD':
            thisConId = rscpLib.rscpFindTag(decodedMsg,
                                            'SERVER_CONNECTION_ID')[2]
            data = rscpLib.rscpFrameDecode(
                rscpLib.rscpFindTag(decodedMsg, 'SERVER_RSCP_DATA')[2])[0]
            response = ''
            self.responseCallbackCalled = False
            while len(data) > 0:
                decoded, l = rscpLib.rscpDecode(data)
                #print "Inner frame chunk decoded", decoded
                data = data[l:]
                responseChunk = self.respondToINFORequest(decoded)
                if responseChunk is None:
                    # this is not a standard request: call the registered callback
                    if self.responseCallback is not None:
                        self.responseCallback(
                            decoded
                        )  # !!! Important!!! This is where the callback is called with the decoded inner frame
                        self.responseCallbackCalled = True
                    responseChunk = ''

                response += responseChunk
            if self.responseCallbackCalled:
                self.responseCallback = None  # unregister the callback. Good idea??
            if len(response) == 0: return  # do not send an empty response
            innerFrame = rscpLib.rscpFrame(response)
            responseContainer = rscpLib.rscpEncode(
                "SERVER_REQ_RSCP_CMD", "Container",
                [("SERVER_CONNECTION_ID", "Int64", self.conId),
                 ("SERVER_AUTH_LEVEL", "UChar8", self.authLevel),
                 ("SERVER_RSCP_DATA_LEN", "Int32", len(innerFrame)),
                 ("SERVER_RSCP_DATA", "ByteArray", innerFrame)])

            self.ws.send(rscpLib.rscpFrame(responseContainer),
                         websocket.ABNF.OPCODE_BINARY)
Exemple #2
0
    def _sendRequest_internal(self,
                              innerFrame,
                              callback=None,
                              synchronous=False):
        """
            sendRequest(self, innerFrame, dataType=None, content=None):
            
            This can be called in two ways:
            sendRequest(<RSCP encoded frame>, [callback], [synchronous])
            sendRequest(<tuple>, [callback], [synchronous])
            
            If synchronous == True, the method waits for a response (i.e. exits after calling callback).
            If synchronous == True and callback = None, the method returns the (last) response message
        """
        if not self.isConnected:
            raise SocketNotReady

        if isinstance(innerFrame, tuple):
            # if innerframe is a tuple then the message is not encoded
            innerFrame = rscpLib.rscpFrame(rscpLib.rscpEncode(*innerFrame))

        self.requestResult = None
        self.responseCallbackCalled = False
        if callback is not None:
            self.responseCallback = callback
        else:
            if synchronous:
                self.responseCallback = lambda msg: self._defaultRequestCallback(
                    msg)
            else:
                self.responseCallback = None

        outerFrame = rscpLib.rscpFrame(
            rscpLib.rscpEncode(
                "SERVER_REQ_RSCP_CMD", "Container",
                [("SERVER_CONNECTION_ID", "Int64", self.virtConId),
                 ("SERVER_AUTH_LEVEL", "UChar8", self.virtAuthLevel),
                 ("SERVER_RSCP_DATA_LEN", "Int32", len(innerFrame)),
                 ("SERVER_RSCP_DATA", "ByteArray", innerFrame)]))

        self.ws.send(outerFrame, websocket.ABNF.OPCODE_BINARY)

        if synchronous == True:
            for i in xrange(self.TIMEOUT * 10):
                if self.responseCallbackCalled: break
                time.sleep(0.1)
            if not self.responseCallbackCalled: raise RequestTimeoutError

            if callback is None:  # the default callback was called
                return self.requestResult
    def buildVirtualConn(self):
        virtualConn = rscpLib.rscpFrame(
            rscpLib.rscpEncode(
                "SERVER_REQ_NEW_VIRTUAL_CONNECTION", "Container",
                [("SERVER_USER", "CString", self.username),
                 ("SERVER_PASSWD", "CString", self.password),
                 ("SERVER_IDENTIFIER", "CString", "S10-" + self.serialNumber),
                 ("SERVER_TYPE", "Int32", 4),
                 ("SERVER_HASH_CODE", "Int32", 1234567890)]))

        #print "--------------------- Sending virtual conn"
        self.ws.send(virtualConn, websocket.ABNF.OPCODE_BINARY)
 def respondToINFORequest(self, decoded):
     TIMEZONE_STR, utcDiffS = calcTimeZone()
     tag = decoded[0]
     if tag == 'INFO_REQ_IP_ADDRESS':
         return rscpLib.rscpEncode("INFO_IP_ADDRESS", "CString", "0.0.0.0")
     elif tag == "INFO_REQ_SUBNET_MASK":
         return rscpLib.rscpEncode("INFO_SUBNET_MASK", "CString", "0.0.0.0")
     elif tag == "INFO_REQ_GATEWAY":
         return rscpLib.rscpEncode("INFO_GATEWAY", "CString", "0.0.0.0")
     elif tag == "INFO_REQ_DNS":
         return rscpLib.rscpEncode("INFO_DNS", "CString", "0.0.0.0")
     elif tag == "INFO_REQ_DHCP_STATUS":
         return rscpLib.rscpEncode("INFO_DHCP_STATUS", "Bool", "false")
     elif tag == "INFO_REQ_TIME":
         return rscpLib.rscpEncode("INFO_TIME", "ByteArray",
                                   timestampEncode(time.time()))
     elif tag == "INFO_REQ_TIME_ZONE":
         return rscpLib.rscpEncode("INFO_TIME_ZONE", "CString",
                                   TIMEZONE_STR)
     elif tag == "INFO_REQ_UTC_TIME":
         return rscpLib.rscpEncode("INFO_UTC_TIME", "ByteArray",
                                   timestampEncode(time.time() - utcDiffS))
     elif tag == "INFO_REQ_A35_SERIAL_NUMBER":
         return rscpLib.rscpEncode("INFO_A35_SERIAL_NUMBER", "CString",
                                   "123456")
     elif tag == "INFO_REQ_INFO":
         return rscpLib.rscpEncode(
             "INFO_INFO", "Container",
             [("INFO_SERIAL_NUMBER", "CString", "WEB_" +
               hashlib.md5(self.username + str(self.conId)).hexdigest()),
              ("INFO_PRODUCTION_DATE", "CString", "570412800000"),
              ("INFO_MAC_ADDRESS", "CString", "00:00:00:00:00:00")])
     elif tag == "INFO_SERIAL_NUMBER":
         self.webSerialno = decoded[2]
         self.buildVirtualConn()
         return ''
     return None  # this is no standard request
 def registerConnectionHandler(self, decodedMsg):
     if self.conId is None:
         self.conId = rscpFindTag(decodedMsg, 'SERVER_CONNECTION_ID')[2]
         self.authLevel = rscpFindTag(decodedMsg, 'SERVER_AUTH_LEVEL')[2]
     else:
         self.virtConId = rscpFindTag(decodedMsg, 'SERVER_CONNECTION_ID')[2]
         self.virtAuthLevel = rscpFindTag(decodedMsg,
                                          'SERVER_AUTH_LEVEL')[2]
     #reply = rscpLib.rscpFrame(rscpLib.rscpEncode("SERVER_CONNECTION_REGISTERED", "Container", [decodedMsg[2][0], decodedMsg[2][1]]));
     reply = rscpLib.rscpFrame(
         rscpLib.rscpEncode("SERVER_CONNECTION_REGISTERED", "Container", [
             rscpFindTag(decodedMsg, 'SERVER_CONNECTION_ID'),
             rscpFindTag(decodedMsg, 'SERVER_AUTH_LEVEL')
         ]))
     self.ws.send(reply, websocket.ABNF.OPCODE_BINARY)
Exemple #6
0
 def _send(self, plainMsg):
     sendData = rscpFrame(rscpEncode(plainMsg))
     encData = self.encdec.encrypt(sendData)
     self.socket.send(encData)
Exemple #7
0
 def sendCommand(self, message):
     return self._sendRequest_internal(
         rscpLib.rscpFrame(rscpLib.rscpEncode(message)), None, False)
Exemple #8
0
 def sendRequest(self, message):
     return self._sendRequest_internal(
         rscpLib.rscpFrame(rscpLib.rscpEncode(message)), None, True)