HTTP binding - openHAB 3 version

Sure. Here is the exception:

2020-07-09 21:40:09.798 [WARN ] [mmon.WrappedScheduledExecutorService] - Scheduled runnable ended with an exception: 

java.lang.IllegalArgumentException: Illegal character in scheme name at index 0: "https://api.github.com/repos/arendst/Tasmota/tags"

	at java.net.URI.create(URI.java:852) ~[?:1.8.0_222]

	at org.eclipse.jetty.client.HttpClient.newRequest(HttpClient.java:442) ~[?:?]

	at org.openhab.binding.http.internal.http.RefreshingUrlCache.refresh(RefreshingUrlCache.java:79) ~[?:?]

	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[?:1.8.0_222]

	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) ~[?:1.8.0_222]

	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) ~[?:1.8.0_222]

	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) ~[?:1.8.0_222]

	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_222]

	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_222]

	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]

Caused by: java.net.URISyntaxException: Illegal character in scheme name at index 0: "https://api.github.com/repos/arendst/Tasmota/tags"

	at java.net.URI$Parser.fail(URI.java:2848) ~[?:1.8.0_222]

	at java.net.URI$Parser.checkChars(URI.java:3021) ~[?:1.8.0_222]

	at java.net.URI$Parser.checkChar(URI.java:3031) ~[?:1.8.0_222]

	at java.net.URI$Parser.parse(URI.java:3047) ~[?:1.8.0_222]

	at java.net.URI.<init>(URI.java:588) ~[?:1.8.0_222]

	at java.net.URI.create(URI.java:850) ~[?:1.8.0_222]

	... 9 more

Ps.: My bad, I had a look at the exception again and the problem was in the base url, I forgot to delete the quotation marks, it works now!

1 Like

Would you post your example (in case you use text file definition)?

INFO level logging ?
Ideally on state changes only (server gone between last and current request + when server came back)

I’m using PaperUI. Anyway the config is really straightforward.
Define the BaseURL at thing level and add state transform for a channel as you would do in MQTT2 binding.
So state transform should look like this (assuming a json input):

JSONPATH:$[0].name

Thanks for bringing that up!
Yes it was really annoying and it not even showed these errors between the specified poll intervals but every few seconds…
@J-N-K
I think an UNDEF state for channels on a Thing is great for showing unreachable hosts. Maybe also use ThingStatus to represent availability on a Thing level?

For anyone who wants to start off using/migrating textual config, here is a very simple example that works for me:


Thing http:url:exchangerate "USD-EUR Exchange Rate" [ baseURL="https://api.exchangeratesapi.io/latest?symbols=USD", refresh=3600] {
    Channels:
        Type number : USDEUR "Exchange Rate" [ stateTransformation="JSONPATH:$.rates.USD" ]
}

Item:
Number ExchangeRateEURUSD  "EUR - USD [%.3f]"                  <exchangerate>          (gAll)  {channel="http:url:exchangerate:USDEUR"}
2 Likes

There usually ends up being use cases for both ways.

I would suggest a user option , perhaps per data channel
postUndefOnError=true
or something like, defaulting false.

An alternative might be dateTime channel(s) for
lastReadSuccess
lastReadError
which allows users to trap and manage errors for themselves in rules.

The config per channel might be an option. lastReadSuccess is difficult, because it could be that reading one channel is successfull (e.g. with one stateExtension) while another channel (with another stateExtension) always fails. The lastReadSuccess would not help here.

Really I was thinking this would be a built-in channel exposing something about the Thing. It’s the Thing doing the HTTP work from the user viewpoint.

I see this binding does both read and write - so I suppose a better name might be more like
lastCommSuccess
lastCommError
or something if it covers both uses.

Maybe it is less useful these days where Thing status is more accessible from rules, if this Thing reflects its comms problems in its own status…

Hello @J-N-K

thank for creating this new OH2 binding, great job! :slight_smile:

Finally had time to test it.

I have one feature request:

In http1 binding you had the possibility to use up to 3 different urls.

In the new binding there are unfortunately only two:

Simple example: Switch

baseURL + stateExtension
baseURL + commandExtension

.

But there are the following cases possible and in http1 and you were able to do so:

URL1 = ON
URL2 = OFF
URL3 = state

e.g:

ON = 192.168.100.44:3000/on/3ef204
OFF = 192.168.100.44:3000/off/3ef204
state = 192.168.100.44:3000/state/3ef204
.

Example .items file:

Switch GHoma1	"G-Homa Socket 1"	{ http=">[ON:GET:http://192.168.100.44:3000/on/3ef204] >[OFF:GET:http://192.168.100.44:3000/off/3ef204] <[http://192.168.100.44:3000/state/3ef204:60000:MAP(onoff.map)]" } 

.
.

This would be in http2 binding:

baseURL = 192.168.100.44:3000
stateExtension = /state/3ef204
commandExtension = /on/3ef204 OR /off/3ef204
.

But I need one commandExtension for ON and another commandExtention for OFF.

Or can I use commandTransformation? How do I have to use it?
.
.

Second question:

How do I mix GET and POST commands?

e.g:

stateExtension = GET
commandExtension = POST or PUT

Thank you very much. :slight_smile:

I think that /%2$s/3ef204 as commanfExtension should work. You need to set onValue to on and offValue to off.

3 Likes

This is now working for commandExtension, thanks! :slight_smile:

But the state is “on” or “off” with the quotation marks! That’s why I used MAP (see example above).

How to set the State Transformation?

image

.
.
.
EDIT: I now found out on my own! :slight_smile:

State Transformation: MAP:onoff.map

and the onoff.map must contain the following:

"on"=on
"off"=off

Now it works very well! Many thanks to @J-N-K !

.
.
P.S: A further running example of a State Transformation is e.g: JSONPATH:$.value

2 Likes

I tried another tricky transformation example that didn’t work. Here’s the v1 item definition:
http="<[http://foscam:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=admin&pwd=password:10000:REGEX(.?(.?).*)]" }

That request returns a response like

<CGI_Result>
    <result>0</result>
    <IOAlarm>0</IOAlarm>
    <motionDetectAlarm>1</motionDetectAlarm>
    <soundAlarm>1</soundAlarm>
    <record>0</record>
    <sdState>0</sdState>
    <sdFreeSpace>0k</sdFreeSpace>
    <sdTotalSpace>0k</sdTotalSpace>
    <ntpState>1</ntpState>
    <ddnsState>0</ddnsState>
    <upnpState>2</upnpState>
    <isWifiConnected>1</isWifiConnected>
    <infraLedState>0</infraLedState>
</CGI_Result>

But no matter what I try all I always get is a server response </CGI_Result>-2</CGI_Result>

The debug output is mostly useless as it does not show the URL or headers going out, not even info like SSL or user auth, or responses coming back.

All debug level log shows is

2020-07-10 17:09:30.119 [DEBUG] [.transform.SingleValueTransformation] - Transformation ChannelStateTransformation{pattern='.*?<motionDetectAlarm>(.*?)</motionDetectAlarm>.*', serviceName='REGEX'} returned empty result when applied to <CGI_Result>
    <result>-2</result>
</CGI_Result>
.

I was able to confirm this is what the server returns using wireshark, but for sure I need to be able to see that in the OH log to be able to make good use of it.

@mstormi

If I follow the logic of MAP and JSONPATH it should be as follows:

Binding MAP JSONPATH REGEX
http1 MAP(onoff.map) JSONPATH($.value) REGEX(. ?(. ?).*)
http2 MAP:onoff.map JSONPATH:$.value REGEX:. ?(. ?).*

.
Did you also try REGEX:. ?(. ?).*

I have updated the link above. If you set org.openhab.binding.http.internal.http to TRACE you’ll see the URL when requesting and the responses received.

Dowloaded it but did that change anything about http logging ?
That’s not really useful yet. Would need to see Headers, body and reponse code of the request

2020-07-10 20:24:04.590 [TRACE] [ttp.internal.http.RefreshingUrlCache] - Sending 'http://foscam:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState' to 'HttpRequest[GET /cgi-bin/CGIProxy.fcgi HTTP/1.1]@13442bb'
2020-07-10 20:24:04.639 [TRACE] [ttp.internal.http.RefreshingUrlCache] - RefreshingUrlCache for 'http://foscam:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState' received 'org.openhab.binding.http.internal.http.Content@66d12b'

I‘ll see how I can improve that.

Please check if the updated jar works for you.

It does, thanks.
It helped me find my own error now that I see the 200 response.
Turned out it was the request I didn’t copy properly. That device needs to have http://host/yaddayadda&usr=username&pwd=password rather than basic auth.

What’s left is that apparently I must be having another HTTP thing active (resulting from first tests yesterday ?) as I keep seeing both URLs below (the difference is the additional ‘/’). I don’t find that 2nd one in the list of smarthome:things though - is it something your binding does ? (although I don’t think so as it has a different request interval).

But that reminds me of this: if I’m not mistaken, you add State and Command URL Extension to Base URL but insert a fixed ‘/’, correct?
Then please remove that fixed ‘/’ insertion as the requirement could as well be to continue the URL with ? or & like in my test case.

2020-07-11 17:29:48.576 [TRACE] [ttp.internal.http.RefreshingUrlCache] - Requesting refresh from 'http://foscam:88/cgi-bin/CGIProxy.fcgi?/cmd=getDevState' with timeout 3000ms
2020-07-11 17:29:48.579 [TRACE] [ttp.internal.http.RefreshingUrlCache] - Sending 'http://foscam:88/cgi-bin/CGIProxy.fcgi?/cmd=getDevState' to 'HttpRequest[GET /cgi-bin/CGIProxy.fcgi HTTP/1.1]@5f3465'
2020-07-11 17:29:50.176 [TRACE] [ttp.internal.http.RefreshingUrlCache] - Requesting refresh from 'http://foscam:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=admin&pwd=password' with timeout 1000ms
2020-07-11 17:29:50.178 [TRACE] [ttp.internal.http.RefreshingUrlCache] - Sending to 'http://foscam:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=admin&pwd=password': Method = {GET}, Headers = {Accept-Encoding: gzip, User-Agent: Jetty/9.4.20.v20190813, Authorization: Basic YWRtaW46Y1ROVzBUcGU=}, Content = {null}
2020-07-11 17:29:50.418 [TRACE] [p.internal.http.HttpResponseListener] - Received from 'http://foscam:88/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=admin&pwd=password': Code = {200}, Headers = {Content-Type: text/plain, Transfer-Encoding: chunked, Date: Sat, 11 Jul 2020 15:29:47 GMT, Server: lighttpd/1.4.35}, Content = {<CGI_Result>
    <result>0</result>
    <IOAlarm>0</IOAlarm>
    <motionDetectAlarm>1</motionDetectAlarm>
    <soundAlarm>1</soundAlarm>
    <record>0</record>
    <sdState>0</sdState>
    <sdFreeSpace>0k</sdFreeSpace>
    <sdTotalSpace>0k</sdTotalSpace>
    <ntpState>1</ntpState>
    <ddnsState>0</ddnsState>
    <upnpState>2</upnpState>
    <isWifiConnected>1</isWifiConnected>
    <infraLedState>0</infraLedState>
</CGI_Result>
}

1 Like

@J-N-K

Same here. I also need no fixed slash / at the end of baseURL or beginning of Extension. Many extensions will begin with ? or &.