Closed rwifall closed 8 years ago
Hi,
The seems interesting. Can you give me a more detailed explanation of how this works. In what scenario would you use it.
I have some home automation equipment that respond to http requests for status but do not return just an integer value or they return status for multiple devices. With this change I can use those devices with this plugin.
The way this works is when the status http call gets a response it checks to see of the user has filled out the status_regex field. If the user hasn't, then it works the same way that it has previously. However if the user has filled out the field, then it uses the provided string to create a regular expression which it tests against the http response body string. The regular expression test returns a boolean true/false that then gets passed to the status callback.
If you aren't familiar with regular expressions, here is how they work: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Here is the "test" method that is being used in this code: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
Here are some examples:
One device I have returns the following for status:
<root ZWaveStatus="1" LocalTime="2016-09-18 17:44:04 D">
<Device_Num_15 status="-1">
<states>
<state id="4" service="urn:upnp-org:serviceId:SwitchPower1" variable="Status" value="0"/>
<state id="5" service="urn:upnp-org:serviceId:Dimming1" variable="LoadLevelStatus" value="0"/>
<state id="6" service="urn:micasaverde-com:serviceId:HaDevice1" variable="Configured" value="0"/>
</states>
<Jobs/>
<tooltip display="0"/>
</Device_Num_15>
</root>
For this one I can use a regular expression string of
"status_regex": "service=\"urn:upnp-org:serviceId:SwitchPower1\" variable=\"Status\" value=\"1\""
It will match when the switch power status is 1 and not match when the switch power status is 0.
Another device returns the status for multiple devices in one status call. The status it returns looks like this:
0,1,0,1,0,0,1,0,1,0
For this one if I want to get the status for the third device I can use a regular expression string of:
"status_regex": "\d,\d,1,\d,\d,\d,\d,\d,\d,\d"
It will match when the third device is set to 1 and not match when the third device is set to 0.
So with this change the plugin should be able to handle nearly any possible status response that a device could provide.
Nice. When I get a chance to test it and assuming you updated the readme with examples then there should be no reason this can't be added in.
I don't understand, why use regex when you can use JSON?
The status_url could output:
{
"devices": [{
"status": 1
}, {
"status": 0
}, {
"status": 1
}]
}
No need to do any fancy regex magic, just read it as if its a plain object.
@dolanmiu do a pull request for json and I will merge that. I agree it makes more sense.
when I get time, I will do this
Can someone tell me why we would need an array of statuses in the first place?
0,1,0,1,0,0,1,0,1,0
Isn't the on_url
and off_url
controlling ONE light?
You wouldn't, you only control one at a time
You wouldn't, you only control one at a time
So we wouldn't want an array system right?
a simple:
{
"status": "on"
}
is sufficient?
Array is fine but only one element can be selected eg
One http can do $.power and $.volume
The reason for regex is to support devices that return data in ways that don't conform with your current expectations. I.e. I don't have control over what the status_url outputs.
I gather from this discussion that this plugin is not intended to support any possible http device designed by other companies/people, but only specific http devices/apis that you have control over?
It's mainly designed as a generic plugin for people to build a basis for their own HTTP plugin.
Ok, thanks.
These changes add the ability to use status urls that don't just return a boolean in response to the http request. To do this it adds support for a new configuration setting: status_regex
If status_regex is missing or empty, the status request code will work the same way that it has previously.
However, if status_regex contains a string, that will be used as a regular expression to test against the response body from the status http request. If that regular expression matches, then the status will be reported as true. If the regular expression does not match, then the status will be reported as false.
With this change, the status_url can be easily integrated into a much wider range of devices that return their status in different unique ways.