YAFConfig: config loads without refreshing the page, code refactoring, fht80 feature: change size of text with "size" yaf-attribute (set "em" size like "size=0.6")

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@3522 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
marcproe 2013-07-28 15:01:32 +00:00
parent 8b3bf46858
commit 7ae7f97a17
4 changed files with 98 additions and 74 deletions

View File

@ -31,6 +31,7 @@ my $yaf_version=0.41;
my %fhemwidgets; my %fhemwidgets;
my %fhemviews; my %fhemviews;
my $isInit = 0;
####################################################################################### #######################################################################################
# #
@ -44,7 +45,7 @@ my %fhemviews;
sub YAF_FHEMConfig { #this is called via ajax when the page is loaded. sub YAF_FHEMConfig { #this is called via ajax when the page is loaded.
#get the views #get the views
my $views = AttrVal("yaf","views",undef); my $views = AttrVal("yaf","views",undef);
if(defined $views) { if(defined $views and $isInit == 0) {
foreach my $view (split (/;/,$views)) { foreach my $view (split (/;/,$views)) {
my @aview = split(/,/,$view); my @aview = split(/,/,$view);
$fhemviews{$aview[0]} = $aview[1]; $fhemviews{$aview[0]} = $aview[1];
@ -67,6 +68,9 @@ sub YAF_FHEMConfig { #this is called via ajax when the page is loaded.
} else { } else {
return 0; return 0;
} }
Log 3, "YAF initialized";
$isInit = 1;
return 1;
} }
####################################################################################### #######################################################################################
@ -453,6 +457,7 @@ sub YAF_setWidgetPosition{
# viewId - The view id to search # viewId - The view id to search
# widgetId - The widget id # widgetId - The widget id
# attributeName - name of the attribute properties to search for # attributeName - name of the attribute properties to search for
# default - Value to return on undefined
# #
# @return The value property if successful, otherwise 0 # @return The value property if successful, otherwise 0
# #
@ -462,7 +467,12 @@ sub YAF_getWidgetAttribute{
my $viewId = $_[0]; my $viewId = $_[0];
my $widgetId = $_[1]; my $widgetId = $_[1];
my $attributeName = $_[2]; my $attributeName = $_[2];
my $default = (defined $_[3]) ? $_[3] : 0;
if($isInit == 0) { #after a restart of fhem the config hashes might be empty, because they are filled while
YAF_FHEMConfig(); #loading the "yaf.htm" page. However, when restarting FHEM without reloading the page, there
} #will be lots of errors. Since this method is called by any update method, we check if YAF
#is initialized and load the config, if not.
my $retAttr = ""; my $retAttr = "";
my $widgetName = ""; my $widgetName = "";
@ -470,7 +480,7 @@ sub YAF_getWidgetAttribute{
$widgetName = $fhemwidgets{$viewId}{$widgetId}; $widgetName = $fhemwidgets{$viewId}{$widgetId};
} }
if("fhemname" eq $attributeName) { #special case: get the fhemname if("fhemname" eq $attributeName) { #special case: get the fhemname
$retAttr = $widgetName; #the key is the name of the device $retAttr = $widgetName; #the key is the name of the device
} else { } else {
my $attrString = AttrVal($widgetName,"yaf_$viewId",undef); my $attrString = AttrVal($widgetName,"yaf_$viewId",undef);
@ -488,7 +498,7 @@ sub YAF_getWidgetAttribute{
if(length $retAttr > 0) { if(length $retAttr > 0) {
return $retAttr; #return the found config return $retAttr; #return the found config
} else { } else {
return 0; return $default;
} }
} }
@ -505,7 +515,7 @@ sub YAF_getRefreshTime{
if (defined $ret) { if (defined $ret) {
return $ret; return $ret;
} else { } else {
Log 1,"YAF_getRefreshTime: refresh_interval attribute was not found (so it was created with a default value)"; Log 1,"YAF_getRefreshTime: refresh_interval attribute was not found (so it will be created with a default value)";
fhem("attr yaf refresh_interval 60"); fhem("attr yaf refresh_interval 60");
fhem("save"); fhem("save");
return 60; return 60;
@ -527,7 +537,7 @@ sub YAF_setRefreshTime{
#fhem("save"); #fhem("save");
return 1; return 1;
} else { } else {
Log 1,"YAF_setRefreshTime: no valid refresh value or refresh node was not found"; Log 1,"YAF_setRefreshTime: no valid refresh value or refresh attribute was not found";
return 0; return 0;
} }
} }

View File

@ -54,7 +54,7 @@ sub fht80_get_widgetcss() {
text-align: center; text-align: center;
} }
.widget_fht80_alias { .widget_fht80_alias {
font-size: 12px; font-size: 0.7em;
} }
"; ";
return $output; return $output;
@ -78,8 +78,12 @@ sub fht80_get_widgetjs() {
url: "../../ajax/widget/fht80/get_temp", url: "../../ajax/widget/fht80/get_temp",
data: "view_id="+view_id+"&widget_id="+widget_id, data: "view_id="+view_id+"&widget_id="+widget_id,
context: document.body, context: document.body,
success: function(get_temp) { success: function(get_data) {
var data = jQuery.parseJSON(get_data);
var get_temp = data[0];
var sizefac = data[1];
var widget = $("#widget_"+view_id+"_"+widget_id); var widget = $("#widget_"+view_id+"_"+widget_id);
widget.css("font-size", sizefac+"em");
widget.html(get_temp); widget.html(get_temp);
} }
}); });
@ -183,52 +187,57 @@ sub fht80_getwidget_html() {
######################################################################################## ########################################################################################
sub fht80_get_temp() { sub fht80_get_temp() {
my $attribute = YAF_getWidgetAttribute($_GET{"view_id"}, $_GET{"widget_id"}, "fhemname"); my @ret = ();
if(!($attribute)) { my $viewid = $_GET{"view_id"};
return("Widget not found ".$_GET{"view_id"}." ".$_GET{"widget_id"}); my $widgetid = $_GET{"widget_id"};
my $fhemname = YAF_getWidgetAttribute($viewid, $widgetid, "fhemname", undef);
if(!defined $fhemname) {
$ret[0] = "Widget not found " . $viewid . " " . $widgetid;
$ret[1] = 1;
return(encode_json(\@ret));
} }
my $labeltype = YAF_getWidgetAttribute($_GET{"view_id"}, $_GET{"widget_id"}, "labeltype"); # get all the needed data
my $temp = fht80_isdef($defs{$fhemname}{READINGS}{temperature}{VAL}, 0);
my $temptimestamp = fht80_isdef($defs{$fhemname}{READINGS}{temperature}{TIME}, "");
my $actuator = fht80_isdef($defs{$fhemname}{READINGS}{actuator}{VAL}, "");
my $mode = fht80_isdef($defs{$fhemname}{READINGS}{mode}{VAL}, "none");
my $desi = fht80_isdef($defs{$fhemname}{READINGS}{"desired-temp"}{VAL}, "");
my $battery = fht80_isdef($defs{$fhemname}{READINGS}{battery}{VAL}, "");
my $nomode = YAF_getWidgetAttribute($viewid, $widgetid, "nomode", 0);
my $labeltype = YAF_getWidgetAttribute($viewid, $widgetid, "labeltype", "");
$ret[1] = YAF_getWidgetAttribute($viewid, $widgetid, "size", 1); #we don't process the size, so put it in the return array right away.
my $d = $defs{$attribute}; #process data
my $ret = "";
my $temp = $defs{$attribute}{READINGS}{temperature}{VAL};
my $temptimestamp = $defs{$attribute}{READINGS}{temperature}{TIME};
my $actuator = $defs{$attribute}{READINGS}{actuator}{VAL};
my $label = ""; my $label = "";
if($labeltype eq "Comment") { if($labeltype eq "Comment") {
$label = AttrVal($attribute,"comment",$d->{NAME}); $label = AttrVal($fhemname,"comment",$fhemname);
} elsif ($labeltype eq "Alias") { } elsif ($labeltype eq "Alias") {
$label = AttrVal($attribute,"alias",$d->{NAME}); $label = AttrVal($fhemname,"alias",$fhemname);
} else { } else {
$label = $d->{NAME} $label = $fhemname;
} }
$label =~ s/(_)/ /g; $label =~ s/(_)/ /g;
my $mode = $defs{$attribute}{READINGS}{mode}{VAL};
my $desi = $defs{$attribute}{READINGS}{"desired-temp"}{VAL};
my $battery = $defs{$attribute}{READINGS}{battery}{VAL};
if($mode eq "manual") { $mode = ($mode eq "manual") ? " <span title='manual'>&oplus;</span>" : " <span title='$mode'>&otimes;</span>";
$mode = " <span title='manual'>&oplus;</span> "; $desi .= ($desi ne "off") ? " &deg;C" : "";
} else { $battery = ($battery ne "ok") ? "Check Battery: " . $battery . "<br />" : "";
$mode = " <span title='$mode'>&otimes;</span> ";
#create returnstring
$ret[0] = "<span class='widget_fht80_alias'>" . $label . "</span><br />" . $battery;
$ret[0] .= "<span title='$temptimestamp'>" . $temp . "</span>" . " &deg;C";
if($nomode == 0) {
$ret[0] .= $mode;
} }
if($desi ne "off") { if($desi ne "off") {
$desi .= " &deg;C"; $ret[0] .= " <span title='Actuator: $actuator'>" . $desi . "</span>";
} }
if($battery ne "ok") { return encode_json(\@ret);
$battery = "Check Battery: " . $battery . "<br />"; }
} else {
$battery = "";
}
$ret = "<span class='widget_fht80_alias'>" . $label . "</span><br />" . $battery; sub fht80_isdef() {
$ret .= "<span title='$temptimestamp'>" . $temp . "</span>" . " &deg;C" . $mode; return ((defined $_[0]) ? $_[0] : $_[1]);
$ret .= "<span title='Actuator: $actuator'>" . $desi . "</span>";
return $ret;
} }
1; 1;

View File

@ -82,7 +82,7 @@ sub fhttk_get_widgetjs() {
context: document.body, context: document.body,
success: function(get_state) { success: function(get_state) {
var widget = $("#widget_"+view_id+"_"+widget_id); var widget = $("#widget_"+view_id+"_"+widget_id);
$("#widget_"+view_id+"_"+widget_id).attr("title",get_state); $("#widget_"+view_id+"_"+widget_id).attr("title",get_state);
if (endsWith(get_state,"Open")) { if (endsWith(get_state,"Open")) {
if (widget.hasClass("widget_fhttk_closed")) { if (widget.hasClass("widget_fhttk_closed")) {
widget.removeClass("widget_fhttk_closed"); widget.removeClass("widget_fhttk_closed");
@ -90,6 +90,7 @@ sub fhttk_get_widgetjs() {
if (!widget.hasClass("widget_fhttk_open")) { if (!widget.hasClass("widget_fhttk_open")) {
widget.addClass("widget_fhttk_open"); widget.addClass("widget_fhttk_open");
} }
widget.html("<span />");
} }
else if (endsWith(get_state,"Closed")) { else if (endsWith(get_state,"Closed")) {
if (!widget.hasClass("widget_fhttk_closed")) { if (!widget.hasClass("widget_fhttk_closed")) {
@ -98,9 +99,10 @@ sub fhttk_get_widgetjs() {
if (widget.hasClass("widget_fhttk_open")) { if (widget.hasClass("widget_fhttk_open")) {
widget.removeClass("widget_fhttk_open"); widget.removeClass("widget_fhttk_open");
} }
widget.html("<span />");
} }
else { else {
widget.html("Error:"+get_state); widget.html("Error:"+get_state);
} }
} }
}); });
@ -193,14 +195,17 @@ sub fhttk_getwidget_html() {
######################################################################################## ########################################################################################
sub fhttk_get_state() { sub fhttk_get_state() {
my $attribute = YAF_getWidgetAttribute($_GET{"view_id"}, $_GET{"widget_id"}, "fhemname"); my $fhemname = YAF_getWidgetAttribute($_GET{"view_id"}, $_GET{"widget_id"}, "fhemname"); #get name of device
my $d = $defs{$attribute}; my $d = $defs{$fhemname}; #get device
my $name = AttrVal($fhemname,"alias",undef); #get alias
if(!defined $name) { #if alias is defined, use it as name
$name = $fhemname;
}
if(defined $d) { if(defined $d) {
my $ret = $attribute.": ".trim($d->{STATE}); my $ret = $name.": ".$d->{STATE}; #return "name: state"
return $ret; return $ret;
} else { } else {
return "Widget not found. Maybe reload this page?"; return "<span onClick=document.location.reload(true)>Widget not found. Maybe reload this page?</span>";
} }
} }
1; 1;

View File

@ -17,8 +17,8 @@ DIR FHEM/YAF/www/js
DIR FHEM/YAF/xml DIR FHEM/YAF/xml
UPD 2013-07-23_20:45:00 11477 FHEM/01_YAF.pm UPD 2013-07-23_20:45:00 11477 FHEM/01_YAF.pm
UPD 2013-05-15_20:00:00 6590 FHEM/YAF/widgets/fs20st/fs20st.pm UPD 2013-05-15_20:00:00 6590 FHEM/YAF/widgets/fs20st/fs20st.pm
UPD 2013-07-20_16:00:00 6869 FHEM/YAF/widgets/fht80/fht80.pm UPD 2013-07-28_17:00:00 6869 FHEM/YAF/widgets/fht80/fht80.pm
UPD 2013-07-23_20:45:00 6095 FHEM/YAF/widgets/fht80/fhttk.pm UPD 2013-07-28_17:00:00 6095 FHEM/YAF/widgets/fht80/fhttk.pm
UPD 2013-05-15_20:00:00 6617 FHEM/YAF/widgets/fs20easylamp/fs20easylamp.pm UPD 2013-05-15_20:00:00 6617 FHEM/YAF/widgets/fs20easylamp/fs20easylamp.pm
UPD 2013-05-15_20:00:00 2608 FHEM/YAF/www/img/loading.gif UPD 2013-05-15_20:00:00 2608 FHEM/YAF/www/img/loading.gif
UPD 2013-05-15_20:00:00 766 FHEM/YAF/www/img/lamp_off.png UPD 2013-05-15_20:00:00 766 FHEM/YAF/www/img/lamp_off.png
@ -50,5 +50,5 @@ UPD 2013-05-15_20:00:00 101 FHEM/YAF/www/css/smoothness/images/ui-bg_highlight-s
UPD 2013-05-15_20:00:00 26086 FHEM/YAF/www/css/smoothness/jquery-ui-1.9.1.custom.min.css UPD 2013-05-15_20:00:00 26086 FHEM/YAF/www/css/smoothness/jquery-ui-1.9.1.custom.min.css
UPD 2013-05-15_20:00:00 3641 FHEM/YAF/xml/xmlSchema.xsd UPD 2013-05-15_20:00:00 3641 FHEM/YAF/xml/xmlSchema.xsd
UPD 2013-05-15_20:00:00 1690 FHEM/YAF/xml/yafConfig.xml UPD 2013-05-15_20:00:00 1690 FHEM/YAF/xml/yafConfig.xml
UPD 2013-07-23_20:45:00 14744 FHEM/YAF/YAFConfig.pm UPD 2013-07-28_17:00:00 14744 FHEM/YAF/YAFConfig.pm
UPD 2013-05-15_20:00:00 3439 FHEM/YAF/YAFWidgets.pm UPD 2013-05-15_20:00:00 3439 FHEM/YAF/YAFWidgets.pm