Device Monitoring and triggers

How does the Wink Hub do what it does? What's stored in the database? What does the aprond daemon do? what does aprontest communicate with? What is the ultimate question?
CloneNum3
Site Admin
Posts: 107
Joined: Wed Jan 07, 2015 10:02 am

Device Monitoring and triggers

Postby CloneNum3 » Fri Jan 23, 2015 9:26 pm

what exactly does everyone expect the notification to do once we do find a way to catch it? ( personally id love to have it send an http bind to OpenHab so rules can be built. Forget using WInk Hub as anything but a wifi enabled radio relay. there is a terrific post where someone combines openhab with wink hub.


So bad news and GREAT news...
Bad news... from what I'm reading, while sqlite does indeed have triggers... all the triggers can do is update the database, not actually do anything outside the database such as running a script.

GREAT news... I have a working proof of concept that you can easily write a monitoring script that runs outside the database.

rootwink.com.monitor.sh

Code: Select all

#!/bin/bash

tail -fn0 /tmp/messages | \
while read line ; do
        echo "$line" | grep -q "0x7ce5240000065ded:1; Cluster 0x0006; Attribute ID: 0x0000; State 0"
        if [ $? = 0 ]
        then
                echo "Hey... the light turned off!"
        fi
        echo "$line" | grep -q "0x7ce5240000065ded:1; Cluster 0x0006; Attribute ID: 0x0000; State 1"
        if [ $? = 0 ]
        then
                echo "Hey... the light turned on!"
        fi
done


And it works!!!

Script output when I turn the bulb on, off, and back on either via the iPhone app and/or the command line:

Code: Select all

Hey... the light turned on!
Hey... the light turned off!
Hey... the light turned on!


Command I'm running to turn the light on/off manually

Code: Select all

[root@flex-dvt ~]# aprontest -u -r zigbee -m 1 -t 1 -v ON
Update device with master ID 1, setting value ON for attribute 1
[root@flex-dvt ~]# aprontest -u -r zigbee -m 1 -t 1 -v OFF
Update device with master ID 1, setting value OFF for attribute 1


And with a case statement...
[root@flex-dvt database]# cat /tmp/rootwink.com.monitor.sh

Code: Select all

#!/bin/bash

tail -fn0 /tmp/messages | grep aprond | \
while read line
do
        crop=`echo $line | grep aprond | sed 's/^.*): //g'`
        #echo $crop
        case $crop in
        "NetAddr: 0x9924; Source: 0x7ce5240000065ded:1; Cluster 0x0006; Attribute ID: 0x0000; State 1")
                echo "0x7ce5240000065ded:1 was turned ON!"
                ;;
        "NetAddr: 0x9924; Source: 0x7ce5240000065ded:1; Cluster 0x0008; Attribute ID: 0x0000; State 255")
                echo "0x7ce5240000065ded:1 was set to full brightness"
                ;;
        "NetAddr: 0x9924; Source: 0x7ce5240000065ded:1; Cluster 0x0006; Attribute ID: 0x0000; State 1")
                echo "0x7ce5240000065ded:1 was turned ON!"
                ;;
        "NetAddr: 0x9924; Source: 0x7ce5240000065ded:1; Cluster 0x0006; Attribute ID: 0x0000; State 0")
                echo "0x7ce5240000065ded:1 was turned OFF!"
                ;;
        "GOT ZB_ASYNC_MSG_SENT_SUCCESS_MSG from 0x7ce5240000065ded:1 with status 0")
                echo "0x7ce5240000065ded:1 Confirmed receipt of my request"
                ;;
        "Sending ZCL ON/OFF to 0x7ce5240000065ded:01")
                echo "0x7ce5240000065ded:1 was told to toggle on/off"
                ;;
        *)
                echo "CROP=\"$crop\"" | grep 7ce52400
                ;;
        esac
done


Results in...

Code: Select all

0x7ce5240000065ded:1 was set to full brightness
0x7ce5240000065ded:1 was told to toggle on/off
0x7ce5240000065ded:1 Confirmed receipt of my request
0x7ce5240000065ded:1 was turned OFF!
CROP="NetAddr: 0x9924; Source: 0x7ce5240000065ded:1; Cluster 0x0008; Attribute ID: 0x0000; State 0"
0x7ce5240000065ded:1 was set to full brightness
0x7ce5240000065ded:1 was told to toggle on/off
0x7ce5240000065ded:1 Confirmed receipt of my request
CROP="NetAddr: 0x9924; Source: 0x7ce5240000065ded:1; Cluster 0x0008; Attribute ID: 0x0000; State 0"
0x7ce5240000065ded:1 was turned ON!


Though do not trust my conversion of what the status lines mean... I'll verify them later.

the /tmp/messages file appears to some times take a bit to update... I wonder if it doesn't update the file until the commands have made it through the cloud api or not... TBD

Middyeasy
Posts: 17
Joined: Fri Jan 23, 2015 3:56 pm

Re: Device Monitoring and triggers

Postby Middyeasy » Sat Jan 24, 2015 10:56 am

Brilliant Work! Your two post go along way toward helping me out ! Thanks.. Ill need some time to experiment with what you've posted.

So in the link I reference he was quoting from his blog entry on calling an external C app from within SQLite using a trigger and gives a tutorial. The only caveat I saw was the last line about the Trigger being a one time use only event.. but I wonder if it applies since we aren't looking to have a lot of different people connect.

http://freethinking.it/2011/01/08/sqlit ... functions/

I believe monitoring the database for changes is the way to go is because this abstracts all the different protocols from the different devices and makes it an easy to understand point of entry to hook into all the different attributes.

Middyeasy
Posts: 17
Joined: Fri Jan 23, 2015 3:56 pm

Re: Device Monitoring and triggers

Postby Middyeasy » Sat Jan 24, 2015 2:24 pm

So for use with your monitor as it is now... one could upload mosquito which is a MQTT publisher/subscriber system and then send that on to whatever device whats notifications of X.

this client would need to be installed on the wink via winscp.
http://mosquitto.org/man/mosquitto_pub-1.html

and if you have openHab.
https://github.com/openhab/openhab/wiki/MQTT-Binding
you can use this binding to interface with it.

Now I haven't tried it quite myself.. but I thought it might be useful information for those out there interested as I see no reason why it wouldn't work.

Middyeasy
Posts: 17
Joined: Fri Jan 23, 2015 3:56 pm

Re: Device Monitoring and triggers

Postby Middyeasy » Mon Jan 26, 2015 3:14 pm

and ran across this one for monitoring and updating openhab!
http://www.reddit.com/r/winkhub/comment ... t_locally/

I rigged it up using a script on the hub that monitors the appropriate attribute via aprontest every second and when the value of the attribute changes, the script uses curl to simulate someone pressing a button via the openhab interface (http://openhab/CMD?itemname=ON&......). Working okay for the deadbolt and tripper I'm testing with right now.
Using that, in openhab, I finally got the status of a tripper. I'm using a switch (that my hub script modifies the value of ) to postUpdate an 'open' or 'closed' to a contact, and that contact is in the sitemap. The switch isn't in the sitemap.
I'll be happy to share my config files and rules when I work the bugs out

https://gist.github.com/wpskier/6688e3172d66b633ea8e

and he created a script in /etc/init.d based on the init scripts already in there called S81openhab to run it.

#!/bin/sh

case "${1}" in
start)
echo -n "Starting openhab-monitor..."
/root/bin/openhab-monitor >> /var/log/openhab-monitor &
echo
;;

stop)
echo -n "Stopping openhab-monitor..."
killall openhab-monitor
echo
;;

restart)
${0} stop
sleep 1
${0} start
;;

*)
echo "Usage: $0 [start|stop|restart]"
;;
esac

marknmel
Posts: 3
Joined: Wed Jan 07, 2015 11:46 am

Re: Device Monitoring and triggers

Postby marknmel » Tue Jan 27, 2015 10:33 am

Middyeasy wrote:I rigged it up using a script on the hub that monitors the appropriate attribute via aprontest every second and when the value of the attribute changes, the script uses curl to simulate someone pressing a button via the openhab interface (http://openhab/CMD?itemname=ON&......).
https://gist.github.com/wpskier/6688e3172d66b633ea8e



^This is pretty clever. I've been wondering how to handle callbacks, but this is crude & effective... ;)

I still haven't unboxed my WH yet....(I lack enough hours in a day, so I'm looking forward to the switch to metric time :lol: )

If I may - a couple of q's:

-How do you find the latency into OpenHAB on status change through your script? Is it really 1 second?
-How responsive is the aprontest command to do a query? Immediate,or does that take a second as well? If there are many devices in the array...this may become an annoyance.
-Can aprontest be called multiple times(concurrency), or does it lock for exclusive access to the DB ?
-Does aprontest produce sane (usable) return codes? (ie. if it produces an error - what is the value of 'echo $?')

And another idea is do create a wrapper around aprontest to daemonize it. I dunno - something like netcat, php or a small binary in C ...
Is there any value in offloading this to a more capable system (such as that hosting OpenHAB)? Just a thought...

Middyeasy
Posts: 17
Joined: Fri Jan 23, 2015 3:56 pm

Re: Device Monitoring and triggers

Postby Middyeasy » Tue Jan 27, 2015 3:32 pm

Well I haven't yet deployed this script yet til I find time this weekend BUT I can answer a few of your questions..

This Script is polling the database for the changes in attributes.. so I don't see where it even has a delay Hub wise.. so the wink hub is sent an update message from the device it takes X amount of time to update the database.. this just keeps looping thru the database for changes from its previous state and then sends it on to OPENHAB. I imagine the speed delay really comes in how fast OPenhab handles inputs over the network.. since the EVENT handler for OpenHab is at the center of everything OPenhab I imagine under a second to update the 5 devices he is seeing here, are well achievable.

Ive been using Blink from my android phone and I can tell you that since its just a shell around the aprontest command.. its beyond blink of the eye fast.. Since you would be going thru openhab to make it, I wouldn't image its much different.. you can youtube openhab and wink hub and see his integration videos.. but I noticed that with the later firmware revs >.47 the updates are said to be much much faster. Again ill have more data this weekend.

I cant answer that for sure.. but SQL databases are meant to have concurrent connections. I think the hub has different threads running for each device frequency supported => the database is updated concurrently.

yes Aprontest will return codes of success or a number as to the possible failure.. his method above assumes that Wink Hub is doing its job and handling updating the devices database status and so doesn't rely on Aprontest directly.

From what I understand aprontest was a developers left in tool for testing.. Wink Hub is most likely running its own programs to monitor the serial ports devices to the various radios based on what protocol is meant to be parsed.. those threads update the database..

Hence this method above is definitely dependent on the device being atleast some what supported..

For instance the PEQ motion sensor I bought while it connected .. does have an attribute 6343 for State update ( ie sensor sees someone )when I do an aprontest -l -m on its database entries... Now when I do a tail -f monitor on the stream and trigger the device im definitely getting a status change on attribute 2 message from 32 to 34 and seven seconds later it sends a 32 if no motion detected but there is no 2 in the database!!!... Whether this is showing up in the database or not I don't know yet!!! I need to check that. luckly in the next thread someone showed how to add attribute support!

So in short its apples and oranges.. aprontest is how OpenHab is telling the winkhub what to do ... while this script is merely polling the database and telling Openhab to update in its http format.

winker
Posts: 7
Joined: Fri Jan 16, 2015 5:14 pm

Re: Device Monitoring and triggers

Postby winker » Tue Feb 10, 2015 12:25 pm

Middyeasy wrote:and ran across this one for monitoring and updating openhab!
http://www.reddit.com/r/winkhub/comment ... t_locally/



Thanks for sharing this.

Not sure if I'm reading the code right, but if you're looking at the status of tripper every second, are you also sending the http command to OpenHAB every second?

Would it be possible t to only send the OpenHAB HTTP command when there's a change in the tripper status, and force send the command every minute just in case there was a comm issue? That way, you really only update every minute instead of every second, and you still get immediate updates. If you're running OpenHAB on something like a Raspberry Pi, it already doesn't have that much CPU power...so this might help. I don't know scripting well enough, so if you can give me some hints how how to make these changes, I'd appreciate it.

Middyeasy
Posts: 17
Joined: Fri Jan 23, 2015 3:56 pm

Re: Device Monitoring and triggers

Postby Middyeasy » Thu Feb 12, 2015 10:07 am

The script uses wink local Apronttest.exe and runs thru the list of devices you assigned in items array and polls for the status of each.. it then looks at the last field in the array for that item to see if it has changed.. if it has THEN it sends it on to openHab.

Im going to use this a basis to rewrite something that doesn't use APRONtest.exe rather uses SQLite commands to directly query the database for changes.. maybe automate the items array with devices and then sends that on to Openhab?. there still has to exist a way to sync the two name and device wise.. but how often do you add a new device ? so maybe I wont bother with that.. but I think this approach is the best since SQLite allows concurrency .. its only reading the database so you can open just one connection from this monitor app and just loop away.. and I believe it will be more efficient and less buggy then using aprontest directly.

winker
Posts: 7
Joined: Fri Jan 16, 2015 5:14 pm

Re: Device Monitoring and triggers

Postby winker » Sat Feb 14, 2015 12:03 am

That database stuff sounds complicated, but I'd be interested in what you come up with, thanks.

Thanks for all the info with the Aprontest method for monitoring. I'm going to try it, but don't know the Wink system very well, or PHP. Can you help with a few (well, all) the issues?

1. where do you put the PHP file?
2. how do you run it on the wink hub
3. It looks like the while loop runs forever - how does he control the frequency that the hub does this process?
4. How to get it to start running on startup?

Middyeasy
Posts: 17
Joined: Fri Jan 23, 2015 3:56 pm

Re: Device Monitoring and triggers

Postby Middyeasy » Mon Feb 16, 2015 9:57 am

If you read thru this thread youll see that it talks about the start up script.. and in it, ittells you where to upload your PHP file too.. you can use winscp if your a windows users to ssh in. The script to auto start it also needs to be uploaded and it tells you were as well.

Yes it just loops thru there but remember wink is running Linux which is a a real OS and not just a microprocessor .. so it is multi threaded and time based software management.. yes it continuously loops with no delays and maybe it will be issue I haven't tried it myself but the OS will handle allowing other processes to do what it needs too... im just finishing up my openhab GUI.


Return to “How does it work?”

Who is online

Users browsing this forum: No registered users and 1 guest