PHP Custom Floorplan for Domoticz

wervisser
Posts: 21
Joined: Monday 28 November 2016 19:38
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: PHP Floorplan, cron script and Google Calendar

Post by wervisser » Tuesday 06 December 2016 9:49

Yes, I am. I created a room plan which got ID 2 (offcourse, this is the first one after the $Hidden devices).

Since I added Philips_Hue numereous times in the floorplan.php I included this device in the roomplan with ID 2.

The output I am receiving is as follows.
Spoiler: show
{
"ActTime" : 1481010423,
"ServerTime" : "2016-12-06 08:47:03",
"Sunrise" : "08:32",
"Sunset" : "16:30",
"result" : [
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 0.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 255,
"CustomImage" : 0,
"Data" : "Off",
"Description" : "",
"Favorite" : 1,
"HardwareID" : 2,
"HardwareName" : "Aeotec Gen5 Z-Wave USB",
"HardwareType" : "OpenZWave USB",
"HardwareTypeVal" : 21,
"HaveDimmer" : true,
"HaveGroupCmd" : true,
"HaveTimeout" : false,
"ID" : "00000501",
"Image" : "Light",
"IsSubDevice" : false,
"LastUpdate" : "2016-12-06 08:26:54",
"Level" : 100,
"LevelInt" : 100,
"MaxDimLevel" : 100,
"Name" : "Eetkamer_plafond_dimmer",
"Notifications" : "false",
"PlanID" : "2",
"PlanIDs" : [ 2 ],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"Status" : "Off",
"StrParam1" : "",
"StrParam2" : "",
"SubType" : "Switch",
"SwitchType" : "Dimmer",
"SwitchTypeVal" : 7,
"Timers" : "false",
"Type" : "Light/Switch",
"TypeImg" : "dimmer",
"Unit" : 1,
"Used" : 1,
"UsedByCamera" : false,
"XOffset" : "0",
"YOffset" : "0",
"idx" : "39"
},
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 0.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 255,
"CustomImage" : 0,
"Data" : "Off",
"Description" : "",
"Favorite" : 1,
"HardwareID" : 2,
"HardwareName" : "Aeotec Gen5 Z-Wave USB",
"HardwareType" : "OpenZWave USB",
"HardwareTypeVal" : 21,
"HaveDimmer" : true,
"HaveGroupCmd" : true,
"HaveTimeout" : false,
"ID" : "00000D01",
"Image" : "Light",
"IsSubDevice" : false,
"LastUpdate" : "2016-12-06 08:26:54",
"Level" : 99,
"LevelInt" : 99,
"MaxDimLevel" : 100,
"Name" : "Philips_Hue",
"Notifications" : "false",
"PlanID" : "2",
"PlanIDs" : [ 2 ],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"Status" : "Off",
"StrParam1" : "",
"StrParam2" : "",
"SubType" : "Switch",
"SwitchType" : "On/Off",
"SwitchTypeVal" : 0,
"Timers" : "false",
"Type" : "Light/Switch",
"TypeImg" : "lightbulb",
"Unit" : 1,
"Used" : 1,
"UsedByCamera" : false,
"XOffset" : "0",
"YOffset" : "0",
"idx" : "112"
}
],
"status" : "OK",
"title" : "Devices"
}

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

Re: PHP Floorplan, cron script and Google Calendar

Post by Egregius » Tuesday 06 December 2016 10:13

/floorplan.php:

Code: Select all

Dimmer('Eetkamer_plafond_dimmer');
Schakelaar('Philips_Hue');
/styles/floorplan.php:

Code: Select all

.Eetkamer_plafond_dimmer{top:200px;left:100px;}#Eetkamer_plafond_dimmer{height:40px;width:auto;}
.Philips_Hue{top:200px;left:200px;}#badkamervuur{Philips_Hue:40px;width:auto;}
Should do it.

If not pull the url in the test file. You may need to add the curl function to settings.php

wervisser
Posts: 21
Joined: Monday 28 November 2016 19:38
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: PHP Floorplan, cron script and Google Calendar

Post by wervisser » Tuesday 06 December 2016 11:52

Ok, updated the files with the suggested lines and got some devices with the old files. I uploaded the new php files from your website and it goes to black again. It seems to have difficulties processing

Code: Select all

'$domoticz=json_decode(curl('http://xxx.xxx.xxx.xxx:8084/json.htm?type=devices&plan=2',true,$ctx),true);'[\code]

It give me the error [i]Fatal error: Call to undefined function curl() in /volume1/web/floorplan.php on line 40[/i].

I cannot find the variable $ctx which is called in $domoticz in one of the files (settings.php/floorplan.php/functions.php)

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

Re: PHP Floorplan, cron script and Google Calendar

Post by Egregius » Tuesday 06 December 2016 12:07

The $ctx is a leftover from when I used file_get_contents instead of curl. It can be removed:

Code: Select all

$domoticz=json_decode(curl('http://127.0.0.1:8084/json.htm?type=devices&plan=2'),true);
The curl function may be added to settings.php

Code: Select all

function curl($url){
	$ch=curl_init();
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
  curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
  curl_setopt($ch,CURLOPT_TIMEOUT,3);
  curl_setopt($ch,CURLOPT_HTTPHEADER,array('Accept: application/json'));
	curl_setopt($ch,CURLOPT_URL,$url);
	//curl_setopt($ch,CURLOPT_FRESH_CONNECT, TRUE);
	$data=curl_exec($ch);
	curl_close($ch);
	return $data;
}
Difficult to keep github or my site in sync because I need to remove a lot of stuff from the files as I use it for several other things also.

wervisser
Posts: 21
Joined: Monday 28 November 2016 19:38
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: PHP Floorplan, cron script and Google Calendar

Post by wervisser » Tuesday 06 December 2016 12:15

Well, this looks like it going somewhere. I now get a lot of icons instead of a black roomplan.

Since it's clear you understand PHP very well and I, well... simply don't, this all makes more sense to you than me :oops:

I will go ahead now and draw some kind of floorplan for myself and will combine the required devices in the files.

You mention you use these scripts for more than just switching the lights, does this mean I only need the files...

/floorplan.php
/secure/settings.php
/secure/functions.php
/style/floorplan.php
/style/general.php

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

Re: PHP Floorplan, cron script and Google Calendar

Post by Egregius » Tuesday 06 December 2016 12:33

Correct, those 5 files should do it.
You could even combine settings and functions into 1.
Same for the css, you could also combine them into 1.
I have settings and functions seperated because settings is used in all my pages (it holds an authentication thing) and functions is only used for the floorplan. That way the code is minimalistic.
Same goes for the css. general holds stuff I use on all pages, floorplan ... well, you get it I guess.
Other pages are for example interface to control Denon receiver, Kodi media player, Kodi movie database, electricity/gas consumption, budget control,...

I'm not a php guru either, but I have a very good friend who knows all about it. Maybe you know him to? Google :P

wervisser
Posts: 21
Joined: Monday 28 November 2016 19:38
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: PHP Floorplan, cron script and Google Calendar

Post by wervisser » Tuesday 06 December 2016 13:13

Thanks, I will try to figure it out. First I'll move away the files I most definitely won't need.

Hmm, your friends name sounds kinda familiar. Got a mail address ? :lol:

wervisser
Posts: 21
Joined: Monday 28 November 2016 19:38
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: PHP Floorplan, cron script and Google Calendar

Post by wervisser » Thursday 08 December 2016 11:26

Just to give some closure about the issues.

It seems to be working now and I'm trying to figure out the correct locations of the desired icons. I usually just open up the image in GIMP and determine the locations, but the offset is different due to padding (I think). I also changed the button size, it didn't seem to fit in the original background image nor in my own floorplan image.

One question though, I have commented out a lot in the code but can't find the reload/refresh code. Where did you put in the reload action for the webpage to refresh ?

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

Re: PHP Floorplan, cron script and Google Calendar

Post by Egregius » Thursday 08 December 2016 13:00

at the bottom of floorplan.php: SetTimeout refreshes the page after 4963 msecs (5 seconds minus page load time)

Code: Select all

<script type="text/javascript">
	function toggle_visibility(id){
		var e=document.getElementById(id);
		if(e.style.display=='inherit') e.style.display='none';
		else e.style.display='inherit';
	}
	setTimeout('window.location.href=window.location.href;',4963);
	function navigator_Go(url){
		window.location.assign(url);
	}
</script>
Used this small javascript because the refresh thing in a html header refresh everything, also images and css.

edje1205
Posts: 24
Joined: Tuesday 20 December 2016 13:09
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: Amsterdam Nieuwendam
Contact:

Re: PHP Floorplan, cron script and Google Calendar

Post by edje1205 » Saturday 24 December 2016 14:28

I Have WinScp sftp file transfer will I put everything in

Code: Select all

domoticz/www

after adding my settings.php ?


Sorry new with domoticz; I can handle php stuff but this new

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

Re: PHP Floorplan, cron script and Google Calendar

Post by Egregius » Saturday 24 December 2016 14:38

The built-in webserver doesn't support PHP files. You'll need a separate PHP enabled webserver like Apache2, Nginx,...

denisj
Posts: 62
Joined: Saturday 17 December 2016 14:13
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: PHP Floorplan, cron script and Google Calendar

Post by denisj » Tuesday 27 December 2016 12:44

Hi Egregius,
like you told me on the other thread, I just installed your script in my /www/frontphp folder

Then I copy the sample test on the page 3 of this thread.
And at first time I had an error on curl() function:

Code: Select all

Fatal error: Uncaught Error: Call to undefined function curl() in /etc/domoticz/www/frontphp/test.php:6 Stack trace: #0 {main} thrown in /etc/domoticz/www/frontphp/test.php on line 6 
I see on my php info page that the curl is installed (php7.0)

Anyway I then try to use the file_get_contents command, I see in the thread that you use this one now and I finded it in your secure/test.php file.
I put "type=devices&plan=0" cause it was "type=openzwavenodes&idx='.$zwaveidx"
So the call now is:

Code: Select all

$domoticz=json_decode(file_get_contents($domoticzurl.'json.htm?type=devices&plan=0'));
if($domoticz)
 ... etc
But now it give me an error like:

Code: Select all

Warning: file_get_contents(http://127.0.0.1:8080/json.htm?type=devices&plan=0): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in /etc/domoticz/www/frontphp/test.php on line 7
I so there is a file settings (require "./secure/settings.php";") and inside is a variable "$authenticated=true;"
...I must put my user/pass there someware in order to ask the web (cause in my settings of domoticz I use user/pass auth)

Thanks again
Denis

denisj
Posts: 62
Joined: Saturday 17 December 2016 14:13
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: PHP Floorplan, cron script and Google Calendar

Post by denisj » Tuesday 27 December 2016 13:28

I solve the problem :-)

For user that will have this problem can auth like here:

Code: Select all

$username = 'userxxx';
$password = 'passxxx';
$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Basic " . base64_encode("$username:$password")
    )
));
$domoticz=json_decode(file_get_contents($domoticzurl.'json.htm?type=devices&plan=0',false,$context));
Now I have another problem with decode:

Code: Select all

Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /etc/domoticz/www/frontphp/test.php:20 Stack trace: #0 {main} thrown in /etc/domoticz/www/frontphp/test.php on line 20
On the line 20 I have "foreach($domoticz['result'] as $dom)"
I think now, in this case the $domoticz is another type of array.

I still try :-)

Denis

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

Re: PHP Floorplan, cron script and Google Calendar

Post by Egregius » Tuesday 27 December 2016 13:47

You need ,true on the end of the json_decode command to store the result as a associated array.

denisj
Posts: 62
Joined: Saturday 17 December 2016 14:13
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: PHP Floorplan, cron script and Google Calendar

Post by denisj » Tuesday 27 December 2016 15:22

I make it like this:

Code: Select all

$result = file_get_contents($domoticzurl.'json.htm?type=devices&plan=0',false,$context);
$domoticz = json_decode($result, true);
And it's work :-)
...I receive data.

Now it's time to begin with the big packet.

How can I adapt the auth on the script please ?
I mean... there is a lot of "file_get_contents" commands in the files, is there possibility to make a change
in settings that will work with all "file_get_contents" commands ?

Thanks again
Denis

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

Re: PHP Floorplan, cron script and Google Calendar

Post by Egregius » Tuesday 27 December 2016 18:29

It's easier to just disable authentication for local calls by adding 127.0.0.1 to the local networks setting in Domoticz.
Then you don't have to worry about all that.

What do you mean by "the big packet"? For the Floorplan you only need about 5 files.

edje1205
Posts: 24
Joined: Tuesday 20 December 2016 13:09
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: Amsterdam Nieuwendam
Contact:

Re: PHP Custom Floorplan for Domoticz

Post by edje1205 » Wednesday 28 December 2016 13:08

denisj wrote:
like you told me on the other thread, I just installed your script in my /www/frontphp folder
then I don't see anything.

at
/home/pi/domoticz/www: neither

yes in var/www/html I do see floorplan.php but not in floorplan page of domoticz

What's the working directory in RPi for custom floorplan ?

Thnx !

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

Re: PHP Custom Floorplan for Domoticz

Post by Egregius » Wednesday 28 December 2016 13:24

the folder depends on the settings of the webserver.
For apache2 that should be /var/www by default. I think for Nginx /var/www/html. For webstation on syno /volume1/web

denisj
Posts: 62
Joined: Saturday 17 December 2016 14:13
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: PHP Floorplan, cron script and Google Calendar

Post by denisj » Wednesday 28 December 2016 16:04

Egregius wrote: What do you mean by "the big packet"? For the Floorplan you only need about 5 files.
I make a clone of your "packet" ... there is a lot of files
But it's ok, I just learn a lot looking to your files.

Also I set my apache log in order to see the microseconds time of apache answer.
You are write... via php request it's less than half time :-)
Great job !!!

Thanks again
Denis

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests