pdmangel / node-red-contrib-openhab2

Other
21 stars 14 forks source link

kind request for openhab 3.0 adaptation #53

Closed serg013 closed 3 years ago

serg013 commented 4 years ago

Dear Developers Group.

Thank you so much for such interface for openhab 2. I really enjoy it and just trying to learn node red and it's features. I just update to openhab 3.0.0 snapshot. I understand that it's beta version and it could be errors. It looks pretty stable since my last try in about one month. I finally found out bindings and 3.0.0 eats previous config from 2.9 and config files. When I try to deal with node-red-contrib-openhab2 I got error below. Nothing changed in nodes - only update from 2.9 to 3.0.0. I will understand if it is openhab error or I just rush for something new and have to wait for a release, but if it would be list efforts to fix it could you please help?

response error '{"statusCode":415,"body":"","headers":{"connection":"close","date":"Wed, 23 Sep 2020 19:13:58 GMT","content-length":"0","server":"Jetty(9.4.20.v20190813)"},"request":{"uri":{"protocol":"http:","slashes":true,"auth":null,"host":"myhost:8080","port":"8080","hostname":"myhost","hash":null,"search":null,"query":null,"pathname":"/rest/items/cor_lt_st","path":"/rest/items/cor_lt_st","href":"http://myhost:8080/rest/items/cor_lt_st"},"method":"POST","headers":{"content-length":2}}}' on 'http://myhost:8080/rest/items/cor_lt_st'

serg013 commented 4 years ago

It looks like "in" node able to read states and automation is working but I'm unable to choose or change items in node configuration.

gunther1977 commented 3 years ago

replacing "smarthome" by "openhab" for the 3 instances in the code makes the nodes work for openhab3

node.es= new EventSource(getConnectionString(config) + "/rest/events?topics=smarthome/items", {}); has to become node.es= new EventSource(getConnectionString(config) + "/rest/events?topics=openhab/items", {});

gunther1977 commented 3 years ago

correction makes the in-node work again. I've tested the outnode but this fails. In both cases basic authorisation was used

gunther1977 commented 3 years ago

I was able to update the item state with a httprequest node. This can only be done when the following header entries are made via a function node:

msg.payload = "0"; msg.headers = {}; msg.headers['accept'] = '/'; msg.headers['Content-Type'] = 'text/plain'; return msg;

hope this helps

gunther1977 commented 3 years ago

accept header not just / but as indicated below Knipsel

gunther1977 commented 3 years ago

all above works also without authentication

bdormael commented 3 years ago

replacing "smarthome" by "openhab" for the 3 instances in the code makes the nodes work for openhab3

node.es= new EventSource(getConnectionString(config) + "/rest/events?topics=smarthome/items", {}); has to become node.es= new EventSource(getConnectionString(config) + "/rest/events?topics=openhab/items", {});

This works when I do the changes above but still updating devices do not work with this trick.

Any updates on making this node compatible with OH3 or have a spinoff so we can keep OH2 compatibility as well ?

gunther1977 commented 3 years ago

the code below also make the updates work for openhab3

`

    //startEventSource();
    // give the system few seconds
    setTimeout(function() {
        startEventSource();
    }, 5000);

    this.control = function(itemname, topic, payload, okCb, errCb) {
        var url;

        if ( topic === "ItemUpdate" )
        {
            url = getConnectionString(config) + "/rest/items/" + itemname + "/state";
            headers = {"Content-type":"text/plain"};
    method = request.put;
        }
        else if ( topic === "ItemCommand" )
        {
            url = getConnectionString(config) + "/rest/items/" + itemname;
            headers = {"Content-type":"text/plain"};
            method = request.post;
        }
        else
        {
            url = getConnectionString(config) + "/rest/items/" + itemname;
            method = request.get;
        }

        method({url: url, body: String(payload), headers: headers}, function(error, response, body) {
            if ( error )
            {
                node.emit('CommunicationError', error);
                errCb("request error '" + error + "' on '" + url + "'");
            }
            else if ( Math.floor(response.statusCode / 100) != 2 )
            {
                node.emit('CommunicationError', response);
                errCb("response error '" + JSON.stringify(response) + "' on '" + url + "'");
            }
            else
            {
                okCb(body);
            }
        });

`

Notice the added headers

bdormael commented 3 years ago

working perfect, thx

pdmangel commented 3 years ago

Thanks to you all for contributing to a solution for openhab 3. I made the proposed changes and tested with openhab 2. This seems OK. I published this as v1.1.8 and should be available soon for npm install/update. Next I will upgrade to openhab 3 and test again.

pdmangel commented 3 years ago

Openhab 3 testing succesful!

gunther1977 commented 3 years ago

Super many thanks