Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke direct Method and configure OPC Publisher through MQTT topic #2039

Closed
SaifAlmaliki opened this issue Aug 28, 2023 · 7 comments
Closed
Assignees
Labels
bug Something isn't working
Milestone

Comments

@SaifAlmaliki
Copy link

I am publishing my telemetry successsfully from opcpublisher to MQTT broker using --ttt (telemetrytopictemplate).

Since MQTT v5 supports RPC which allows a client to call the OPC Publisher over MQTT. I want to Invoke and configure OPC Publisher through MQTT topic.

As mentioned in the doc here: In addition to calling the API through HTTP REST calls (Preview) you can call the configuration API through MQTT v5 (Preview) in OPC Publisher 2.9.

Following commandline.md , I configured my opcpublisher by adding --mtt(method topic tempelate) flag and append the method name after the topic template: xx/xxxx/methods/GetConfiguredEndpoints_V2.

However I wasn't able to invoke/Configure the opcpublisher

I need a demo or simple example showing end-to-end how to invoke and configure opcpublisher module using mqtt topic

thanks

@marcschier
Copy link
Collaborator

This is correct. Can you provide me with the --mtt value you used so I can repro this? Did you make sure you provide a response topic in the MQTT v5 message sent to the topic?

Samples are on the list of most requested feature. I hope I can get around to it after 2.9.1 is out.

@marcschier marcschier self-assigned this Aug 28, 2023
@marcschier marcschier added this to the 2.9.2 milestone Aug 28, 2023
@marcschier marcschier added need more information Needs more information documentation Issue in the documentation labels Aug 28, 2023
@SaifAlmaliki
Copy link
Author

Thanks marcshier for your response.
In my case --mtt =“fi/publisher/methods/GetConfiguredEndpoints_V2”
But not sure how to provide the response topic in the MQTT message, I assumed the response will be on the same above topic. is in it?

@marcschier
Copy link
Collaborator

The mtt option allows setting the root path for all methods. Using a templating model. When you use it though the actual topic to call for a method is the configured path, plus the method name. So if you use --mtt x/y/z to call the method a, you send the payload to /x/y/z/a. If using mqtt 5 the payload needs to also specify the response topic. That can be any tpoc you have already subscribed to on the broker. Although I recommend to use a unique one per call. Your client library will allow you to specify that topic during publish, typically on the message object you publish. In mqttnet you do this as a property of the applocationmessage object you pass to Publish method.

@SaifAlmaliki
Copy link
Author

SaifAlmaliki commented Aug 30, 2023

Thanks marcschier, I'm sharing my snippet for better understanding.

Iam running the sample project from Industrial-IoT Repo and play with it to fits my needs.
The telemetry flow to the mqtt broker works perfectly but I am struggling with the DirectMethod

Below snippet of my opcpublisher with --mtt=fi/publisher/methods representing the method topic template

image

I simple tried to invoke GetConfiguredEndpoints_V1 here

below my simple python mqttclient using paho with that publish payload {} to the topic fi/publisher/methods/GetConfiguredEndpoints_V1 and and I ve added the response topic as property to the public message and subscribed to the response topic so I should expect to receive the list of configured endpoints.

import paho.mqtt.client as mqtt
import paho.mqtt.properties as props
from paho.mqtt.packettypes import PacketTypes
import time

# MQTT broker settings
broker_address = "localhost"
broker_port = 1883
client_id = "mypython_client"
request_topic = "fi/publisher/methods/GetConfiguredEndpoints_V1"
response_topic = "fi/publisher/methods/response"

# Callback function for when a connection is established with the MQTT broker
def on_connect(client, userdata, flags, rc, properties=None):
    print("Connected to MQTT broker with result code: " + str(rc))
    if rc == 0:
        print("Connected to MQTT Broker")
        client.subscribe(response_topic)  # Subscribe to the response topic

    else:
        print("Connection failed")

# Callback when a message is received
def on_message(client, userdata, message):
    print(f"Received response: '{message.payload.decode()}' on topic '{message.topic}'")


# Create a MQTT client instance
client = mqtt.Client()

# Set the callback
client.on_connect = on_connect

# Connect to the broker
client.connect(broker_address, broker_port)

# Start the loop to handle incoming messages
client.loop_start()

try:
    while True:
        payload = "{}"

        # Create properties for the PUBLISH message
        properties= mqtt.Properties(PacketTypes.PUBLISH)
        properties.ResponseTopic = response_topic
        client.publish(request_topic, payload, qos=1, properties=properties)
        print(f"Published message to '{request_topic}' with response topic '{response_topic}'")
        time.sleep(5)

except KeyboardInterrupt:
    print("Disconnecting...")
    client.disconnect()
    client.loop_stop()

But the terminal not showing any response on the response topic
image

Also I implemented the same mqtt client using dotnet mqttnet library but still getting the same results.
Not sure what I part I missing so far
Many thanks

@marcschier marcschier added bug Something isn't working and removed need more information Needs more information documentation Issue in the documentation labels Aug 31, 2023
@marcschier marcschier modified the milestones: 2.9.2, 2.9.1 Aug 31, 2023
@marcschier
Copy link
Collaborator

I have tried the same and found a hole in our testing. I also wrote a quick sample using mqttnet while at it.

@marcschier
Copy link
Collaborator

Samples showing several calls via mqtt are now in the samples folder (in main). They only work with 2.9.1 which will be released soon.

@SaifAlmaliki
Copy link
Author

Thanks marcshier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants