|
|
|
|
@@ -26,151 +26,126 @@ package main;
|
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
use warnings;
|
|
|
|
|
use XML::LibXML;
|
|
|
|
|
use XML::LibXML::PrettyPrint;
|
|
|
|
|
|
|
|
|
|
my $yaf_version=0.41;
|
|
|
|
|
my $mp = AttrVal("global", "modpath", ".");
|
|
|
|
|
my $configurationFilepath = $mp."/FHEM/YAF/xml/yafConfig.xml";
|
|
|
|
|
my $schemaFilepath = $mp."/FHEM/YAF/xml/xmlSchema.xsd";
|
|
|
|
|
my $xmlSchema;
|
|
|
|
|
my $prettyPrinter;
|
|
|
|
|
my $config;
|
|
|
|
|
|
|
|
|
|
my %fhemwidgets;
|
|
|
|
|
my %fhemviews;
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_Config - Initializes this module by creating the schema, pretty printer and
|
|
|
|
|
# loading the configuration from the filepath
|
|
|
|
|
#
|
|
|
|
|
# YAF_FHEMConfig - Initializes the module after loading the Website
|
|
|
|
|
# Loads the distributed config from the devices
|
|
|
|
|
#
|
|
|
|
|
# no parameter
|
|
|
|
|
#
|
|
|
|
|
########################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_Config {
|
|
|
|
|
$xmlSchema = XML::LibXML::Schema->new(location => $schemaFilepath);
|
|
|
|
|
$prettyPrinter = XML::LibXML::PrettyPrint->new(indent_string => " ");
|
|
|
|
|
$config = XML::LibXML->load_xml(location => $configurationFilepath);
|
|
|
|
|
YAF_validate();
|
|
|
|
|
sub YAF_FHEMConfig { #this is called via ajax when the page is loaded.
|
|
|
|
|
#get the views
|
|
|
|
|
my $views = AttrVal("yaf","views",undef);
|
|
|
|
|
if(defined $views) {
|
|
|
|
|
foreach my $view (split (/;/,$views)) {
|
|
|
|
|
my @aview = split(/,/,$view);
|
|
|
|
|
$fhemviews{$aview[0]} = $aview[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $retAttr = "";
|
|
|
|
|
foreach my $viewId (keys %fhemviews) {
|
|
|
|
|
foreach my $key (keys %defs) { #for every def in the system
|
|
|
|
|
my $attrvalue = AttrVal($key,"yaf_$viewId",undef); #check if it has the correct attribute set
|
|
|
|
|
if (defined $attrvalue && length $attrvalue > 0) { #if the attr is set
|
|
|
|
|
my @tokens = split(/,/, $attrvalue); #split the value by comma
|
|
|
|
|
|
|
|
|
|
my @idarr = split(/=/,$tokens[0]);
|
|
|
|
|
my $widgetId = $idarr[1];
|
|
|
|
|
|
|
|
|
|
$fhemwidgets{$viewId}{$widgetId} = $key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_validate - Validates the current state of the configuration instance
|
|
|
|
|
#
|
|
|
|
|
# no parameter
|
|
|
|
|
# Returns 1 if valid, otherwise 0.
|
|
|
|
|
# YAF_getViews - Assembles defined views from config
|
|
|
|
|
#
|
|
|
|
|
########################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_validate{
|
|
|
|
|
eval{ $xmlSchema->validate($config); };
|
|
|
|
|
|
|
|
|
|
if($@){
|
|
|
|
|
Log 1,"YAF: error validating configuration file";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_getViews - Assembles defined views from configuration file
|
|
|
|
|
#
|
|
|
|
|
# no parameter
|
|
|
|
|
# Returns pointer to array of views.
|
|
|
|
|
#
|
|
|
|
|
########################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_getViews{
|
|
|
|
|
my @views = $config->findnodes('//view');
|
|
|
|
|
my @viewsArray;
|
|
|
|
|
my $index = 0;
|
|
|
|
|
|
|
|
|
|
foreach my $view (@views){
|
|
|
|
|
$viewsArray[$index][0] = $view->findvalue('@id');
|
|
|
|
|
$viewsArray[$index][1] = $view->findvalue('@name');
|
|
|
|
|
$index++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return \@viewsArray;
|
|
|
|
|
my @viewsArray;
|
|
|
|
|
my $index = 0;
|
|
|
|
|
|
|
|
|
|
foreach my $view (keys %fhemviews){
|
|
|
|
|
$viewsArray[$index][0] = $view;
|
|
|
|
|
$viewsArray[$index][1] = $fhemviews{$view};
|
|
|
|
|
$index++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return \@viewsArray;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_getView - Assembles parts of a view from configuration file
|
|
|
|
|
#
|
|
|
|
|
# YAF_getView - Assembles parts of a view from config
|
|
|
|
|
#
|
|
|
|
|
# viewId - The view id to search
|
|
|
|
|
# Returns Pointer to the view hash, hash may be empty
|
|
|
|
|
#
|
|
|
|
|
########################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_getView{
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
|
|
|
|
|
my %viewHash = ();
|
|
|
|
|
|
|
|
|
|
#-- query view id
|
|
|
|
|
my $viewResult = $config->findnodes('//view[@id = '.$viewId.']');
|
|
|
|
|
if($viewResult->size() == 1){
|
|
|
|
|
my $view = $viewResult->get_node(0);
|
|
|
|
|
|
|
|
|
|
#-- prepare view hash and add simple key/value pairs (name)
|
|
|
|
|
$viewHash{'name'} = $view->findvalue('@name');
|
|
|
|
|
|
|
|
|
|
#-- collect all widgets and add them to an array which is then connected to the view hash
|
|
|
|
|
my @widgetsArray = ();
|
|
|
|
|
my @widgets = $view->findnodes('widgets/widget');
|
|
|
|
|
|
|
|
|
|
foreach my $widget (@widgets){
|
|
|
|
|
my @attributes = $widget->attributes();
|
|
|
|
|
my %widgetHash = ();
|
|
|
|
|
|
|
|
|
|
foreach my $attribute (@attributes){
|
|
|
|
|
$widgetHash{$attribute->nodeName} = $attribute->getValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#-- collect attr nodes in a hash and add to widget
|
|
|
|
|
my %attrHash = ();
|
|
|
|
|
my @attrs = $widget->getChildrenByTagName('attr');
|
|
|
|
|
|
|
|
|
|
foreach my $attr (@attrs){
|
|
|
|
|
my $key = $attr->findvalue('@name');
|
|
|
|
|
my $value = $attr->findvalue('@value');
|
|
|
|
|
$attrHash{$key} = $value;
|
|
|
|
|
}
|
|
|
|
|
$widgetHash{'attr'} = \%attrHash;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
push(@widgetsArray, \%widgetHash);
|
|
|
|
|
}
|
|
|
|
|
$viewHash{'widgets'} = \@widgetsArray;
|
|
|
|
|
|
|
|
|
|
#-- collect all backgrounds and add them to an array which is then connected to the view hash
|
|
|
|
|
my @backgroundsArray = ();
|
|
|
|
|
my @backgrounds = $view->findnodes('backgrounds/background');
|
|
|
|
|
|
|
|
|
|
foreach my $background (@backgrounds){
|
|
|
|
|
my @attributes = $background->attributes();
|
|
|
|
|
my %backgroundHash = ();
|
|
|
|
|
|
|
|
|
|
foreach my $attribute (@attributes){
|
|
|
|
|
$backgroundHash{$attribute->nodeName} = $attribute->getValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
push(@backgroundsArray, \%backgroundHash);
|
|
|
|
|
}
|
|
|
|
|
$viewHash{'backgrounds'} = \@backgroundsArray;
|
|
|
|
|
} else{
|
|
|
|
|
Log 1,"YAF_getView: view with id = ".$viewId." was not found";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return \%viewHash;
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
my %viewHash = ();
|
|
|
|
|
my @widgetsArray = ();
|
|
|
|
|
|
|
|
|
|
foreach my $widget (keys %{$fhemwidgets{$viewId}}) {
|
|
|
|
|
my @attributes = split(/,/,AttrVal($fhemwidgets{$viewId}{$widget},"yaf_".$viewId,undef));
|
|
|
|
|
my %widgetHash = ();
|
|
|
|
|
|
|
|
|
|
$widgetHash{fhemname} = $fhemwidgets{$viewId}{$widget};
|
|
|
|
|
|
|
|
|
|
foreach my $attribute (@attributes){
|
|
|
|
|
my @attrArr = split(/=/,$attribute);
|
|
|
|
|
$widgetHash{$attrArr[0]} = $attrArr[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#my %attrHash = (); #needed?
|
|
|
|
|
#%attrHash = %widgetHash;
|
|
|
|
|
|
|
|
|
|
push(@widgetsArray, \%widgetHash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$viewHash{'widgets'} = \@widgetsArray;
|
|
|
|
|
|
|
|
|
|
my @backgroundsArray = ();
|
|
|
|
|
my @backgrounds = split (/;/,AttrVal("yaf","backgrounds",undef));
|
|
|
|
|
foreach my $background (@backgrounds){
|
|
|
|
|
my @attributes = split (/,/,$background);
|
|
|
|
|
if ($attributes[0] eq $viewId) {
|
|
|
|
|
my %backgroundHash = ();
|
|
|
|
|
|
|
|
|
|
$backgroundHash{x_pos} = $attributes[1];
|
|
|
|
|
$backgroundHash{y_pos} = $attributes[2];
|
|
|
|
|
$backgroundHash{img_url} = $attributes[3];
|
|
|
|
|
|
|
|
|
|
push(@backgroundsArray, \%backgroundHash);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$viewHash{'backgrounds'} = \@backgroundsArray;
|
|
|
|
|
return \%viewHash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_editView - Edits the view with the given id
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# viewId - The view id to search
|
|
|
|
|
# viewName The view name to be set
|
|
|
|
|
# @return 1 if successful, otherwise 0
|
|
|
|
|
@@ -178,99 +153,155 @@ sub YAF_getView{
|
|
|
|
|
########################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_editView{
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
my $viewName = $_[1];
|
|
|
|
|
|
|
|
|
|
my $viewResult = $config->findnodes('//view[@id = '.$viewId.']');
|
|
|
|
|
if($viewResult->size() == 1){
|
|
|
|
|
my $view = $viewResult->get_node(0);
|
|
|
|
|
$view->setAttribute('name', $viewName);
|
|
|
|
|
YAF_saveConfiguration();
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
Log 1,"YAF_editView: view with id = ".$viewId." was not found";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
my $viewName = $_[1];
|
|
|
|
|
|
|
|
|
|
my %viewhash = ();
|
|
|
|
|
|
|
|
|
|
#load current config
|
|
|
|
|
foreach my $views (split(/;/,AttrVal("yaf","views",undef))) {
|
|
|
|
|
my @view = split(/,/,$views);
|
|
|
|
|
$viewhash{$view[0]} = $view[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#set new config value
|
|
|
|
|
$viewhash{$viewId} = $viewName;
|
|
|
|
|
|
|
|
|
|
#create new config
|
|
|
|
|
my $newview = "";
|
|
|
|
|
foreach my $key (keys %viewhash) {
|
|
|
|
|
$newview .= $key . "," . $viewhash{$key} . ";;";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#save new config
|
|
|
|
|
fhem ("attr yaf views $newview");
|
|
|
|
|
#fhem("save");
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_deleteView - Deletes the view with the given id
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# viewId - The view id to search
|
|
|
|
|
# @return 1 if successful, otherwise 0
|
|
|
|
|
#
|
|
|
|
|
#######################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_deleteView{
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
|
|
|
|
|
my $viewResult = $config->findnodes('//view[@id = '.$viewId.']');
|
|
|
|
|
if($viewResult->size() == 1){
|
|
|
|
|
my $view = $viewResult->get_node(0);
|
|
|
|
|
my $views = $view->parentNode;
|
|
|
|
|
$views->removeChild($view);
|
|
|
|
|
YAF_saveConfiguration();
|
|
|
|
|
return 1;
|
|
|
|
|
} else{
|
|
|
|
|
Log 1,"YAF_deleteView: view with id = ".$viewId." was not found";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
|
|
|
|
|
my %viewhash = ();
|
|
|
|
|
my %backgroundhash = ();
|
|
|
|
|
|
|
|
|
|
delete $fhemviews{$viewId};
|
|
|
|
|
|
|
|
|
|
foreach my $delwidget (keys %{$fhemwidgets{$viewId}}) {
|
|
|
|
|
YAF_deleteWidget($viewId,$delwidget);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete $fhemwidgets{$viewId};
|
|
|
|
|
|
|
|
|
|
my $userattr = AttrVal("global","userattr",undef);
|
|
|
|
|
my $newuserattr = "";
|
|
|
|
|
foreach my $attr (split (/ /,$userattr)) {
|
|
|
|
|
if($attr ne "yaf_$viewId") {
|
|
|
|
|
$newuserattr .= $attr . " ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#load current config
|
|
|
|
|
foreach my $views (split(/;/,AttrVal("yaf","views",undef))) {
|
|
|
|
|
my @view = split(/,/,$views);
|
|
|
|
|
$viewhash{$view[0]} = $view[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach my $bgs (split(/;/,AttrVal("yaf","backgrounds",undef))) {
|
|
|
|
|
my @bg = split(/,/,$bgs);
|
|
|
|
|
$backgroundhash{$bg[0]} = $bg[1] . "," . $bg[2] . "," . $bg[3];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#create new config, leave out the deleted view
|
|
|
|
|
my $newview = "";
|
|
|
|
|
foreach my $key (keys %viewhash) {
|
|
|
|
|
if($key ne $viewId) {
|
|
|
|
|
$newview .= $key . "," . $viewhash{$key} . ";;";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $newbackground = "";
|
|
|
|
|
foreach my $key (keys %backgroundhash) {
|
|
|
|
|
if($key ne $viewId) {
|
|
|
|
|
$newbackground .= $key . "," . $backgroundhash{$key} . ";;";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(length($newview) == 0) { #remove the attributes if they are empty
|
|
|
|
|
fhem("deleteattr yaf views");
|
|
|
|
|
fhem("deleteattr yaf backgrounds");
|
|
|
|
|
fhem ("attr global userattr $newuserattr");
|
|
|
|
|
} else {
|
|
|
|
|
#save new config
|
|
|
|
|
fhem ("attr yaf views $newview");
|
|
|
|
|
fhem ("attr yaf backgrounds $newbackground");
|
|
|
|
|
fhem ("attr global userattr $newuserattr");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#fhem("save");
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_addView - Add the view with the given id
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# viewId - The view id to search
|
|
|
|
|
# @return 1 if successful, otherwise 0
|
|
|
|
|
#
|
|
|
|
|
#######################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_addView{
|
|
|
|
|
my $viewName = $_[0];
|
|
|
|
|
|
|
|
|
|
#-- determine id for new element
|
|
|
|
|
my $newId = 0;
|
|
|
|
|
my @views = $config->findnodes('//view');
|
|
|
|
|
|
|
|
|
|
foreach my $view (@views){
|
|
|
|
|
my $tempId = $view->findvalue('@id');
|
|
|
|
|
|
|
|
|
|
if($newId < $tempId){
|
|
|
|
|
$newId = $tempId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$newId++;
|
|
|
|
|
|
|
|
|
|
#-- initialize view and append to document
|
|
|
|
|
my $view = $config->createElement('view');
|
|
|
|
|
$view->setAttribute('id', $newId);
|
|
|
|
|
$view->setAttribute('name', $viewName);
|
|
|
|
|
#-- set default background
|
|
|
|
|
my $backgrounds = $config->createElement('backgrounds');
|
|
|
|
|
my $background = $config->createElement('background');
|
|
|
|
|
$background->setAttribute('img_url', "./img/background.png");
|
|
|
|
|
$background->setAttribute('x_pos', 1);
|
|
|
|
|
$background->setAttribute('y_pos', 1);
|
|
|
|
|
$backgrounds->appendChild($background);
|
|
|
|
|
$view->appendChild($backgrounds);
|
|
|
|
|
#-- initialize empty widgets node
|
|
|
|
|
my $widgets = $config->createElement('widgets');
|
|
|
|
|
$view->appendChild($widgets);
|
|
|
|
|
|
|
|
|
|
#-- add new view to configuration
|
|
|
|
|
my $parent = $config->findnodes('//views')->get_node(0);
|
|
|
|
|
$parent->appendChild($view);
|
|
|
|
|
|
|
|
|
|
YAF_saveConfiguration();
|
|
|
|
|
return 1;
|
|
|
|
|
my $viewName = $_[0];
|
|
|
|
|
|
|
|
|
|
my %viewhash = ();
|
|
|
|
|
my %backgroundhash = ();
|
|
|
|
|
|
|
|
|
|
#-- determine id for new element
|
|
|
|
|
my $newId = 0;
|
|
|
|
|
my @views = sort {$a <=> $b} keys %fhemviews;
|
|
|
|
|
|
|
|
|
|
foreach my $view (@views){
|
|
|
|
|
my $tempId = $view;
|
|
|
|
|
|
|
|
|
|
if($newId < $tempId){
|
|
|
|
|
$newId = $tempId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$newId++;
|
|
|
|
|
|
|
|
|
|
my $newuserattr = AttrVal("global","userattr","") . " yaf_" . $newId;
|
|
|
|
|
my $newview = $newId . "," . $viewName. ";" .AttrVal("yaf","views","");
|
|
|
|
|
my $newbackground = $newId . ",1,1,FILENAME;" . AttrVal("yaf","backgrounds","");
|
|
|
|
|
|
|
|
|
|
#escape ";"
|
|
|
|
|
$newview =~ s/;/;;/g;
|
|
|
|
|
$newbackground =~ s/;/;;/g;
|
|
|
|
|
|
|
|
|
|
#save new config
|
|
|
|
|
$fhemviews{$newId} = $viewName;
|
|
|
|
|
fhem ("attr yaf views $newview");
|
|
|
|
|
fhem ("attr yaf backgrounds $newbackground");
|
|
|
|
|
fhem ("attr global userattr $newuserattr");
|
|
|
|
|
#fhem("save");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_addWidget - Add widget the the view with the given id
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# Parameters:
|
|
|
|
|
# widgetName The name of the new widget
|
|
|
|
|
# xPos The x coordinate of the widget position
|
|
|
|
|
@@ -281,58 +312,57 @@ sub YAF_addView{
|
|
|
|
|
#########################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_addWidget{
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
my $widgetName = $_[1];
|
|
|
|
|
my $xPos = $_[2];
|
|
|
|
|
my $yPos = $_[3];
|
|
|
|
|
my @attributesArray = @{$_[4]};
|
|
|
|
|
|
|
|
|
|
my $viewsResult = $config->findnodes('//view[@id = '.$viewId.']');
|
|
|
|
|
if($viewsResult->size() == 1){
|
|
|
|
|
my $view = $viewsResult->get_node(0);
|
|
|
|
|
|
|
|
|
|
#-- create a new widget with given properties
|
|
|
|
|
my $widget = $config->createElement('widget');
|
|
|
|
|
$widget->setAttribute('name', $widgetName);
|
|
|
|
|
$widget->setAttribute('x_pos', $xPos);
|
|
|
|
|
$widget->setAttribute('y_pos', $yPos);
|
|
|
|
|
my @widgets = $view->findnodes('widgets/widget');
|
|
|
|
|
my $newId = 0;
|
|
|
|
|
|
|
|
|
|
foreach my $currentWidget (@widgets){
|
|
|
|
|
my $tempId = $currentWidget->findvalue('@id');
|
|
|
|
|
|
|
|
|
|
if($newId < $tempId){
|
|
|
|
|
$newId = $tempId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$newId++;
|
|
|
|
|
$widget->setAttribute('id', $newId);
|
|
|
|
|
|
|
|
|
|
#-- add widgets attribute nodes
|
|
|
|
|
foreach my $attribute (@attributesArray){
|
|
|
|
|
my $attr = $config->createElement('attr');
|
|
|
|
|
$attr->setAttribute('name', @$attribute[0]);
|
|
|
|
|
$attr->setAttribute('value', @$attribute[1]);
|
|
|
|
|
$widget->appendChild($attr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#-- append the new widget to the configuration
|
|
|
|
|
my $widgetsNode = $view->findnodes('widgets')->get_node(0);
|
|
|
|
|
$widgetsNode->appendChild($widget);
|
|
|
|
|
|
|
|
|
|
YAF_saveConfiguration();
|
|
|
|
|
return $newId;
|
|
|
|
|
} else{
|
|
|
|
|
Log 1,"YAF_addWidget: view with id = ".$viewId." was not found";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if($_[0] ne "null") { #if you want to add a widget, but there is no view
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
my $widgetName = $_[1];
|
|
|
|
|
my $xPos = $_[2];
|
|
|
|
|
my $yPos = $_[3];
|
|
|
|
|
my @attributesArray = @{$_[4]};
|
|
|
|
|
|
|
|
|
|
my $widgetString = "name=" . $widgetName . ",x_pos=" . $xPos . ",y_pos=" . $yPos;
|
|
|
|
|
|
|
|
|
|
my $newId = 0;
|
|
|
|
|
my @sortedWidgets = sort {$a <=> $b} (keys %{$fhemwidgets{$viewId}});
|
|
|
|
|
foreach my $currentWidget (@sortedWidgets) {
|
|
|
|
|
my $tempId = $currentWidget;
|
|
|
|
|
|
|
|
|
|
if($newId < $tempId){
|
|
|
|
|
$newId = $tempId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$newId++;
|
|
|
|
|
$widgetString = "id=" . $newId . "," . $widgetString; #put id as first attribute
|
|
|
|
|
|
|
|
|
|
#-- add widgets attributes
|
|
|
|
|
my $fhemname = "";
|
|
|
|
|
foreach my $attribute (@attributesArray){
|
|
|
|
|
$widgetString .= "," . @$attribute[0] . "=" . @$attribute[1];
|
|
|
|
|
|
|
|
|
|
if(@$attribute[0] eq "fhemname") {
|
|
|
|
|
$fhemname = @$attribute[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#-- append the new widget to the configuration
|
|
|
|
|
$fhemwidgets{$viewId}{$newId} = $fhemname;
|
|
|
|
|
if(defined AttrVal($fhemname,"yaf_$viewId",undef)) {
|
|
|
|
|
Log 3, "Device $fhemname has already been added to view " . $fhemviews{$viewId};
|
|
|
|
|
$newId = 0;
|
|
|
|
|
} else {
|
|
|
|
|
fhem("attr $fhemname yaf_$viewId $widgetString");
|
|
|
|
|
#fhem("save");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $newId;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_deleteWidget - Delete the Widget
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# Parameters
|
|
|
|
|
# viewId - The view id to search
|
|
|
|
|
# widgetId - The widget id
|
|
|
|
|
@@ -341,49 +371,43 @@ sub YAF_addWidget{
|
|
|
|
|
#######################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_deleteWidget{
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
my $widgetId = $_[1];
|
|
|
|
|
|
|
|
|
|
my $widgetResult = $config->findnodes('//view[@id = '.$viewId.']/widgets/widget[@id = '.$widgetId.']');
|
|
|
|
|
if($widgetResult->size() == 1){
|
|
|
|
|
my $widget = $widgetResult->get_node(0);
|
|
|
|
|
my $widgets = $widget->parentNode;
|
|
|
|
|
$widgets->removeChild($widget);
|
|
|
|
|
|
|
|
|
|
YAF_saveConfiguration();
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
my $widgetId = $_[1];
|
|
|
|
|
|
|
|
|
|
my $widgetname = $fhemwidgets{$viewId}{$widgetId};
|
|
|
|
|
|
|
|
|
|
delete $fhemwidgets{$viewId}{$widgetId};
|
|
|
|
|
|
|
|
|
|
fhem("deleteattr $widgetname yaf_$viewId");
|
|
|
|
|
#fhem("save");
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
} else{
|
|
|
|
|
Log 1,"YAF_deleteWidget: widget with id = ".$widgetId." in view with id = ".$viewId." was not found";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_isWidget - test, if a FHEM device name is already a widget
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# viewId - The view id to search
|
|
|
|
|
# fhemname - the name of a FHEM device
|
|
|
|
|
#
|
|
|
|
|
########################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_isWidget {
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
my $fhemname = $_[1];
|
|
|
|
|
my $ret = 0;
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
my $fhemname = $_[1];
|
|
|
|
|
|
|
|
|
|
my $widgetResult = $config->findnodes('//view[@id = '.$viewId.']/widgets/widget/attr[@value = "'.$fhemname.'"]');
|
|
|
|
|
$ret = 1
|
|
|
|
|
if($widgetResult->size() != 0);
|
|
|
|
|
#Log 1,"YAF_isWidget: Checking with XPath //view[\@id = ".$viewId."]/widgets/widget/attr[\@value = \"".$fhemname."\"] => $ret";
|
|
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
if(defined AttrVal($fhemname,"yaf_".$viewId,undef)) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_setWidgetPosition - Sets the position (x, y) of the widget to the given values
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# Parameters
|
|
|
|
|
# viewId - The view id to search
|
|
|
|
|
# widgetId - The widget id
|
|
|
|
|
@@ -394,29 +418,37 @@ sub YAF_isWidget {
|
|
|
|
|
#######################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_setWidgetPosition{
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
my $widgetId = $_[1];
|
|
|
|
|
my $xPos = $_[2];
|
|
|
|
|
my $yPos = $_[3];
|
|
|
|
|
|
|
|
|
|
my $widgetResult = $config->findnodes('//view[@id = '.$viewId.']/widgets/widget[@id = '.$widgetId.']');
|
|
|
|
|
if($widgetResult->size() == 1){
|
|
|
|
|
my $widget = $widgetResult->get_node(0);
|
|
|
|
|
$widget->setAttribute('x_pos', $xPos);
|
|
|
|
|
$widget->setAttribute('y_pos', $yPos);
|
|
|
|
|
|
|
|
|
|
YAF_saveConfiguration();
|
|
|
|
|
return 1;
|
|
|
|
|
} else{
|
|
|
|
|
Log 1,"YAF_setWidgetPosition: widget with id = ".$widgetId." in view with id = ".$viewId." was not found";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
my $widgetId = $_[1];
|
|
|
|
|
my $xPos = $_[2];
|
|
|
|
|
my $yPos = $_[3];
|
|
|
|
|
|
|
|
|
|
my $widgetname = $fhemwidgets{$viewId}{$widgetId};
|
|
|
|
|
my %attrhash = ();
|
|
|
|
|
|
|
|
|
|
foreach my $attrs (split (/,/,AttrVal($widgetname, "yaf_".$viewId, undef))) {
|
|
|
|
|
my @attr = split(/=/,$attrs);
|
|
|
|
|
$attrhash{$attr[0]} = $attr[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attrhash{x_pos} = $xPos;
|
|
|
|
|
$attrhash{y_pos} = $yPos;
|
|
|
|
|
|
|
|
|
|
my $newattr = "id=" . $widgetId . ",";
|
|
|
|
|
foreach my $key (keys %attrhash) {
|
|
|
|
|
if ($key ne "id") {
|
|
|
|
|
$newattr .= $key."=".$attrhash{$key}.",";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fhem("attr $widgetname yaf_$viewId $newattr");
|
|
|
|
|
#fhem("save");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_getWidgetAttribute - Searches the widget attribute properties of the specified widget
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# Parameters
|
|
|
|
|
# viewId - The view id to search
|
|
|
|
|
# widgetId - The widget id
|
|
|
|
|
@@ -427,39 +459,57 @@ sub YAF_setWidgetPosition{
|
|
|
|
|
#######################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_getWidgetAttribute{
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
my $widgetId = $_[1];
|
|
|
|
|
my $attributeName = $_[2];
|
|
|
|
|
|
|
|
|
|
my $attributes = $config->findnodes('//view[@id = '.$viewId.']/widgets/widget[@id = '.$widgetId.']/attr');
|
|
|
|
|
|
|
|
|
|
foreach my $attr (@{$attributes}){
|
|
|
|
|
if ($attr->getAttribute('name') eq $attributeName) {
|
|
|
|
|
return $attr->getAttribute('value');
|
|
|
|
|
my $viewId = $_[0];
|
|
|
|
|
my $widgetId = $_[1];
|
|
|
|
|
my $attributeName = $_[2];
|
|
|
|
|
|
|
|
|
|
my $retAttr = "";
|
|
|
|
|
my $widgetName = "";
|
|
|
|
|
|
|
|
|
|
if(defined $fhemwidgets{$viewId}{$widgetId}) {
|
|
|
|
|
$widgetName = $fhemwidgets{$viewId}{$widgetId};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if("fhemname" eq $attributeName) { #special case: get the fhemname
|
|
|
|
|
$retAttr = $widgetName; #the key is the name of the device
|
|
|
|
|
} else {
|
|
|
|
|
my $attrString = AttrVal($widgetName,"yaf_$viewId",undef);
|
|
|
|
|
if(defined $attrString) {
|
|
|
|
|
my @tokens = split(/,/,$attrString);
|
|
|
|
|
foreach my $akey (@tokens) { #cycle through the other values
|
|
|
|
|
my @skey = split(/=/, $akey); #split them for =
|
|
|
|
|
if($skey[0] eq $attributeName) { #the first value is the key, if it is the wanted attribute
|
|
|
|
|
$retAttr = $skey[1]; #return it.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(length $retAttr > 0) {
|
|
|
|
|
return $retAttr; #return the found config
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Log 1,"YAF_getWidgetAttribute: attribute $attributeName was not found for widget with id = $widgetId";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_getRefreshTime - Get refresh time interval
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# @return time successful, otherwise 0
|
|
|
|
|
#
|
|
|
|
|
#######################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_getRefreshTime{
|
|
|
|
|
my $refreshNodeResult = $config->findnodes('configuration/settings/refresh');
|
|
|
|
|
if($refreshNodeResult->size() == 1){
|
|
|
|
|
my $refreshNode = $refreshNodeResult->get_node(0);
|
|
|
|
|
my $refreshTime = $refreshNode->getAttribute('interval');
|
|
|
|
|
return $refreshTime;
|
|
|
|
|
} else{
|
|
|
|
|
Log 1,"YAF_getRefreshTime: refresh node was not found";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
my $ret = AttrVal("yaf","refresh_interval",undef);
|
|
|
|
|
if (defined $ret) {
|
|
|
|
|
return $ret;
|
|
|
|
|
} else {
|
|
|
|
|
Log 1,"YAF_getRefreshTime: refresh_interval attribute was not found (so it was created with a default value)";
|
|
|
|
|
fhem("attr yaf refresh_interval 60");
|
|
|
|
|
fhem("save");
|
|
|
|
|
return 60;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
@@ -470,38 +520,16 @@ sub YAF_getRefreshTime{
|
|
|
|
|
#######################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_setRefreshTime{
|
|
|
|
|
my $newRefreshInterval = $_[0];
|
|
|
|
|
|
|
|
|
|
my $refreshNodeResult = $config->findnodes('configuration/settings/refresh');
|
|
|
|
|
if(($newRefreshInterval =~ /^\d+$/) && ($refreshNodeResult->size() == 1)){
|
|
|
|
|
my $refreshNode = $refreshNodeResult->get_node(0);
|
|
|
|
|
$refreshNode->setAttribute('interval', $newRefreshInterval);
|
|
|
|
|
|
|
|
|
|
YAF_saveConfiguration();
|
|
|
|
|
return 1;
|
|
|
|
|
} else{
|
|
|
|
|
Log 1,"YAF_setRefreshTime: no valid refresh value or refresh node was not found";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
my $newRefreshInterval = $_[0];
|
|
|
|
|
|
|
|
|
|
if($newRefreshInterval =~ /^\d+$/) {
|
|
|
|
|
fhem("attr yaf refresh_interval $newRefreshInterval");
|
|
|
|
|
#fhem("save");
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
Log 1,"YAF_setRefreshTime: no valid refresh value or refresh node was not found";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
#
|
|
|
|
|
# YAF_saveConfiguration - Save XML configuration file
|
|
|
|
|
#
|
|
|
|
|
# no parameter
|
|
|
|
|
# @return 1 if successful, otherwise 0
|
|
|
|
|
#
|
|
|
|
|
#######################################################################################
|
|
|
|
|
|
|
|
|
|
sub YAF_saveConfiguration{
|
|
|
|
|
my $state = 0;
|
|
|
|
|
|
|
|
|
|
if(YAF_validate() == 1){
|
|
|
|
|
$prettyPrinter->pretty_print($config);
|
|
|
|
|
$state = $config->toFile("$configurationFilepath");
|
|
|
|
|
}
|
|
|
|
|
return $state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
1;
|
|
|
|
|
|