Hier mein Skript. Es durchsucht alle Kategorien unter einem definierten Startpunkt (SHUTTER_ROOT), und verwandelt zusammengehörige EIB Gruppen in EIB Shutter Instanzen. Die Gruppen werden nach dem erfolgreichen Anlegen gelöscht, um Variablen zu sparen und aufzuräumen. Man kann dieses Skript also beliebig oft laufen lassen, insbesondere nach einem Neuimport der OPC Datei.
Zusammengehörige EIB Gruppen werden erkannt, wenn ihre Namen mit „Auf/Ab“, „Ab/Auf“ oder „STOP“ enden, und es zwei zusammenpassende Objekte gibt. Auf Groß-/Kleinschreibung wird dabei nicht geachtet.
Beispiel: Folgende EIB Gruppen Struktur in IPS mit Kategorien
- Erdgeschoss
- – Rollo Wohnzimmer Auf/Ab
- – Rollo Wohnzimmer STOP
- Obergeschoss
- –Rollo Schlafzimmer ab/auf
- –Rollo Schlafzimmer stop
wird in folgende EIB Shutter Instanzen umgewandelt:
- Erdgeschoss
- – Rollo Wohnzimmer
- Obergeschoss
- –Rollo Schlafzimmer
<?
/* ***************************************************************
* This script creates "EIB Shutter" instances out of ordinary
* "EIB Group" instances, which for example were created when
* importing a OPC (*.esf) file.
* The script searches for all EIB group instances whose name
* matches a certain pattern (see below).
*
* (c) Josef "Elandril" S., Austria, 2015-06-06
* ***************************************************************/
/* The root path for the EIB instances which shall be converted.
* All categories below this will be searched!
*/
define("SHUTTER_ROOT", utf8_decode("Rolläden, Jalousien"));
/* The search pattern for the EIB groups which will become shutter
* instances.
* By default this are names ending with 'Auf/Ab', 'Ab/Auf' or 'STOP'
* with a case-insensitive comparison. Be careful when modifying this
* since the matching result is used for creating the name of the final
* EIB shutter instances! Modification also need to be taken into account
* for the two findObject calls!
*/
define("SHUTTER_NAME_PATTERN", '#^((.+?)\s+)(Auf/Ab|Ab/Auf|STOP)$#i');
/* Utility functions */
function findModuleGUID($name) {
foreach(IPS_GetModuleList() as $guid)
{
$module = IPS_GetModule($guid);
if ($module['ModuleName'] == $name)
return $guid;
}
}
function findObject($IdList, $parentId, $guess, $pattern) {
$obj = false;
if (is_array($guess)) {
foreach($guess as $name) {
$obj = @IPS_GetObjectIDByName($name, $parentId);
if ($obj !== false) return $obj;
}
} else {
$obj = @IPS_GetObjectIDByName($guess, $parentId);
}
if ($obj === false) {
foreach($IdList as $id) {
if (IPS_ObjectExists($id) && preg_match($pattern, IPS_GetName($id)) == 1)
return $id;
}
} else {
return $obj;
}
return false;
}
function deleteEIBGroup($instance, $withChildren = true) {
if ($withChildren && IPS_HasChildren($instance)) {
foreach(IPS_GetChildrenIDs($instance) as $childId) {
IPS_DeleteVariable($childId);
}
}
return IPS_DeleteInstance($instance);
}
function walkTree($base, $shutterGUID, $groupGUID) {
$cntCreate = 0;
$cntDelete = 0;
$cntError = 0;
$deleted = array();
$children = IPS_GetChildrenIDs($base);
foreach ($children as $childId) {
if (in_array($childId, $deleted)) continue;
$child = IPS_GetObject($childId);
//print_r($child);
if ($child['ObjectType'] == 0 && $child['HasChildren']) { // Kategorie
$counts = walkTree($childId, $shutterGUID, $groupGUID);
$cntCreate += $counts[0];
$cntDelete += $counts[1];
$cntError += $counts[2];
} elseif ($child['ObjectType'] == 1) { // Instanz
$instance = IPS_GetInstance($childId);
if (is_array($instance) && $instance['ModuleInfo']['ModuleID'] == $groupGUID &&
preg_match( SHUTTER_NAME_PATTERN, $child['ObjectName'], $match)==1 ) {
//print_r(IPS_GetInstance($childId));
if (preg_match( '#\s+STOP$#i', $child['ObjectName']) == 1) {
$stopId = $childId;
$moveId = findObject($children, $base, array( $match[1].'Auf/Ab', $match[1].'auf/ab',
$match[1].'Ab/Auf', $match[1].'ab/auf'), '#^'.$match[1].'(Auf/Ab|Ab/Auf)$#i');
} else {
$moveId = $childId;
$stopId = findObject($children, $base, array( $match[1].'STOP', $match[1].'stop',
$match[1].'Stop'), '#^'.$match[1].'(STOP)$#i');
}
if ($moveId !== false && $stopId !== false) {
$moveGA = array(
IPS_GetProperty($moveId, 'GroupAddress1'),
IPS_GetProperty($moveId, 'GroupAddress2'),
IPS_GetProperty($moveId, 'GroupAddress3')
);
$stopGA = array(
IPS_GetProperty($stopId, 'GroupAddress1'),
IPS_GetProperty($stopId, 'GroupAddress2'),
IPS_GetProperty($stopId, 'GroupAddress3')
);
if ($shutter = IPS_CreateInstance($shutterGUID)) {
printf("New EIB Shutter Instance %d: Move %d/%d/%d - Stop %d/%d/%d : %s
", $shutter,
$moveGA[0], $moveGA[1], $moveGA[2], $stopGA[0], $stopGA[1], $stopGA[2], $match[2]);
$cntCreate++;
if (!IPS_SetName($shutter, $match[2])) $cntError++;
if (!IPS_SetIcon($shutter, 'Shutter')) $cntError++;
if (!IPS_SetParent($shutter, $base)) $cntError++;
IPS_SetProperty($shutter, 'GroupMoveAddress1', $moveGA[0]);
IPS_SetProperty($shutter, 'GroupMoveAddress2', $moveGA[1]);
IPS_SetProperty($shutter, 'GroupMoveAddress3', $moveGA[2]);
IPS_SetProperty($shutter, 'GroupStopAddress1', $stopGA[0]);
IPS_SetProperty($shutter, 'GroupStopAddress2', $stopGA[1]);
IPS_SetProperty($shutter, 'GroupStopAddress3', $stopGA[2]);
if (IPS_ApplyChanges($shutter)) {
printf("Delete Instance %d: %s
", $moveId, IPS_GetName($moveId));
if (deleteEIBGroup($moveId))
$cntDelete++;
else
$cntError++;
printf("Delete Instance %d: %s
", $stopId, IPS_GetName($stopId));
if (deleteEIBGroup($stopId))
$cntDelete++;
else
$cntError++;
array_push($deleted, $moveId, $stopId);
} else
$cntError++;
} else
$cntError++;
}
}
}
}
return array($cntCreate, $cntDelete, $cntError);
}
$rootId = IPSUtil_ObjectIDByPath(SHUTTER_ROOT);
$shutterGUID = findModuleGUID('EIB Shutter');
$groupGUID = findModuleGUID('EIB Group');
list($numCreated, $numDeleted, $numError) = walkTree($rootId, $shutterGUID, $groupGUID);
printf("Result: %d created, %d deleted, %d errors
", $numCreated, $numDeleted, $numError);
?>
Ich hoffe, das Skript ist für euch genauso hilfreich, wie für mich. Falls ihr noch Fragen habt oder Probleme auftreten, einfach bei mir melden.