egregius.be

Urban Exploration, PHP and others…

LUA Pass2PHP v2.1 minimal script

Please read the information on LUA Pass2PHP v2.1 complete script for information about new stuff in 2.1
This script only contains the minimal stuff to get you started.
In this example a PIR with idx 123 will switch a light on.

<?php
$c=json_decode(base64_decode($_REQUEST['c']),true);
$s=json_decode(base64_decode($_REQUEST['s']),true);
$i=json_decode(base64_decode($_REQUEST['i']),true);
$t=json_decode(base64_decode($_REQUEST['t']),true);
$a=$s[key($c)];$devidx=$i[key($c)];
$events=array(
	123=>'pirhall'
	);
if(isset($events[$devidx]))$events[$devidx]();
//START USERFUNCTIONS
function pirhall(){
	global $a,$s,$i,$t;
	if($a=="On"){
		if($s['lighthall']=='Off')sw($i['hall'],'On','hall by pir',1000);
	}
}
//END USERFUNCTIONS. Only change IP:PORT and ios,sms account details below
function sw($idx,$action="",$info="",$Usleep=600000){lg("SWITCH ".$action." ".$info);if(empty($action))curl("http://127.0.0.1:8084/json.htm?type=command&param=switchlight&idx=".$idx."&switchcmd=Toggle");else curl("http://127.0.0.1:8084/json.htm?type=command&param=switchlight&idx=".$idx."&switchcmd=".$action);usleep($Usleep);}
function sl($idx,$level,$info="",$Usleep=600000){lg("SETLEVEL ".$level." ".$info);curl("http://127.0.0.1:8084/json.htm?type=command&param=switchlight&idx=".$idx."&switchcmd=Set%20Level&level=".$level);usleep($Usleep);}
function ud($idx,$nvalue,$svalue,$info="",$Usleep=600000){if(!in_array($idx, array(395,532,534)))lg("UPDATE ".$nvalue." ".$svalue." ".$info);curl('http://127.0.0.1:8084/json.htm?type=command&param=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);usleep($Usleep);}
function double($idx,$action,$comment='',$wait=4000000){sw($idx,$action,$comment,$wait);sw($idx,$action,$comment.' repeat',0);}
function lg($msg){curl('http://127.0.0.1:8084/json.htm?type=command&param=addlogmessage&message='.urlencode('=> '.$msg));}
function curl($url){$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);curl_setopt($ch,CURLOPT_TIMEOUT,5);$data=curl_exec($ch);curl_close($ch);return $data;}