From 253c30e0a9f5d3863ad531cb9a648699e203acb6 Mon Sep 17 00:00:00 2001 From: Florian Arndt Date: Sat, 10 Aug 2013 16:41:06 +0200 Subject: [PATCH] Added password authentication (needed if not logged in via browser from the same ip address) and status (which ports are on/off). --- class.EnerGenieSwitcher.php | 63 ++++++++++++++++++++++++++++++++----- test.php | 2 +- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/class.EnerGenieSwitcher.php b/class.EnerGenieSwitcher.php index 3e4b95a..bc2146a 100644 --- a/class.EnerGenieSwitcher.php +++ b/class.EnerGenieSwitcher.php @@ -8,16 +8,34 @@ class EnerGenieSwitcher { /** * Check prerequisites and set-up */ - public function __construct($ip, $debug=false) { + public function __construct($ip, $password, $debug=false) { if(!extension_loaded('curl')) { die('Fatal error: CURL extension needed.'); } $this->ip = $ip; + $this->password = $password; $this->debug = $debug; } + public function doLogin() { + $this->postRequest('http://'.$this->ip.'/login.html', array('pw' => $this->password)); + } + + /** + * Get status + */ + public function getStatus() { + $this->doLogin(); + $html = $this->getRequest('http://'.$this->ip.'/energenie.html', array()); + preg_match_all('/var sockstates \= \[([0-1],[0,1],[0,1],[0,1])\]/', $html, $matches); + if(!isset($matches[1][0])) { return false; } + $states = explode(',', $matches[1][0]); + return array(1=>$states[0], 2=>$states[1], 3=>$states[2], 4=>$states[3]); + } + /** * Do the switch */ public function doSwitch($switches) { + $this->doLogin(); foreach($switches as $port => $state) { $ports = array(1 => '', 2 => '', 3 => '', 4 => ''); $ports[$port] = $state; @@ -27,14 +45,14 @@ class EnerGenieSwitcher { $params['cte'.$port] = $state; } } - $this->post_request('http://'.$this->ip, $params); + $this->postRequest('http://'.$this->ip, $params); } } - function post_request($url, $fields) { - $fields_string = ''; - foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } - rtrim($fields_string, '&'); + function postRequest($url, $fields) { + $fields_string_array = array(); + foreach($fields as $key=>$value) { $fields_string_array[] = $key.'='.$value; } + $fields_string = join('&', $fields_string_array); //open connection $ch = curl_init(); @@ -48,9 +66,40 @@ class EnerGenieSwitcher { //execute post $result = curl_exec($ch); - if($this->debug === true) { echo "Calling " . $url . '?' . $fields_string . "\n"; } + if($this->debug === true) { + echo "Calling " . $url . '?' . $fields_string . "\n"; + } //close connection curl_close($ch); + + // provide html + return $result; + } + + function getRequest($url, $fields) { + $fields_string_array = array(); + foreach($fields as $key=>$value) { $fields_string_array[] = $key.'='.$value; } + $fields_string = join('&', $fields_string_array); + //open connection + $ch = curl_init(); + + // configure + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 5000); + //set the url, number of POST vars, POST data + curl_setopt($ch,CURLOPT_URL, $url . ($fields_string != '' ? '?' . $fields_string : '')); + + //execute post + $result = curl_exec($ch); + if($this->debug === true) { + echo "Calling " . $url . '?' . $fields_string . "\n"; + } + + //close connection + curl_close($ch); + + // provide html + return $result; } } diff --git a/test.php b/test.php index 8caf7b6..1f701e8 100644 --- a/test.php +++ b/test.php @@ -1,6 +1,6 @@ doSwitch(array( 1 => EnerGenieSwitcher::ON, 2 => EnerGenieSwitcher::OFF,