thin-edge.io icon indicating copy to clipboard operation
thin-edge.io copied to clipboard

Custom operations do not accept arbitrary topics

Open toewsar opened this issue 1 year ago • 6 comments

Describe the bug The topic key does nothing in a custom operation file.

To Reproduce

  1. Add a custom SmartRest template (e.g. myTemplate).
  2. Subscribe to it sudo tedge config set c8y.smartrest.templates myTemplate
  3. Add the custom operation to /etc/tedge/operations/c8y/myOperation:
[exec]
  topic = "c8y/s/dc/myTemplate"
  on_message = "cds502"
  command = "/etc/tedge/operations/myOperation"
  1. Fire the operation
  2. 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

toewsar avatar Aug 22 '22 14:08 toewsar

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?

mbay-ODW avatar Aug 24 '22 08:08 mbay-ODW

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.

toewsar avatar Aug 24 '22 09:08 toewsar

Could you Post the Template anyway? ;-)

mbay-ODW avatar Aug 24 '22 11:08 mbay-ODW

{
  "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"
}

toewsar avatar Aug 24 '22 13:08 toewsar

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

albinsuresh avatar Aug 30 '22 10:08 albinsuresh

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.

rina23q avatar Sep 06 '22 21:09 rina23q

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

PradeepKiruvale avatar Oct 06 '22 13:10 PradeepKiruvale

Resolved with #1450

PradeepKiruvale avatar Oct 06 '22 13:10 PradeepKiruvale

Tested and passed the test

gligorisaev avatar Oct 07 '22 06:10 gligorisaev

@toewsar The fix was already merged to the main branch. It will be part of the release 0.7.6.

rina23q avatar Oct 10 '22 11:10 rina23q

Great.

toewsar avatar Oct 17 '22 05:10 toewsar