updated removed
[home-automation.git] / check_power_status.php
1 <?
2
3 $ch = curl_init();
4 curl_setopt($ch, CURLOPT_URL,"http://TED/api/LiveData.xml");
5 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
6 $output = curl_exec($ch);
7
8 #echo "<pre>";
9 $array=xml2array($output);
10 #print_r($array);
11
12 echo "AC Line Power Now:" . ($array['LiveData']['Voltage']['Total']['VoltageNow']/10)."<br>";
13 #echo "Current Kw:" . ($array['LiveData']['Power']['Total']['PowerNow']/1000)."<br>";
14 echo "Server Kw Draw:" . ($array['LiveData']['Power']['MTU1']['PowerNow']/1000)."<br>";
15 echo "House Kw Draw:" . abs(($array['LiveData']['Power']['MTU1']['PowerNow']/1000)-($array['LiveData']['Power']['MTU2']['PowerNow']/1000))."<br>";
16 echo "Total Kw Draw:" . ($array['LiveData']['Power']['MTU2']['PowerNow']/1000)."<br>";
17
18
19
20
21
22
23
24
25 function xml2array($contents, $get_attributes=1, $priority = 'tag') {
26     if(!$contents) return array();
27
28     if(!function_exists('xml_parser_create')) {
29         //print "'xml_parser_create()' function not found!";
30         return array();
31     }
32
33     //Get the XML parser of PHP - PHP must have this module for the parser to work
34     $parser = xml_parser_create('');
35     xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
36     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
37     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
38     xml_parse_into_struct($parser, trim($contents), $xml_values);
39     xml_parser_free($parser);
40
41     if(!$xml_values) return;//Hmm...
42
43     //Initializations
44     $xml_array = array();
45     $parents = array();
46     $opened_tags = array();
47     $arr = array();
48
49     $current = &$xml_array; //Refference
50
51     //Go through the tags.
52     $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
53     foreach($xml_values as $data) {
54         unset($attributes,$value);//Remove existing values, or there will be trouble
55
56         //This command will extract these variables into the foreach scope
57         // tag(string), type(string), level(int), attributes(array).
58         extract($data);//We could use the array by itself, but this cooler.
59
60         $result = array();
61         $attributes_data = array();
62         
63         if(isset($value)) {
64             if($priority == 'tag') $result = $value;
65             else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
66         }
67
68         //Set the attributes too.
69         if(isset($attributes) and $get_attributes) {
70             foreach($attributes as $attr => $val) {
71                 if($priority == 'tag') $attributes_data[$attr] = $val;
72                 else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
73             }
74         }
75
76         //See tag status and do the needed.
77         if($type == "open") {//The starting of the tag '<tag>'
78             $parent[$level-1] = &$current;
79             if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
80                 $current[$tag] = $result;
81                 if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
82                 $repeated_tag_index[$tag.'_'.$level] = 1;
83
84                 $current = &$current[$tag];
85
86             } else { //There was another element with the same tag name
87
88                 if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
89                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
90                     $repeated_tag_index[$tag.'_'.$level]++;
91                 } else {//This section will make the value an array if multiple tags with the same name appear together
92                     $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
93                     $repeated_tag_index[$tag.'_'.$level] = 2;
94                     
95                     if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
96                         $current[$tag]['0_attr'] = $current[$tag.'_attr'];
97                         unset($current[$tag.'_attr']);
98                     }
99
100                 }
101                 $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
102                 $current = &$current[$tag][$last_item_index];
103             }
104
105         } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
106             //See if the key is already taken.
107             if(!isset($current[$tag])) { //New Key
108                 $current[$tag] = $result;
109                 $repeated_tag_index[$tag.'_'.$level] = 1;
110                 if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
111
112             } else { //If taken, put all things inside a list(array)
113                 if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
114
115                     // ...push the new element into that array.
116                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
117                     
118                     if($priority == 'tag' and $get_attributes and $attributes_data) {
119                         $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
120                     }
121                     $repeated_tag_index[$tag.'_'.$level]++;
122
123                 } else { //If it is not an array...
124                     $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
125                     $repeated_tag_index[$tag.'_'.$level] = 1;
126                     if($priority == 'tag' and $get_attributes) {
127                         if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
128                             
129                             $current[$tag]['0_attr'] = $current[$tag.'_attr'];
130                             unset($current[$tag.'_attr']);
131                         }
132                         
133                         if($attributes_data) {
134                             $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
135                         }
136                     }
137                     $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
138                 }
139             }
140
141         } elseif($type == 'close') { //End of tag '</tag>'
142             $current = &$parent[$level-1];
143         }
144     }
145     
146     return($xml_array);
147 }  
148
149 ?>