Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Implement WebQuery Command #13209

Closed
wants to merge 1 commit into from

Conversation

JVital2013
Copy link

@JVital2013 JVital2013 commented Sep 24, 2021

Description:

Implements new WebQuery command. The command allows for GET, POST, PUT, and PATCH HTTP Queries, complete with Request Headers and request body (when applicable).

Syntax examples:
WebQuery http://192.168.1.1/path GET: Sends HTTP GET http://192.168.1.1/path
WebQuery http://192.168.1.1/path POST {"some":"message"}: Sends HTTP POST to http://192.168.1.1/path with body {"some":"message"}
WebQuery http://192.168.1.1/path PUT [Autorization: Bearer abcdxyz|Second:Header] potato: Sends HTTP PUT to http://192.168.1.1/path with authorization header, a second header, and body "potato"
WebQuery http://192.168.1.1/path PATCH patchInfo: Sends HTTP PATCH to http://192.168.1.1/path with body "patchInfo"

An unlimited number of headers can be sent per request, and a body can be sent for all command types. The body will be ignored if sending a GET command

This command enables Tasmota to send advanced web queries in addition to GET. It is my vision that this is an open-ended command that can control other SmartHome Devices, such as Philips Hue Hubs, the vacuum cleaner mentioned in #12249, and anything else that can be controlled over HTTP. It is designed to be more flexible than other Tasmota HTTP PRs I've seen

Let me know if you think anything needs changed!

Checklist:

  • The pull request is done against the latest development branch
  • Only relevant files were touched
  • Only one feature/fix was added per PR and the code change compiles without warnings
  • The code change is tested and works with Tasmota core ESP8266 V.2.7.4.9
  • The code change is tested and works with Tasmota core ESP32 V.1.0.7.4
  • I accept the CLA.

NOTE: The code change must pass CI tests. Your PR cannot be merged unless tests pass

@s-hadinger
Copy link
Collaborator

Thanks that's a smart approach. Did you measure the difference in code size?

@arendst
Copy link
Owner

arendst commented Sep 24, 2021

Code size increase is 1k

arendst added a commit that referenced this pull request Sep 24, 2021
Add command ``WebQuery <url> GET|POST|PUT|PATCH [<headers>] <body>`` to extent HTTP requests (#13209)
@arendst
Copy link
Owner

arendst commented Sep 24, 2021

Implemented today.

@arendst arendst closed this Sep 24, 2021
@ygelt
Copy link

ygelt commented Oct 31, 2023

Nice function, I use it extensively :-)
In case the wifi OR the web service is unavaiable, is there a way to detect it ?

@barbudor
Copy link
Contributor

You can use rules to detect wifi connectivity:
on wifi#disconnected do ...
on wifi#connected do ...

By webservice I assume you mean the server you are trying to connect to ?
That should raise an error such as "Connect Failed" instead of "Done"

@ygelt
Copy link

ygelt commented Oct 31, 2023

Many thanks for your fast answer, I will start to try your solution. I'm using WebQuery in scripting but still newbie ! ;-)

My device is looping calls to my web service. To simulate an issue on the web service server side, I remove the web service and watch what happens.

The system seems to bufferize something since I always get {"WebQuery":"Done"} even when the web service is no longer responding.

20:35:42.462 SCR: performs "WebQuery http://xxxx?id=test GET"
20:35:42.662 RSL: RESULT = {"WebQuery":"Done"}

I do not see what's wrong but I get the same behaviour with WebSend.

When tasmota is starting with an inexisting web service, it detects correctly when the web service is running. But then, if the web service drops again, tasmota seems to go on with the last answer received and {"WebQuery":"Done"}.

Any idea of what should be done ?

@sfromis
Copy link
Contributor

sfromis commented Oct 31, 2023

Did a small test with WebQuery, first a working URL (got data), and switching the other device off, I got the expected {"WebQuery":"Connect failed"}. Also if failing DNS lookup. Any caching done would be outside of Tasmota, like in your web service framework.

@ygelt
Copy link

ygelt commented Oct 31, 2023

Thanks for your check.

On my side I checked what happens when calling the web service which is removed, it returns a 404 error and a system fault string.
I did not find how to get the http error code.
In my case the system fault string which is returned is 408 characters long and it is unfortunately too large ? (I can't change it :.( )

(I use an ESP8266-based athom board)

@sfromis
Copy link
Contributor

sfromis commented Oct 31, 2023

Then the connection was ok, hence no reason for getting a "Connect failed". And it does indeed look like code 404 does not give error symptoms, and still no data with a very short 404 response text.

@ygelt
Copy link

ygelt commented Oct 31, 2023

Understood !
So it would be useful to have a system variable with the last HTTP status code to improve web services integration.

My script needs to trigger an audible alarm when the tasmota device does not get a correct web service answers after N bad retries.
In my case (but not only!) there is a "long" body returned on error 404 but unfortunately not standardized, not even always JSON-compliant :-(

@sfromis
Copy link
Contributor

sfromis commented Oct 31, 2023

One workaround could be to yell if you did not get a valid response within a reasonable time frame. Then, you're also avoiding any dependence of what error symptoms (or not) you may get in varied situations.

@ygelt
Copy link

ygelt commented Oct 31, 2023

It would be a good solution if I could distinguish a good answer from a bad answer.
I use a JSON {"alv":1} to say "I'm alive" but unfortunately when the web service "goes out", WebQuery/WebSend always returns the last answer, probably because either the 404 error has no "alv" or is not JSON. So I indefinitely get 1 in WebQuery#alv :.(

@sfromis
Copy link
Contributor

sfromis commented Oct 31, 2023

WebQuery is not returning the last answer in case of errors. Any caching you may experience is outside of what Tasmota controls or can act on.

Inside of what Tasmota gets, it can work great if you do not worry about trying to detect badness, but only detect good answers. Then you can yell if last good answer was too long ago.

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

Successfully merging this pull request may close these issues.

6 participants