Advertisement
enishoca

openhab-monitor

Feb 22nd, 2015
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.49 KB | None | 0 0
  1. <?php
  2. # Usage details in this post https://groups.google.com/forum/#!category-topic/openhab/examples/AROBZn-WjnM
  3.  
  4. date_default_timezone_set('EST');
  5.  
  6. $debug = 0;
  7. $sleepinterval = 9;
  8.  
  9. # $item[0] Item_name - OpenHAB item name
  10. # $item[1] master_id - master_id, as listed by aprontest -l
  11. # $item[2] aprontest attribute - attribute to monitor
  12. # $item[3] value1 or "AttributeValue" or "Percent" - if set to "AttributeValue" send back whatever comes back, if set to "Percent" attribute is converted to a percent of value in $item[4]
  13. # $item[4] send str1 or base of the number if $item[3] is "Percent" - if aprontest attribute changes to value1, send this string
  14. # $item[5] value2
  15. # $item[6] send str2 - if aprontest attribute changes to value2, send this string
  16. # $item[7] prev_value - value stored from the previous call to aprontest
  17.  
  18. $items = array
  19. (
  20.   array("denlight",6,"Level","Percent","255","","","")
  21.   array("dendimmer",6,"ConfigurationParameter_0","AttributeValue","","","","")
  22.   #array("side_door_contact",13,"ZoneStatus","48","CLOSED","49","OPEN",""),
  23.  #array("front_door_lock",5,"Lock_Unlock","TRUE","ON","FALSE","OFF","")
  24. );
  25.  
  26. while(true) {
  27.   $count = 0;
  28.   foreach($items as &$item) {
  29.     $cmd = "aprontest -l -m " . $item[1] . " | grep " . $item[2] . " | awk '{ print $9 }'";
  30.     # echo "$cmd\n";
  31.    unset($out);
  32.     exec($cmd,$out,$retval);
  33.  
  34.     if($retval == 0) {
  35.       if($debug == 1) { echo "$item[0]: $out[0]\n"; }
  36.       if($item[7] == "" ) {
  37.         if($debug == 1) { echo "* Setting initial value\n"; }
  38.         $item[7] = $out[0];
  39.       } else {
  40.         if($debug == 1) { echo "* Comparing to existing value...\n"; }
  41.         if($item[7] != $out[0]) {
  42.           echo date(DATE_RFC2822) . ":  $item[0]: $item[7] -> $out[0]\n";
  43.           $item[7] = $out[0];
  44.           $url = "http://pi:8080/rest/items/$item[0]/state";
  45.  
  46.           if ($item[3] == "SourceValue") {
  47.             $payload = $out[0];
  48.           } elseif ($item[3] == "Percent")  {
  49.             $payload = intval($out[0] / intval($item[4]) * 100);
  50.           }
  51.           elseif($out[0] == $item[3]) {
  52.             $payload = $item[4];
  53.           } elseif($out[0] == $item[5]) {
  54.             $payload = $item[6];
  55.           }                              
  56.  
  57.           exec("curl -s -X PUT -d $payload -H \"Content-Type: text/plain\" $url");
  58.         } else {
  59.           if($debug == 1) { echo "No change\n"; }
  60.         }
  61.       }
  62.     }
  63.     $count += 1;
  64.   }
  65.   if($debug == 1) { echo "\n"; }
  66.   sleep($sleepinterval);
  67. }
  68.  
  69. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement