Hi Kai,
da wird doch der Hund in der Pfanne verrückt
Hier mal die neue Version! Schau mal ob Du damit zurechtkommst. Achtung: Webhook korrekt setzen!
<?
################################################################################
# Scriptbezeichnung: System.Dashboard.ips.php
# Version: 2.0,20180217
# Author: Heiko Wilknitz (@Pitti)
# Original von Horst (12.11.2010)
# Angepasst für RasPi lueralba (31.3.2015)
#
# Meldungsanzeige im WebFront
# ===========================
#
# Dieses Skript dient zur Verwaltung einer Meldungsliste im WebFront.
# Meldungen können hinzugefügt und entfernt werden. Es ist auch möglich,
# Meldungen zu einem bestimmten Zeitpunkt automatisch löschen zu lassen,
# sowie das Löschen von Meldungen durch Klick im WebFront zu aktivieren.
# Mit der Version 2.0 ist es möglich den Button zum Wechseln der Seite
# im Webfront zu benutzen (Typ 4).
#
# Installation:
# -------------
#
# Dieses Skript richtet automatisch alle nötigen Objekte bei manueller
# Ausführung ein. Eine weitere manuelle Ausführung setzt alle benötigten Objekte
# wieder auf den Ausgangszustand.
#
# - Neues Skript erstellen
# - Diesen PHP-Code hineinkopieren
# - Skript Abspeichern
# - Webfront ID eintragen (Abschnitt 'Konfiguration')
# - Skript Ausführen
#
# Meldung durch ein anderes Skript hinzufügen lassen:
# ---------------------------------------------------
#
# $number = IPS_RunScriptWaitEx(ObjektID, array('action' => 'add', 'text' => 'Test', 'expires' => time() + 60, 'removable' => true));
# Die Rückgabe des Aufrufes ist die Identifikationsnummer der neuen Nachricht,
# bei Misserfolg wird der Wert 0 zurückgegeben.
#
# Parameter:
# - 'text': Meldungstext
# - 'expires' (optional): Zeitpunkt des automatischen Löschens der Meldung
# als Unix-Timestamp. Ist der Wert kleiner als die aktuelle Timestamp,
# wird nicht automatisch gelöscht.
# - 'removable' (optional): Anzeige eines Buttons zum Löschen der Meldung im WebFront.
# - 'type' (optional): Art der Meldung ... 0 => Normal(grün),
# 1 => Fehler(rot), 2 => Warnung(gelb), 3 => Todo(blau), 4 => Goto(orange)
# - 'image' (optional): Name des WebFront-Icons (ipsIcon<name>), welches
# für Meldung verwendet werden soll, Standard ist "Talk"
# - 'page' (optional): Nur in Verbindung mit Type 4 - Seitenname
#
# Meldung durch ein anderes Skript löschen lassen:
# ------------------------------------------------
#
# $success = IPS_RunScriptWaitEx(ObjektID, array('action' => 'remove', 'number' => 123));
# Bei erfolgreichem Löschen wird der Wert 1 zurückgegeben, bei Misserfolg der Wert 0.
#
# Parameter:
# - 'number': Identifikationsnummer der zu löschenden Meldung
#
# Meldung eines bestimmten Types löschen:
# ------------------------------------------------
#
# $success = IPS_RunScriptWaitEx(ObjektID, array('action' => 'removeType', 'type' => x));
# Bei erfolgreichem Löschen wird der Wert 1 zurückgegeben, bei Misserfolg der Wert 0.
#
# Parameter:
# - 'type': Meldungstyp der gelöscht werden soll (x = 0|1|2|3|4)
#
# Alle vorhandenen Meldungen durch ein anderes Skript löschen lassen:
# -------------------------------------------------------------------
#
# $success = IPS_RunScriptWaitEx(ObjektID, array('action' => 'removeAll'));
# Bei erfolgreichem Löschen wird der Wert 1 zurückgegeben, bei Misserfolg der Wert 0.
#
# Eine Meldungen hinzufügen, welche bei Klick auf den Button die Seite wechselt:
# ------------------------------------------------------------------------------
#
# $id = IPS_RunScriptWaitEx(ObjektID , array('action' => 'add', 'text' => $text, 'type' => 4, 'image' => 'Telephone', 'page' => 'catAnrufe'));
# Der Parameter 'page' definiert zu welcher Seite im Webfront gewechselt werden soll.
# Der Name der Seite muss einer existierenden 'Element ID' im konfigurierten Webfront
# entsprechen (z.b. item32435).
#
# ----------------------------- Konfigruration ---------------------------------
#
# Hier Eure ID eintregen!!!
$wfc = 12345; // WebFront Configuration
#
################################################################################
switch ($_IPS['SENDER']) {
case 'Execute':
install();
break;
case 'RunScript':
$result = 0;
switch ($_IPS['action']) {
case 'add':
$expires = isset($_IPS['expires']) ? $_IPS['expires'] : 0;
$removable = isset($_IPS['removable']) ? $_IPS['removable'] : false;
$text = isset($_IPS['text']) ? $_IPS['text'] : 'leer';
$type = isset($_IPS['type']) ? $_IPS['type'] : 0;
$image = isset($_IPS['image']) ? $_IPS['image'] : 'Talk';
$page = isset($_IPS['page']) ? $_IPS['page'] : '';
if (!($expires > time())) { $expires = 0; }
if (!($removable === true)) { $removable = false; }
if (!($type > 0)) { $type = 0; }
if (!(is_string($page))) { $page = ''; }
if (!($image != '')) { $image = 'Talk'; }
if (is_string($text) && $text != '') {
$result = addMessage($text, $expires, $removable, $type, $image, $page);
}
break;
case 'remove':
$number = isset($_IPS['number']) ? $_IPS['number'] : -1;
if ($number > 0) {
$result = removeMessage($number);
}
break;
case 'removeAll':
$result = removeAllMessages();
break;
case 'removeType':
$type = isset($_IPS['type']) ? $_IPS['type'] : -1;
if ($type >= 0) {
$result = removeTypes($type);
}
break;
}
echo $result;
break;
case 'TimerEvent':
$number = explode('#', IPS_GetName($_IPS['EVENT']));
$number = $number[1];
IPS_DeleteEvent($_IPS['EVENT']);
removeMessage($number);
break;
case 'WebHook':
$result = 0;
switch ($_GET['action']) {
case 'remove':
$number = isset($_GET['number']) ? $_GET['number'] : -1;
if ($number > 0) {
$result = removeMessage($number);
}
break;
case 'switch':
$page = isset($_GET['page']) ? $_GET['page'] : '';
if (is_string($page) && $page !='') {
$result = switchPage($wfc, $page);
}
break;
}
echo $result;
break;
}
function removeAllMessages ()
{
$ParentID = IPS_GetParent($_IPS['SELF']);
$DataID = IPS_GetVariableIDByName('Daten', $ParentID);
$MessagesID = IPS_GetVariableIDByName('Meldungen', $ParentID);
$LastNumberID = IPS_GetVariableIDByName('letzte Meldungsnummer', $ParentID);
$ids = IPS_GetChildrenIDs($_IPS['SELF']);
foreach ($ids as $id) {
if (IPS_EventExists($id) && substr(IPS_GetName($id), 0, 16) == 'Remove Message #') {
IPS_DeleteEvent($id);
}
}
SetValueString($DataID, json_encode(array()));
SetValueString($MessagesID, 'Keine Meldungen vorhanden!');
SetValueInteger($LastNumberID, 0);
return 1;
}
function removeTypes($type) {
$ParentID = IPS_GetParent($_IPS['SELF']);
$DataID = IPS_GetVariableIDByName('Daten', $ParentID);
$result = 0;
$i = 0;
$j = 0;
$data = json_decode(GetValueString($DataID), true);
foreach($data as $dataid => $dataval) {
if($dataval['type'] == $type) {
if(removeMessage($dataid)) $i++;
$j++;
}
}
if ($i == $j) $result = 1;
return $result;
}
function removeMessage ($number)
{
$ParentID = IPS_GetParent($_IPS['SELF']);
$DataID = IPS_GetVariableIDByName('Daten', $ParentID);
$result = 0;
if (IPS_SemaphoreEnter($_IPS['SELF'].'DataUpdate', 2000)) {
$data = json_decode(GetValueString($DataID), true);
if (isset($data[$number])) {
unset($data[$number]);
$eventID = @IPS_GetEventIDByName('Remove Message #'.$number, $_IPS['SELF']);
if ($eventID !== false) {
IPS_DeleteEvent($eventID);
}
SetValueString($DataID, json_encode($data));
$result = 1;
}
else {
throwException('Could not remove message #'.$number.': Unknown message number!');
}
IPS_SemaphoreLeave($_IPS['SELF'].'DataUpdate');
renderData($data);
}
else {
throwException('Could not remove message #'.$number.': Semaphore timeout!');
}
return $result;
}
function addMessage ($text, $expires, $removable, $type, $image, $page)
{
$ParentID = IPS_GetParent($_IPS['SELF']);
$DataID = IPS_GetVariableIDByName('Daten', $ParentID);
$LastNumberID = IPS_GetVariableIDByName('letzte Meldungsnummer', $ParentID);
$number = 0;
if (IPS_SemaphoreEnter($_IPS['SELF'].'DataUpdate', 2000)) {
$data = json_decode(GetValueString($DataID), true);
if (!is_array($data)) {
$data = array();
}
$number = GetValueInteger($LastNumberID) + 1;
$data[$number] = array('text' => utf8_encode($text), 'expires' => $expires, 'removable' => $removable, 'type' => $type, 'image' => $image, 'page' => $page);
if ($expires > time()) {
$eventID = IPS_CreateEvent(1);
IPS_SetParent($eventID, $_IPS['SELF']);
IPS_SetName($eventID, 'Remove Message #'.$number);
IPS_SetEventCyclic($eventID, 1, 0, 0, 0, 0, 0);
if($expires == 0) {
IPS_SetEventCyclicDateFrom($eventID, 0, 0, 0);
} else {
IPS_SetEventCyclicDateFrom($eventID, (int)date('j',$expires), (int)date('n', $expires), (int)date('Y', $expires));
}
IPS_SetEventCyclicDateTo($eventID, 0, 0, 0);
if($expires == 0) {
IPS_SetEventCyclicTimeFrom($eventID, 0, 0, 0);
} else {
IPS_SetEventCyclicTimeFrom($eventID, (int)date("H", $expires), (int)date("i", $expires), (int)date("s", $expires));
}
IPS_SetEventCyclicTimeTo($eventID, 0, 0, 0);
IPS_SetEventActive($eventID, true);
}
SetValueString($DataID, json_encode($data));
SetValueInteger($LastNumberID, $number);
IPS_SemaphoreLeave($_IPS['SELF'].'DataUpdate');
renderData($data);
}
else {
throwException('Could not add message: Semaphore timeout!');
}
return $number;
}
function switchPage($wfc, $page)
{
$result = WFC_SwitchPage($wfc, $page);
return $result;
}
function install ()
{
IPS_SetHidden($_IPS['SELF'], true);
$ParentID = IPS_GetParent($_IPS['SELF']);
$instanceID = 0;
if (IPS_InstanceExists($ParentID)) {
$instance = IPS_GetInstance($ParentID);
if ($instance['ModuleInfo']['ModuleID'] == '{485D0419-BE97-4548-AA9C-C083EB82E61E}') {
$instanceID = $ParentID;
}
}
if ($instanceID == 0) {
$instanceID = IPS_CreateInstance('{485D0419-BE97-4548-AA9C-C083EB82E61E}');
IPS_SetParent($instanceID, $ParentID);
IPS_SetName($instanceID, 'Meldungen');
IPS_SetParent($_IPS['SELF'], $instanceID);
$ParentID = $instanceID;
}
$DataID = @IPS_GetVariableIDByName('Daten', $ParentID);
if ($DataID === false) {
$DataID = IPS_CreateVariable(3);
IPS_SetParent($DataID, $ParentID);
IPS_SetName($DataID, 'Daten');
}
SetValueString($DataID, json_encode(array()));
$MessagesID = @IPS_GetVariableIDByName('Meldungen', $ParentID);
if ($MessagesID === false) {
$MessagesID = IPS_CreateVariable(3);
IPS_SetParent($MessagesID, $ParentID);
IPS_SetName($MessagesID, 'Meldungen');
IPS_SetVariableCustomProfile($MessagesID, '~HTMLBox');
}
SetValueString($MessagesID, 'Keine Meldungen vorhanden!');
$LastNumberID = @IPS_GetVariableIDByName('letzte Meldungsnummer', $ParentID);
if ($LastNumberID === false) {
$LastNumberID = IPS_CreateVariable(1);
IPS_SetParent($LastNumberID, $ParentID);
IPS_SetName($LastNumberID, 'letzte Meldungsnummer');
}
SetValueInteger($LastNumberID, 0);
$ids = IPS_GetChildrenIDs($_IPS['SELF']);
foreach ($ids as $id) {
if (IPS_EventExists($id) && substr(IPS_GetName($id), 0, 16) == 'Remove Message #') {
IPS_DeleteEvent($id);
}
}
}
function renderData ($data)
{
$ParentID = IPS_GetParent($_IPS['SELF']);
$DataID = IPS_GetVariableIDByName('Daten', $ParentID);
$MessagesID = IPS_GetVariableIDByName('Meldungen', $ParentID);
// Etwas CSS und HTML
$style = "";
$style = $style.'<style type="text/css">';
$style = $style.'table { width:100%; border-collapse: collapse; }';
$style = $style.'td.fst { width: 36px; padding: 2px; border-left: 1px solid rgba(255, 255, 255, 0.2); border-top: 1px solid rgba(255, 255, 255, 0.1); }';
$style = $style.'td.mid { padding: 2px; border-top: 1px solid rgba(255, 255, 255, 0.1); }';
$style = $style.'td.lst { width: 42px; text-align:center; padding: 2px; border-right: 1px solid rgba(255, 255, 255, 0.2); border-top: 1px solid rgba(255, 255, 255, 0.1); }';
$style = $style.'tr:last-child { border-bottom: 1px solid rgba(255, 255, 255, 0.2); }';
$style = $style.'.blue { padding: 5px; color: rgb(255, 255, 255); background-color: rgb(0, 0, 255); background-image: linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -o-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -moz-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -webkit-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -ms-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); }';
$style = $style.'.red { padding: 5px; color: rgb(255, 255, 255); background-color: rgb(255, 0, 0); background-image: linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -o-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -moz-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -webkit-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -ms-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); }';
$style = $style.'.green { padding: 5px; color: rgb(255, 255, 255); background-color: rgb(0, 255, 0); background-image: linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -o-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -moz-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -webkit-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -ms-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); }';
$style = $style.'.yellow { padding: 5px; color: rgb(255, 255, 255); background-color: rgb(255, 255, 0); background-image: linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -o-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -moz-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -webkit-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -ms-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); }';
$style = $style.'.orange { padding: 5px; color: rgb(255, 255, 255); background-color: rgb(255, 160, 0); background-image: linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -o-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -moz-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -webkit-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -ms-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); }';
$style = $style.'</style>';
$content = $style;
$content = $content.'<table>';
if (count($data) == 0) {
$content = $content.'<tr>';
$content = $content.'<td class="fst"><img src=\'img/icons/Ok.svg\'></img></td>';
$content = $content.'<td class="mid">Keine Meldungen vorhanden!</td>';
$content = $content.'<td class=\'lst\'><div class=\'green\' onclick=\'alert("Nachricht kann nicht bestätigt werden.");\'>OK</div></td>';
$content = $content.'</tr>';
}
else {
foreach ($data as $number => $message) {
if ($message['type']) {
switch ($message['type']) {
case 4:
$type = 'orange';
break;
case 3:
$type = 'blue';
break;
case 2:
$type = 'yellow';
break;
case 1:
$type = 'red';
break;
default:
$type = 'green';
break;
}
}
else {
$type = 'green';
}
if ($message['image']) {
$image = '<img src=\'img/icons/'.$message['image'].'.svg\'></img>';
}
else {
$image = '<img src=\'img/icons/Ok.svg\'></img>';
}
$content .= '<tr>';
$content = $content.'<td class="fst">'.$image.'</td>';
$content = $content.'<td class="mid">'.utf8_decode($message['text']).'</td>';
if ($message['removable']) {
$content = $content.'<td class=\'lst\'><div class=\''.$type.'\' onclick="window.xhrGet=function xhrGet(o) {var HTTP = new XMLHttpRequest();HTTP.open(\'GET\',o.url,true);HTTP.send();};window.xhrGet({ url: \'hook/msg?ts=\' + (new Date()).getTime() + \'&action=remove&number='.$number.'\' });">OK</div></td>';
}
elseif ($message['page']) {
$content = $content.'<td class=\'lst\'><div class=\''.$type.'\' onclick="window.xhrGet=function xhrGet(o) {var HTTP = new XMLHttpRequest();HTTP.open(\'GET\',o.url,true);HTTP.send();};window.xhrGet({ url: \'hook/msg?ts=\' + (new Date()).getTime() + \'&action=switch&page='.$message['page'].'\' });">OK</div></td>';
}
else {
$content = $content.'<td class=\'lst\'><div class=\''.$type.'\' onclick=\'alert("Nachricht kann nicht bestätigt werden.");\'>OK</div></td>';
}
$content .= '</tr>';
}
}
$content = $content. '</table>';
SetValueString($MessagesID, $content);
}
function throwException ($message)
{
IPS_LogMessage($_IPS['SELF'], $message);
}
?>
Ciao Pitti