<commit_before><?php

class DevoloDHC {

	public $_version = "2017.3.5";

	function __construct($login, $password, $localHost, $uuid=null, $gateway=null, $passkey=null)
	{
		$this->_login = $login;
		$this->_password = $password;
		$this->_localHost = $localHost;
		if (isset($uuid)) $this->_uuid = $uuid;
		if (isset($gateway)) $this->_gateway = $gateway;
		if (isset($passkey)) $this->_passkey = $passkey;
		$this->init();
	}

	//user functions======================================================
	//Infos
	public function getAuth() //return array of infos for faster connections with all datas
	{
		$auth = array(
					"uuid" => $this->_uuid,
					"gateway" => $this->_gateway,
					"passkey" => $this->_passkey,
					"call" => 'new DevoloDHC($login, $password, $localIP, $uuid, $gateway, $passkey)'
					);
		return $auth;
	}

	public function getInfos() //return infos from this api and the Devolo central
	{
		global $login, $password;
		$path = $this->_apiVersion.'/users/'.$this->_uuid.'/hc/gateways/'.$this->_gateway;
		$data = $this->_request('https', 'GET', $this->_Host, $path, null, $this->_login, $this->_password, null);

		$data = json_decode($data, true);

		$infos = array(
				'phpAPI version' => $this->_version,
				'uuid' => $this->_uuid,
				'gateway' => $this->_gateway,
				'name' => $data["name"],
				'role' => $data["role"],
				'status' => $data["status"],
				'state' => $data["state"],
				'type' => $data["type"],
				'firmwareVersion' => $data["firmwareVersion"],
				'externalAccess' => $data["externalAccess"]
				);

		return $infos;
	}

	//IS:
	public function isRuleActive($rule)
	{
		if ( is_string($rule) ) $rule = $this->getRuleByName($rule);
		if ( isset($rule['error']) ) return $rule;

		$jsonArray = $this->fetchItems(array($rule["element"]));
		$state = $jsonArray["result"]["items"][0]["properties"]["enabled"];
		$state = ($state > 0 ? "active" : "inactive");
		return $state;
	}

	public function isTimerActive($timer)
	{
		if ( is_string($timer) ) $timer = $this->getTimerByName($timer);
		if ( isset($timer['error']) ) return $timer;

		return $this->isRuleActive($timer);
	}

	public function isDeviceOn($device) //return true of false if find a sensor state in device
	{
		if ( is_string($device) ) $device = $this->getDeviceByName($device);
		if ( isset($device['error']) ) return $device;

		$sensors = (isset($device['sensors']) ? $device['sensors'] : null);
		if ($sensors == null) return "Unfound sensor for device";

		$sensors = json_decode($sensors, true);

		for($i=0; $i<count($sensors); $i++)
		{
			$sensor = $sensors[$i];
			$sensorType = $this->getSensorType($sensor);

			if (in_array($sensorType, $this->_DevicesOnOff))
			{
				$sensorDatas = $this->fetchItems(array($sensor));
				if ( isset($sensorDatas["result"]["items"][0]["properties"]["state"]) )
				{
					$state = $sensorDatas["result"]["items"][0]["properties"]["state"];
					$isOn = ($state > 0 ? "on" : "off");
					return $isOn;
				}
				$param = $this->getValuesByType($sensorType);
			}
		}
		return "Unfound OnOff sensor for device";
	}

	//GET:
	public function getDeviceStates($device, $DebugReport=null) //return array of sensor type and state
	{
		if ( is_string($device) ) $device = $this->getDeviceByName($device);
		if ( isset($device['error']) ) return $device;

		$sensors = (isset($device['sensors']) ? $device['sensors'] : null);
		if ($sensors == null) return array('error' => 'Unfound device');

		//fetch sensors:
		$sensors = json_decode($sensors, true);
		$arrayStates = array();

		for($i=0; $i<count($sensors); $i++)
		{
			$sensor = $sensors[$i];
			$sensorType = $this->getSensorType($sensor);
			$param = $this->getValuesByType($sensorType);
			if ($param != null)
			{
				$sensorDatas = $this->fetchItems(array($sensor));
				if ($DebugReport == true) echo "<br><pre>".json_encode($sensorDatas, JSON_PRETTY_PRINT)."</pre><br>";
				$jsonSensor = array('sensorType' => $sensorType);
				foreach ($param as $key)
				{
					$value = $sensorDatas["result"]["items"][0]["properties"][$key];
					//Seems Devolo doesn't know all about its own motion sensor...
					if ($key=="sensorType" and $value=="unknown") continue;
					//echo "sensorType:".$sensorType.", key:".$key.", value:".$value."<br>";
					$value = $this->formatStates($sensorType, $key, $value);
					$jsonSensor[$key] = $value;
				}
				array_push($arrayStates, $jsonSensor);
			}
			elseif( !in_array($sensorType, $this->_SensorsNoValues) ) //Unknown, unsupported sensor!
			{
				$sensorDatas = $this->fetchItems(array($sensor));
				echo "DEBUG - UNKNOWN PARAM - Please help and report this message on https://github.com/KiboOst/php-devoloDHC or email it to".base64_decode('a2lib29zdEBmcmVlLmZy')." <br>";
				echo "<pre>infos:".json_encode($sensorDatas, JSON_PRETTY_PRINT)."</pre><br>";
			}
		}
		return $arrayStates;
	}
	
	public function getDeviceData($device, $askData=null) //get device sensor data. If not asked data, return available datas
	{
		if ( is_string($device) ) $device = $this->getDeviceByName($device);
		if ( isset($device['error']) ) return $device;

		$datas = $this->getDeviceStates($device);
		$itemCount = count($datas);
		$availableDatas = array();
		foreach ($datas as $item)
		{
			array_push($availableDatas, $item['sensorType']);
			if ($item['sensorType'] == $askData) return $item;
		}
		$error = array('error' => 'Unfound data for this Device',
					'available' => $availableDatas
					);
		return $error;
	}

	public function getDeviceBattery($device)
	{
		if ( is_string($device) ) $device = $this->getDeviceByName($device);
		if ( isset($device['error']) ) return $device;

		return $device['batteryLevel'];
	}

	public function getAllBatteries($lowLevel=100)
	{
		$jsonDatas = array();
		for($i=0; $i<count($this->_AllDevices); $i++)
		{
			$thisDevice = $this->_AllDevices[$i];
			$thisDeviceName = $thisDevice['name'] ;
			$thisBatLevel = $thisDevice['batteryLevel'];
			if (($thisBatLevel == -1) or ($thisBatLevel == "None")) continue;

			$datas = array("name" => $thisDeviceName, "battery_percent" => $thisBatLevel);
			if ($thisBatLevel <= $lowLevel) array_push($jsonDatas, $datas);
		}
		return $jsonDatas;
	}
<commit_msg>Update phpDevoloAPI.php<commit_after><?php

class DevoloDHC {

	public $_version = "2017.3.6";

	function __construct($login, $password, $localHost, $uuid=null, $gateway=null, $passkey=null)
	{
		$this->_login = $login;
		$this->_password = $password;
		$this->_localHost = $localHost;
		if (isset($uuid)) $this->_uuid = $uuid;
		if (isset($gateway)) $this->_gateway = $gateway;
		if (isset($passkey)) $this->_passkey = $passkey;
		$this->init();
	}

	//user functions======================================================
	//Infos
	public function getAuth() //return array of infos for faster connections with all datas
	{
		$auth = array(
					"uuid" => $this->_uuid,
					"gateway" => $this->_gateway,
					"passkey" => $this->_passkey,
					"call" => 'new DevoloDHC($login, $password, $localIP, $uuid, $gateway, $passkey)'
					);
		return $auth;
	}

	public function getInfos() //return infos from this api and the Devolo central
	{
		global $login, $password;
		$path = $this->_apiVersion.'/users/'.$this->_uuid.'/hc/gateways/'.$this->_gateway;
		$data = $this->_request('https', 'GET', $this->_Host, $path, null, $this->_login, $this->_password, null);

		$data = json_decode($data, true);

		$infos = array(
				'phpAPI version' => $this->_version,
				'uuid' => $this->_uuid,
				'gateway' => $this->_gateway,
				'name' => $data["name"],
				'role' => $data["role"],
				'status' => $data["status"],
				'state' => $data["state"],
				'type' => $data["type"],
				'firmwareVersion' => $data["firmwareVersion"],
				'externalAccess' => $data["externalAccess"]
				);

		return $infos;
	}

	//IS:
	public function isRuleActive($rule)
	{
		if ( is_string($rule) ) $rule = $this->getRuleByName($rule);
		if ( isset($rule['error']) ) return $rule;

		$jsonArray = $this->fetchItems(array($rule["element"]));
		$state = $jsonArray["result"]["items"][0]["properties"]["enabled"];
		$state = ($state > 0 ? "active" : "inactive");
		return $state;
	}

	public function isTimerActive($timer)
	{
		if ( is_string($timer) ) $timer = $this->getTimerByName($timer);
		if ( isset($timer['error']) ) return $timer;

		return $this->isRuleActive($timer);
	}

	public function isDeviceOn($device) //return true of false if find a sensor state in device
	{
		if ( is_string($device) ) $device = $this->getDeviceByName($device);
		if ( isset($device['error']) ) return $device;

		$sensors = (isset($device['sensors']) ? $device['sensors'] : null);
		if ($sensors == null) return "Unfound sensor for device";

		$sensors = json_decode($sensors, true);

		for($i=0; $i<count($sensors); $i++)
		{
			$sensor = $sensors[$i];
			$sensorType = $this->getSensorType($sensor);

			if (in_array($sensorType, $this->_DevicesOnOff))
			{
				$sensorDatas = $this->fetchItems(array($sensor));
				if ( isset($sensorDatas["result"]["items"][0]["properties"]["state"]) )
				{
					$state = $sensorDatas["result"]["items"][0]["properties"]["state"];
					$isOn = ($state > 0 ? "on" : "off");
					return $isOn;
				}
				$param = $this->getValuesByType($sensorType);
			}
		}
		return "Unfound OnOff sensor for device";
	}

	//GET:
	public function getDeviceStates($device, $DebugReport=null) //return array of sensor type and state
	{
		if ( is_string($device) ) $device = $this->getDeviceByName($device);
		if ( isset($device['error']) ) return $device;

		$sensors = (isset($device['sensors']) ? $device['sensors'] : null);
		if ($sensors == null) return array('error' => 'Unfound device');

		//fetch sensors:
		$sensors = json_decode($sensors, true);
		$arrayStates = array();

		for($i=0; $i<count($sensors); $i++)
		{
			$sensor = $sensors[$i];
			$sensorType = $this->getSensorType($sensor);
			$param = $this->getValuesByType($sensorType);
			if ($param != null)
			{
				$sensorDatas = $this->fetchItems(array($sensor));
				if ($DebugReport == true) echo "<br><pre>".json_encode($sensorDatas, JSON_PRETTY_PRINT)."</pre><br>";
				$jsonSensor = array('sensorType' => $sensorType);
				foreach ($param as $key)
				{
					$value = $sensorDatas["result"]["items"][0]["properties"][$key];
					//Seems Devolo doesn't know all about its own motion sensor...
					if ($key=="sensorType" and $value=="unknown") continue;
					//echo "sensorType:".$sensorType.", key:".$key.", value:".$value."<br>";
					$value = $this->formatStates($sensorType, $key, $value);
					$jsonSensor[$key] = $value;
				}
				array_push($arrayStates, $jsonSensor);
			}
			elseif( !in_array($sensorType, $this->_SensorsNoValues) ) //Unknown, unsupported sensor!
			{
				$sensorDatas = $this->fetchItems(array($sensor));
				echo "DEBUG - UNKNOWN PARAM - Please help and report this message on https://github.com/KiboOst/php-devoloDHC or email it to".base64_decode('a2lib29zdEBmcmVlLmZy')." <br>";
				echo "<pre>infos:".json_encode($sensorDatas, JSON_PRETTY_PRINT)."</pre><br>";
			}
		}
		return $arrayStates;
	}
	
	public function getDeviceData($device, $askData=null) //get device sensor data. If not asked data, return available datas
	{
		if ( is_string($device) ) $device = $this->getDeviceByName($device);
		if ( isset($device['error']) ) return $device;

		$datas = $this->getDeviceStates($device);
		$itemCount = count($datas);
		$availableDatas = array();
		foreach ($datas as $item)
		{
			array_push($availableDatas, $item['sensorType']);
			if ($item['sensorType'] == $askData) return $item;
		}
		$error = array('error' => 'Unfound data for this Device',
					'available' => $availableDatas
					);
		return $error;
	}

	public function getDeviceBattery($device)
	{
		if ( is_string($device) ) $device = $this->getDeviceByName($device);
		if ( isset($device['error']) ) return $device;

		return $device['batteryLevel'];
	}
	
	public function getDeviceURL($device)
	{
		if ( is_string($device) ) $device = $this->getDeviceByName($device);
		if ( isset($device['error']) ) return $device;

		$uid = $device['uid'];
		if (!stristr($uid, 'hdm:DevoloHttp:virtual')) return array('error' => 'This is not an http virtual device');

		$hdm = str_replace("hdm:DevoloHttp:virtual", "hs.hdm:DevoloHttp:virtual", $uid);
		$hdmDatas = $this->fetchItems(array($hdm));
		$url = $hdmDatas['result']['items'][0]['properties']['httpSettings']['request'];
		return $url;
	}

	public function getAllBatteries($lowLevel=100)
	{
		$jsonDatas = array();
		for($i=0; $i<count($this->_AllDevices); $i++)
		{
			$thisDevice = $this->_AllDevices[$i];
			$thisDeviceName = $thisDevice['name'] ;
			$thisBatLevel = $thisDevice['batteryLevel'];
			if (($thisBatLevel == -1) or ($thisBatLevel == "None")) continue;

			$datas = array("name" => $thisDeviceName, "battery_percent" => $thisBatLevel);
			if ($thisBatLevel <= $lowLevel) array_push($jsonDatas, $datas);
		}
		return $jsonDatas;
	}

{'fraction_non_alphanumeric': 0.2065419115372719, 'fraction_numerical': 0.0037890504175688215, 'mean_word_length': 4.4569620253164555, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 150, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4701384', 'n_tokens_mistral': 4981, 'n_tokens_neox': 4405, 'n_words': 926}