egregius.be

Urban Exploration, PHP and others…

LUA Pass2PHP v2.0 Small example and explanation

Based on the script LUA Pass2PHP v2.0 a small example:

2 pirs in this script. They have idx 123 and idx 234. In the $events array we define wich function is called when this $idx is the changed device.
Have a look at LUA Pass2PHP v2.0 for many more examples and conditional switching.

Some explanation about the code:
All data retrieved from the lua device script is decoded in to arrays:

  • $a is the status of the changed device
  • $s array of all device statusses. Ex: $s[‘pirhall’]
  • $i array of all device idxs. Ex: $i[‘hall’]
  • $t array of all last update times off devices. Ex: strtotime($t[‘hall’]) gives the unix timestamp.
#!/usr/bin/php
<?php error_reporting(E_ALL);ini_set("display_errors","on");date_default_timezone_set('Europe/Brussels');
define('api',"http://127.0.0.1:8084/");
$t=microtime(true);$micro=sprintf("%03d",($t-floor($t))*1000);define('stamp',strftime("%Y-%m-%d %H:%M:%S.", $t).$micro);
$c=json_decode(base64_decode($argv[1]),true);$s=json_decode(base64_decode($argv[2]),true);$i=json_decode(base64_decode($argv[3]),true);$t=json_decode(base64_decode($argv[4]),true);$a=$s[key($c)];$devidx=$i[key($c)];
//Create the array of events. Wich idx calls wich function?
$events=array(123=>'pirgarage',234=>'pirhall');
if(isset($events[$devidx]))call_user_func($events[$devidx]);
//Start of user functions
function pirgarage(){
	global $a,$s,$i,$t;
	if($a=="On"){
		sw($i['garage'],'On');
	}
}
function pirhall(){
	global $a,$s,$i,$t;
	if($a=="On"){
		sw($i['hall'],'On');
	}
}
//End of user functions
// ========== FUNCTIONS ==========
/* sw switches $idx on/off. If no action is provided a toggle is made. $info is optional logging */
function sw($idx,$action="",$info=""){
	$t=microtime(true);$micro=sprintf("%03d",($t-floor($t))*1000);$stamp=strftime("%Y-%m-%d %H:%M:%S.", $t).$micro;
	print $stamp."          Switch ".$idx." (".ucfirst($info).") ".strtoupper($action)."
";
	if(empty($action)) curl(api."json.htm?type=command&param=switchlight&idx=".$idx."&switchcmd=Toggle");
	else curl(api."json.htm?type=command&param=switchlight&idx=".$idx."&switchcmd=".$action);
	usleep(400000);
}
/* sl sets dimmer $idx to level $level. $info is optional logging */
function sl($idx,$level,$info=""){
	$t=microtime(true);$micro=sprintf("%03d",($t-floor($t))*1000);$stamp=strftime("%Y-%m-%d %H:%M:%S.", $t).$micro;
	print $stamp."        Set Level ".$idx." ".ucfirst($info)." ".$level."
";
	curl(api . "json.htm?type=command&param=switchlight&idx=".$idx."&switchcmd=Set%20Level&level=".$level);
	usleep(400000);
}
/* ud updates a device $idx with $nvalue and $svalue. $info is optional logging */
function ud($idx,$nvalue,$svalue,$info=""){
	$t=microtime(true);$micro=sprintf("%03d",($t-floor($t))*1000);$stamp=strftime("%Y-%m-%d %H:%M:%S.", $t).$micro;
	if(!in_array($idx, array(395,532,534))) print $stamp."  --- UPDATE ".$idx." ".$info." ".$nvalue." ".$svalue."
";
	curl(api.'json.htm?type=command&param=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
	usleep(400000);
}
function curl($url){dl("curl.so");$headers=array('Content-Type: application/json',);$ch=curl_init();curl_setopt($ch,CURLOPT_URL,$url);curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);$data=curl_exec($ch);curl_close($ch);return $data;}