openhab-docs icon indicating copy to clipboard operation
openhab-docs copied to clipboard

REST API SSE / visibility changes in sitemap

Open johnjore opened this issue 5 years ago • 1 comments

Current REST API documentation, https://www.openhab.org/docs/configuration/restdocs.html, is rather thin when documenting /rest/events, and how to detect visibility changes in sitemaps (not items), is missing.

Here is an extract from a sitemap:

Switch item=SCP3 label=“Rain Daily Accumulated” mappings=[1=“Hour”, 2=“Day”, 3=“Week” ]
Chart item=Sensor_Garden_Rain_Daily period=h refresh=10000 service=“rrd4j” visibility=[SCP3==1,SCP3==“Uninitialized”]
Chart item=Sensor_Garden_Rain_Daily period=D refresh=10000 service=“rrd4j” visibility=[SCP3==2]
Chart item=Sensor_Garden_Rain_Daily period=W refresh=10000 service=“rrd4j” visibility=[SCP3==3]

While it's easy to track when SCP3 changes using /rest/events :

event: message
data: {“topic”:“smarthome/items/SCP3/state”,“payload”:"{“type”:“Decimal”,“value”:“4”}",“type”:“ItemStateEvent”}

The documentation for seeing when visibility changes in sitemaps is missing. None of the responses here provide any usable technical information: https://community.openhab.org/t/sse-not-seeing-visibility-changes-in-sitemap/97800

I am guessing it has something to do with "rest/sitemaps/events/subscribe", but I can't figure out how to use this: ` curl -X GET --header "Accept: text/event-stream" "http://servername:8080/rest/sitemaps/events/cb61105c-6dc6-49c8-ab5f-2fbce226cdf2"

{"error": {"message": "Subscription id cb61105c-6dc6-49c8-ab5f-2fbce226cdf2 is not yet linked to a sitemap/page.", "http-code": 400 }}

?!?

Would it be possible for someone to provide this information, or point me to the relevant documentation?

Edit: Came across this: https://github.com/eclipse/smarthome/pull/2030. Some testing has shown this to work. Only took me 3 days.... best if documentation could be updated as subscribing to sitemap changes is different from subscribing to item changes.

johnjore avatar May 02 '20 08:05 johnjore

Suggested text:

Unlike subscribing to item changes, subscribing to changes in sitemaps (i.e. visibility), a 4 step process is needed:

  1. Get the WidgetId (This is sometimes referred to as the PageId in some legacy documentation) from the sitemap:
{
  "name": "test1",
  "label": "Test1",
  "link": "http://openhabserver.local:8080/rest/sitemaps/test1",
  "homepage": {
    "id": "test1",
    "title": "Test1",
    "link": "http://openhabserver.local:8080/rest/sitemaps/test1/test1",
    "leaf": false,
    "timeout": false,
    "widgets": [
      {
        **"widgetId": "00",**
        "type": "Text",
        "visibility": true,
        "label": "Graph1",
        "icon": "text",
        "mappings": [],
        "linkedPage": {
          "id": "00",
          "title": "Graph1",
          "icon": "text",
          "link": "http://openhabserver.local/rest/sitemaps/test1/00",
          "leaf": true,
          "timeout": false,
          "widgets": [
            {
              "widgetId": "0000",
              "type": "Switch",
              "visibility": true,
              "label": "Rain (last 1 min)",
              "icon": "number",
              "mappings": [
                {
                  "command": "1",
                  "label": "Hour"
                },
                {
                  "command": "2",
                  "label": "Day"
                },
               ...

The WidgetId here is "00"

  1. Create a SubscriptionId

curl -X POST --header "Content-Type: application/json" http://{openHAB_IP}:8080/rest/sitemaps/events/subscribe

The last field of the Location is the SubscriptionId: I.e. 095d539a-c7f6-4b31-8c8e-ff9e1e44719e

  1. Subscribe to changes in the WidgetId of the Sitemap:

curl -X GET --header "Content-Type: application/json" "http://{openHAB_IP}:8080/rest/sitemaps/{Sitemap}/{WidgetID}?subscriptionId={SubscriptionId}"

  1. Download (forever)
    Subscribe to http://openhablocal.server:8080/rest/sitemaps/events/{SubscriptionId}

PowerShell:

$WidgetId = "00"
$BaseURL = "http://openhabserver.local:8080/rest/sitemaps"

[String]$Result= (Invoke-RestMethod -Headers @{"Content-Type" = "application/json"} -Method "Post"  -Uri "$BaseURL/events/subscribe").context.headers.Location
$SubscriptionID = $result.split("/")[-1]
$SubscriptionID
$Void = Invoke-RestMethod -Headers @{"Content-Type" = "application/json"} -Method "Get" -Uri $("$BaseURL/test1/$PageID" +"?subscriptionid=$subscriptionId&includeHidden=true")

Invoke-RestMethod -Headers @{"Content-Type" = "application/json"} -Method "Get" -Uri "$BaseURL/events/$subscriptionId"

johnjore avatar May 02 '20 12:05 johnjore