Пример #1
0
 def process(self, rx_record):
     ctr = self.__counter
     self.__counter += 1
     msg = connection.Message(connection.Message.ACK,
                              code=constants.CONTENT,
                              payload='%d' % (ctr, ))
     rx_record.ack(msg)
Пример #2
0
 def process(self, rx_record):
     msg = connection.Message(connection.Message.ACK,
                              code=constants.CONTENT,
                              content_type='application/link-format')
     msg.payload = ",".join(
         [_s.encode() for _s in self.__services.itervalues()])
     rx_record.ack(msg)
    def process(self, rx_record, uri):
        self.uriQuery_ = uri
        print "TESTING STORE SERVICE......"
        print "URI QUERY ................ %s" % self.uriQuery_
        payloadtype = False

        msg = rx_record.message
        if constants.PUT == msg.code or constants.POST == msg.code:
            self.__storage = msg.payload

            pars.parsePayload(str(self.__storage), payloadtype)
            strInfo.createResource(self.uriQuery_, pars.returnParsePayload(),
                                   pars.returnPayloadType())

            print 'Storing value: %s' % (self.__storage,)
            msg = connection.Message(connection.Message.ACK,
                                     code=constants.CHANGED,
                                     payload='Storing value: %s' % (
                                     self.__storage,))
        elif constants.DELETE == msg.code:
            self.__storage = ""
            print 'Deleting value: %s' % (self.__storage,)
            msg = connection.Message(connection.Message.ACK,
                                     code=constants.DELETED,
                                     payload='Deleting value: %s' % (
                                     self.__storage,))
        elif constants.GET == msg.code:
            print 'Stored value: %s' % (self.__storage,)

            pars.parsePayload(str(self.__storage), payloadtype)

            # print "URI QUERY FROM GET %s" %len(self.uriQuery_)

            strInfo.getResource(self.uriQuery_, pars.returnParsePayload())

            msg = connection.Message(connection.Message.ACK,
                                     code=constants.CONTENT, payload=json.dumps(
                    strInfo.returnGetResource()))  # 'Stored value: %s' % (self.__storage,))
        rx_record.ack(msg)
Пример #4
0
 def process(self, rx_record):
     msg = rx_record.message
     if constants.PUT == msg.code or constants.POST == msg.code:
         self.__storage = msg.payload
         print 'Storing value: %s' % (self.__storage, )
         msg = connection.Message(connection.Message.ACK,
                                  code=constants.CHANGED,
                                  payload='Storing value: %s' %
                                  (self.__storage, ))
     elif constants.DELETE == msg.code:
         self.__storage = ""
         print 'Deleting value: %s' % (self.__storage, )
         msg = connection.Message(connection.Message.ACK,
                                  code=constants.DELETED,
                                  payload='Deleting value: %s' %
                                  (self.__storage, ))
     elif constants.GET == msg.code:
         print 'Stored value: %s' % (self.__storage, )
         msg = connection.Message(connection.Message.ACK,
                                  code=constants.CONTENT,
                                  payload='Stored value: %s' %
                                  (self.__storage, ))
     rx_record.ack(msg)
Пример #5
0
    def resource_update(self, rx_record, uriQuery1, remote):
        """ Updates the resources in the local database and LWM2M server  """
        
        store_query = []
        for val in self.uriQuery1:
            splitQuery = str(str(val).split(":")[1]).strip()
            store_query.append({"id" : -1,
                                "code" : str(splitQuery).split("=")[0],
                                "value" : str(splitQuery).split("=")[1]})
        split_payload = str(rx_record.message.payload).split("/")

        local_lwm2m_client.update_mgmt_object(split_payload[0], split_payload[1], store_query)        
        payloadForServer = rx_record.message.payload
        
        msg = connection.Message(connection.Message.ACK, code=constants.CHANGED, payload="Resource Updated")
        self.local_server.sendto(msg._pack(rx_record.transaction_id), remote)
        
        # Updates the LWM2M Server
        path = "rd/" + location_address +"?"
        query = str(splitQuery).split("=")[0] + "=" + str(splitQuery).split("=")[1]
        payload = payloadForServer
        client_request.clientUpdate(lwm2m_server_ip_port, query, payload, path=path, client_port=self.local_client_port)
Пример #6
0
    def process(self, rx_record, remote, uriQ):
        """ Processes the POST, PUT and GET requests """
        msg=rx_record.message
        self.uriQuery1 = uriQ
        self.payload = msg.payload
        global resourceString
        if constants.POST == msg.code:
            #Registration Client
            global server_ip_port, location_address

            q = rx_record.message.findOption(URI_QUERY_VALUE)[0].value
            #TODO: check if coap says somthing about execute messages
            if str(q).find("execute") != -1:
                print "entered in client:: execute field"
                self.executeResource(rx_record)
                msg = connection.Message(connection.Message.ACK, code=constants.CHANGED, payload="execution")
                self.local_server.sendto(msg._pack(rx_record.transaction_id), remote)
            else:
                server_ip = self.lwm2m_dm_server_ip
                server_port = self.lwm2m_dm_server_port
                server_ip_port = server_ip + ":" + str(server_port)
                payload = msg.payload
                path = "rd?"
                query = rx_record.message.findOption(URI_QUERY_VALUE)[0].value.strip()
                #Creates objects in the local database
                local_lwm2m_client.create_mgmt_objects(query, payload)
                
                msg = connection.Message(connection.Message.ACK, code=constants.CREATED, payload="Client Registered")
                self.local_server.sendto(msg._pack(rx_record.transaction_id), remote)
                #Registration in the Server
                res_register = client_request.clientRegistration(lwm2m_server_ip_port, path, query, payload, client_port = 5683)

                location_address = res_register.findOption(LOCATION_VALUE)[0].value
                self.storeDiscoverPaths.append({ "path" : "rd", "location" : location_address, "objectID" : None, "objectInstID" : None, "resID" : None})
            
        elif constants.PUT == msg.code:
            check_pmax = 0
            #pmin and pmax imposed from the dm server, upath related to an object
            for val1 in uriQ:
                if str(val1).find("pmax") != -1:
                    check_pmax = 1
            if check_pmax == 1:
               	path = []
                for v in rx_record.message.findOption(URI_PATH_VALUE):
                    path.append(v.value)
                upath = "/".join(path)

        	pmax = str(rx_record.message.findOption(URI_QUERY_VALUE)[0].value).split("=")[1]
        	pmin = str(rx_record.message.findOption(URI_QUERY_VALUE)[1].value).split("=")[1]
                check_pmax = 0
                filtered_resources = self.write_attributes(upath, pmax, pmin)
                
                msg = connection.Message(connection.Message.ACK, code=constants.CHANGED, payload=filtered_resources)
                self.local_server.sendto(msg._pack(rx_record.transaction_id), remote)
            else:
                #TODO: Write: standard exists for writing resources?
                self.resource_update(rx_record, self.uriQuery1, remote)
            
        elif constants.GET == msg.code:
            try:
                observe_value = rx_record.message.findOption(options.Observe).value
            except ValueError:
                observe_value = None #-1
            
            if observe_value == OBSERVE_OPTION_VALUE_OBSERVATION:
                filtered_resources = self.observe_resource(rx_record, self.uriQuery1)
            elif observe_value == OBSERVE_OPTION_VALUE_CANCEL_OBSERVATION:
                filtered_resources = self.cancel_observe_resource(rx_record)
            elif str(rx_record.message.findOption(URI_PATH_VALUE)[0].value).find("rd") != -1:
                filtered_resources = self.handle_discover_request(rx_record)
                
            msg = connection.Message(connection.Message.ACK, code=constants.CONTENT, payload=json.dumps(filtered_resources))
            self.local_server.sendto(msg._pack(rx_record.transaction_id), remote)
Пример #7
0
 def process(self, rx_record):
     uptime = time.time() - self.__started
     msg = connection.Message(connection.Message.ACK,
                              code=constants.CONTENT,
                              payload='%g' % (uptime, ))
     rx_record.ack(msg)
Пример #8
0
def putResource(ep, uri_path, remote, value):
    msg = connection.Message(code=constants.PUT,
                             payload=value,
                             uri_path=uri_path)
    resp = wait_for_response(ep, ep.send(msg, remote))
    return resp.payload
Пример #9
0
def getResource(ep, uri_path, remote):
    msg = connection.Message(code=constants.GET, uri_path=uri_path)
    resp = wait_for_response(ep, ep.send(msg, remote))
    return resp.payload