thin-edge.io
thin-edge.io copied to clipboard
Custom operations do not accept arbitrary topics
Describe the bug
The topic
key does nothing in a custom operation file.
To Reproduce
- Add a custom SmartRest template (e.g.
myTemplate
). - Subscribe to it
sudo tedge config set c8y.smartrest.templates myTemplate
- Add the custom operation to
/etc/tedge/operations/c8y/myOperation
:
[exec]
topic = "c8y/s/dc/myTemplate"
on_message = "cds502"
command = "/etc/tedge/operations/myOperation"
- Fire the operation
- Nothing happens...
Expected behavior
/etc/tedge/operations/myOperation
should be executed by the mapper
, but doesn't.
Environment (please complete the following information):
- RaspberryPI
- thin-edge.io version 0.7.3
Could you post the corresponding SmartRest Template myTemplate from your tenant as well?
Did you reconnected via tedge disconnect/connect c8y after setting myTemplate via tedge cli?
Could you post the log output of tedge-agent.log in /var/log?
Yes, I did the disconnect/connect.
I also see the incomming operation (mosquitto_sub -t '#' -v
), but my executeable /etc/tedge/operations/myOperation
is not called.
I don't have the setup running, but as far as I remember the log was empty.
@albinsuresh : has already confirmed in Discord, that it won't work with custom templates.
On his advise, I created this issue.
Could you Post the Template anyway? ;-)
{
"name": "myTemplate",
"type": "c8y_SmartRest2Template",
"com_cumulocity_model_smartrest_csv_CsvSmartRestTemplate": {
"requestTemplates": [],
"responseTemplates": [
{
"msgId": "cds502",
"condition": "myOperation",
"base": "",
"name": "My Operation",
"pattern": [
"myOperation"
]
}
]
},
"__externalId": "myTemplate"
}
Investigation points
- Reproduce the issue
- Confirm if the c8y-mapper is subscribing to the custom template topics
- Validate if the custom operation file is accepted by the mapper as a valid operation
- Validate if the message prefixes are "accidentally" ignored
I found the problem.
Currently, the mapper expects that the message response ID is EXACT 3 digits. In your case, if cds502
were cds
, it would have worked. However, of course it is a workaround and not designed behaviour.
The problematic code is here.
let message_id: &str = &payload[..3];
match message_id {
"528" => forward_software_request(payload, http_proxy).await,
"510" => forward_restart_request(payload),
template => forward_operation_request(payload, template, operations, operation_logs).await,
}
You can see the message_id
takes just the first 3 letters of the payload. We need to fix this part.
How to test this feature
Step 1: Install thin edge from the below link https://github.com/thin-edge/thin-edge.io/actions/runs/3112206083
Step 2: Create the custom template
Create a custom operation file /etc/tedge/operations/c8y/c8y_Temp
, Add the below content
[exec]
topic = "c8y/s/dc/myTemp"
on_message = "cds502"
command = "/usr/bin/custom.sh"
Step 3: Create a custom command as custom.sh
#!/bin/bash
/usr/bin/mosquitto_pub -t hello -m 'custom template'
Move it to /usr/bin
if its created someother place, give the execute permission by all using `sudo chmod a+x custom.sh'
Step 4: Update the tedge config to use the new custom template
sudo tedge config set c8y.smartrest.templates myTemp
Step 5:
sudo tedge connect c8y
Step 6:
Start a mqtt subscriber as below
sudo tedge mqtt sub '#'
Step 7:
Publish the custom operation as below
sudo tedge mqtt pub c8y/s/dc/myTemp 'cds502, **Your_device_id**'
Use your device id
here
Step 8: Verification
The mqtt subscriber that started in step 5, should receive the below message
[c8y/s/dc/myTemp] cds502, Your device id
[hello] custom template
Resolved with #1450
Tested and passed the test
@toewsar The fix was already merged to the main branch. It will be part of the release 0.7.6.
Great.