Exemplo n.º 1
0
    def test_ServiceSendMessage(self):
        msg_type = 3
        msg_subtype = 3
        msg_url = "/sensor/temp"
        msg = {"test": "msg"}
        msg_data = {}
        msg_dir = MessageSpecification.MSG_DIRECTION_SEND

        msg_spec = MessageSpecification(msg_type, msg_subtype, msg_data,
                                        msg_url, msg_dir)

        # Initialize the Service on the first run
        self.MsgEx.SvcInit()

        # Override the MQTT message receive callback set by the Service
        # so messages are received by the test.
        self.MqttClient.set_callback(test_MessageExchange.MqttMsgRecvCallback)
        # Subscribe to the topic.
        self.MqttClient.subscribe(msg_url)

        self.MsgEx.RegisterMessageType(msg_spec)
        self.MsgEx.MessagePut(msg, msg_type, msg_subtype)

        # Run the Service again to send the message
        self.MsgEx.SvcRun()

        recv_msg = Message.Deserialize(self.RecvMsg)
        self.assertEqual(self.RecvMsgCount, 1)
        print(recv_msg)
        self.assertEqual(recv_msg[MessageTemplate.MSG_SECTION_DATA], msg)
    def test_PutGetMessageRoundTrip(self):
        ep = Endpoint()

        msg_type = 3
        msg_subtype = 3
        msg_url = "<pn>/<id>/temp"
        msg = {"test": "msg"}
        msg_dir = MessageSpecification.MSG_DIRECTION_BOTH

        msg_spec = MessageSpecification(msg_type, msg_subtype, msg, msg_url,
                                        msg_dir)

        self.MsgEx.RegisterMessageType(msg_spec)

        # Initialize the Service on the first run
        self.MsgEx.SvcInit()

        ep.MessagePut(msg, msg_type, msg_subtype)

        # Run the Service again to publish the message.
        self.MsgEx.SvcRun()

        utime.sleep(1)

        # Run the Service again to receive the message.
        self.MsgEx.SvcRun()

        recv_msg = ep.MessageGet(msg_type, msg_subtype)
        print(recv_msg)
        self.assertEqual(recv_msg[Message.MSG_SECTION_DATA], msg)
 def setUp(self):
     MessageSpecification.Config(self.UrlFields)
     self.RecvMsgCount = 0
     self.RecvTopic = None
     self.RecvMsg = None
     self.MqttClient = MQTTClient(self.ID, self.BROKER, self.PORT)
     self.MsgEx = MessageExchange(self.DIR, self.MqttClient, self.ID,
                                  self.RETRIES)
Exemplo n.º 4
0
    def test_ConstructorNoUrlFieldSubstitution(self):
        url = "/sensor/temp"
        msg_spec = MessageSpecification(self.TYPE, self.SUBTYPE, self.DATA_DEF,
                                        url, self.DIRECTION)

        self.assertEqual(msg_spec.Type, self.TYPE)
        self.assertEqual(msg_spec.Subtype, self.SUBTYPE)
        self.assertEqual(msg_spec.DataDef, self.DATA_DEF)
        self.assertEqual(msg_spec.Url, url)
        self.assertEqual(msg_spec.Direction, self.DIRECTION)
Exemplo n.º 5
0
    def test_ConstructorWithUrlFieldSubstitution(self):
        static_url = "/sensor/temp"
        url = "<pn>/<id>" + static_url
        msg_spec = MessageSpecification(self.TYPE, self.SUBTYPE, self.DATA_DEF,
                                        url, self.DIRECTION)

        self.assertEqual(msg_spec.Type, self.TYPE)
        self.assertEqual(msg_spec.Subtype, self.SUBTYPE)
        self.assertEqual(msg_spec.DataDef, self.DATA_DEF)
        self.assertEqual(
            msg_spec.Url,
            self.UrlFields["<pn>"] + '/' + self.UrlFields["<id>"] + static_url)
        self.assertEqual(msg_spec.Direction, self.DIRECTION)
Exemplo n.º 6
0
    def test_MessageGetTypeIsSend(self):
        msg_type = 3
        msg_subtype = 3
        msg_url = "/sensor/temp"
        msg_data = {}
        msg_dir = MessageSpecification.MSG_DIRECTION_SEND

        msg_spec = MessageSpecification(msg_type, msg_subtype, msg_data,
                                        msg_url, msg_dir)

        self.MsgEx.RegisterMessageType(msg_spec)
        recv_msg = self.MsgEx.MessageGet(msg_type, msg_subtype)

        self.assertEqual(recv_msg, -2)
Exemplo n.º 7
0
    def test_MessageMapFromUrlEntryPresent(self):
        msg_type = 3
        msg_subtype = 3
        msg_url = "/sensor/temp"
        msg_data = {}
        msg_dir = MessageSpecification.MSG_DIRECTION_BOTH

        msg_spec = MessageSpecification(msg_type, msg_subtype, msg_data,
                                        msg_url, msg_dir)

        self.MsgEx.RegisterMessageType(msg_spec)
        msg_map = self.MsgEx.MessageMapFromRoute(msg_url)

        self.assertEqual(msg_map[MessageExchange.MSG_MAP_TYPE], msg_type)
        self.assertEqual(msg_map[MessageExchange.MSG_MAP_SUBTYPE], msg_subtype)
        self.assertEqual(msg_map[MessageExchange.MSG_MAP_ROUTING], msg_url)
Exemplo n.º 8
0
    def test_RegisterMessageTypeDirectionSend(self):
        msg_type = 1
        msg_subtype = 3
        msg_url = "/sensor/temp"
        msg_data = {}
        msg_dir = MessageSpecification.MSG_DIRECTION_SEND

        msg_spec = MessageSpecification(msg_type, msg_subtype, msg_data,
                                        msg_url, msg_dir)

        self.MsgEx.RegisterMessageType(msg_spec)
        msg_map = self.MsgEx.MessageMapFromType(msg_type, msg_subtype)
        print(msg_map)
        self.assertEqual(msg_map[MessageExchange.MSG_MAP_TYPE], msg_type)
        self.assertEqual(msg_map[MessageExchange.MSG_MAP_SUBTYPE], msg_subtype)
        self.assertEqual(msg_map[MessageExchange.MSG_MAP_ROUTING], msg_url)
        self.assertIsNone(msg_map[MessageExchange.MSG_MAP_RECV_BUFFER])
Exemplo n.º 9
0
def main(argv):
    ID = 'server_node'
    BROKER = '192.168.0.103'
    PORT = 1883

    global _MsgListFile
    global _MsgList
    global _BaseDir

    _MsgListFile = argv[0]
    print(_MsgListFile)

    _BaseDir = argv[1]
    print(_BaseDir)

    msg_file = open(_MsgListFile, 'r')
    msg_file_str = msg_file.read()
    print(msg_file_str)
    msg_file.close()

    _MsgList = json.loads(msg_file_str)

    for msg_entry in _MsgList.keys():
        print(msg_entry)
        msg_spec = MessageSpecification.CreateFromFile(_MsgList[msg_entry]["spec"])
        print(msg_spec)
        MessageSpecs.add(msg_spec)

        output_dir = ComposePath(_BaseDir, msg_spec.Type, msg_spec.Subtype)
        print("Creating folder: {}".format(output_dir))
        try:
            os.mkdir(output_dir)
        except OSError:
            print("Could not create folder")

    mqtt_client = MQTTClient(ID, BROKER, PORT)
    mqtt_client.set_callback(MqttMsgRecvCallback)
    mqtt_client.connect()

    for spec in MessageSpecs:
        print("Subscribing to: {}".format(spec.Url))
        mqtt_client.subscribe(spec.Url)

    while True:
        while mqtt_client.check_msg() is None:
            time.sleep(3)
Exemplo n.º 10
0
    def test_MessageGetSuccessful(self):
        msg_type = 3
        msg_subtype = 3
        msg_url = "/sensor/temp"
        msg_data = {}
        msg = {"test": "msg"}
        msg_dir = MessageSpecification.MSG_DIRECTION_BOTH

        msg_spec = MessageSpecification(msg_type, msg_subtype, msg_data,
                                        msg_url, msg_dir)

        self.MsgEx.RegisterMessageType(msg_spec)
        msg_map = self.MsgEx.MessageMapFromType(msg_type, msg_subtype)
        buf = msg_map[MessageExchange.MSG_MAP_RECV_BUFFER]

        Message.Serialize(msg)
        buf.MessagePut(Message.Stream().getvalue().decode('utf-8'))
        recv_msg = self.MsgEx.MessageGet(msg_type, msg_subtype)
        print(recv_msg)
        print(type(recv_msg))
        self.assertEqual(recv_msg[MessageTemplate.MSG_SECTION_DATA], msg)
Exemplo n.º 11
0
    def test_MessagePut(self):
        msg_type = 3
        msg_subtype = 3
        msg_url = "/sensor/temp"
        msg_data = {}
        msg = {"test": "msg"}
        msg_dir = MessageSpecification.MSG_DIRECTION_BOTH

        msg_spec = MessageSpecification(msg_type, msg_subtype, msg_data,
                                        msg_url, msg_dir)

        self.MsgEx.RegisterMessageType(msg_spec)
        res = self.MsgEx.MessagePut(msg, msg_type, msg_subtype)
        msg_tuple = self.MsgEx.SendMessageBuffer.MessageGet()

        self.assertEqual(res, 1)
        self.assertEqual(msg_tuple[MessageBuffer.MSG_STRUCT_TYPE], msg_type)
        self.assertEqual(msg_tuple[MessageBuffer.MSG_STRUCT_SUBTYPE],
                         msg_subtype)
        self.assertEqual(
            Message.Deserialize(msg_tuple[MessageBuffer.MSG_STRUCT_DATA])[
                MessageTemplate.MSG_SECTION_DATA], msg)
Exemplo n.º 12
0
    def test_ServiceSendAndReceiveRoundTrip(self):
        msg_type = 3
        msg_subtype = 3
        msg_url = "/sensor/temp"
        msg = {"test": "msg"}
        msg_data = {}
        msg_dir = MessageSpecification.MSG_DIRECTION_BOTH

        msg_spec = MessageSpecification(msg_type, msg_subtype, msg_data,
                                        msg_url, msg_dir)

        # Initialize the Service on the first run
        self.MsgEx.SvcInit()

        self.MsgEx.RegisterMessageType(msg_spec)
        self.MsgEx.MessagePut(msg, msg_type, msg_subtype)

        # Run the Service again to send and check for received messages.
        self.MsgEx.SvcRun()

        recv_msg = self.MsgEx.MessageGet(msg_type, msg_subtype)
        print(recv_msg)
        self.assertEqual(recv_msg[MessageTemplate.MSG_SECTION_DATA], msg)
Exemplo n.º 13
0
 def Send(self, msg_map, payload, size):
     topic = MessageSpecification.UrlResolve(
         msg_map[MessageExchange.MSG_MAP_ROUTING])
     self.Client.publish(topic, payload)
     return
Exemplo n.º 14
0
    def __init__(self, netcon_obj):
        # Configure the URL fields.
        MessageSpecification.Config(self.UrlFields)

        DemoApp.NetCon = netcon_obj
Exemplo n.º 15
0
 def setUp(self):
     MessageSpecification.Config(self.UrlFields)
     return
Exemplo n.º 16
0
    def setUp(self):
        # Configure the URL fields.
        MessageSpecification.Config(self.UrlFields)

        # Create objects.
        filter_depth = len(self.TempSamples) / 2
        dummy_temp_sensor = DummySensor.DummySensor(self.TempSamples)
        dummy_moist_sensor = DummySensor.DummySensor(self.MoistSamples)
        wlan_ap = WLAN()
        self.Time = SystemTime.InstanceGet()
        self.NetCon = NetCon(self.DIR, self.ApCfg, NetCon.MODE_STATION,
                             wlan_ap)
        self.TempSensor = Sensor.Sensor(self.DIR, SensorReportTemp.NAME_TEMP,
                                        filter_depth, dummy_temp_sensor)
        self.MoistSensor = Sensor.Sensor(self.DIR,
                                         SensorReportMoist.NAME_MOIST,
                                         filter_depth, dummy_moist_sensor)
        self.MqttClient = MQTTClient(self.ID, self.BROKER, self.PORT)
        self.MsgEx = MessageExchange(self.DIR, self.MqttClient, self.ID,
                                     self.RETRIES)
        self.MsgEp = Endpoint()
        self.Scheduler = ServiceScheduler()

        # Set service dependencies.
        self.Time.SvcDependencies(
            {self.NetCon: Service.DEP_TYPE_RUN_ALWAYS_BEFORE_RUN})
        self.MsgEx.SvcDependencies({
            self.Time:
            Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN,
            self.NetCon:
            Service.DEP_TYPE_RUN_ALWAYS_BEFORE_INIT
        })
        self.TempSensor.SvcDependencies(
            {self.Time: Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN})
        self.MoistSensor.SvcDependencies(
            {self.Time: Service.DEP_TYPE_RUN_ONCE_BEFORE_RUN})

        # Register all services to the scheduler.
        self.Scheduler.ServiceRegister(self.Time)
        self.Scheduler.ServiceRegister(self.NetCon)
        self.Scheduler.ServiceRegister(self.MsgEx)
        self.Scheduler.ServiceRegister(self.TempSensor)
        self.Scheduler.ServiceRegister(self.MoistSensor)

        # Create message specifications.
        self.TempMsgSpec = SensorReportTemp()
        self.MoistMsgSpec = SensorReportMoist()
        self.LogMsgSpec = LogMessage()

        # Create a Messaging Endpoint and MessageFormatAdapters.
        self.TempAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE,
            self.TempMsgSpec)
        self.MoistAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE,
            self.MoistMsgSpec)
        self.LogAdapt = MessageFormatAdapter(
            self.MsgEp, MessageFormatAdapter.SEND_ON_COMPLETE, self.LogMsgSpec)

        # Register message specs.
        self.MsgEx.RegisterMessageType(self.TempMsgSpec)
        self.MsgEx.RegisterMessageType(self.MoistMsgSpec)
        self.MsgEx.RegisterMessageType(self.LogMsgSpec)

        # Create observers for the sensor data.
        self.TempObserver = self.TempAdapt.CreateObserver(
            SensorReportTemp.DATA_KEY_SENSOR_REPORT_ARRAY,
            self.SamplesPerMessage)
        self.MoistObserver = self.MoistAdapt.CreateObserver(
            SensorReportMoist.DATA_KEY_SENSOR_REPORT_ARRAY,
            self.SamplesPerMessage)

        # Link the observers to the sensors.
        self.TempSensor.ObserverAttachNewSample(self.TempObserver)
        self.MoistSensor.ObserverAttachNewSample(self.MoistObserver)

        # Create a stream for the log messages.
        self.LogStream = self.LogAdapt.CreateStream(
            LogMessage.DATA_KEY_LOG_MSG, ExtLogging.WRITES_PER_LOG)

        # Configure the ExtLogging class.
        ExtLogging.ConfigGlobal(level=ExtLogging.INFO, stream=self.LogStream)

        # Configure the station settings to connect to a WLAN AP.
        self.NetCon.StationSettingsStore(self.ApCfg["ssid"], self.ApCfg["pwd"])

        # Declare test variables.
        self.RecvMsgCount = 0
        self.RecvTopic = None
        self.RecvMsg = None