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.
# 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
call, Atrribut min and max Value for Reading-
Warnings

View File

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

View File

@ -4,16 +4,15 @@
#
# $Id$
#
# Version 0.8 beta
# Version 0.9 beta
#
# 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:
#
# 88_HMCCU.pm
# threads
# Thread::Queue
# Time::HiRes
@ -36,13 +35,37 @@ use RPC::XML::Server;
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;
# Maximum number of elements in queue
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
my %HMCCURPC_RPC_NUMPORT = (
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
#
# X = Start RPC server
# X = Start RPC server
# X+HMCCURPC_INIT_INTERVAL1 = Register RPC callback
# X+HMCCURPC_INIT_INTERVAL2 = Read RPC Queue
#
@ -67,11 +90,15 @@ my $HMCCURPC_INIT_INTERVAL1 = 7;
my $HMCCURPC_INIT_INTERVAL2 = 5;
my $HMCCURPC_INIT_INTERVAL3 = 25;
# Thread type flags
my $HMCCURPC_THREAD_DATA = 1;
my $HMCCURPC_THREAD_SERVER = 2;
my $HMCCURPC_THREAD_ALL = 3;
my $HMCCURPC_MAX_EVENTS = 50;
######################################################################
# Functions
######################################################################
# Standard functions
sub HMCCURPC_Initialize ($);
@ -121,6 +148,7 @@ sub HMCCURPC_ReaddDevicesCB ($$$);
sub HMCCURPC_EventCB ($$$$$);
sub HMCCURPC_ListDevicesCB ($$);
######################################################################
# Initialize module
######################################################################
@ -361,7 +389,7 @@ sub HMCCURPC_Notify ($$)
my $devtype = $devhash->{TYPE};
my $disable = AttrVal ($name, 'disable', 0);
my $rpcserver = AttrVal ($name, 'rpcserver', 'off');
my $rpcserver = AttrVal ($name, 'rpcServer', 'off');
return if ($disable);
my $events = deviceEvents ($devhash, 1);
@ -1074,12 +1102,12 @@ sub HMCCURPC_StartRPCServer ($)
my $localaddr = HMCCURPC_GetAttribute ($hash, 'rpcServerAddr', 'rpcserveraddr', '');
my $rpcserverport = HMCCURPC_GetAttribute ($hash, 'rpcServerPort', 'rpcserverport', 5400);
my $ccuflags = AttrVal ($name, 'ccuflags', 'null');
$thrpar{socktimeout} = AttrVal ($name, 'rpcWriteTimeout', 0.001);
$thrpar{conntimeout} = AttrVal ($name, 'rpcConnTimeout', 10);
$thrpar{acctimeout} = AttrVal ($name, 'rpcAcceptTimeout', 1);
$thrpar{waittime} = AttrVal ($name, 'rpcWaitTime', 100000);
$thrpar{queuesize} = AttrVal ($name, 'rpcQueueSize', $HMCCURPC_MAX_QUEUESIZE);
$thrpar{triggertime} = AttrVal ($name, 'rpcTriggerTime', 10);
$thrpar{socktimeout} = AttrVal ($name, 'rpcWriteTimeout', $HMCCURPC_TIMEOUT_WRITE);
$thrpar{conntimeout} = AttrVal ($name, 'rpcConnTimeout', $HMCCURPC_TIMEOUT_CONNECTION);
$thrpar{acctimeout} = AttrVal ($name, 'rpcAcceptTimeout', $HMCCURPC_TIMEOUT_ACCEPT);
$thrpar{waittime} = AttrVal ($name, 'rpcWaitTime', $HMCCURPC_TIME_WAIT);
$thrpar{queuesize} = AttrVal ($name, 'rpcQueueSize', $HMCCURPC_MAX_QUEUESIZE);
$thrpar{triggertime} = AttrVal ($name, 'rpcTriggerTime', $HMCCURPC_TIME_TRIGGER);
$thrpar{name} = $name;
my $ccunum = $hash->{CCUNum};
@ -1588,6 +1616,7 @@ sub HMCCURPC_ProcessData ($$$$)
$ec++;
Log3 $name, 2, "CCURPC: I/O error during data processing ($err)" if ($ec == 1);
$ec = 0 if ($ec == $HMCCURPC_MAX_IOERRORS);
sleep ($thrpar->{triggertime});
}
else {
$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.
Default value is 50.
</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/>
If set to 'on' start RPC server(s) after FHEM start. Default is 'off'.
</li><br/>
@ -1844,10 +1878,10 @@ sub HMCCURPC_ListDevicesCB ($$)
If attribute is missing the corresponding attribute of I/O device (HMCCU device)
is used. Default value is 5400.
</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><b>rpcTriggerTime &lt;seconds&gt;</b><br/>
Set time to wait before trigger I/O again after I/O error. Default value is 10 seconds.
On fast systems this value can be set to 5 seconds. Reduces number of log messages
written if FHEM is busy and not able to read data from CCU.
</li><br/>
<li><b>rpcWaitTime &lt;microseconds&gt;</b><br/>
Specify time to wait for data processing thread after each loop. Default value is