* @copyright Basemotive V.O.F. */ class IpoMan { private $ipaddress = null; private $username = null; private $password = null; private $immediateswitching = null; private $client = null; /** * Constructs the IpoMan class * * @param string $ipaddress The IP-address (or hostname) for the IpoMan * @param string $username The username to authenticate with * @param string $password The username to authenticate with * @param boolean $immediateswitching Whether or not we want to temporary set the on/off delays to 0 when switching an outlet */ public function __construct($ipaddress, $username, $password, $immediateswitching=false) { $this->ipaddress = $ipaddress; $this->username = $username; $this->password = $password; $this->immediateswitching = $immediateswitching; $this->client = new HttpClient($ipaddress); $this->client->setAuthorization($username, $password); } /** * Turns an outlet off and on. * * @param integer $outlet The outlet number to switch, 0 is the first outlet, 11 is the last * @param integer $delay The delay between switching off and on in seconds */ public function resetOutlet($outlet, $delay=1) { // Set outlet on/off delays to 0 if ($this->immediateswitching) { $outletSettings = $this->getOutletSettings(); if ($outlet < 2) $outletonid = "XAAAAAAABADL".chr(ord('A')+$outlet+14); else $outletonid = "XAAAAAAABADM".chr(ord('A')+$outlet-2); $outletoffid = "XAAAAAAABADL".chr(ord('A')+$outlet+2); if ($outletSettings[$outletonid]!="0" || $outletSettings[$outletoffid]!="0") { $tmpOutletSettings = $outletSettings; $tmpOutletSettings[$outletonid] = "0"; $tmpOutletSettings[$outletoffid] = "0"; $this->setOutletSettings($tmpOutletSettings); var_dump($tmpOutletSettings); $needRestoreOutletSettings = true; } else { $needRestoreOutletSettings = false; } } // Turn outlet off $callarray = array(); for ($i=0; $i<12; $i++) { $outletid = "XAAAAAAABOEB".chr(ord('A')+$i); $callarray[$outletid] = ($outlet==$i ? "3" : "1"); } $this->client->post('/PageControl.htm', $callarray); sleep($delay); // Turn outlet on $callarray = array(); for ($i=0; $i<12; $i++) { $outletid = "XAAAAAAABOEB".chr(ord('A')+$i); $callarray[$outletid] = ($outlet==$i ? "2" : "1"); } $this->client->post('/PageControl.htm', $callarray); // Restore default outlet settings if ($this->immediateswitching && $needRestoreOutletSettings) { $this->setOutletSettings($outletSettings); } } /** * Turns an outlet on. * * @param integer $outlet The outlet number to switch, 0 is the first outlet, 11 is the last */ public function switchOn($outlet) { // Make sure we have something to do if ($this->getOutletStatus($outlet)=="1") return; // Set outlet on delay to 0 if ($this->immediateswitching) { $outletSettings = $this->getOutletSettings(); if ($outlet < 2) $outletonid = "XAAAAAAABADL".chr(ord('A')+$outlet+14); else $outletonid = "XAAAAAAABADM".chr(ord('A')+$outlet-2); if ($outletSettings[$outletonid]!="0") { $tmpOutletSettings = $outletSettings; $tmpOutletSettings[$outletonid] = "0"; $this->setOutletSettings($tmpOutletSettings); $needRestoreOutletSettings = true; } else { $needRestoreOutletSettings = false; } } // Turn outlet on $callarray = array(); for ($i=0; $i<12; $i++) { $outletid = "XAAAAAAABOEB".chr(ord('A')+$i); $callarray[$outletid] = ($outlet==$i ? "2" : "1"); } $this->client->post('/PageControl.htm', $callarray); // Restore default outlet settings if ($this->immediateswitching && $needRestoreOutletSettings) { $this->setOutletSettings($outletSettings); } } /** * Turns an outlet off. * * @param integer $outlet The outlet number to switch, 0 is the first outlet, 11 is the last */ public function switchOff($outlet) { // Make sure we have something to do if ($this->getOutletStatus($outlet)=="0") return; // Set outlet off delay to 0 if ($this->immediateswitching) { $outletSettings = $this->getOutletSettings(); $outletoffid = "XAAAAAAABADL".chr(ord('A')+$outlet+2); if ($outletSettings[$outletoffid]!="0") { $tmpOutletSettings = $outletSettings; $tmpOutletSettings[$outletoffid] = "0"; $this->setOutletSettings($tmpOutletSettings); $needRestoreOutletSettings = true; } else { $needRestoreOutletSettings = false; } } // Turn outlet off $callarray = array(); for ($i=0; $i<12; $i++) { $outletid = "XAAAAAAABOEB".chr(ord('A')+$i); $callarray[$outletid] = ($outlet==$i ? "3" : "1"); } $this->client->post('/PageControl.htm', $callarray); // Restore default outlet settings if ($this->immediateswitching && $needRestoreOutletSettings) { $this->setOutletSettings($outletSettings); } } /** * Retrieves the outlet status. * * @param integer $outlet The outlet number to retrieve the status for; if ommitted, all outlet statuses are returned * @return array All outlet statuses, or an integer if a specific outlet was specified */ public function getOutletStatus($outlet='') { $result = array(); $this->client->get("/Outlet.js"); $content = $this->client->getContent(); $content_split = explode("\n", $content); foreach ($content_split as $line) { if (strpos($line, "var Power_status") !== false) { $pos1 = strpos($line, '('); $pos2 = strpos($line, ')'); $arraypart = substr($line, $pos1+1, $pos2-$pos1-1); $outletstatusarray = explode(',', $arraypart); foreach ($outletstatusarray as $status) { $result[] = ($status=='"power on"'?1:0); } } } if ($outlet!='') return $result[$outlet]; else return $result; } /** * Retrieves the value for a form element from the PageControl html. */ private function getContentValue($content, $id) { $pos = strpos($content, $id); $pos2 = strpos($content, 'VALUE="', $pos)+7; $pos3 = strpos($content, '"', $pos2); $value = substr($content, $pos2, $pos3-$pos2); return $value; } /** * Retrieves all the outlet settings from the IpoMan as an array, so we can post it * again. */ private function getOutletSettings() { $result = $this->client->get("/PageControl.htm"); $content = $this->client->getContent(); $outletSettings = array(); for ($i=0; $i<12; $i++) { // Outlet Name $outletid = "XAAAAAAABADJ".chr(ord('A')+$i+1); $outletSettings[$outletid] = $this->getContentValue($content, $outletid); // Outlet Location $outletid = "XAAAAAAABADK".chr(ord('A')+$i+1); $outletSettings[$outletid] = $this->getContentValue($content, $outletid); // Power on Delay (secs) if ($i < 2) $outletid = "XAAAAAAABADL".chr(ord('A')+$i+14); else $outletid = "XAAAAAAABADM".chr(ord('A')+$i-2); $outletSettings[$outletid] = $this->getContentValue($content, $outletid); // Power off Delay (secs) $outletid = "XAAAAAAABADL".chr(ord('A')+$i+2); $outletSettings[$outletid] = $this->getContentValue($content, $outletid); // Output current threshold $outletid = "XAAAAAAABADG".chr(ord('A')+$i+1); $outletSettings[$outletid] = $this->getContentValue($content, $outletid); } return $outletSettings; } /** * Sets all the outlet settings to the values given in the array. */ private function setOutletSettings($outletSettings) { $this->client->post('/PageControl.htm', $outletSettings); } public function dump() { //$result = $this->client->get("/PagePDUStatus.htm"); $result = $this->client->get("/OutletTableStatus.js"); $content = $this->client->getContent(); echo $content."\n\n"; } } ?>