Pass2PHP

ropske
Posts: 225
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: Pass2PHP

Post by ropske » Monday 27 February 2017 21:16

Yes, completely migrated into php and working :D
sincze wrote:
ropske wrote:

Code: Select all

#region Wasmachine Script
	$wasmachine_status_var = cget('swasmachine_status_var');
	$wasmachine_counter_var = cget('swasmachine_counter_var');
	$wasmachine_manuallyEnd_var = cget('swasmachine_manuallyEnd_uservar');
	$wasmachine_energy_consumption = cget('sVerbruik_Wasmachine');
	$idle_minutes = 5;
	$consumption_upper = 20;
	$consumption_lower = 3;
	$consumption_standby = 1;
	$wasmachinecurrentPowerUsage = cget('sVerbruik_Wasmachine');
	
	//Virtual switch is off, but consumption is higher than configured level, so washing has started
	if(($wasmachinecurrentPowerUsage>$consumption_upper)&&($wasmachine_status_var==0)){
		cset('swasmachine_status_var',1);
		cset('twasmachine_status_var',time);
		cset('swasmachine_manuallyEnd_uservar',0);
		lg('Huidig verbruik wasmachine ('.$wasmachinecurrentPowerUsage.'W) is boven ingestelde waarde ('.$consumption_upper.'W), dus wasmachine is gestart!');
		cset('swasmachine_counter_var',$idle_minutes);
		sw(cget('iStatus_Wasmachine'),'Set%20Level&level=10','Set Status wasmachine on Werkend');
	}
	//Washing machine is not using a lot of energy, check the counter
	if(($wasmachinecurrentPowerUsage<$consumption_lower)&&($wasmachine_status_var==1)){
		cset('swasmachine_counter_var',$wasmachine_counter_var-1);
		$wasmachine_counter_var = cget('swasmachine_counter_var');
		lg('Huidig verbruik wasmachine ('.$wasmachinecurrentPowerUsage.'W) is lager dan idle waarde ('.$consumption_lower.'W), wasmachine is klaar of bijna klaar');
		lg('Herbereken teller, oude waarde: '.$wasmachine_counter_var.' minuten');
	}
	else{
		if(($wasmachine_counter_var!=$idle_minutes)&&$wasmachine_status_var==1){
			cset('swasmachine_counter_var',$idle_minutes);
			lg('Resetting wasmachine timer');
		}
	}
	//Washingmachine is done
	if($wasmachine_status_var==1&&$wasmachine_counter_var==0){
		//lg('wasmachine is klaar');
		//lg('Huidig verbruik wasmachine '.$wasmachine_energy_consumption.'W');
		$wasmachine_tijdsduur=(time-cget('twasmachine_status_var'));
		lg('Wasmachine is klaar, gelieve deze te ledigen!  Tijdsduur bedraagt: '.($wasmachine_tijdsduur/60).' minuten');
		cset('swasmachine_status_var',0);
		sw(cget('iStatus_Wasmachine'),'Set%20Level&level=20','Set Status wasmachine on klaar');
		$msg='Wasmachine is klaar met werken!  Tijdsduur bedraagt: '.($wasmachine_tijdsduur/60).' minuten';
		telegram($msg,true);
	}
	//wasmachine is switched off (manually)
	if(($wasmachinecurrentPowerUsage<$consumption_standby)&&($wasmachine_status_var==0)&&($wasmachine_manuallyEnd_var==0)){
		sw(cget('iStatus_Wasmachine'),'Set%20Level&level=0','Set Status wasmachine on Off');
		cset('swasmachine_manuallyEnd_uservar',1);
	}
	
	#end
@sincze: here's an example for my 'wasmachine' :p
got the same running for droogkast and vaatwas :p
Tnx @Robske ;-) really appreciated. Completely migrated to pass2php ??? :D Once it is running it keeps on going.

User avatar
sincze
Posts: 713
Joined: Monday 02 June 2014 22:46
Target OS: Linux
Domoticz version: 3.8153
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Saturday 04 March 2017 12:40

Egregius wrote: About influxdb: can't you do anything with the MySQL examples? If a http poller can do it, it must be possible.
I created the following:

Code: Select all

function influxdb($device,$value)
{
// FAMOUS STRING REPLACEMENT UTILITY
    if (strpos($device, " ") !== false) { $device=str_replace(" ","_",$device); }
// ENDOF FAMOUS STRING REPLACEMENT UTILITY

    $postdata = ("device_$device value=$value ".system('date +%s%N'));
    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-Type:application/octet-stream',
            'content' => $postdata
            )
    );

    $context  = stream_context_create($opts);
    $result = file_get_contents(influxmachine.'/write?db=domoticz', false, $context);
    lg('InluxDB Notification'.$result);
}
I can now add the following line to a device.php file, for example the boiler.

Code: Select all

influxdb($device,$s['Boiler Water Temperature']);
It will show up in a InfluxDB and in Grafana with the correct date/time. Don't know if this is the smartest move adding these lines to all my devices.php however it does work and stores every slight change in the DB. Suggestions??
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
Egregius
Posts: 2416
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Saturday 04 March 2017 13:45

Depends on the database structure and how you want to read it.
I store all temps in one record, one field for every temp sensor. That way I don't need to store idx or name and I only have one timestamp.

User avatar
sincze
Posts: 713
Joined: Monday 02 June 2014 22:46
Target OS: Linux
Domoticz version: 3.8153
Location: Netherlands
Contact:

Re: RE: Re: Pass2PHP

Post by sincze » Saturday 04 March 2017 13:58

Egregius wrote:Depends on the database structure and how you want to read it.
I store all temps in one record, one field for every temp sensor. That way I don't need to store idx or name and I only have one timestamp.
Influx is time based and does not really care about structure :) just dump a value with time stamp and voila. Records are only kept 180 days and auto removed. Not for future reference.

Sent from my SM-G925F using Tapatalk
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
Egregius
Posts: 2416
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Saturday 04 March 2017 17:05

Then I prefer MySQL :P

User avatar
sincze
Posts: 713
Joined: Monday 02 June 2014 22:46
Target OS: Linux
Domoticz version: 3.8153
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Saturday 04 March 2017 18:53

Egregius wrote:Then I prefer MySQL :P
I can imagine. :D. Call me a die-hard mysql user as well. (Used to have DomotiGa as home domotica controller).

In this case I thought why not use it to create a nice overview of... something as some kind of monitor to display on tablet or so.
Egregius_Grafana.JPG
Egregius_Grafana.JPG (188.29 KiB) Viewed 1407 times
I used to have some crashes (too many open files) when I did this in Domoticz directly. You know via More Options -> Data Push.
Now I thought about the power of php.. Let's see what happens..

Thanks again for your help. :D
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

rjblake
Posts: 111
Joined: Friday 21 October 2016 9:25
Target OS: NAS (Synology & others)
Domoticz version:
Location: Netherlands
Contact:

Re: Pass2PHP

Post by rjblake » Monday 06 March 2017 23:34

I'm trying to use your function RefreshZwave($node) to refresh my Qubino Relay temperature (it only reports on temp changes of 0.1deg and sometimes seems to just plain go to sleep for a long time), but seem to have a problem with a constant 'time'. I'm using php70 and apcu seems to be there, but if I report errors, I always get a "PHP Notice: Use of undefined constant time - assumed 'time' in /volume1/web/domoticzstuff/qubino2.php on line 36". As result, my $last is always less than time-3600. Is this constant defined elsewhere in Pass2PHP?

Function as used called with "php70 -f myphp.php 8"

Code: Select all

<?php
$argument1 = $argv[1];
$refresh = RefreshZwave($argument1);

function RefreshZwave($node){
	$last=apcu_fetch('refresh'.$node);
	apcu_store('refresh'.$node,time);
	if($last<time-3600){
		$devices=json_decode(file_get_contents('http://192.168.1.1:8084/json.htm?type=openzwavenodes&idx=9',false),true);
		foreach($devices['result'] as $devozw)
			if($devozw['NodeID']==$node){
				$device=$devozw['Description'].' '.$devozw['Name'];
				break;
			}
		print strftime("%Y-%m-%d %H:%M:%S",time()).'   => Refreshing node '.$node.' '.$device.PHP_EOL;
		for($k=1;$k<=5;$k++){
			$result=file_get_contents('http://192.168.1.1:8084/ozwcp/refreshpost.html',false,stream_context_create(array('http'=>array('header'=>'Content-Type: application/x-www-form-urlencoded\r\n','method'=>'POST','content'=>http_build_query(array('fun'=>'racp','node'=>$node)),),)));
			if($result==='OK')break;
			sleep(1);
		}
		if(apcu_fetch('timedeadnodes')<time-298){
			apcu_store('timedeadnodes',time);
			foreach($devices as $node=>$data){
				if($node=="result"){
					foreach($data as $index=>$eltsNode){
						if($eltsNode["State"]=="Dead"&&!in_array($eltsNode['NodeID'],array(57))){
							telegram('Node '.$eltsNode['NodeID'].' '.$eltsNode['Description'].' ('.$eltsNode['Name'].') marked as dead, reviving '.ZwaveCommand($eltsNode['NodeID'],'HasNodeFailed'));
							ControllerBusy(10);
							ZwaveCommand(1,'Cancel');
						}
					}
				}
			}
		}
	}
}
?>

User avatar
Egregius
Posts: 2416
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Monday 06 March 2017 23:57

It should be on the first line of pass2php

Code: Select all

define('time',$_SERVER['REQUEST_TIME']);

rjblake
Posts: 111
Joined: Friday 21 October 2016 9:25
Target OS: NAS (Synology & others)
Domoticz version:
Location: Netherlands
Contact:

Re: Pass2PHP

Post by rjblake » Wednesday 08 March 2017 17:34

Aarg! My bad - missed that. Need more coffee or more sleep, or both :)

BarryT
Posts: 222
Joined: Tuesday 31 March 2015 22:06
Target OS: Raspberry Pi
Domoticz version: beta
Location: east netherlands
Contact:

Re: Pass2PHP

Post by BarryT » Sunday 16 April 2017 15:55

Egregius, is it so simple as install the scripts, and it works, or does i need to edit the files to all devices/switches?
I see names like:

Code: Select all

if($action=='On'){
		$items=array('eettafel','zithoek','kamer','tobi');
		foreach($items as $item)if($s[$item]!='On')sl($i[$item],100,$item);
		$items=array('bureel','tvled','kristal','wasbak','keuken','kookplaat','werkblad','inkom','hall','lichtbadkamer1');
		foreach($items as $item)if($s[$item]!='On')sw($i[$item],'On',$item);
I have 100+ devices/switches, wich works perfectly atm, but the on/off times are sometimes over 3 or more secs..
Also 7 pir sensors and 16 dimmers..
I was finding a way to make it all working a bit faster, as it looks like it should do with what you made, but i dont have the time to "edit" all devices/names/etc :(

Thanks
6x Raspberry 3B
A lot of scripts, switches and sensors :)

User avatar
Egregius
Posts: 2416
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Sunday 16 April 2017 19:04

You don't need to change your devices, only the script.

BarryT
Posts: 222
Joined: Tuesday 31 March 2015 22:06
Target OS: Raspberry Pi
Domoticz version: beta
Location: east netherlands
Contact:

Re: Pass2PHP

Post by BarryT » Sunday 16 April 2017 19:08

Egregius wrote:You don't need to change your devices, only the script.
Yeah, i meant in your script ofc..
That would be a lot of work then!
6x Raspberry 3B
A lot of scripts, switches and sensors :)

User avatar
Egregius
Posts: 2416
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Sunday 16 April 2017 20:01

Start with one pir, feel the difference and then add the rest one by one.
If you don't want to spent some time in then don't bother starting with this.

BarryT
Posts: 222
Joined: Tuesday 31 March 2015 22:06
Target OS: Raspberry Pi
Domoticz version: beta
Location: east netherlands
Contact:

Re: Pass2PHP

Post by BarryT » Sunday 16 April 2017 20:36

Egregius wrote:Start with one pir, feel the difference and then add the rest one by one.
If you don't want to spent some time in then don't bother starting with this.
I will do that for sure, thanks :)
Ofcourse i will spend many time in it but not for now.

Good job!
6x Raspberry 3B
A lot of scripts, switches and sensors :)

User avatar
sincze
Posts: 713
Joined: Monday 02 June 2014 22:46
Target OS: Linux
Domoticz version: 3.8153
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Monday 17 April 2017 13:28

Egregius wrote:Start with one pir, feel the difference and then add the rest one by one.
If you don't want to spent some time in then don't bother starting with this.
It is worth it :D .
Took me my full christmas holiday :shock: :shock:

But now it responds like crazy. Still have to add my Zwave stuff (bought after christmas) and network related things.
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

ropske
Posts: 225
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: Pass2PHP

Post by ropske » Monday 17 April 2017 13:29

sincze: you still havent added your zwave?
shame!
:lol:

User avatar
sincze
Posts: 713
Joined: Monday 02 June 2014 22:46
Target OS: Linux
Domoticz version: 3.8153
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Monday 17 April 2017 15:20

ropske wrote:sincze: you still havent added your zwave?
shame!
:lol:
Whoeps.
Oh no I was wrong... I use them alright but I don't use the functions RefreshZwave e.g.
Motion detection with a zwave sensor is really really fast.

It was YOU :lol: that got me into the z-wave mess :D when your wasmachine monitoring flashed by :D
I even wrote a function to put the received data directly into my influxDB :shock:
yeah who would guess that i was able to do so.

Now I still need to add 'ping' devices as this is still done by domoticz.
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

ropske
Posts: 225
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: Pass2PHP

Post by ropske » Monday 17 April 2017 16:16

:lol: good

you have some new 'functions' added?
i'm just curious :p
maybe some things i can use too :D

User avatar
sincze
Posts: 713
Joined: Monday 02 June 2014 22:46
Target OS: Linux
Domoticz version: 3.8153
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Monday 17 April 2017 21:02

ropske wrote::lol: good

you have some new 'functions' added?
i'm just curious :p
maybe some things i can use too :D
I posted the on the forum some time ago.
Like this one: viewtopic.php?f=64&t=12343&start=380#p122311
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

tuspam
Posts: 37
Joined: Friday 25 December 2015 1:19
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: Pass2PHP

Post by tuspam » Friday 28 April 2017 13:53

I got problem with migrate to pass2PHP

Here's what I did:

sudo apt-get install php5 php5-cli

JSON.lua - file allredy exist
/home/pi/domoticz/scripts/lua/JSON.lua

ee5_base64.lua
/home/pi/domoticz/scripts/lua/ee5_base64.lua

Code: Select all

--[[**************************************************************************]]
-- base64.lua
-- Copyright 2014 Ernest R. Ewert
--
--  This Lua module contains the implementation of a Lua base64 encode
--  and decode library.
--
--  The library exposes these methods.
--
--      Method      Args
--      ----------- ----------------------------------------------
--      encode      String in / out
--      decode      String in / out
--
--      encode      String, function(value) predicate
--      decode      String, function(value) predicate
--
--      encode      file, function(value) predicate
--      deocde      file, function(value) predicate
--
--      encode      file, file
--      deocde      file, file
--
--      alpha       alphabet, term char
--


--------------------------------------------------------------------------------
-- known_base64_alphabets
--
--
local known_base64_alphabets=
{
    base64= -- RFC 2045 (Ignores max line length restrictions)
    {
        _alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
        _strip="[^%a%d%+%/%=]",
        _term="="
    },

    base64noterm= -- RFC 2045 (Ignores max line length restrictions)
    {
        _alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
        _strip="[^%a%d%+%/]",
        _term=""
    },

    base64url= -- RFC 4648 'base64url'
    {
        _alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
        _strip="[^%a%d%+%-%_=]",
        _term=""
    },
}
local c_alpha=known_base64_alphabets.base64
local pattern_strip

--[[**************************************************************************]]
--[[****************************** Encoding **********************************]]
--[[**************************************************************************]]


-- Precomputed tables (compromise using more memory for speed)
local b64e    -- 6 bit patterns to ANSI 'char' values
local b64e_a  -- ready to use
local b64e_a2 -- byte addend
local b64e_b1 -- byte addend
local b64e_b2 -- byte addend
local b64e_c1 -- byte addend
local b64e_c  -- ready to use


-- Tail padding values
local tail_padd64=
{
    "==",   -- two bytes modulo
    "="     -- one byte modulo
}


--------------------------------------------------------------------------------
-- e64
--
--  Helper function to convert three eight bit values into four encoded
--  6 (significant) bit values.
--
--                 7             0 7             0 7             0
--             e64(a a a a a a a a,b b b b b b b b,c c c c c c c c)
--                 |           |           |           |
--  return    [    a a a a a a]|           |           |
--                        [    a a b b b b]|           |
--                                    [    b b b b c c]|
--                                                [    c c c c c c]
--
local function e64( a, b, c )
    -- Return pre-calculated values for encoded value 1 and 4
    -- Get the pre-calculated extractions for value 2 and 3, look them
    -- up and return the proper value.
    --
    return  b64e_a[a],
            b64e[ b64e_a2[a]+b64e_b1[b] ],
            b64e[ b64e_b2[b]+b64e_c1[c] ],
            b64e_c[c]
end


--------------------------------------------------------------------------------
-- encode_tail64
--
--  Send a tail pad value to the output predicate provided.
--
local function encode_tail64( out, x, y )
    -- If we have a number of input bytes that isn't exactly divisible
    -- by 3 then we need to pad the tail
    if x ~= nil then
        local a,b,r = x,0,1

        if y ~= nil then
            r = 2
            b = y
        end

        -- Encode three bytes of info, with the tail byte as zeros and
        -- ignore any fourth encoded ASCII value. (We should NOT have a
        -- forth byte at this point.)
        local b1, b2, b3 = e64( a, b, 0 )

        -- always add the first 2 six bit values to the res table
        -- 1 remainder input byte needs 8 output bits
        local tail_value = string.char( b1, b2 )

        -- two remainder input bytes will need 18 output bits (2 as pad)
        if r == 2 then
            tail_value=tail_value..string.char( b3 )
        end

        -- send the last 4 byte sequence with appropriate tail padding
        out( tail_value .. tail_padd64[r] )
    end
end


--------------------------------------------------------------------------------
-- encode64_io_iterator
--
--  Create an io input iterator to read an input file and split values for
--  proper encoding.
--
local function encode64_io_iterator(file)

    assert( io.type(file)  == "file", "argument must be readable file handle" )
    assert( file.read ~= nil,         "argument must be readable file handle" )

    local ii = { } -- Table for the input iterator

    setmetatable(ii,{ __tostring=function() return "base64.io_iterator" end})

    -- Begin returns an input read iterator
    --
    function ii.begin()
        local sb  = string.byte

        -- The iterator returns three bytes from the file for encoding or nil
        -- when the end of the file has been reached.
        --
        return function()
            s = file:read(3)
            if s ~= nil and #s == 3 then
                return sb(s,1,3)
            end
            return nil
        end
    end

    -- The tail method on the iterator allows the routines to run faster
    -- because each sequence of bytes doesn't have to test for EOF.
    --
    function ii.tail()
        -- If one or two "overflow" bytes exist, return those.
        --
        if s ~= nil then return s:byte(1,2) end
    end

    return ii
end


--------------------------------------------------------------------------------
-- encode64_with_ii
--
--      Convert the value provided by an encode iterator that provides a begin
--      method, a tail method, and an iterator that returns three bytes for
--      each call until at the end. The tail method should return either 1 or 2
--      tail bytes (for source values that are not evenly divisible by three).
--
local function encode64_with_ii( ii, out )
    local sc=string.char

    for a, b, c in ii.begin() do
        out( sc( e64( a, b, c ) ) )
    end

    encode_tail64( out, ii.tail() )

end


--------------------------------------------------------------------------------
-- encode64_with_predicate
--
--      Implements the basic raw data --> base64 conversion. Each three byte
--      sequence in the input string is converted to the encoded string and
--      given to the predicate provided in 4 output byte chunks. This method
--      is slightly faster for traversing existing strings in memory.
--
local function encode64_with_predicate( raw, out )
    local rem=#raw%3     -- remainder
    local len=#raw-rem   -- 3 byte input adjusted
    local sb=string.byte -- Mostly notational (slight performance)
    local sc=string.char -- Mostly notational (slight performance)

    -- Main encode loop converts three input bytes to 4 base64 encoded
    -- ACSII values and calls the predicate with the value.
    for i=1,len,3 do
        -- This really isn't intended as obfuscation. It is more about
        -- loop optimization and removing temporaries.
        --
        out( sc( e64( sb( raw ,i , i+3 ) ) ) )
        --   |   |    |
        --   |   |    byte i to i + 3
        --   |   |
        --   |   returns 4 encoded values
        --   |
        --   creates a string with the 4 returned values
    end

    -- If we have a number of input bytes that isn't exactly divisible
    -- by 3 then we need to pad the tail
    if rem > 0 then
        local x, y = sb( raw, len+1 )

        if rem > 1 then
            y = sb( raw, len+2 )
        end

        encode_tail64( out, x, y )
    end
end


--------------------------------------------------------------------------------
-- encode64_tostring
--
--      Convenience method that accepts a string value and returns the
--      encoded version of that string.
--
local function encode64_tostring(raw)

    local sb={} -- table to build string

    local function collection_predicate(v)
        sb[#sb+1]=v
    end

    -- Test with an 818K string in memory. Result is 1.1M of data.
    --
    --      lua_base64      base64 (gnu 8.21)
    --      202ms           54ms
    --      203ms           48ms
    --      204ms           50ms
    --      203ms           42ms
    --      205ms           46ms
    --
    encode64_with_predicate( raw, collection_predicate )

    return table.concat(sb)
end


--[[**************************************************************************]]
--[[****************************** Decoding **********************************]]
--[[**************************************************************************]]


-- Precomputed tables (compromise using more memory for speed)
local b64d    -- ANSI 'char' to right shifted bit pattern
local b64d_a1 -- byte addend
local b64d_a2 -- byte addend
local b64d_b1 -- byte addend
local b64d_b2 -- byte addend
local b64d_c1 -- byte addend
local b64d_z  -- zero


--------------------------------------------------------------------------------
-- d64
--
--  Helper function to convert four six bit values into three full eight
--  bit values. Input values are the integer expression of the six bit value
--  encoded in the original base64 encoded string.
--
--     d64( _ _1 1 1 1 1 1,
--             |       _ _ 2 2 2 2 2 2,
--             |           |       _ _ 3 3 3 3 3 3,
--             |           |           |       _ _ 4 4 4 4 4 4)
--             |           |           |           |
--  return ', [1 1 1 1 1 1 2 2]        |           |
--         ',                 [2 2 2 2 3 3 3 3]    |
--         '                                  [3 3 4 4 4 4 4 4]
--
local function d64( b1, b2, b3, b4 )
    -- We can get away with addition instead of anding the values together
    -- because there are no  overlapping bit patterns.
    --
    return
        b64d_a1[b1] + b64d_a2[b2],
        b64d_b1[b2] + b64d_b2[b3],
        b64d_c1[b3] + b64d[b4]
end


--------------------------------------------------------------------------------
-- decode_tail64
--
--  Send the end of stream bytes that didn't get decoded via the main loop.
--
local function decode_tail64( out, e1, e2 ,e3, e4 )

    if tail_padd64[2] == "" or e4 == tail_padd64[2]:byte() then
        local n3 = b64d_z

        if e3 ~= nil and e3 ~= tail_padd64[2]:byte() then
            n3 = e3
        end

        -- Unpack the six bit values into the 8 bit values
        local b1, b2 = d64( e1, e2, n3, b64d_z )

        -- And add them to the res table
        if e3 ~= nil and e3 ~= tail_padd64[2]:byte() then
            out( string.char( b1, b2 ) )
        else
            out( string.char( b1 ) )
        end
    end
end


--------------------------------------------------------------------------------
-- decode64_io_iterator
--
--  Create an io input iterator to read an input file and split values for
--  proper decoding.
--
local function decode64_io_iterator( file )

    local ii = { }

    -- An enumeration coroutine that handles the reading of an input file
    -- to break data into proper pieces for building the original string.
    --
    local function enummerate( file )
        local sc=string.char
        local sb=string.byte
        local ll="" -- last line storage
        local len
        local yield = coroutine.yield

        -- Read a "reasonable amount" of data into the line buffer. Line by
        -- line is not used so that a file with no line breaks doesn't
        -- cause an inordinate amount of memory usage.
        --
        for cl in file:lines(2048) do
            -- Reset the current line to contain valid chars and any previous
            -- "leftover" bytes from the previous read
            --
            cl = ll .. cl:gsub(pattern_strip,"")
            --   |     |
            --   |     Remove "Invalid" chars (white space etc)
            --   |
            --   Left over from last line
            --
            len = (#cl-4)-(#cl%4)

            -- see the comments in decode64_with_predicate for a rundown of
            -- the results of this loop (sans the coroutine)
            for i=1,len,4 do
                yield( sc( d64( sb( cl, i, i+4 ) ) ) )
            end

            ll = cl:sub( len +1, #cl )
        end

        local l = #ll

        if l >= 4 and ll:sub(-1) ~= tail_padd64[2] then
            yield( sc( d64( sb( ll, 1, 4 ) ) ) )
            l=l-4
        end

        if l > 0 then

            local e1,e2,e3,e4 = ll:byte( 0 - l, -1 )

            if e1 ~= nil then
                decode_tail64( function(s) yield( s ) end, e1, e2, e3, e4 )
            end
        end

    end

    -- Returns an input iterator that is implemented as a coroutine. Each
    -- yield of the co-routine sends reconstructed bytes to the loop handling
    -- the iteration.
    --
    function ii.begin()
        local co = coroutine.create( function() enummerate(file) end )

        return function()
            local code,res = coroutine.resume(co)
            assert(code == true)
            return res
        end
    end

    return ii
end


--------------------------------------------------------------------------------
-- decode64_with_ii
--
--      Convert the value provided by a decode iterator that provides a begin
--      method, a tail method, and an iterator that returns four (usable!) bytes
--      for each call until at the end.
--
local function decode64_with_ii( ii, out )

    -- Uses the iterator to pull values. Each reconstructed string
    -- is sent to the output predicate.
    --
    for l in ii.begin() do out( l ) end

end


--------------------------------------------------------------------------------
-- decode64_with_predicate
--
-- Decode an entire base64 encoded string in memory using the predicate for
-- output.
--
local function decode64_with_predicate( raw, out )
    -- Sanitize the input to strip characters that are not in the alphabet.
    --
    -- Note: This is a deviation from strict implementations where "bad data"
    --       in the input stream is unsupported.
    --
    local san = raw:gsub(pattern_strip,"")
    local len = #san-#san%4
    local rem = #san-len
    local sc  = string.char
    local sb  = string.byte

    if san:sub(-1,-1) == tail_padd64[2] then
        rem = rem + 4
        len = len - 4
    end

    for i=1,len,4 do
        out( sc( d64( sb( san, i, i+4 ) ) ) )
    end

    if rem > 0 then
        decode_tail64( out, sb( san, 0-rem, -1 ) )
    end
end


--------------------------------------------------------------------------------
-- decode64_tostring
--
--  Takes a string that is encoded in base64 and returns the decoded value in
--  a new string.
--
local function decode64_tostring( raw )

    local sb={} -- table to build string

    local function collection_predicate(v)
        sb[#sb+1]=v
    end

    decode64_with_predicate( raw, collection_predicate )

    return table.concat(sb)
end


--------------------------------------------------------------------------------
-- set_and_get_alphabet
--
--  Sets and returns the encode / decode alphabet.
--
--
local function set_and_get_alphabet(alpha,term)

    if alpha ~= nil then
        local magic=
        {
    --        ["%"]="%%",
            [" "]="% ",
            ["^"]="%^",
            ["$"]="%$",
            ["("]="%(",
            [")"]="%)",
            ["."]="%.",
            ["["]="%[",
            ["]"]="%]",
            ["*"]="%*",
            ["+"]="%+",
            ["-"]="%-",
            ["?"]="%?",
        }

        c_alpha=known_base64_alphabets[alpha]
        if c_alpha == nil then
            c_alpha={ _alpha=alpha, _term=term }
        end

        assert( #c_alpha._alpha == 64,    "The alphabet ~must~ be 64 unique values."  )
        assert( #c_alpha._term  <=  1,    "Specify zero or one termination character.")

        b64d={}  -- Decode table alpha          -> right shifted int values
        b64e={}  -- Encode table 0-63 (6 bits)  -> char table
        local s=""
        for i = 1,64 do
            local byte = c_alpha._alpha:byte(i)
            local str  = string.char(byte)
            b64e[i-1]=byte
            assert( b64d[byte] == nil, "Duplicate value '"..str.."'" )
            b64d[byte]=i-1
            s=s..str
        end


        local ext --Alias for extraction routine that avoids extra table lookups

        if bit32 then
            ext = bit32.extract -- slight speed, vast visual (IMO)
        elseif bit then
            local band = bit.band
            local rshift = bit.rshift
            ext =
                function(n, field, width)
                    width = width or 1
                    return band(rshift(n, field), 2^width-1)
                end
        else
            error("Neither Lua 5.2 bit32 nor LuaJit bit library found!")
        end

        -- preload encode lookup tables
        b64e_a  = {}
        b64e_a2 = {}
        b64e_b1 = {}
        b64e_b2 = {}
        b64e_c1 = {}
        b64e_c  = {}

        for f = 0,255 do
            b64e_a  [f]=b64e[ext(f,2,6)]
            b64e_a2 [f]=ext(f,0,2)*16
            b64e_b1 [f]=ext(f,4,4)
            b64e_b2 [f]=ext(f,0,4)*4
            b64e_c1 [f]=ext(f,6,2)
            b64e_c  [f]=b64e[ext(f,0,6)]
        end

        -- preload decode lookup tables
        b64d_a1 = {}
        b64d_a2 = {}
        b64d_b1 = {}
        b64d_b2 = {}
        b64d_c1 = {}
        b64d_z  = b64e[0]

        for k,v in pairs(b64d) do
            -- Each comment shows the rough C expression that would be used to
            -- generate the returned triple.
            --
            b64d_a1 [k] = v*4                   -- ([b1]       ) << 2
            b64d_a2 [k] = math.floor( v / 16 )  -- ([b2] & 0x30) >> 4
            b64d_b1 [k] = ext( v, 0, 4 ) * 16   -- ([b2] & 0x0F) << 4
            b64d_b2 [k] = math.floor( v / 4 )   -- ([b3] & 0x3c) >> 2
            b64d_c1 [k] = ext( v, 0, 2 ) * 64   -- ([b3] & 0x03) << 6
        end

        if c_alpha._term ~= "" then
            tail_padd64[1]=string.char(c_alpha._term:byte(),c_alpha._term:byte())
            tail_padd64[2]=string.char(c_alpha._term:byte())
        else
            tail_padd64[1]=""
            tail_padd64[2]=""
        end

        local esc_term

        if magic[c_alpha._term] ~= nil then
            esc_term=c_alpha._term:gsub(magic[c_alpha._term],function (s) return magic[s] end)
        elseif c_alpha._term == "%" then
            esc_term = "%%"
        else
            esc_term=c_alpha._term
        end

        if not c_alpha._strip then
            local p=s:gsub("%%",function (s) return "__unique__" end)
            for k,v in pairs(magic)
            do
                p=p:gsub(v,function (s) return magic[s] end )
            end
            local mr=p:gsub("__unique__",function() return "%%" end)

            c_alpha._strip = string.format("[^%s%s]",mr,esc_term)
        end

        assert( c_alpha._strip )

        pattern_strip = c_alpha._strip

        local c =0 for i in pairs(b64d) do c=c+1 end

        assert( c_alpha._alpha == s,        "Integrity error." )
        assert( c == 64,                    "The alphabet must be 64 unique values." )
        if esc_term ~= "" then
            assert( not c_alpha._alpha:find(esc_term), "Tail characters must not exist in alphabet." )
        end

        if known_base64_alphabets[alpha] == nil then
            known_base64_alphabets[alpha]=c_alpha
        end
    end

    return c_alpha._alpha,c_alpha._term
end


--------------------------------------------------------------------------------
-- encode64
--
--  Entry point mode selector.
--
--
local function encode64(i,o)
    local method

    if o ~= nil and io.type(o) == "file" then
        local file_out = o
        o = function(s) file_out:write(s) end
    end

    if type(i) == "string" then
        if type(o) == "function" then
            method = encode64_with_predicate
        else
            assert( o == nil, "unsupported request")
            method = encode64_tostring
        end
    elseif io.type(i) == "file" then
        assert( type(o) == "function", "file source requires output predicate")
        i      = encode64_io_iterator(i)
        method = encode64_with_ii
    else
        assert( false, "unsupported mode" )
    end

    return method(i,o)
end


--------------------------------------------------------------------------------
-- decode64
--
--  Entry point mode selector.
--
--
local function decode64(i,o)
    local method

    if o ~= nil and io.type(o) == "file" then
        local file_out = o
        o = function(s) file_out:write(s) end
    end

    if type(i) == "string" then
        if type(o) == "function" then
            method = decode64_with_predicate
        else
            assert( o == nil, "unsupported request")
            method = decode64_tostring
        end
    elseif io.type(i) == "file" then
        assert( type(o) == "function", "file source requires output predicate")
        i      = decode64_io_iterator(i)
        method = decode64_with_ii
    else
        assert( false, "unsupported mode" )
    end

    return method(i,o)
end

set_and_get_alphabet("base64")

--[[**************************************************************************]]
--[[******************************  Module  **********************************]]
--[[**************************************************************************]]
return
{
    encode      = encode64,
    decode      = decode64,
    alpha       = set_and_get_alphabet,
}
make new event script in lua named:script_device_pass2php

Code: Select all

JSON=loadfile('/home/pi/domoticz/scripts/lua/JSON.lua')()
base64=loadfile('/home/pi/domoticz/scripts/lua/ee5_base64.lua')()
changed=base64.encode(JSON:encode(devicechanged))
devices=base64.encode(JSON:encode(otherdevices))
idx=base64.encode(JSON:encode(otherdevices_idx))
lastupdate=base64.encode(JSON:encode(otherdevices_lastupdate))
os.execute( '/home/pi/domoticz/scripts/php/pass2php.php "'..changed..'" "'..devices..'" "'..idx..'" "'..lastupdate..'" &')
commandArray = {}
return commandArray
pass2php.php
/home/pi/domoticz/scripts/php/pass2php.php

Code: Select all

<?php
$sumtoday=0;
$domoticz=json_decode(file_get_contents('http://192.168.1.247:8080/json.htm?type=devices&plan=2'),true);
if($domoticz){
    foreach($domoticz['result'] as $dom){
        $today=number_format(str_replace(' kWh','',$dom['CounterToday']),3);
        lg('$today: '.$today);
        $sumtoday=$sumtoday+$today;
    }
}
lg('$sumtoday: '.$sumtoday);
Udevice(390,0,$sumtoday);

function Udevice($idx,$nvalue,$svalue){
      echo file_get_contents('http://192.168.1.247:8080/json.htm?type=command&param=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);

}
I make somewhere mistake because no log in domoticz, any help?

Post Reply

Who is online

Users browsing this forum: commentator and 1 guest