Skip to content

Add support to publish data via different query #93

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

NiklasCi
Copy link
Contributor

@NiklasCi NiklasCi commented Feb 12, 2024

This PR adds support to publish data via the datasource by sending a different payload.
This can be used by plugins like Buttons Panel or Data Manipulation.

Publish timeouts after 1s and subscribe timeouts 1s after publish was successful.

Schema of query:

interface Query {
  topic string // topic to publish payload to
  payload object // a js/json object that contains the publish message
  response string // (Optional): a topic to listen for a response
}

Example of a query:

query: {
  topic: "cmd/send/values",
  payload: {
    value1: "Hallo",
    value2: 12.34
  },
  response: "response/from/send"
}

The returned response from the query contains the reponse of the mqtt command under a new frame with name Response and a field Body where the field values contain the responded message.
Note: I only tested json response messages.

@NiklasCi NiklasCi marked this pull request as draft February 12, 2024 10:03
@NiklasCi NiklasCi marked this pull request as ready for review February 12, 2024 10:40
@DonatoD
Copy link

DonatoD commented Feb 12, 2024

can it work with plugin like htmlgraphic https://grafana.com/grafana/plugins/gapit-htmlgraphics-panel/

@NiklasCi
Copy link
Contributor Author

NiklasCi commented Feb 12, 2024

https://grafana.com/grafana/plugins/gapit-htmlgraphics-panel/

If this plugin supports getting the backend server and the access to the grafana api sure.
You can send a query via grafana datasource api: https://grafana.com/docs/grafana/latest/developers/http_api/data_source/#query-a-data-source

Should look like this:

POST /api/ds/query HTTP/1.1
Accept: application/json
Content-Type: application/json

{
   "queries":[
      {
         "refId":"1",
         "datasource":{
            "uid":"MQTT-UID" // need to be known
         },
         "topic": "cmd/send/values",
         "payload": {
             "value1": "Hallo",
             "value2": 12.34
         },
         "response": "response/from/send"
      }
   ],
}

@DonatoD
Copy link

DonatoD commented Feb 12, 2024

Thanks. I'm not that expert on this stuff, so I need to study all this more in details. But the html graphic panel allow you to write in javascript, so I think it should works, shouldn't it?

@NiklasCi
Copy link
Contributor Author

Thanks. I'm not that expert on this stuff, so I need to study all this more in details. But the html graphic panel allow you to write in javascript, so I think it should works, shouldn't it?

Yes as long as you get the backend server. There are helper functions under @grafana/runtime.
For example getDataSourceSrv() or getbackendSrv(). Maybe cehckout iof you can access them in the html panel.

If so you can publish mqtt data.

@DonatoD
Copy link

DonatoD commented Feb 12, 2024

Thanks so much. I'll give it a look

@excalq
Copy link

excalq commented Apr 27, 2025

Would love to make use of this. Is there a possibility of this being merged?

@NiklasCi
Copy link
Contributor Author

NiklasCi commented May 5, 2025

@toddtreece Sorry to ping you. I want to know what you thing about the idea to make publishing via this datasource possible? I saw that with Grafana 11.6 there are now actions that could make use of it. We using this functionality now over a year without any problems.

Following things i would change in the upcoming days/weeks before this could be reviewed/merged:

  • Resolve conflicts and make use of new go functionality like errgroup if possible,
  • Make this an opt-in functionality, so that it does not break anything by default.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants