2013-08-08 23:31:32 +02:00
|
|
|
<?php
|
2014-01-03 17:44:32 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
2013-08-08 23:31:32 +02:00
|
|
|
const ON = 1;
|
|
|
|
const OFF = 0;
|
2014-01-03 17:44:32 +01:00
|
|
|
const TIMEOUT = 1000;
|
|
|
|
|
|
|
|
private $debug = true;
|
|
|
|
|
2013-08-08 23:31:32 +02:00
|
|
|
/**
|
|
|
|
* Check prerequisites and set-up
|
|
|
|
*/
|
2014-01-03 17:44:32 +01:00
|
|
|
public function __construct($ip, $password, $debug=false)
|
|
|
|
{
|
|
|
|
if(!extension_loaded('curl'))
|
|
|
|
{
|
|
|
|
die('Fatal error: CURL extension needed.');
|
|
|
|
}
|
2013-08-08 23:31:32 +02:00
|
|
|
$this->ip = $ip;
|
2013-08-10 16:41:06 +02:00
|
|
|
$this->password = $password;
|
2013-08-08 23:31:32 +02:00
|
|
|
$this->debug = $debug;
|
2014-01-03 17:44:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2013-08-10 16:41:06 +02:00
|
|
|
|
2014-01-03 17:44:32 +01:00
|
|
|
public function doLogin()
|
|
|
|
{
|
|
|
|
$html=$this->postRequest('http://'.$this->ip.'/login.html', array('pw' => $this->password));
|
|
|
|
|
|
|
|
if ($html=="" OR strstr($html, "EnerGenie Web:"))
|
|
|
|
$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;
|
|
|
|
}
|
2013-12-28 10:52:25 +01:00
|
|
|
|
2013-08-10 16:41:06 +02:00
|
|
|
/**
|
|
|
|
* Get status
|
|
|
|
*/
|
2014-01-03 17:44:32 +01:00
|
|
|
public function getStatus()
|
|
|
|
{
|
|
|
|
if ($this->doLogin())
|
|
|
|
{
|
|
|
|
$html = $this->getRequest('http://'.$this->ip.'/energenie.html', array());
|
|
|
|
//echo $this->ip.$html."<hr>";
|
|
|
|
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]);
|
|
|
|
$this->doLogout();
|
|
|
|
|
|
|
|
return array(1=>$states[0], 2=>$states[1], 3=>$states[2], 4=>$states[3]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-10 16:41:06 +02:00
|
|
|
|
2013-08-08 23:31:32 +02:00
|
|
|
/**
|
|
|
|
* Do the switch
|
|
|
|
*/
|
2014-01-03 17:44:32 +01:00
|
|
|
public function doSwitch($switches)
|
|
|
|
{
|
|
|
|
if ($this->doLogin())
|
|
|
|
{
|
|
|
|
foreach($switches as $port => $state)
|
|
|
|
{
|
|
|
|
$ports = array(1 => '', 2 => '', 3 => '', 4 => '');
|
|
|
|
$ports[$port] = $state;
|
|
|
|
$params = array();
|
|
|
|
foreach($ports as $port => $state)
|
|
|
|
{
|
|
|
|
if(in_array($state, array(self::ON, self::OFF)))
|
|
|
|
{
|
|
|
|
$params['cte'.$port] = $state;
|
|
|
|
}
|
|
|
|
}
|
2013-08-08 23:31:32 +02:00
|
|
|
}
|
2013-08-10 16:41:06 +02:00
|
|
|
$this->postRequest('http://'.$this->ip, $params);
|
2014-01-03 17:44:32 +01:00
|
|
|
$this->doLogout();
|
|
|
|
}
|
2013-08-08 23:31:32 +02:00
|
|
|
}
|
2014-01-03 17:44:32 +01:00
|
|
|
|
|
|
|
function postRequest($url, $fields)
|
|
|
|
{
|
2013-08-10 16:41:06 +02:00
|
|
|
$fields_string_array = array();
|
2014-01-03 17:44:32 +01:00
|
|
|
foreach($fields as $key=>$value)
|
|
|
|
{
|
|
|
|
$fields_string_array[] = $key.'='.$value;
|
|
|
|
}
|
2013-08-10 16:41:06 +02:00
|
|
|
$fields_string = join('&', $fields_string_array);
|
2013-08-08 23:31:32 +02:00
|
|
|
//open connection
|
|
|
|
$ch = curl_init();
|
2014-01-03 17:44:32 +01:00
|
|
|
|
2013-08-08 23:31:32 +02:00
|
|
|
// configure
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
2014-01-03 17:44:32 +01:00
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, self::TIMEOUT);
|
2013-08-08 23:31:32 +02:00
|
|
|
//set the url, number of POST vars, POST data
|
2014-01-03 17:44:32 +01:00
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, count($fields));
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
|
|
|
|
|
2013-08-08 23:31:32 +02:00
|
|
|
//execute post
|
|
|
|
$result = curl_exec($ch);
|
2014-01-03 17:44:32 +01:00
|
|
|
if($this->debug === true)
|
|
|
|
{
|
|
|
|
echo "Calling " . $url . '?' . $fields_string . "...<br>\n";
|
|
|
|
}
|
|
|
|
|
2013-08-08 23:31:32 +02:00
|
|
|
//close connection
|
|
|
|
curl_close($ch);
|
2014-01-03 17:44:32 +01:00
|
|
|
|
2013-08-10 16:41:06 +02:00
|
|
|
// provide html
|
|
|
|
return $result;
|
2014-01-03 17:44:32 +01:00
|
|
|
}
|
2013-08-10 16:41:06 +02:00
|
|
|
|
2014-01-03 17:44:32 +01:00
|
|
|
|
|
|
|
function getRequest($url, $fields)
|
|
|
|
{
|
2013-08-10 16:41:06 +02:00
|
|
|
$fields_string_array = array();
|
2014-01-03 17:44:32 +01:00
|
|
|
foreach($fields as $key=>$value)
|
|
|
|
{
|
|
|
|
$fields_string_array[] = $key.'='.$value;
|
|
|
|
}
|
2013-08-10 16:41:06 +02:00
|
|
|
$fields_string = join('&', $fields_string_array);
|
|
|
|
//open connection
|
|
|
|
$ch = curl_init();
|
2014-01-03 17:44:32 +01:00
|
|
|
|
2013-08-10 16:41:06 +02:00
|
|
|
// configure
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
2014-01-03 17:44:32 +01:00
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, self::TIMEOUT);
|
2013-08-10 16:41:06 +02:00
|
|
|
//set the url, number of POST vars, POST data
|
2014-01-03 17:44:32 +01:00
|
|
|
curl_setopt($ch, CURLOPT_URL, $url . ($fields_string != '' ? '?' . $fields_string : ''));
|
|
|
|
|
2013-08-10 16:41:06 +02:00
|
|
|
//execute post
|
|
|
|
$result = curl_exec($ch);
|
2014-01-03 17:44:32 +01:00
|
|
|
if($this->debug === true)
|
|
|
|
{
|
|
|
|
echo "Calling " . $url . '?' . $fields_string . "...<br>\n";
|
|
|
|
}
|
|
|
|
|
2013-08-10 16:41:06 +02:00
|
|
|
//close connection
|
|
|
|
curl_close($ch);
|
2014-01-03 17:44:32 +01:00
|
|
|
|
2013-08-10 16:41:06 +02:00
|
|
|
// provide html
|
|
|
|
return $result;
|
2014-01-03 17:44:32 +01:00
|
|
|
}
|
2013-08-08 23:31:32 +02:00
|
|
|
}
|