77 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			77 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
if (!array_key_exists("pws2mqtt", $topics))		$topics["pws2mqtt"] = new topic;
 | 
						|
if (!array_key_exists("pws2mqtt", $devices))	$devices["pws2mqtt"] = array();
 | 
						|
 | 
						|
function pws2mqttCallback($topic, $message)
 | 
						|
{
 | 
						|
	global $logFh, $devices, $included, $topics;
 | 
						|
 | 
						|
	$topicName = $topic[0];
 | 
						|
	$topics[$topicName]->lastSeen = time();
 | 
						|
	$fn = $topic[1];
 | 
						|
	logger(INFO, sprintf(_("Incoming notification of device %s => friendly name : %s"), $topicName, $fn), __FILE__ . ":" . __LINE__);
 | 
						|
	$device = & $devices[$topicName];
 | 
						|
	$payloadArray = json_decode($message->payload, true);
 | 
						|
				// friendlyname
 | 
						|
	if ( $fn != "get")
 | 
						|
	{
 | 
						|
		if (!array_key_exists($fn, $device))
 | 
						|
		{
 | 
						|
			echo "initializing $fn";
 | 
						|
			$device[$fn] = array("device" => new device);
 | 
						|
			$device = &$device[$fn];
 | 
						|
			$device["device"]->friendlyName = $payloadArray["friendly_name"];
 | 
						|
			$device["device"]->ieeeAddress =  $payloadArray["ieeeAddress"];
 | 
						|
			$device["device"]->type = $payloadArray["type"];
 | 
						|
			$device["device"]->topic = $topicName;
 | 
						|
			//$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
 | 
						|
			//$indexFriendlyNames[$topicName][$fn] = & $device["device"];
 | 
						|
			mkIndexes();
 | 
						|
 | 
						|
		}else
 | 
						|
		{
 | 
						|
			$device = &$device[$fn];
 | 
						|
		}
 | 
						|
		unset($payloadArray["friendly_name"]);
 | 
						|
		unset($payloadArray["ieeeAddress"]);
 | 
						|
		unset($payloadArray["type"]);
 | 
						|
 | 
						|
		foreach ($payloadArray as $property => $value)
 | 
						|
		{
 | 
						|
			$str = substr($property, -1);
 | 
						|
			if ($str == "f")
 | 
						|
			{
 | 
						|
				$newProperty = rtrim($property, "f") . "c";
 | 
						|
				$payloadArray[$newProperty] = farenheit2celsius($value);
 | 
						|
			}elseif ($str == "h")
 | 
						|
			{
 | 
						|
				$newProperty = substr($property, 0, -3) . "kmh";
 | 
						|
				$payloadArray[$newProperty] =  mph2kmh($value);
 | 
						|
			}
 | 
						|
			if ($property == "baromin")
 | 
						|
			{
 | 
						|
				$newProperty = "barominmb";
 | 
						|
				$payloadArray["barominmb"] =  millibars($value);
 | 
						|
			}
 | 
						|
		}
 | 
						|
		//$device = getDeviceByFriendlyname($topicName, $fn, $payloadArray, true);
 | 
						|
		if (!empty($payloadArray))
 | 
						|
		{
 | 
						|
			changeDevice($topicName, $fn, $device["device"], $payloadArray);
 | 
						|
		}
 | 
						|
	}
 | 
						|
};
 | 
						|
$topics["pws2mqtt"]->callback = "pws2mqttCallback";
 | 
						|
$topics["pws2mqtt"]->timeOut = 3;           //timeOut in minutes
 | 
						|
$topics["pws2mqtt"]->lastSeen = time();
 | 
						|
 | 
						|
if (!is_callable("pws2mqttGetList"))
 | 
						|
{
 | 
						|
	function pws2mqttGetList()
 | 
						|
	{
 | 
						|
			publish("pws2mqtt", array("",""), "get" );
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
?>
 |