From 6450c9c7962ae604f420ae8585fa3766c51c2677 Mon Sep 17 00:00:00 2001 From: phenning <> Date: Fri, 12 Jul 2019 06:54:38 +0000 Subject: [PATCH] =?UTF-8?q?36=5FShelly.pm:=20Bugfix=2095=5FBabble.pm:=20Mi?= =?UTF-8?q?nimale=20Erg=C3=A4nzungen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.fhem.de/fhem/trunk@19819 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/36_Shelly.pm | 14 ++++--- fhem/FHEM/95_Babble.pm | 89 +++++++++++++++++++++++++++--------------- 2 files changed, 66 insertions(+), 37 deletions(-) diff --git a/fhem/FHEM/36_Shelly.pm b/fhem/FHEM/36_Shelly.pm index 66619db79..f3afcb201 100644 --- a/fhem/FHEM/36_Shelly.pm +++ b/fhem/FHEM/36_Shelly.pm @@ -31,14 +31,15 @@ package main; use strict; use warnings; -use JSON; # imports encode_json, decode_json, to_json and from_json. +use JSON; +use HttpUtils; use vars qw{%attr %defs}; sub Log($$); #-- globals on start -my $version = "2.03"; +my $version = "2.04"; #-- these we may get on request my %gets = ( @@ -178,6 +179,9 @@ sub Shelly_Define($$) { readingsBulkUpdate($hash,"network","not connected"); } readingsEndUpdate($hash,1); + + #-- perform status update in a minute or so + InternalTimer(time()+60, "Shelly_status", $hash,0); $init_done = $oid; @@ -993,7 +997,7 @@ sub Shelly_pwd($){ my $green = $jhash->{'lights'}[0]{'green'}; my $blue = $jhash->{'lights'}[0]{'blue'}; my $white = $jhash->{'lights'}[0]{'white'}; - my $rgb = sprintf("%X%X%X", $red,$green,$blue); + my $rgb = sprintf("%02X%02X%02X", $red,$green,$blue); readingsBulkUpdateIfChanged($hash,"rgb",$rgb); readingsBulkUpdateIfChanged($hash,"L-red",$red); @@ -1005,9 +1009,9 @@ sub Shelly_pwd($){ for( my $i=0;$i<$meters;$i++){ $subs = ($meters == 1) ? "" : "_".$i; $power = $jhash->{'meters'}[$i]{'power'}; - $energy = int($jhash->{'meters'}[$i]{'total'}/6)/10; + $energy = (defined($jhash->{'meters'}[$i]{'total'}))?int($jhash->{'meters'}[$i]{'total'}/6)/10:"undefined"; readingsBulkUpdateIfChanged($hash,"power".$subs,$power); - #readingsBulkUpdateIfChanged($hash,"energy".$subs,$energy); + readingsBulkUpdateIfChanged($hash,"energy".$subs,$energy); } } #-- common to all Shelly models diff --git a/fhem/FHEM/95_Babble.pm b/fhem/FHEM/95_Babble.pm index 96244262d..05b842395 100644 --- a/fhem/FHEM/95_Babble.pm +++ b/fhem/FHEM/95_Babble.pm @@ -55,7 +55,7 @@ if (eval {require RiveScript;1;} ne 1) { my $babblelinkname = "babbles"; # link text my $babblehiddenroom = "babbleRoom"; # hidden room my $babblepublicroom = "babble"; # public room -my $babbleversion = "1.35"; +my $babbleversion = "1.42"; my @babblerows; my %babble_transtable_EN = ( @@ -188,7 +188,9 @@ sub Babble_Initialize ($) { #$hash->{AttrFn} = "Babble_Attr"; my $attst = "lockstate:locked,unlocked helpFunc confirmFunc noChatBot:0,1 dnuFile testParm0 testParm1 testParm2 testParm3 ". "remoteFHEM0 remoteFHEM1 remoteFHEM2 remoteFHEM3 remoteFunc0 remoteFunc1 remoteFunc2 remoteFunc3 remoteToken0 remoteToken1 remoteToken2 remoteToken3 ". - "babbleIds babblePreSubs babbleDevices babblePlaces babbleNotPlaces babbleVerbs babbleVerbParts babblePrepos babbleQuests babbleArticles babbleStatus babbleWrites babbleTimes"; + "babbleIds babblePreSubs:textField-long babbleDevices:textField-long babblePlaces:textField-long babbleNotPlaces:textField-long babbleVerbs:textField-long ". + "babbleVerbParts:textField-long babblePrepos:textField-long babbleQuests:textField-long babbleArticles:textField-long babbleStatus:textField-long ". + "babbleWrites:textField-long babbleTimes:textField-long"; $hash->{AttrList} = $attst; if( !defined($babble_tt) ){ @@ -372,7 +374,12 @@ sub Babble_CreateEntry($) { sub Babble_Set($@) { my ( $hash, $name, $cmd, @args ) = @_; - if ( $cmd =~ /^lock(ed)?$/ ) { + if ( $cmd =~ /^((talk)|(doit))/ ) { + #-- put args together, then split again at comma boundaries + my ($str,@newargs) = split(',',join(' ',@args)); + return Babble_DoIt($name,$str,@newargs); + #----------------------------------------------------------- + }elsif ( $cmd =~ /^lock(ed)?$/ ) { readingsSingleUpdate( $hash, "lockstate", "locked", 0 ); return; #----------------------------------------------------------- @@ -396,7 +403,7 @@ sub Babble_Set($@) { return Babble_restore($hash,1); } else { - my $str = "[babble] Unknown argument " . $cmd . ", choose one of locked:noArg unlocked:noArg save:noArg restore:noArg test:noArg "; + my $str = "[babble] Unknown argument " . $cmd . ", choose one of talk doit locked:noArg unlocked:noArg save:noArg restore:noArg test:noArg "; $str .= "rivereload:noArg" if($rive == 1 && AttrVal($name,"noChatBot",0) != 1); return $str; @@ -644,6 +651,9 @@ sub Babble_Normalize($$){ } my @word = split(' ',$sentmod,15); my $len = int(@word); + #-- emergency: only one word given + return ($word[0],"none","none","none","none","none","none","X.X.X") + if( $len==1 ); ############################# POS tagging ################### @@ -1096,6 +1106,7 @@ sub Babble_DoIt{ my $star = ""; my $reply = ""; + readingsSingleUpdate( $hash, "text", $sentence, 0 ); #-- semantic analysis my ($device,$verb,$reading,$value,$article,$reserve,$place,$cat) = Babble_Normalize($name,$sentence); $verb = "none" @@ -1120,6 +1131,7 @@ sub Babble_DoIt{ #-- find command directly my $cmd = $hash->{DATA}{"command"}{$device}{$place}{$verb}{$reading}; + #Log3 $name,5,"[Babble_DoIt] Result after first access of Cmd-Hash =".((defined $cmd)?$cmd:"none"); #-- not directly - but maybe we have an alias device ? if( !defined($cmd) || $cmd eq "" ){ @@ -1138,6 +1150,7 @@ sub Babble_DoIt{ last; } } + #Log3 $name,5,"[Babble_DoIt] Result after second access of Cmd-Hash =".((defined $cmd)?$cmd:"none"); } #-- not directly - but maybe we have a device which is an extension of an alias device @@ -1158,6 +1171,7 @@ sub Babble_DoIt{ } } } + #Log3 $name,5,"[Babble_DoIt] Result after third access of Cmd-Hash =".((defined $cmd)?$cmd:"none"); } #-- command found after all @@ -1174,18 +1188,21 @@ sub Babble_DoIt{ $cmd =~ s/\$PARM$i/$parms[$i]/g; } if( $testit==0 || ($testit==1 && $exflag==1 )){ - Log3 $name,1,"[Babble_DoIt] Executing from hash: $device.$place.$verb.$reading/$value "; + Log3 $name,5,"[Babble_DoIt] Executing from hash: $device.$place.$verb.$reading/$value "; my $contact = $hash->{DATA}{"devcontacts"}{$device}[2]; my $fhemdev = $hash->{DATA}{"devcontacts"}{$device}[1]; if( $contact == 0 ){ $res = fhem($cmd); + readingsSingleUpdate( $hash, "cmd", $cmd, 0 ); }else{ my $ip = AttrVal($name,"remoteFHEM".$contact,undef); my $token = AttrVal($name,"remoteToken".$contact,undef); my $func = AttrVal($name,"remoteFunc".$contact,undef); if( $func && $func ne "" ){ - $res = eval($func."(\"".$cmd."\")") + $res = eval($func."(\"".$cmd."\")"); + readingsSingleUpdate( $hash, "cmd", $res, 0 ); }else{ + readingsSingleUpdate( $hash, "cmd", "remoteFHEM".$contact." ".$cmd, 0 ); $cmd =~ s/\s/\%20/g; my $url = "http://".$ip."/fhem?XHR=1&fwcsrf=".$token."&cmd.$fhemdev=$cmd"; HttpUtils_NonblockingGet({ @@ -1212,13 +1229,12 @@ sub Babble_DoIt{ } } #-- what to do in conclusion - if( $testit==0 ){ - return undef; - }else{ + if( $testit==1 ){ $str .= "==> $cmd"; return $str; } - + return undef; + #-- no command found, acquire alternate text }else{ #-- ChatBot available @@ -1239,12 +1255,13 @@ sub Babble_DoIt{ close $fh; } } - + readingsSingleUpdate( $hash, "cmd", "none => ChatBot text = $reply", 0 ); #-- no chatbot, use help text directly }else{ $reply = defined($hash->{DATA}{"help"}{$device}) ? $hash->{DATA}{"help"}{$device} : ""; + readingsSingleUpdate( $hash, "cmd", "none => Help text = $reply", 0 ); } - #-- get help function + #-- get help function - embed reply of chatbot here my $func = AttrVal($name,"helpFunc",undef); if( $func && $func ne "" ){ #-- substitution @@ -1254,23 +1271,25 @@ sub Babble_DoIt{ for(my $i=0;$i{DATA}{"command"}{$bdev}{$place}{$verb}{$target}) ){ - Log3 $name, 1,"[Babble_RemCmd] Deleting from hash: $bdev.$place.$verb.$target => ".$hash->{DATA}{"command"}{$bdev}{$place}{$verb}{$target}; + Log3 $name, 5,"[Babble_RemCmd] Deleting from hash: $bdev.$place.$verb.$target => ".$hash->{DATA}{"command"}{$bdev}{$place}{$verb}{$target}; delete($hash->{DATA}{"command"}{$bdev}{$place}{$verb}{$target}); return #-- try to figure out data from index (fallback strategy) }else{ my $cmdstr = $babblerows[$fallback-1]; ($bdev,$place,$verb,$target)=split('\+\|\+',$cmdstr); - Log3 $name, 1,"[Babble_RemCmd] Deleting in fallback strategy from hash: $bdev.$place.$verb.$target => ".$hash->{DATA}{"command"}{$bdev}{$place}{$verb}{$target}; + Log3 $name, 5,"[Babble_RemCmd] Deleting in fallback strategy from hash: $bdev.$place.$verb.$target => ".$hash->{DATA}{"command"}{$bdev}{$place}{$verb}{$target}; delete($hash->{DATA}{"command"}{$bdev}{$place}{$verb}{$target}); return } @@ -2157,21 +2176,23 @@ sub Babble_Html($)

Define

- define <name> babble + define <name> Babble
Defines the Babble device.

Notes:

Usage

- To use this module, call the Perl function Babble_DoIt("<name>","<sentence>"[,<parm0>,<parm1>,...]). - <name> is the name of the Babble device, <parm0> <parm1> are arbitrary parameters. - + To use this module, +
  1. call the Perl function Babble_DoIt("<name>","<sentence>"[,<parm0>,<parm1>,...]).
  2. +
  3. execute the FHEM command set <name> talk|doit <sentence>[,<parm0>,<parm1>,...].
  4. +
+ <name> is the name of the Babble device, <parm0> <parm1> are arbitrary parameters passed to the executed command. The module will analyze the sentence passed an isolate a device to be addressed, a place identifier, - a verb, a target and its value from the sentence passed. + a verb, a target and its value from the sentence passed. Attention: in case the FHEM command is used, <sentence> must not contain commas. If a proper command has been stored with device, place, verb and target, it will be subject to substitutions and then will be executed. In these substitutions, a string $VALUE will be replaced by the value for the target reading, a string $DEV will be replaced by the device name identified by Babble, @@ -2185,7 +2206,11 @@ sub Babble_Html($)

Set