From ed2aa8c01366f16ff4bb1f5eb5fa02685f33d832 Mon Sep 17 00:00:00 2001 From: fhemzap Date: Mon, 19 Sep 2016 18:14:32 +0000 Subject: [PATCH] HMCCU: Bugfix and autocreate git-svn-id: https://svn.fhem.de/fhem/trunk@12176 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/88_HMCCU.pm | 102 +++++++++++++++++++++++++++++++++++------- 2 files changed, 86 insertions(+), 17 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 3cd4390e0..60674dfa7 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - change: 88_HMCCU: fixed bug in attribute stripnumber. Added autocreate - change: 49_SSCam: internal timer of start-routines optimized - bugfix: 88_HMCCUCHN: fixed bug in toggle command - bugfix: 88_HMCCUDEV: fixed bug in toggle command diff --git a/fhem/FHEM/88_HMCCU.pm b/fhem/FHEM/88_HMCCU.pm index 2d9380aca..05a6034a9 100755 --- a/fhem/FHEM/88_HMCCU.pm +++ b/fhem/FHEM/88_HMCCU.pm @@ -23,6 +23,8 @@ # get configdesc {|} # get deviceinfo # get devicelist [dump] +# get devicelist create [s=] [p=] [f=] +# [= [...]]}] # get dump {devtypes|datapoints} [] # get parfile [] # get rpcevents @@ -337,11 +339,11 @@ sub HMCCU_SetDefaults ($) return 0 if (!exists ($HMCCU_DEV_DEFAULTS->{$ccutype})); foreach my $a (keys %{$HMCCU_DEV_DEFAULTS->{$ccutype}}) { - $attr{$name}{$a} = $HMCCU_DEV_DEFAULTS->{$ccutype}{$a}; + CommandAttr (undef, "$name, $a ".$HMCCU_DEV_DEFAULTS->{$ccutype}{$a}); } # Set standard attributes - $attr{$name}{'event-on-change-reading'} = '.*'; + CommandAttr (undef, "$name event-on-change-reading .*"); return 1; } @@ -736,20 +738,76 @@ sub HMCCU_Get ($@) return "RPC process not running"; } elsif ($opt eq 'devicelist') { - my $dumplist = shift @a; - $hash->{DevCount} = HMCCU_GetDeviceList ($hash); return HMCCU_SetError ($hash, -2) if ($hash->{DevCount} < 0); return HMCCU_SetError ($hash, "No devices received from CCU") if ($hash->{DevCount} == 0); - HMCCU_SetState ($hash, "OK"); + $result = "Read ".$hash->{DevCount}." devices/channels from CCU"; - if (defined ($dumplist) && $dumplist eq 'dump') { - foreach my $add (sort keys %HMCCU_Devices) { - $result .= $HMCCU_Devices{$add}{name}."\n"; + my $optcmd = shift @a; + if (defined ($optcmd)) { + if ($optcmd eq 'dump') { + foreach my $add (sort keys %HMCCU_Devices) { + $result .= $HMCCU_Devices{$add}{name}."\n"; + } + return $result; + } + elsif ($optcmd eq 'create') { + my $devprefix = ''; + my $devsuffix = ''; + my $devformat = '%n'; + my $newcount = 0; + my @devattr; + + my $devspec = shift @a; + return "Please specify expression for CCU device or channel names" + if (!defined ($devspec)); + + foreach my $defopt (@a) { + if ($defopt =~ /^s=(.+)$/) { + $devsuffix = $1; + } + elsif ($defopt =~ /^p=(.+)$/) { + $devprefix = $1; + } + elsif ($defopt =~ /^f=(.+)$/) { + $devformat = $1; + } + else { + push (@devattr, $defopt); + } + } + + foreach my $add (sort keys %HMCCU_Devices) { + my $defmod = $HMCCU_Devices{$add}{addtype} eq 'dev' ? 'HMCCUDEV' : 'HMCCUCHN'; + my $ccuname = $HMCCU_Devices{$add}{name}; + my $ccudevname = HMCCU_GetDeviceName ($add, $ccuname); + next if ($ccuname !~ /$devspec/); + my $devname = $devformat; + $devname = $devprefix.$devname.$devsuffix; + $devname =~ s/%n/$ccuname/g; + $devname =~ s/%d/$ccudevname/g; + $devname =~ s/%a/$add/g; + $devname =~ s/[^A-Za-z\d_\.]+/_/g; + my $ret = CommandDefine (undef, $devname." $defmod ".$ccuname); + if ($ret) { + Log3 $name, 2, "HMCCU: Define command failed $devname $defmod $ccuname"; + Log3 $name, 2, "$defmod: $ret"; + next; + } + foreach my $da (@devattr) { + my ($at, $vl) = split ('=', $da); + CommandAttr (undef, "$devname $at $vl") if (defined ($vl)); + } + Log3 $name, 2, "$defmod: Created device $devname"; + $newcount++; + } + + $result .= ", created $newcount client devices"; } - return $result; } - return "Read ".$hash->{DevCount}." devices/channels from CCU"; + + HMCCU_SetState ($hash, "OK"); + return $result; } elsif ($opt eq 'configdesc') { my $ccuobj = shift @a; @@ -1016,7 +1074,8 @@ sub HMCCU_GetReadingName ($$$$$$$) } ################################################################## -# Format reading value depending attribute stripnumber +# Format reading value depending on attribute stripnumber. Integer +# values are ignored. # 0 = Preserve all digits # 1 = Preserve 1 digit # 2 = Remove trailing zeroes @@ -1028,13 +1087,13 @@ sub HMCCU_FormatReadingValue ($$) my ($hash, $value) = @_; my $stripnumber = AttrVal ($hash->{NAME}, 'stripnumber', '0'); + return $value if ($stripnumber eq '0' || $value !~ /\.[0-9]+$/); if ($stripnumber eq '1') { - $value =~ s/(\.[0-9])[0-9]+/$1/; + return sprintf ("%.1f", $value); } elsif ($stripnumber eq '2') { - $value =~ s/[0]+$//; - $value =~ s/\.$//; + return sprintf ("%g", $value); } elsif ($stripnumber =~ /^-([0-9])$/) { my $fmt = '%.'.$1.'f'; @@ -4491,13 +4550,22 @@ sub HMCCU_CCURPC_ListDevicesCB ($$) queried directly. Otherwise device information from CCU is listed.
  • get <name> devicelist [dump]
    + get <name> devicelist create <devexp>: [p=<prefix>] [s=<suffix>] + [f=<format>] [<attr>=<value> [...]]
    Read list of devices and channels from CCU. This command is executed automatically - after device the definition of an I/O device. It must be executed manually after + after the definition of an I/O device. It must be executed manually after module HMCCU is reloaded or after devices have changed in CCU (added, removed or - renamed). With option dump devices are displayed in browser window. If a RPC + renamed). With option 'dump' devices are displayed in browser window. If a RPC server is running HMCCU will raise events "count devices added in CCU" or "count devices deleted in CCU". It's recommended to set up a notification - which reacts with execution of command 'get devicelist' on these events. + which reacts with execution of command 'get devicelist' on these events.
    + With option 'create' HMCCU will automatically create client devices for all CCU devices + and channels matching specified regular expression. Optionally a prefix and/or a + suffix for the FHEM device name can pe specified. The parameter format + defines a template for the FHEM device names. Prefix, suffix and format can contain + format identifiers which are substituted by corresponding values of the CCU device or + channel: %n = CCU object name (channel or device), %d = CCU device name, %a = CCU address. + In addition a list of default attributes for the created client devices can be specified.

  • get <name> parfile [<parfile>]
    Get values of all channels / datapoints specified in parfile. The parameter