eclipse-ditto / ditto

Eclipse Ditto™: Digital Twin framework of Eclipse IoT - main repository
https://eclipse.dev/ditto/
Eclipse Public License 2.0
695 stars 230 forks source link

What is the Incoming script for sending multiple signal from the device(via mqtt connection) to the different path in the things? #1911

Closed VETRIVEL001 closed 8 months ago

VETRIVEL001 commented 9 months ago

If suppose, this is my thing

{ "definition": "com.acme:coffeebrewer:0.1.0", "attributes": { "manufacturer": "ACME demo corp.", "location": "Berlin, main floor", "serialno": "42", "model": "Speaking coffee machine" }, "features": { "coffee-brewer": { "definition": [ "com.acme:coffeebrewer:0.1.0" ], "properties": { "brewed-coffees": 0 } }, "water-tank": { "properties": { "configuration": { "smartMode": true, "brewingTemp": 87, "tempToHold": 44, "timeoutSeconds": 6000 }, "status": { "waterAmount": 731, "temperature": 44 } } } } }

Now I want to change the value of these three fields 'water-tank/properties/configuration/brewingTemp', 'water-tank/properties/status/temperature' and 'water-tank/properties/status/waterAmount' through the mqtt connections. To create connection what I have to give in incoming script? Can anyone send the incomingScripts for this case?

thfries commented 8 months ago

If your device is able to send the signals in ditto protocol format, you don't need an incoming script at all. You need an incoming mapping script only for specific reasons like:

In that case you need an incoming script to map the device data to ditto protocol messages. But that mapping depends on the input format (topics and payloads sent by the device).

Otherwise, just use a standard MQTT connection type with a source that listens to a simple topic structure:

<environment>/telemetry/<deviceId>/

From the device publish a ditto message to that topic:

{
  "topic": "org.eclipse.ditto/e6904e03-9919-4739-8924-6e902b6d79bf/things/twin/commands/merge",
  "headers": {
    "content-type": "application/merge-patch+json"
  },
  "path": "/features/water-tank/properties",
  "value": {
    "configuration": {
      "brewingTemp": 1234
    },
    "status": {
      "temperature": 95,
      "waterAmount": 3
    }
  }
}
VETRIVEL001 commented 8 months ago

will I have to give this Json while publishing message through mqtt like "mosquitto_pub -h 'test.mosquitto.org' -m ''{ "topic": "org.eclipse.ditto/e6904e03-9919-4739-8924-6e902b6d79bf/things/twin/commands/merge", "headers": { "content-type": "application/merge-patch+json" }, "path": "/features/water-tank/properties", "value": { "configuration": { "brewingTemp": 1234 }, "status": { "temperature": 95, "waterAmount": 3 } } }" if it so, what should i give it in requestBody to create this mqtt connection?

thfries commented 8 months ago

yes,

Here is a working example for a connection (only source is relevant, target is needed to send commands to the device):

{
  "name": "MQTT 3.1.1",
  "connectionType": "mqtt",
  "connectionStatus": "open",
  "uri": "tcp://test.mosquitto.org:1883",
  "sources": [
    {
      "addresses": [
        "eclipse-ditto-sandbox/t/#"
      ],
      "consumerCount": 1,
      "qos": 0,
      "authorizationContext": [
        "nginx:ditto"
      ],
      "headerMapping": {},
      "replyTarget": {
        "address": "{{header:reply-to}}",
        "headerMapping": {},
        "expectedResponseTypes": [
          "response",
          "error"
        ],
        "enabled": true
      }
    }
  ],
  "targets": [
    {
      "address": "eclipse-ditto-sandbox/c/{{ thing:id }}",
      "topics": [
        "_/_/things/twin/events",
        "_/_/things/live/messages"
      ],
      "qos": 0,
      "authorizationContext": [
        "nginx:ditto"
      ],
      "headerMapping": {}
    }
  ],
  "clientCount": 1,
  "failoverEnabled": true,
  "validateCertificates": true,
  "processorPoolSize": 1,
  "tags": []
}
VETRIVEL001 commented 8 months ago

I tried it the way mentioned above that's working fine when I publish single time. whereas, when I publish the message frequently to that topic it's not reflected in ditto thing but I would able to subscribe that message in that topic. what is the cause of the issue?

thfries commented 8 months ago

Hard to tell. If you send the same values over and over again, you may just not see the difference. You need to activate connection logs on your connection and see if there is a failure. In general it may help if you use the Ditto UI to play around and see if there are incoming messages on your thing or something goes wrong on your connection logs.

VETRIVEL001 commented 8 months ago

No, I was sending different values only but it's not reflecting in ditto thing.

thfries commented 8 months ago

Then you need to check the connection logs for failures and their reasons. It could be anything like json payload can not be parsed, thing can not be found, etc.. You need to analyze the logs.

VETRIVEL001 commented 8 months ago

this is my connection { "id": "merge", "name": "MQTT 3.1.1", "connectionType": "mqtt", "connectionStatus": "open", "uri": "tcp://test.mosquitto.org:1883", "sources": [ { "addresses": [ "mqtt-merge" ], "consumerCount": 1, "qos": 2, "authorizationContext": [ "nginx:ditto" ], "headerMapping": {}, "replyTarget": { "address": "{{header:reply-to}}", "headerMapping": {}, "expectedResponseTypes": [ "response", "error" ], "enabled": true } } ], "targets": [ { "address": "mqtt-merge-target", "topics": [ "//things/twin/events", "//things/live/messages" ], "qos": 2, "authorizationContext": [ "nginx:ditto" ], "headerMapping": {} } ], "clientCount": 1, "failoverEnabled": true, "validateCertificates": true, "processorPoolSize": 1, "tags": [] }

this is how i publish message through python code:

import paho.mqtt.publish as publish import time import json m='{"topic":"mqtt-merge","thingId": "abcd:123","headers": {"content-type": "application/merge-patch+json"}, "path": "/features/water-tank/properties","value": {"configuration": {"brewingTemp": 122222},"status": {"temperature": 222,"waterAmount": 222}}}'

publish.single("mqtt-merge", m, hostname="test.mosquitto.org")

my connection logs:

{ "connectionId": "merge", "connectionLogs": [ { "correlationId": "", "timestamp": "2024-03-14T08:40:41.966819342Z", "category": "connection", "type": "other", "level": "success", "message": "Connection successful" }, { "correlationId": "", "timestamp": "2024-03-14T08:42:50.644791359Z", "category": "source", "type": "consumed", "level": "success", "message": "Received signal. - Message headers: [mqtt.qos=0, mqtt.retain=false, mqtt.topic=mqtt-merge] - Message payload: {\"thingId\": \"abcd:123\",\"headers\": {\"content-type\": \"application/merge-patch+json\"}, \"path\": \"/features/water-tank/properties\",\"value\": {\"configuration\": {\"brewingTemp\": 122222},\"status\": {\"temperature\": 222,\"waterAmount\": 222}}}", "address": "mqtt-merge" }, { "correlationId": "", "timestamp": "2024-03-14T08:42:50.645544766Z", "category": "source", "type": "mapped", "level": "failure", "message": "Got exception when processing external message with mapper : JSON did not include required field! - Message headers: [mqtt.qos=0, mqtt.retain=false, mqtt.topic=mqtt-merge] - Message payload: {\"thingId\": \"abcd:123\",\"headers\": {\"content-type\": \"application/merge-patch+json\"}, \"path\": \"/features/water-tank/properties\",\"value\": {\"configuration\": {\"brewingTemp\": 122222},\"status\": {\"temperature\": 222,\"waterAmount\": 222}}}", "address": "mqtt-merge" }, { "correlationId": "57091899-661a-40b3-a5db-5fb691735c50", "timestamp": "2024-03-14T08:42:50.645917472Z", "category": "response", "type": "dispatched", "level": "failure", "message": "Response was not successful. This may be the case for when a thing could not be found or the authorization subject of the consuming source was not allowed to write a thing. - Message headers: [mqtt.qos=0, ditto-origin=merge, response-required=false, correlation-id=57091899-661a-40b3-a5db-5fb691735c50, ditto-auth-context={\"type\":\"pre-authenticated-connection\",\"subjects\":[\"nginx:ditto\"]}, ditto-expected-response-types=[\"response\",\"error\"], ditto-reply-target=0, mqtt.retain=false, mqtt.topic=mqtt-merge] - Message payload: {\"type\":\"things.responses:errorResponse\",\"status\":400,\"thingId\":\"unknown:unknown\",\"payload\":{\"status\":400,\"error\":\"json.field.missing\",\"message\":\"JSON did not include required field!\",\"description\":\"Check if all required JSON fields were set.\"}}", "address": "_responses", "entityType": "thing", "entityId": "unknown:unknown" }, { "correlationId": "", "timestamp": "2024-03-14T08:42:50.646192983Z", "category": "source", "type": "acknowledged", "level": "success", "message": "Sending success acknowledgement. - Message headers: [mqtt.qos=0, mqtt.retain=false, mqtt.topic=mqtt-merge] - Message payload: {\"thingId\": \"abcd:123\",\"headers\": {\"content-type\": \"application/merge-patch+json\"}, \"path\": \"/features/water-tank/properties\",\"value\": {\"configuration\": {\"brewingTemp\": 122222},\"status\": {\"temperature\": 222,\"waterAmount\": 222}}}", "address": "mqtt-merge" }, { "correlationId": "57091899-661a-40b3-a5db-5fb691735c50", "timestamp": "2024-03-14T08:42:50.646515278Z", "category": "response", "type": "mapped", "level": "success", "message": "Mapped outgoing signal with mapper ", "address": "_responses", "entityType": "thing", "entityId": "unknown:unknown" }, { "correlationId": "57091899-661a-40b3-a5db-5fb691735c50", "timestamp": "2024-03-14T08:42:50.647471783Z", "category": "response", "type": "dropped", "level": "success", "message": "Signal dropped, target address unresolved: {{header:reply-to}} - Message headers: [mqtt.qos=0, ditto-origin=merge, response-required=false, correlation-id=57091899-661a-40b3-a5db-5fb691735c50, ditto-auth-context={\"type\":\"pre-authenticated-connection\",\"subjects\":[\"nginx:ditto\"]}, ditto-expected-response-types=[\"response\",\"error\"], ditto-reply-target=0, mqtt.retain=false, mqtt.topic=mqtt-merge] - Message payload: {\"type\":\"things.responses:errorResponse\",\"status\":400,\"thingId\":\"unknown:unknown\",\"payload\":{\"status\":400,\"error\":\"json.field.missing\",\"message\":\"JSON did not include required field!\",\"description\":\"Check if all required JSON fields were set.\"}}", "address": "_responses", "entityType": "thing", "entityId": "unknown:unknown" } ], "enabledSince": "2024-03-14T08:51:12.526891658Z", "enabledUntil": "2024-03-14T09:51:12.526960528Z" }

i don't why this is giving me bad request eventhough mentioned topic and also thingId in mqtt(json).

thfries commented 8 months ago

JSON did not include required field!

Your json must have a topic field instead of a thingId field. The topic includes the thingId