Page 9 of 76
Re: LUA Pass2php
Posted: Friday 23 December 2016 11:50
by sincze
Egregius wrote:Correct, by just concatenating all statusses in to a csv string I avoided the need of json and base64 wich seems to save about 10msec of response time.
Currently I'm at 21msec by just sending devicechanged and otherdevices but that requires quite some recoding of scripts to store all idx's and lastupdate times in APCu cache.
For the switches with a space in them, probably this will do:
Code: Select all
foreach($c as $device=>$status)
if(!empty($status)){
$devicer=str_replace(" ","_",$device);
if(false!==stream_resolve_include_path('pass2php/'.$device.'.php'))include 'pass2php/'.$device.'.php';
elseif(false!==stream_resolve_include_path('pass2php/'.$devicer.'.php'))include 'pass2php/'.$devicer.'.php';
}
include 'pass2php/__CRON.php';
tnx, Some testing to do for me tonight, will report the results

Re: LUA Pass2php
Posted: Friday 23 December 2016 14:46
by sincze
Mmm so I modified my pass2php.php to:
Code: Select all
$c=ex($_REQUEST['c']);
$s=ex($_REQUEST['s']);
$i=ex($_REQUEST['i']);
$t=ex($_REQUEST['t']);
foreach($c as $device=>$status)
if(!empty($status)){
$devicer=str_replace(" ","_",$device);
lg('Step 1: Pass2PHP de functie is: '.key($device));
lg('Step 2: Pass2PHP de functie REVEERSED is: '.key($devicer));
if(false!==stream_resolve_include_path('pass2php/'.$device.'.php'))include 'pass2php/'.$device.'.php';
elseif(false!==stream_resolve_include_path('pass2php/'.$devicer.'.php'))include 'pass2php/'.$devicer.'.php';
}
//include 'pass2php/__CRON.php';
include 'pass2php/Solar_PHP.php';
and the device.lua to:
Code: Select all
print("Start of script device");
c = ''
s = ''
i = ''
t = ''
for k,v in pairs(devicechanged) do c = c..k.."|"..v.."#" end
for k,v in pairs(otherdevices) do s = s..k.."|"..v.."#" end
for k,v in pairs(otherdevices_idx) do i = i..k.."|"..v.."#" end
for k,v in pairs(otherdevices_lastupdate) do t = t..k.."|"..v.."#" end
print("End of script device");
os.execute('curl -s --data "c='..c..'&s='..s..'&i='..i..'&t='..t..'" http://192.168.2.10/php/pass2php.php &')
commandArray={}
return commandArray
The only thing that shows up in the domoticz log is:
Code: Select all
2016-12-23 14:44:45.590 LUA: Start of script device
2016-12-23 14:44:45.602 LUA: End of script device
No .php is executed. It seems something is wrong with the pass2php.php file I would say?
Calling the pass2php.php from a webbrowser ends according to my expectations:
Code: Select all
Notice: Undefined index: c in /var/www/php/pass2php.php on line 19
Notice: Undefined index: s in /var/www/php/pass2php.php on line 20
Notice: Undefined index: i in /var/www/php/pass2php.php on line 21
Notice: Undefined index: t in /var/www/php/pass2php.php on line 22
Any idea? I also added function ex() to my php code, that one I was missing as well.
Re: LUA Pass2php
Posted: Friday 23 December 2016 15:16
by Egregius
the ip address is good? Better to use 127.0.0.1 if the webserver is on the same server as domoticz.
If you want to log what function is executed I would do it like this:
Code: Select all
lg('Step 1: Started');
$c=ex($_REQUEST['c']);
$s=ex($_REQUEST['s']);
$i=ex($_REQUEST['i']);
$t=ex($_REQUEST['t']);
foreach($c as $device=>$status)
lg('Step 2: Changed device = '.$device);
if(!empty($status)){
$devicer=str_replace(" ","_",$device);
if(false!==stream_resolve_include_path('pass2php/'.$device.'.php')){
lg('Step 3: executing file '.$device.'.php');
include 'pass2php/'.$device.'.php';
}elseif(false!==stream_resolve_include_path('pass2php/'.$devicer.'.php')){
lg('Step 3: executing file '.$devicer.'.php');
include 'pass2php/'.$devicer.'.php';
}
}
//include 'pass2php/__CRON.php';
include 'pass2php/Solar_PHP.php';
lg('Step 4: Ready!');
By adding multiple lg's (or even ordanary print/echo lines) it's easier to debug where it goes wrong.
Another nice one herefore is:
lg(__LINE__);
Wich will print the linenumber where you called the lg function (had this morning a script that failed and needed to ad about 40 of them to find out where it crashed...).
Put a double // before the lg lines ones it works to avoid unnescessary calls.
Re: LUA Pass2php
Posted: Friday 23 December 2016 15:37
by sincze
Something must be wrong.
I don't even receive the :
"lg('Step 1: Started');" in my log...
127,0,0,1 <> 192.168.2.10 does not seem to make a difference, both are the same machine indeed.
Code: Select all
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
print("Start of script device");
c = ''
s = ''
i = ''
t = ''
for k,v in pairs(devicechanged) do c = c..k.."|"..v.."#" end
for k,v in pairs(otherdevices) do s = s..k.."|"..v.."#" end
for k,v in pairs(otherdevices_idx) do i = i..k.."|"..v.."#" end
for k,v in pairs(otherdevices_lastupdate) do t = t..k.."|"..v.."#" end
print("End of script device");
-- os.execute('curl -s --data "c='..c..'&s='..s..'&i='..i..'&t='..t..'" http://192.168.2.10/php/pass2php.php &')
-- os.execute('curl -s --data "c='..c..'&s='..s..'&i='..i..'&t='..t..'" http://127.0.0.1/php/pass2php.php &')
print(os.capture('curl -s --data "c='..c..'&s='..s..'&i='..i..'&t='..t..'" http://127.0.0.1/php/pass2php.php &'));
print("Curl ended");
commandArray={}
return commandArray
The code above will show me if something went wrong when executing the pass2php... unfortunately the output is empty so no severe errors...
I created a spare pass3php.php with your latest GITHUB code in it and that seems to work.
Code: Select all
2016-12-23 15:59:58.421 --->> Step 1: Started
2016-12-23 15:59:58.430 --->> Step 2: Changed device = TH7_Slaapkamer_Humidity
2016-12-23 15:59:58.433 --->> Step 2: Changed device = TH7_Slaapkamer
2016-12-23 15:59:58.435 --->> Step 2: Changed device = TH7_Slaapkamer_Dewpoint
2016-12-23 15:59:58.438 --->> Step 2: Changed device = TH7_Slaapkamer_Temperature
2016-12-23 15:59:58.440 --->> Step 2: Changed device =
2016-12-23 15:59:58.443 --->> Step 4: Ready!
There must be an error somewhere in my pass2php..... however if I remove your code revert back to.. JSON /LUA eg.. my pass2php works just fine.
Re: LUA Pass2php
Posted: Friday 23 December 2016 16:05
by Egregius
And in ssh curl -s
http://127.0.0.1/php/pass2php.php?
I don't see anything wrong in the code you posted.
But if the step 1 isn't printed it must be in the lua, no?
Btw, how do you look at your domoticz logfile? There's a difference between in browser and with tail -f domoticz.log... Browser doesn't show everything.
Re: LUA Pass2php
Posted: Friday 23 December 2016 16:13
by sincze
curl-s results
<br />
<b>Notice</b>: Undefined index: c in <b>/var/www/php/pass2php.php</b> on line <b>23</b><br />
<br />
<b>Notice</b>: Undefined index: s in <b>/var/www/php/pass2php.php</b> on line <b>24</b><br />
<br />
<b>Notice</b>: Undefined index: i in <b>/var/www/php/pass2php.php</b> on line <b>25</b><br />
<br />
<b>Notice</b>: Undefined index: t in <b>/var/www/php/pass2php.php</b> on line <b>26</b><br />
Re: LUA Pass2php
Posted: Friday 23 December 2016 16:15
by Egregius
And also no step 1 in the logfiles?
Looked at the logfile in SSH?
I always have the logfile open in ssh, even created a alias in.profile for it so I only need to type dm

alias dm="tail -f -n 250 /volume1/appstore/domoticz/var/domoticz.log -s 0.1"
Re: LUA Pass2php
Posted: Friday 23 December 2016 17:03
by sincze
I quited debugging
I just started over... with your latest git production in pass4php.php
Code: Select all
2016-12-23 16:54:13.999 User: Admin initiated a switch command (621/ZonweringAuto/On)
2016-12-23 16:54:14.054 LUA: End of script device
2016-12-23 16:54:14.065 LUA: Curl ended
2016-12-23 16:54:14.000 (Virtual Device) Light/Switch (ZonweringAuto)
2016-12-23 16:54:14.146 --->> Step 1: Started
2016-12-23 16:54:14.155 --->> Step 2: Changed device = ZonweringAuto
2016-12-23 16:54:14.158 --->> Step 2: Changed device =
2016-12-23 16:54:14.160 --->> Step 4: Ready!
Step 3 is missing so now we have to figure out why... it is not seeing the file..
Re: LUA Pass2php
Posted: Friday 23 December 2016 17:27
by Egregius
The file pass2php/ZonweringAuto.php exists?
Were you already at PHP7 or PHP5.6?
Maybe if(false!==stream_resolve_include_path('pass2php/'.$device.'.php')) doesn't work in PHP5.6 and you need to previous if(file_exists('pass2php/'.$device.'.php'))
Re: LUA Pass2php
Posted: Friday 23 December 2016 17:44
by sincze
still at 5.6 on my ARM based machine

Re: LUA Pass2php
Posted: Friday 23 December 2016 17:54
by Egregius
Does it work with file_exists?
Hmm, according to
http://php.net/manual/en/function.strea ... e-path.php it should work.
Re: LUA Pass2php
Posted: Friday 23 December 2016 18:22
by sincze
Egregius wrote:The file pass2php/ZonweringAuto.php exists?
Were you already at PHP7 or PHP5.6?
Maybe if(false!==stream_resolve_include_path('pass2php/'.$device.'.php')) doesn't work in PHP5.6 and you need to previous if(file_exists('pass2php/'.$device.'.php'))
Function should be supported
http://php.net/manual/en/function.strea ... e-path.php
Code: Select all
<?php
var_dump(stream_resolve_include_path("pass2php.php"));
var_dump(stream_resolve_include_path("pass2php/ZonweringAuto.php"));
?>
Results into:
Code: Select all
string(25) "/var/www/php/pass2php.php" string(39) "/var/www/php/pass2php/ZonweringAuto.php"
So that should be okay, Let me check the code again for a mistaken '/' maybe somewhere.
Re: LUA Pass2php
Posted: Friday 23 December 2016 18:58
by sincze
Code: Select all
lg('Step 1: Started');
$c=ex($_REQUEST['c']);
$s=ex($_REQUEST['s']);
$i=ex($_REQUEST['i']);
$t=ex($_REQUEST['t']);
foreach($c as $device=>$status)
lg('Step 2: Changed device = '.$device);
$sresult0=stream_resolve_include_path('pass2php/'.$device.'.php');
lg('the result is: '.$sresult0);
if(!empty($status))
{
$devicer=str_replace(" ","_",$device);
//lg('Step 2a: File to look for is: pass2php'.$device.'.php');
// lg('Step 2b: File to look for is: pass2php/'.$devicer.'.php');
$sresult1=stream_resolve_include_path('pass2php/'.$device.'.php');
$sresult2=stream_resolve_include_path('pass2php/'.$devicer.'.php');
lg('the result is: '.$sresult1);
lg('the result is: '.$sresult2);
if(false!==stream_resolve_include_path('pass2php/'.$device.'.php')){
lg('Step 3: executing file '.$device.'.php');
include 'pass2php/'.$device.'.php';
}elseif(false!==stream_resolve_include_path('pass2php/'.$devicer.'.php')){
lg('Step 3: executing file '.$devicer.'.php');
include 'pass2php/'.$devicer.'.php';
}
}
//include 'pass2php/__CRON.php';
include 'pass2php/Solar_PHP.php';
lg('Step 4: Ready!');
I think something else is wrong.
Code: Select all
2016-12-23 18:56:26.942 User: Admin initiated a switch command (621/ZonweringAuto/On)
2016-12-23 18:56:26.943 (Virtual Device) Light/Switch (ZonweringAuto)
2016-12-23 18:56:27.101 --->> Step 1: Started
2016-12-23 18:56:27.110 --->> Step 2: Changed device = ZonweringAuto
2016-12-23 18:56:27.113 --->> Step 2: Changed device =
2016-12-23 18:56:27.116 --->> the result is:
2016-12-23 18:56:27.119 --->> Step 4: Ready!
Why whould the second changed device be empty? As a result maybe it will not find a ,php file as well.
The result = is also empty... at least I would have expected "false" as a return from : stream_resolve_include_path
Re: LUA Pass2php
Posted: Friday 23 December 2016 20:57
by Egregius
You forgot line 29 of
https://github.com/Egregius/LUA-Pass2PH ... ss2php.php wich checks if result is filled.
This is because there's nothing in the lua file to check if it was the last item in the devicechanged table. Therefor the passed string always ends with # resulting in an empty value in the $c array.
The foreach $c was added because devicechanged sometimes holds multiple devices.
But, there's smething else wrong. Maybe try with a absolute path? Maybe I have that folder in the include path of php.ini.
Re: LUA Pass2php
Posted: Friday 23 December 2016 21:03
by Egregius
http://stackoverflow.com/questions/1932 ... rms-better
Indicates that stream_resolve... is slower, my test showed otherwise. Same page says is_file should be faster, will test that one later.
Re: LUA Pass2php
Posted: Friday 23 December 2016 21:30
by sincze
Egregius wrote:
But, there's smething else wrong. Maybe try with a absolute path? Maybe I have that folder in the include path of php.ini.
Code: Select all
if(file_exists('pass2php/'.key($c).'.php')) // Added this function to have individual files ;-) .php directory
{ include 'pass2php/'.key($c).'.php';
$c2();
if($debug3=="true")
{
lg('Step 4: Pass2PHP running de file bestaat: '.key($c));
lg('Step 5: Pass2PHP running echo file functie is: '.$c2);
}
}
elseif(file_exists('pass2php/'.($r).'.php')) // Added this function to have individual files ;-) .php directory
{ include 'pass2php/'.($r).'.php';
$r();
if($debug3=="true")
{
lg('Step 6: Pass2PHP running echo file functie is REVERSED: '.$r);
}
}
As this is working just fine It should not be a path issue right??
Re: LUA Pass2php
Posted: Friday 23 December 2016 21:52
by Egregius
Couldn't wait
Did a small test again with this code:
Code: Select all
<?php
$start=microtime(true);
if(false!==stream_resolve_include_path('/volume1/web/secure/pass2php/pirinkom.php')) $result='OK';else $result='NOK';
$total=microtime(true)-$start;
echo number_format(($total*1000),5) .' absolute stream_resolve_include_path '.$result.'<br>';
$start=microtime(true);
if(false!==stream_resolve_include_path('pass2php/pirkeuken.php')) $result='OK';else $result='NOK';
$total=microtime(true)-$start;
echo number_format(($total*1000),5) .' relative stream_resolve_include_path '.$result.'<br>';
$start=microtime(true);
if(file_exists('/volume1/web/secure/pass2php/pirliving.php')) $result='OK';else $result='NOK';
$total=microtime(true)-$start;
echo number_format(($total*1000),5) .' absolute file_exists '.$result.'<br>';
$start=microtime(true);
if(file_exists('pass2php/pirgarage.php')) $result='OK';else $result='NOK';
$total=microtime(true)-$start;
echo number_format(($total*1000),5) .' relative file_exists '.$result.'<br>';
$start=microtime(true);
if(is_file('/volume1/web/secure/pass2php/raamtobi.php')) $result='OK';else $result='NOK';
$total=microtime(true)-$start;
echo number_format(($total*1000),5) .' absolute is_file '.$result.'<br>';
$start=microtime(true);
if(is_file('pass2php/pirhall.php')) $result='OK';else $result='NOK';
$total=microtime(true)-$start;
echo number_format(($total*1000),5) .' relative is_file '.$result.'<br>';
Output should be this:
0.03386 absolute stream_resolve_include_path OK
0.03219 relative stream_resolve_include_path OK
0.17405 absolute file_exists OK
0.17095 relative file_exists OK
0.35405 absolute is_file OK
0.32997 relative is_file OK
I used 6 different existing files to avoid file caching OS things.
Personnally I find it strange that relative files are in each function a little bit faster but that's no noticable number.
The difference between the functions... Clearly in my case the stream_resolve... is the fastest.
Test in on your system, check if each line ends with OK, telling you the function works and the file is found. Use the fastest of them.
Wichever you choose, this will not be the biggest change, we're talking about 0,3 milliseconds here.
Re: LUA Pass2php
Posted: Friday 23 December 2016 21:53
by Egregius
sincze wrote:As this is working just fine It should not be a path issue right??
Not sure, different function could handle files differently.
Still doing research for other ways to Rome...
Re: LUA Pass2php
Posted: Friday 23 December 2016 22:06
by Egregius
Learned something new, sharing and discussing is good for knowledge

adding a @ in front of the function silently ignores the error.
Result is that we can just include the file without checking it's existance.
0.00906 absolute @include existing. OK
0.01216 relative @include existing. OK
0.25487 @include non existing. OK
Without the @:
Warning: include(/volume1/web/secure/pass2php/tobiZ.php): failed
Warning: include(/volume1/web/secure/pass2php/tobiZ_Utility.php): failed
Warning: include(/volume1/web/secure/pass2php/.php): failed
With the @ no error at all with this file selector:
Code: Select all
foreach($c as $device=>$status){
@include '/volume1/web/secure/pass2php/'.$device.'.php';
cset('t'.$device,time);
}
Re: LUA Pass2php
Posted: Saturday 24 December 2016 13:55
by sincze
And the numbers are in of the 6 lookups of different .php files.
Code: Select all
0.13304 absolute stream_resolve_include_path OK
0.08202 relative stream_resolve_include_path OK
0.03505 absolute file_exists OK
0.02694 relative file_exists OK
0.12684 absolute is_file OK
0.02098 relative is_file OK
So last resort will be to look at the row 29 ... as all seems to be working just fine with php 5.6
is_file relative seems to be the fastest.