HMCCU: Code optimized

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@13786 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
zap 2017-03-24 10:27:06 +00:00
parent 768f146650
commit 2bc2d7ce57
3 changed files with 97 additions and 50 deletions

View File

@ -1,6 +1,7 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # 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. # Do not insert empty lines here, update check depends on it.
- update: 88_HMCCURPC: Adjusted network parameters. - update: 88_HMCCU: Optimized code
- update: 88_HMCCURPC: Optimized error handling
- feature: 74_XiaomiFlowerSens: 1.0.0, add Attribut ssh Support for remote - feature: 74_XiaomiFlowerSens: 1.0.0, add Attribut ssh Support for remote
call, Atrribut min and max Value for Reading- call, Atrribut min and max Value for Reading-
Warnings Warnings

View File

@ -4,7 +4,7 @@
# #
# $Id$ # $Id$
# #
# Version 3.9.009 # Version 3.9.010
# #
# Module for communication between FHEM and Homematic CCU2. # Module for communication between FHEM and Homematic CCU2.
# Supports BidCos-RF, BidCos-Wired, HmIP-RF, virtual CCU channels, # Supports BidCos-RF, BidCos-Wired, HmIP-RF, virtual CCU channels,
@ -101,7 +101,7 @@ my %HMCCU_CUST_CHN_DEFAULTS;
my %HMCCU_CUST_DEV_DEFAULTS; my %HMCCU_CUST_DEV_DEFAULTS;
# HMCCU version # HMCCU version
my $HMCCU_VERSION = '3.9.009'; my $HMCCU_VERSION = '3.9.010';
# RPC Ports and URL extensions # RPC Ports and URL extensions
my %HMCCU_RPC_NUMPORT = ( my %HMCCU_RPC_NUMPORT = (
@ -359,7 +359,7 @@ sub HMCCU_Define ($$)
} }
###################################################################### ######################################################################
# Set attribute # Set or delete attribute
###################################################################### ######################################################################
sub HMCCU_Attr ($@) sub HMCCU_Attr ($@)
@ -528,9 +528,9 @@ sub HMCCU_AggregationRules ($$)
return 1; return 1;
} }
##################################### ######################################################################
# Export default attributes. # Export default attributes.
##################################### ######################################################################
sub HMCCU_ExportDefaults ($) sub HMCCU_ExportDefaults ($)
{ {
@ -617,10 +617,10 @@ sub HMCCU_ImportDefaults ($)
return 1; return 1;
} }
##################################### ######################################################################
# Find default attributes # Find default attributes
# Return template reference. # Return template reference.
##################################### ######################################################################
sub HMCCU_FindDefaults ($$) sub HMCCU_FindDefaults ($$)
{ {
@ -768,9 +768,9 @@ sub HMCCU_GetDefaults ($$)
return $result; return $result;
} }
############################################################ ######################################################################
# Handle FHEM events # Handle FHEM events
############################################################ ######################################################################
sub HMCCU_Notify ($$) sub HMCCU_Notify ($$)
{ {
@ -829,9 +829,10 @@ sub HMCCU_Notify ($$)
return; return;
} }
##################################### ######################################################################
# Calculate reading aggregation # Calculate reading aggregation.
##################################### # Called by Notify or via command get aggregation.
######################################################################
sub HMCCU_AggregateReadings ($$) sub HMCCU_AggregateReadings ($$)
{ {
@ -941,9 +942,9 @@ sub HMCCU_AggregateReadings ($$)
return $result; return $result;
} }
##################################### ######################################################################
# Delete device # Delete device
##################################### ######################################################################
sub HMCCU_Undef ($$) sub HMCCU_Undef ($$)
{ {
@ -964,16 +965,26 @@ sub HMCCU_Undef ($$)
return undef; return undef;
} }
##################################### ######################################################################
# Shutdown FHEM # Shutdown FHEM
##################################### ######################################################################
sub HMCCU_Shutdown ($) sub HMCCU_Shutdown ($)
{ {
my ($hash) = @_; my ($hash) = @_;
my $name = $hash->{NAME};
my $ccuflags = AttrVal ($name, 'ccuflags', 'null');
# Shutdown RPC server # Shutdown RPC server
HMCCU_StopRPCServer ($hash); if ($ccuflags =~ /extrpc/) {
HMCCU_StopExtRPCServer ($hash);
}
else {
HMCCU_StopRPCServer ($hash);
}
# Remove existing timer functions
RemoveInternalTimer ($hash); RemoveInternalTimer ($hash);
return undef; return undef;
@ -988,7 +999,8 @@ sub HMCCU_Set ($@)
my ($hash, $a, $h) = @_; my ($hash, $a, $h) = @_;
my $name = shift @$a; my $name = shift @$a;
my $opt = shift @$a; my $opt = shift @$a;
my $options = "var execute hmscript cleardefaults:noArg defaults:noArg importdefaults rpcserver:on,off"; my $options = "var execute hmscript cleardefaults:noArg defaults:noArg ".
"importdefaults rpcserver:on,off";
my $host = $hash->{host}; my $host = $hash->{host};
if ($opt ne 'rpcserver' && HMCCU_IsRPCStateBlocking ($hash)) { if ($opt ne 'rpcserver' && HMCCU_IsRPCStateBlocking ($hash)) {
@ -1169,9 +1181,9 @@ sub HMCCU_Set ($@)
} }
} }
##################################### ######################################################################
# Get commands # Get commands
##################################### ######################################################################
sub HMCCU_Get ($@) sub HMCCU_Get ($@)
{ {
@ -1466,7 +1478,7 @@ sub HMCCU_Get ($@)
} }
} }
################################################################## ######################################################################
# Parse CCU object specification. # Parse CCU object specification.
# Supports classic Homematic and Homematic-IP addresses. # Supports classic Homematic and Homematic-IP addresses.
# Supports team addresses with leading * for BidCos-RF. # Supports team addresses with leading * for BidCos-RF.
@ -1482,15 +1494,15 @@ sub HMCCU_Get ($@)
# Address:Channel # Address:Channel
# Channelname # Channelname
# #
# If object name doesn't match the rules above object is treated # If object name doesn't match the rules above it's treated as name.
# as name. # With parameter flags one can specify if result is filled up with
# With parameter flags one can specify if result is filled up # default values for interface or datapoint.
# with default values for interface or datapoint.
# #
# Return list of detected attributes: # Return list of detected attributes (empty string if attribute is
# not detected):
# (Interface, Address, Channel, Datapoint, Name, Flags) # (Interface, Address, Channel, Datapoint, Name, Flags)
# Flags is a bitmask of detected attributes. # Flags is a bitmask of detected attributes.
################################################################## ######################################################################
sub HMCCU_ParseObject ($$$) sub HMCCU_ParseObject ($$$)
{ {
@ -1587,11 +1599,11 @@ sub HMCCU_ParseObject ($$$)
return ($i, $a, $c, $d, $n, $f); return ($i, $a, $c, $d, $n, $f);
} }
################################################################## ######################################################################
# Filter reading by datapoint and optionally by channel name or # Filter reading by datapoint and optionally by channel name or
# channel address. # channel address.
# Parameters: hash, channel, datapoint # Parameters: hash, channel, datapoint
################################################################## ######################################################################
sub HMCCU_FilterReading ($$$) sub HMCCU_FilterReading ($$$)
{ {
@ -1651,7 +1663,7 @@ sub HMCCU_FilterReading ($$$)
# #
# Valid combinations: # Valid combinations:
# #
# ChannelNam,Datapoint # ChannelName,Datapoint
# Address,Datapoint # Address,Datapoint
# Address,ChannelNo,Datapoint # Address,ChannelNo,Datapoint
# #

View File

@ -4,16 +4,15 @@
# #
# $Id$ # $Id$
# #
# Version 0.8 beta # Version 0.9 beta
# #
# Thread based RPC Server module for HMCCU. # Thread based RPC Server module for HMCCU.
# #
# (c) 2017 zap (zap01 <at> t-online <dot> de) # (c) 2017 by zap (zap01 <at> t-online <dot> de)
# #
############################################################################## ##############################################################################
# Requires modules: # Requires modules:
# #
# 88_HMCCU.pm
# threads # threads
# Thread::Queue # Thread::Queue
# Time::HiRes # Time::HiRes
@ -36,13 +35,37 @@ use RPC::XML::Server;
use SetExtensions; use SetExtensions;
# HMCCU version ######################################################################
my $HMCCURPC_VERSION = '0.8 beta'; # Constants
######################################################################
# Maximum number of errors during TriggerIO() # HMCCURPC version
my $HMCCURPC_VERSION = '0.9 beta';
# Maximum number of events processed per call of Read()
my $HMCCURPC_MAX_EVENTS = 50;
# Maximum number of errors during TriggerIO() before log message is written
my $HMCCURPC_MAX_IOERRORS = 100; my $HMCCURPC_MAX_IOERRORS = 100;
# Maximum number of elements in queue
my $HMCCURPC_MAX_QUEUESIZE = 500; my $HMCCURPC_MAX_QUEUESIZE = 500;
# Time to wait after data processing loop
my $HMCCURPC_TIME_WAIT = 100000;
# Time to wait before calling TriggerIO() again after I/O error
my $HMCCURPC_TIME_TRIGGER = 10;
# Timeout for established CCU connection
my $HMCCURPC_TIMEOUT_CONNECTION = 10;
# Timeout for TriggerIO()
my $HMCCURPC_TIMEOUT_WRITE = 0.001;
# Timeout for accepting incoming connections
my $HMCCURPC_TIMEOUT_ACCEPT = 1;
# RPC Ports and URL extensions # RPC Ports and URL extensions
my %HMCCURPC_RPC_NUMPORT = ( my %HMCCURPC_RPC_NUMPORT = (
2000 => 'BidCos-Wired', 2001 => 'BidCos-RF', 2010 => 'HmIP-RF', 9292 => 'VirtualDevices', 2000 => 'BidCos-Wired', 2001 => 'BidCos-RF', 2010 => 'HmIP-RF', 9292 => 'VirtualDevices',
@ -58,7 +81,7 @@ my %HMCCURPC_RPC_URL = (
# Initial intervals for registration of RPC callbacks and reading RPC queue # Initial intervals for registration of RPC callbacks and reading RPC queue
# #
# X = Start RPC server # X = Start RPC server
# X+HMCCURPC_INIT_INTERVAL1 = Register RPC callback # X+HMCCURPC_INIT_INTERVAL1 = Register RPC callback
# X+HMCCURPC_INIT_INTERVAL2 = Read RPC Queue # X+HMCCURPC_INIT_INTERVAL2 = Read RPC Queue
# #
@ -67,11 +90,15 @@ my $HMCCURPC_INIT_INTERVAL1 = 7;
my $HMCCURPC_INIT_INTERVAL2 = 5; my $HMCCURPC_INIT_INTERVAL2 = 5;
my $HMCCURPC_INIT_INTERVAL3 = 25; my $HMCCURPC_INIT_INTERVAL3 = 25;
# Thread type flags
my $HMCCURPC_THREAD_DATA = 1; my $HMCCURPC_THREAD_DATA = 1;
my $HMCCURPC_THREAD_SERVER = 2; my $HMCCURPC_THREAD_SERVER = 2;
my $HMCCURPC_THREAD_ALL = 3; my $HMCCURPC_THREAD_ALL = 3;
my $HMCCURPC_MAX_EVENTS = 50;
######################################################################
# Functions
######################################################################
# Standard functions # Standard functions
sub HMCCURPC_Initialize ($); sub HMCCURPC_Initialize ($);
@ -121,6 +148,7 @@ sub HMCCURPC_ReaddDevicesCB ($$$);
sub HMCCURPC_EventCB ($$$$$); sub HMCCURPC_EventCB ($$$$$);
sub HMCCURPC_ListDevicesCB ($$); sub HMCCURPC_ListDevicesCB ($$);
###################################################################### ######################################################################
# Initialize module # Initialize module
###################################################################### ######################################################################
@ -361,7 +389,7 @@ sub HMCCURPC_Notify ($$)
my $devtype = $devhash->{TYPE}; my $devtype = $devhash->{TYPE};
my $disable = AttrVal ($name, 'disable', 0); my $disable = AttrVal ($name, 'disable', 0);
my $rpcserver = AttrVal ($name, 'rpcserver', 'off'); my $rpcserver = AttrVal ($name, 'rpcServer', 'off');
return if ($disable); return if ($disable);
my $events = deviceEvents ($devhash, 1); my $events = deviceEvents ($devhash, 1);
@ -1074,12 +1102,12 @@ sub HMCCURPC_StartRPCServer ($)
my $localaddr = HMCCURPC_GetAttribute ($hash, 'rpcServerAddr', 'rpcserveraddr', ''); my $localaddr = HMCCURPC_GetAttribute ($hash, 'rpcServerAddr', 'rpcserveraddr', '');
my $rpcserverport = HMCCURPC_GetAttribute ($hash, 'rpcServerPort', 'rpcserverport', 5400); my $rpcserverport = HMCCURPC_GetAttribute ($hash, 'rpcServerPort', 'rpcserverport', 5400);
my $ccuflags = AttrVal ($name, 'ccuflags', 'null'); my $ccuflags = AttrVal ($name, 'ccuflags', 'null');
$thrpar{socktimeout} = AttrVal ($name, 'rpcWriteTimeout', 0.001); $thrpar{socktimeout} = AttrVal ($name, 'rpcWriteTimeout', $HMCCURPC_TIMEOUT_WRITE);
$thrpar{conntimeout} = AttrVal ($name, 'rpcConnTimeout', 10); $thrpar{conntimeout} = AttrVal ($name, 'rpcConnTimeout', $HMCCURPC_TIMEOUT_CONNECTION);
$thrpar{acctimeout} = AttrVal ($name, 'rpcAcceptTimeout', 1); $thrpar{acctimeout} = AttrVal ($name, 'rpcAcceptTimeout', $HMCCURPC_TIMEOUT_ACCEPT);
$thrpar{waittime} = AttrVal ($name, 'rpcWaitTime', 100000); $thrpar{waittime} = AttrVal ($name, 'rpcWaitTime', $HMCCURPC_TIME_WAIT);
$thrpar{queuesize} = AttrVal ($name, 'rpcQueueSize', $HMCCURPC_MAX_QUEUESIZE); $thrpar{queuesize} = AttrVal ($name, 'rpcQueueSize', $HMCCURPC_MAX_QUEUESIZE);
$thrpar{triggertime} = AttrVal ($name, 'rpcTriggerTime', 10); $thrpar{triggertime} = AttrVal ($name, 'rpcTriggerTime', $HMCCURPC_TIME_TRIGGER);
$thrpar{name} = $name; $thrpar{name} = $name;
my $ccunum = $hash->{CCUNum}; my $ccunum = $hash->{CCUNum};
@ -1588,6 +1616,7 @@ sub HMCCURPC_ProcessData ($$$$)
$ec++; $ec++;
Log3 $name, 2, "CCURPC: I/O error during data processing ($err)" if ($ec == 1); Log3 $name, 2, "CCURPC: I/O error during data processing ($err)" if ($ec == 1);
$ec = 0 if ($ec == $HMCCURPC_MAX_IOERRORS); $ec = 0 if ($ec == $HMCCURPC_MAX_IOERRORS);
sleep ($thrpar->{triggertime});
} }
else { else {
$ec = 0; $ec = 0;
@ -1830,6 +1859,11 @@ sub HMCCURPC_ListDevicesCB ($$)
slows down decrease this value. On a fast system this value can be increased to 100. slows down decrease this value. On a fast system this value can be increased to 100.
Default value is 50. Default value is 50.
</li><br/> </li><br/>
<li><b>rpcQueueSize &lt;count&gt;</b><br/>
Specify maximum size of event queue. When this limit is reached no more CCU events
are forwarded to FHEM. In this case increase this attribute or increase attribute
<b>rpcMaxEvents</b>. Default value is 500.
</li><br/>
<li><b>rpcServer { on | off }</b><br/> <li><b>rpcServer { on | off }</b><br/>
If set to 'on' start RPC server(s) after FHEM start. Default is 'off'. If set to 'on' start RPC server(s) after FHEM start. Default is 'off'.
</li><br/> </li><br/>
@ -1844,10 +1878,10 @@ sub HMCCURPC_ListDevicesCB ($$)
If attribute is missing the corresponding attribute of I/O device (HMCCU device) If attribute is missing the corresponding attribute of I/O device (HMCCU device)
is used. Default value is 5400. is used. Default value is 5400.
</li><br/> </li><br/>
<li><b>rpcQueueSize &lt;count&gt;</b><br/> <li><b>rpcTriggerTime &lt;seconds&gt;</b><br/>
Specify maximum size of event queue. When this limit is reached no more CCU events Set time to wait before trigger I/O again after I/O error. Default value is 10 seconds.
are forwarded to FHEM. In this case increase this attribute or increase attribute On fast systems this value can be set to 5 seconds. Reduces number of log messages
<b>rpcMaxEvents</b>. Default value is 500. written if FHEM is busy and not able to read data from CCU.
</li><br/> </li><br/>
<li><b>rpcWaitTime &lt;microseconds&gt;</b><br/> <li><b>rpcWaitTime &lt;microseconds&gt;</b><br/>
Specify time to wait for data processing thread after each loop. Default value is Specify time to wait for data processing thread after each loop. Default value is