From 5e31361c472b1dfa23627f33871afc26467f4fab Mon Sep 17 00:00:00 2001 From: Marko Oldenburg Date: Tue, 18 Jun 2019 18:11:43 +0200 Subject: [PATCH] fix little bugs --- 98_SmarterCoffee.pm | 163 +++++++++++++++++++++++++++++++------------- 1 file changed, 115 insertions(+), 48 deletions(-) diff --git a/98_SmarterCoffee.pm b/98_SmarterCoffee.pm index 03e00a9..72574c3 100644 --- a/98_SmarterCoffee.pm +++ b/98_SmarterCoffee.pm @@ -105,48 +105,13 @@ # ############################################################# -package main; - -use strict; -use warnings; - -my $version = "1.0.2"; - -sub SmarterCoffee_Initialize($) { - my ($hash) = @_; - - $hash->{DefFn} = 'SmarterCoffee::Define'; - $hash->{UndefFn} = 'SmarterCoffee::Undefine'; - $hash->{GetFn} = 'SmarterCoffee::Get'; - $hash->{SetFn} = 'SmarterCoffee::Set'; - $hash->{ReadFn} = 'SmarterCoffee::Read'; - $hash->{ReadyFn} = 'SmarterCoffee::OpenIfRequiredAndWritePending'; - $hash->{NotifyFn} = 'SmarterCoffee::Notify'; - $hash->{AttrFn} = 'SmarterCoffee::Attr'; - - $hash->{AttrList} = "" - . "default-hotplate-on-for-minutes " - . "ignore-max-cups " - . "set-on-brews-coffee " - . "strength-coffee-weights " - . "strength-extra-percent " - . "strength-extra-pre-brew-cups " - . "strength-extra-pre-brew-delay-seconds " - . "strength-extra-start-on-device-strength:off,weak,medium,strong " - . "devioLoglevel:0,1,2,3,4,5 " - . $readingFnAttributes; - - foreach my $d ( sort keys %{ $modules{SmarterCoffee}{defptr} } ) { - my $hash = $modules{SmarterCoffee}{defptr}{$d}; - $hash->{VERSION} = $version; - } -} - -package SmarterCoffee; +package FHEM::SmarterCoffee; use strict; use warnings; use POSIX; +use utf8; +use FHEM::Meta; use GPUtils qw(:all) ; # wird für den Import der FHEM Funktionen aus der fhem.pl benötigt @@ -157,10 +122,13 @@ use IO::Select; use DevIo; -#use HttpUtils; +our $VERSION = 'v1.0.3'; ## Import der FHEM Funktionen +#-- Run before package compilation BEGIN { + + # Import from main context GP_Import( qw(readingsSingleUpdate readingsBulkUpdate @@ -170,6 +138,7 @@ BEGIN { defs modules Log3 + readingFnAttributes AttrVal ReadingsVal ReadingsNum @@ -186,6 +155,28 @@ BEGIN { ); } +# _Export - Export references to main context using a different naming schema +sub _Export { + no strict qw/refs/; ## no critic + my $pkg = caller(0); + my $main = $pkg; + $main =~ s/^(?:.+::)?([^:]+)$/main::$1\_/g; + foreach (@_) { + *{ $main . $_ } = *{ $pkg . '::' . $_ }; + } +} + +#-- Export to main context with different name +_Export( + qw( + Initialize + GetDevStateIcon + ExtraStrengthHandleBrewing + WritePending + RunDiscoveryProcess + ) +); + my $port = 2081; my $discoveryInterval = 60 * 15; my $strengthExtraDefaultPercent = 1.4; @@ -562,7 +553,7 @@ sub Connect($) { sub OpenIfRequiredAndWritePending($;$) { my ( $hash, $initial ) = @_; return main::DevIo_OpenDev( $hash, ( $initial ? 0 : 1 ), - "SmarterCoffee::WritePending" ); + "SmarterCoffee_WritePending" ); } sub HandleInitialConnectState($) { @@ -669,9 +660,43 @@ sub Read($;$) { return 1; } +sub Initialize($) { + my ($hash) = @_; + + $hash->{DefFn} = 'FHEM::SmarterCoffee::Define'; + $hash->{UndefFn} = 'FHEM::SmarterCoffee::Undefine'; + $hash->{GetFn} = 'FHEM::SmarterCoffee::Get'; + $hash->{SetFn} = 'FHEM::SmarterCoffee::Set'; + $hash->{ReadFn} = 'FHEM::SmarterCoffee::Read'; + $hash->{ReadyFn} = 'FHEM::SmarterCoffee::OpenIfRequiredAndWritePending'; + $hash->{NotifyFn} = 'FHEM::SmarterCoffee::Notify'; + $hash->{AttrFn} = 'FHEM::SmarterCoffee::Attr'; + + $hash->{AttrList} = "" + . "default-hotplate-on-for-minutes " + . "ignore-max-cups " + . "set-on-brews-coffee " + . "strength-coffee-weights " + . "strength-extra-percent " + . "strength-extra-pre-brew-cups " + . "strength-extra-pre-brew-delay-seconds " + . "strength-extra-start-on-device-strength:off,weak,medium,strong " + . "devioLoglevel:0,1,2,3,4,5 " + . $readingFnAttributes; + + foreach my $d ( sort keys %{ $modules{SmarterCoffee}{defptr} } ) { + my $hash = $modules{SmarterCoffee}{defptr}{$d}; + $hash->{VERSION} = $VERSION; + } + + return FHEM::Meta::InitMod( __FILE__, $hash ); +} + sub Define($$) { my ( $hash, $def ) = @_; + my @param = split( '[ \t]+', $def ); + return $@ unless ( FHEM::Meta::SetInternals($hash) ); my $name = $hash->{NAME}; # set default settings on first define @@ -696,12 +721,12 @@ sub Define($$) { } CommandAttr( undef, - $name . 'devStateIcon { SmarterCoffee::GetDevStateIcon($name) }' ) + $name . 'devStateIcon { SmarterCoffee_GetDevStateIcon($name) }' ) if ( AttrVal( $name, 'devStateIcon', 'none' ) eq 'none' or AttrVal( $name, 'devStateIcon', 'none' ) eq '{ SmarterCoffee_GetDevStateIcon($name) }' ); - $hash->{VERSION} = $version; + $hash->{VERSION} = $VERSION; if ( int(@param) < 3 ) { $hash->{AUTO_DETECT} = 1; } @@ -1239,7 +1264,7 @@ sub ProcessEventForExtraStrength($$) { { InternalTimer( gettimeofday() + $delay, - "SmarterCoffee::ExtraStrengthHandleBrewing", + "SmarterCoffee_ExtraStrengthHandleBrewing", $hash, 0 ); } @@ -1556,7 +1581,7 @@ sub RunDiscoveryProcess($;$) { InternalTimer( gettimeofday() + $discoveryInterval, - "SmarterCoffee::RunDiscoveryProcess", + "SmarterCoffee_RunDiscoveryProcess", $hash, 0 ); } @@ -1765,6 +1790,7 @@ sub GetDevStateIcon { =pod =item device =item summary Controls a Wi-Fi Smarter Coffee machine via network connection + =begin html @@ -1934,14 +1960,14 @@ sub GetDevStateIcon { Attributes