Error handling and timeout issues solved thanks to Ulf Haase.

pull/2/head
Florian Arndt 2014-01-03 17:44:32 +01:00
parent bb2bebab55
commit 920bfa3c9e
1 changed files with 133 additions and 61 deletions

View File

@ -1,111 +1,183 @@
<?php <?php
class EnerGenieSwitcher { /**
* Class EnerGenieSwitcher
* @author Florian Arndt <post@florianarndt.com>
* @author Ulf Haase <ulf.haase@uhsb.de>
* @package EnerGenie
* @subpackage
* @copyright https://github.com/flowli/EnerGenie-EG-PM2-LAN
* @version 1.00.00
*/
class EnerGenieSwitcher
{
const ON = 1; const ON = 1;
const OFF = 0; const OFF = 0;
const TIMEOUT = 1000;
private $debug = false;
private $debug = true;
/** /**
* Check prerequisites and set-up * Check prerequisites and set-up
*/ */
public function __construct($ip, $password, $debug=false) { public function __construct($ip, $password, $debug=false)
if(!extension_loaded('curl')) { die('Fatal error: CURL extension needed.'); } {
if(!extension_loaded('curl'))
{
die('Fatal error: CURL extension needed.');
}
$this->ip = $ip; $this->ip = $ip;
$this->password = $password; $this->password = $password;
$this->debug = $debug; $this->debug = $debug;
} }
public function doLogout()
{
$html=$this->postRequest('http://'.$this->ip.'/login.html', array('pw' => ''));
if (strstr($html, "EnerGenie Web:"))
$result=TRUE;
else
$result=FALSE;
if ($this->debug)
{
if ($result)
echo "Logout ".$this->ip.": successful!!!<br>\n";
else
echo "Logout ".$this->ip."-->".$html."<--: failed!!!<br>\n";
}
return $result;
}
public function doLogin() { public function doLogin()
$this->postRequest('http://'.$this->ip.'/login.html', array('pw' => $this->password)); {
} $html=$this->postRequest('http://'.$this->ip.'/login.html', array('pw' => $this->password));
public function doLogout() { if ($html=="" OR strstr($html, "EnerGenie Web:"))
$this->postRequest('http://'.$this->ip.'/login.html', array('pw' => '')); $result=FALSE;
} else
$result=TRUE;
if ($this->debug)
{
if ($result)
echo "Login ".$this->ip.": successful!!!<br>\n";
else
echo "Login ".$this->ip.": failed!!!<br>\n";
}
return $result;
}
/** /**
* Get status * Get status
*/ */
public function getStatus() { public function getStatus()
$this->doLogin(); {
$html = $this->getRequest('http://'.$this->ip.'/energenie.html', array()); if ($this->doLogin())
$this->doLogout(); {
preg_match_all('/var sockstates \= \[([0-1],[0,1],[0,1],[0,1])\]/', $html, $matches); $html = $this->getRequest('http://'.$this->ip.'/energenie.html', array());
if(!isset($matches[1][0])) { return false; } //echo $this->ip.$html."<hr>";
$states = explode(',', $matches[1][0]); preg_match_all('/var sockstates \= \[([0-1],[0,1],[0,1],[0,1])\]/', $html, $matches);
return array(1=>$states[0], 2=>$states[1], 3=>$states[2], 4=>$states[3]); if(!isset($matches[1][0])) { return false; }
} $states = explode(',', $matches[1][0]);
$this->doLogout();
return array(1=>$states[0], 2=>$states[1], 3=>$states[2], 4=>$states[3]);
}
else
return false;
}
/** /**
* Do the switch * Do the switch
*/ */
public function doSwitch($switches) { public function doSwitch($switches)
$this->doLogin(); {
foreach($switches as $port => $state) { if ($this->doLogin())
$ports = array(1 => '', 2 => '', 3 => '', 4 => ''); {
$ports[$port] = $state; foreach($switches as $port => $state)
$params = array(); {
foreach($ports as $port => $state) { $ports = array(1 => '', 2 => '', 3 => '', 4 => '');
if(in_array($state, array(self::ON, self::OFF))) { $ports[$port] = $state;
$params['cte'.$port] = $state; $params = array();
foreach($ports as $port => $state)
{
if(in_array($state, array(self::ON, self::OFF)))
{
$params['cte'.$port] = $state;
}
}
} }
}
$this->postRequest('http://'.$this->ip, $params); $this->postRequest('http://'.$this->ip, $params);
$this->doLogout();
}
} }
$this->doLogout();
} function postRequest($url, $fields)
{
function postRequest($url, $fields) {
$fields_string_array = array(); $fields_string_array = array();
foreach($fields as $key=>$value) { $fields_string_array[] = $key.'='.$value; } foreach($fields as $key=>$value)
{
$fields_string_array[] = $key.'='.$value;
}
$fields_string = join('&', $fields_string_array); $fields_string = join('&', $fields_string_array);
//open connection //open connection
$ch = curl_init(); $ch = curl_init();
// configure // configure
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 5000); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, self::TIMEOUT);
//set the url, number of POST vars, POST data //set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
//execute post //execute post
$result = curl_exec($ch); $result = curl_exec($ch);
if($this->debug === true) { if($this->debug === true)
echo "Calling " . $url . '?' . $fields_string . "\n"; {
} echo "Calling " . $url . '?' . $fields_string . "...<br>\n";
}
//close connection //close connection
curl_close($ch); curl_close($ch);
// provide html // provide html
return $result; return $result;
} }
function getRequest($url, $fields) {
function getRequest($url, $fields)
{
$fields_string_array = array(); $fields_string_array = array();
foreach($fields as $key=>$value) { $fields_string_array[] = $key.'='.$value; } foreach($fields as $key=>$value)
{
$fields_string_array[] = $key.'='.$value;
}
$fields_string = join('&', $fields_string_array); $fields_string = join('&', $fields_string_array);
//open connection //open connection
$ch = curl_init(); $ch = curl_init();
// configure // configure
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 5000); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, self::TIMEOUT);
//set the url, number of POST vars, POST data //set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url . ($fields_string != '' ? '?' . $fields_string : '')); curl_setopt($ch, CURLOPT_URL, $url . ($fields_string != '' ? '?' . $fields_string : ''));
//execute post //execute post
$result = curl_exec($ch); $result = curl_exec($ch);
if($this->debug === true) { if($this->debug === true)
echo "Calling " . $url . '?' . $fields_string . "\n"; {
} echo "Calling " . $url . '?' . $fields_string . "...<br>\n";
}
//close connection //close connection
curl_close($ch); curl_close($ch);
// provide html // provide html
return $result; return $result;
}
} }
}