RWTH-EBC / FiLiP

FIWARE Library for Python (FiLiP) to work with FIWARE API
BSD 3-Clause "New" or "Revised" License
23 stars 13 forks source link

Filip MQTT Client still require object_id #325

Open djs0109 opened 6 days ago

djs0109 commented 6 days ago

Describe the bug The object_idis used in the iotagent_mqtt_client. However, it is not a required variable for device attribute. The iotgent_mqtt_client needs to be adapted

github-actions[bot] commented 6 days ago

Branch 325-Filip-MQTT-Client-still-require-object_id created!

djs0109 commented 6 days ago

Here it requires the object_id to exist.

https://github.com/RWTH-EBC/FiLiP/blob/f8660a2a14b2d049d154552078a9109671ea9e06/filip/clients/mqtt/client.py#L343C1-L344C49

djs0109 commented 12 hours ago

@SystemsPurge here is the script to reproduce

import time
from filip.clients.mqtt import IoTAMQTTClient
from filip.models.ngsi_v2.iot import PayloadProtocol, Device
import paho.mqtt.client as mqtt

Host = "134.130.166.184"
Port = 1883

devices_list = [
    {
        "device_id": "device:001",
        "apikey": "n5gehtutorial",
        "entity_name": "urn:ngsi-ld:WeatherStation:001",
        "entity_type": "WeatherStation",
        "transport": "MQTT",
        "attributes": [
            {
                "name": "temperature",
                "type": "Number",
                # "object_id": "t"  # TODO  without it cant work
            }
        ],
        "lazy": [],
        "commands": [],
        "static_attributes": [],
        "protocol": "IoTA-JSON",
        "ngsiVersion": "v2"
    },
    {
        "device_id": "device:002",
        "apikey": "n5gehtutorial",
        "entity_name": "urn:ngsi-ld:TemperatureSensor:001",
        "entity_type": "TemperatureSensor",
        "transport": "MQTT",
        "attributes": [
            {
                "name": "temperature",
                # "object_id": "t",  # TODO  without it cant work
                "type": "Number"
            }
        ],
        "lazy": [],
        "commands": [],
        "static_attributes": [],
        "protocol": "IoTA-JSON",
        "ngsiVersion": "v2"
    },
    {
        "device_id": "device:003",
        "apikey": "n5gehtutorial",
        "entity_name": "urn:ngsi-ld:Heater:001",
        "entity_type": "Heater",
        "transport": "MQTT",
        "attributes": [],
        "lazy": [],
        "commands": [
            {
                "name": "heaterPower",
                "type": "Number"
            }
        ],
        "static_attributes": [],
        "protocol": "IoTA-JSON",
        "ngsiVersion": "v2"
    }
]
devices = [Device(**device) for device in devices_list]
mqttc = IoTAMQTTClient(protocol=mqtt.MQTTv5, devices=devices)

mqttc.connect(host=Host, port=Port)

def on_command(client, obj, msg):
    """
    Callback function for incoming commands
    """
    # Decode the message payload using the libraries builtin encoders
    apikey, device_id, payload = \
        client.get_encoder(PayloadProtocol.IOTA_JSON).decode_message(
            msg=msg)

    # Transfer the command to the wanted format
    command_dict = {device_id: dict(payload)}

    print(f"receive command {command_dict}")

    # Acknowledge the command (necessary!)
    client.publish(device_id=device_id,
                   command_name=next(iter(payload)),
                   payload=payload)

# add command call back
mqttc.add_command_callback(device_id="device:003", callback=on_command)
mqttc.subscribe()
mqttc.loop_start()
for i in range(10):
    time.sleep(1)
    # TODO I expect the measurements will be sent without any problem
    mqttc.publish(device_id="device:001", payload={"temperature": 21.0+i})
    mqttc.publish(device_id="device:002", payload={"temperature": 22.0+i})
    # TODO only the following way of calling can work.
    #  But the command name must be specified twice, which is redundant.
    mqttc.publish(device_id="device:003", command_name="heaterPower",
                  payload={"heaterPower": 400+i})
    # TODO I expect the command will be received by the on_command function,
    #  you can recognize it by checking the cmdexe in MQTT explorer
mqttc.loop_stop()