Skip to content

Commit

Permalink
Anpassung sreitner
Browse files Browse the repository at this point in the history
Optimierung des Interfaces zur Homematic Skript Abfrage.
  • Loading branch information
christian1180 committed Nov 24, 2024
1 parent dcdc2f8 commit 27b77f1
Show file tree
Hide file tree
Showing 5 changed files with 405 additions and 810 deletions.
50 changes: 18 additions & 32 deletions diagramm_collect.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
<?php

// Eintrag in Crontab
//
// Sofern kein PHP-CLI zur Verfügung steht:
// */1 * * * * curl --silent http://localhost/homehub/diagramm_collect.php >/dev/null 2>&1
//
// mit PHP-CLI
// */1 * * * * /usr/bin/php -f /pfad-zu-homehub/diagramm_collect.php >/dev/null 2>&1

include("config/config.php");

// interface Pfad bestimmen
$interface = $_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].str_replace("diagramm_collect.php", "",$_SERVER['PHP_SELF']);
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$interface = "https://".$interface;
}
else $interface = "http://".$interface;
echo $interface;
require_once(__DIR__.'/interface.php');


date_default_timezone_set("Europe/Berlin");
$tage = array("So", "Mo", "Di", "Mi", "Do", "Fr", "Sa");


// Lese aus custom.json die diagramm ise_id welche geloggt werden sollen
$data = file_get_contents('config/custom.json');
$data = file_get_contents(__DIR__.'/config/custom.json');
$json = json_decode($data, true);


Expand Down Expand Up @@ -113,9 +112,7 @@
$historyZaehler = 0;

// Abfrage an die CCU
$xmlFile = $interface.'interface.php?state.cgi&onlyvalue=1&datapoint_id='.$diagramm_change;
echo $xmlFile;
$xml = simplexml_load_file($xmlFile);
$xml = simplexml_load_string(api_state($ccu, $diagramm_change, true));
echo $xml;
foreach ( $xml->datapoint as $states )
{
Expand All @@ -131,10 +128,10 @@
$states['value'] = $x[0].'.'.substr($x[1],0,1);
}

if(file_exists("cache/diagramm_change_".$states['ise_id']."_".$history[$historyZaehler].".csv"))
if(file_exists(__DIR__."/cache/diagramm_change_".$states['ise_id']."_".$history[$historyZaehler].".csv"))
{
// Lese Datei in Array
$lines = file("cache/diagramm_change_".$states['ise_id']."_".$history[$historyZaehler].".csv");
$lines = file(__DIR__."/cache/diagramm_change_".$states['ise_id']."_".$history[$historyZaehler].".csv");

// Sortiere alte Einträge aus
if(count((array)$lines)>= $history[$historyZaehler])
Expand Down Expand Up @@ -165,7 +162,7 @@
{
// Schreiben
$inhalt = $inhalt.$tage[date("w")]." ".date("H:i",time()).";".$states['value']."\n";
$file_handle = fopen("cache/diagramm_change_".$states['ise_id']."_".$history[$historyZaehler].".csv", 'w+');
$file_handle = fopen(__DIR__."/cache/diagramm_change_".$states['ise_id']."_".$history[$historyZaehler].".csv", 'w+');
fwrite($file_handle, $inhalt);
fclose($file_handle);
}
Expand All @@ -178,7 +175,7 @@
{
// Schreiben
$inhalt = $inhalt.$tage[date("w")]." ".date("H:i",time()).";".$states['value']."\n";
$file_handle = fopen("cache/diagramm_change_".$states['ise_id']."_".$history[$historyZaehler].".csv", 'w+');
$file_handle = fopen(__DIR__."/cache/diagramm_change_".$states['ise_id']."_".$history[$historyZaehler].".csv", 'w+');
fwrite($file_handle, $inhalt);
fclose($file_handle);
}
Expand All @@ -195,10 +192,8 @@
$historyZaehler = 0;

// Abfrage an die CCU
$xmlFile = $interface.'/interface.php?state.cgi&onlyvalue=1&datapoint_id='.$diagramm;
echo $xmlFile;
$xml = simplexml_load_file($xmlFile);
echo $xml;
$xml = simplexml_load_string(api_state($ccu, $diagramm, true));
echo $xml;
foreach ($xml->datapoint as $states)
{
$inhalt = "";
Expand All @@ -225,10 +220,10 @@



if(file_exists("cache/diagramm_".$states['ise_id']."_".$history[$historyZaehler].".csv"))
if(file_exists(__DIR__."/cache/diagramm_".$states['ise_id']."_".$history[$historyZaehler].".csv"))
{
// Lese Datei in Array
$lines = file("cache/diagramm_".$states['ise_id']."_".$history[$historyZaehler].".csv");
$lines = file(__DIR__."/cache/diagramm_".$states['ise_id']."_".$history[$historyZaehler].".csv");

// Sortiere alte Einträge aus
if(count((array)$lines)>= $history[$historyZaehler])
Expand Down Expand Up @@ -256,21 +251,12 @@
{
$inhalt = $inhalt.date("H:i",time()).";".$states['value']."\n";
}
$file_handle = fopen("cache/diagramm_".$states['ise_id']."_".$history[$historyZaehler].".csv", 'w+');
$file_handle = fopen(__DIR__."/cache/diagramm_".$states['ise_id']."_".$history[$historyZaehler].".csv", 'w+');
fwrite($file_handle, $inhalt);
fclose($file_handle);
$historyZaehler++;
}
}
echo "Ende";

function curl_get_content( $url ) {
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($curl_handle);
curl_close($curl_handle);
return $content;
}
?>
51 changes: 11 additions & 40 deletions import.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

$exportFile = 'config/export.json';

ini_set('display_errors', 'on');
$beginn = microtime(true);
$exportFile = 'config/export.json';

require_once(__DIR__.'/interface.php');

// Lösche vorhandene export.json
if(file_exists($exportFile))
Expand All @@ -10,33 +14,12 @@
}
touch($exportFile);


// Wenn Konfiguration nicht existiert wechsele zum Setup
if(!file_exists("config/config.php"))
{
header('Location: setup.php');
exit;
}
else
{
// Lade Konfiuration der Homematic
require("config/config.php");
}

$export = array();
$interface = $_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].str_replace("import.php", "",$_SERVER['PHP_SELF']);
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$interface = "https://".$interface;
}
else $interface = "http://".$interface;

// Devices und Channels von der CCU laden
$devicelistCgi = curl_get_content($interface.'/interface.php?devicelist.cgi');
$devicesXml = simplexml_load_string($devicelistCgi);

$statelistCgi = curl_get_content($interface.'/interface.php?statelist.cgi');
$statesXml = simplexml_load_string($statelistCgi);

// Devices und Channels von der CCU laden
$devicesXml = simplexml_load_string(api_devicelist($ccu));
$statesXml = simplexml_load_string(api_statelist($ccu));


// Devices
Expand Down Expand Up @@ -148,8 +131,7 @@
unset($statesXml);

// Systemvariablen von der CCU laden
$sysvarlistCgi = curl_get_content($interface.'/interface.php?sysvarlist.cgi');
$sysvarsXml = simplexml_load_string($sysvarlistCgi);
$sysvarsXml = simplexml_load_string(api_sysvarlist($ccu));

// Systemvariablen
foreach ($sysvarsXml->systemVariable as $sysvar) {
Expand All @@ -167,8 +149,7 @@
unset($sysvarsXml);

// Programme von der CCU laden
$programlistCgi = curl_get_content($interface.'/interface.php?programlist.cgi');
$programsXml = simplexml_load_string($programlistCgi);
$programsXml = simplexml_load_string(api_programlist($ccu));

// Programme
foreach ($programsXml->program as $program) {
Expand Down Expand Up @@ -206,17 +187,7 @@
}
else
{
header('Location: index.php');
header('Location: ./index.php');
}

function curl_get_content( $url ) {
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($curl_handle);
curl_close($curl_handle);
return $content;
}
?>

19 changes: 2 additions & 17 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,8 @@

}

// definiere Interface
$interface = $_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].str_replace("index.php", "",$_SERVER['PHP_SELF']);
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$interface = "https://".$interface;
}
else $interface = "http://".$interface;

// Prüfe auf config.php
if(!file_exists("config/config.php"))
{
header('Location: setup.php');
exit;
}

// Lade Konfiuration der Homematic
require("config/config.php");
require_once(__DIR__.'/interface.php');

// Setze Variable selectedCat auf die aktuelle Seite
if(!isset($_GET['seite']))
Expand Down Expand Up @@ -436,8 +422,7 @@
$str = file_get_contents('dev/export.json');
} else {

//$xml = simplexml_load_file('http://'.$homematicIp.'/config/xmlapi/systemNotification.cgi?sid='.$apitoken);
$xml = simplexml_load_file($interface.'interface.php?systemNotification.cgi');
$xml = simplexml_load_string(api_systemNotification($ccu));
$str = file_get_contents('config/export.json');
}
// Für Devices
Expand Down
Loading

0 comments on commit 27b77f1

Please sign in to comment.