metbosch / homebridge-http-temperature

HomeBridge HomeKit Plugin for HTTP temperature endpoints
https://www.npmjs.com/package/homebridge-http-temperature
Apache License 2.0
33 stars 18 forks source link

Spacing out multiple instances with calls to the same server or allowing multiple json decodes and appliances per call #25

Closed etomnash closed 3 years ago

etomnash commented 3 years ago

This is an excellent plugin. So I use it for multiple instances of calls to each of two servers using the json decode capability to fetch different data. One wants to space out the calls to each server to avoid overloading it. For example, the Davis WeatherLink tolerates one call each 10 seconds for the local net api. I make several calls to get temp at different locations and hum and rain and wind and ... (I read wind and rain into HomeKit as temperature as there is no other option for such data and that works.) I try to space out the calls with varying update_interval values, but that is not defined in this situation and not ideal since key data gets missed. Would there be a way for this plugin to apply an update_interval to a sequence of instances calling one server?

I could imagine some approach involving the platform capability of homebridge that would allow one call to allow multiple json decoded values to go to different HomeKit appliances at a single update_interval. I don't think I have the knowledge/ability to offer to code that.

metbosch commented 3 years ago

I neither have the knowledge to implement this behavior in the plugin (if it is possible). The solution that comes to my mind is having some kind of http cache. Then you could request the data for all sensors to the cache, and it will periodically retrieve the full set of data but only with 1 request.

A silly way to have it running through a wget and a minimal python http server. The cached data is available at http://localhost:8000/data.json:

pushd `mktemp -d`
watch -n 300 "wget -o wget.log http://192.168.0.0/data.json" &
python -m SimpleHTTPServer
popd
etomnash commented 3 years ago

Thanks for response. I had come to the conclusion that I could resolve the problem with some caching in the php files I call through the Apache server on a raspberry pi for some cases where I calculate on the results. I was caching the Davis json in a local text file and using that if the latest get failed with a FALSE response.

After your message I realized I could do something similar for all my calls to the Davis box. Your json decode capability makes this possible and straightforward. So I keep track of the time when the cached json was retrieved (included in the Davis json) and if more than 11 seconds have gone by, I try to retrieve again. I use file_get_contents and file_put_contents in the php to manage the local cache file.

This is probably the best way to handle getting multiple values from one device, doing it in php. As I suspected you have confirmed it is difficult to provide that capability in an accessory in Homebridge.