コード例 #1
0
        def do_POST(self):
            ''' The ContextBroker is informing us via one of our subscriptions.
                We convert the received content back and publish 
                it in ROS.

                self: The "request" from the Context-Broker

                we invoke """RosTopicHandler.publish""" here!
            '''
            # retreive Data and get the updated information
            recData = self.rfile.read(int(self.headers['Content-Length']))
            receivedData = json.loads(recData)
            data = receivedData['data'][0] # Specific to NGSIv2 
            jsonData = json.dumps(data)
            topics = data.keys() # Convention Topic-Names are the attributes by an JSON Object, except: type, id

            # iterate through every 'topic', since we only receive updates from one topic
            # Only id, type and 'topicname' are present
            for topic in topics:
                if topic != 'id' and topic != 'type':
                    dataStruct = self._buildTypeStruct(data[topic])

                    # Convert Back into a Python-Object
                    obj = self.TypeValue()
                    ObjectFiwareConverter.fiware2Obj(jsonData, obj, setAttr=True, useMetaData=False)
                    # Publish in ROS
                    RosTopicHandler.publish(data['id'], topic, getattr(obj, topic), dataStruct)

            # Send OK!
            self.send_response(204)
            self.end_headers() # Python 3 needs an extra end_headers after send_response
コード例 #2
0
ファイル: cbSubscriber.py プロジェクト: ikh-innovation/firos
        def do_POST(self):
            ''' The ContextBroker is informing us via one of our subscriptions.
                We convert the received content back and publish 
                it in ROS.

                self: The "request" from the Context-Broker

                we invoke """RosTopicHandler.publish""" here!
            '''
            # retreive Data and get the updated information
            recData = self.rfile.read(int(self.headers['Content-Length']))
            receivedData = json.loads(recData)
            data = receivedData['data'][0]  # Specific to NGSIv2
            jsonData = json.dumps(data)

            obj = self.TypeValue()
            ObjectFiwareConverter.fiware2Obj(jsonData,
                                             obj,
                                             setAttr=True,
                                             useMetaData=False,
                                             encoded=True)
            obj.id = obj.id.replace("_slash_", "/")
            obj.type = obj.type.replace("_slash_", "/")

            objType = obj.type
            topic = obj.id

            del data["id"]
            del data["type"]
            tempDict = dict(type=objType, value=data)

            dataStruct = self._buildTypeStruct(tempDict)

            RosTopicHandler.publish(topic, obj.__dict__, dataStruct)
            # # Send OK!
            self.send_response(204)
            self.end_headers(
            )  # Python 3 needs an extra end_headers after send_response