From 8c2e9856c8716cc54b1519cc93f1f1bd9461376d Mon Sep 17 00:00:00 2001 From: justme-1968 Date: Fri, 17 Dec 2021 12:08:57 +0000 Subject: [PATCH] feature: 31_HUEDevice: allow json in set commands for lights. see https://forum.fhem.de/index.php/topic,11020 posting #1864 and #1866 git-svn-id: https://svn.fhem.de/fhem/trunk@25351 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 3 + fhem/FHEM/31_HUEDevice.pm | 120 ++++++++++++++++++++++++++++++++------ 2 files changed, 106 insertions(+), 17 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index ed04065cb..b57ec9fb9 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,8 @@ # 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. + - feature: 31_HUEDevice: allow json in set commands for lights. + see https://forum.fhem.de/index.php/topic,11020 + posting #1864 and #1866 - feature: 73_NUKIBridge: 74_NUKIDevice: add all new features of API Version 1.13 - bugfix: 88_HMCCU: Several improvements and bug fixes diff --git a/fhem/FHEM/31_HUEDevice.pm b/fhem/FHEM/31_HUEDevice.pm index 5a09a0ad4..99414e605 100644 --- a/fhem/FHEM/31_HUEDevice.pm +++ b/fhem/FHEM/31_HUEDevice.pm @@ -1,6 +1,4 @@ - # $Id$ - # "Hue Personal Wireless Lighting" is a trademark owned by Koninklijke Philips Electronics N.V., # see www.meethue.com for more information. # I am in no way affiliated with the Philips organization. @@ -14,7 +12,8 @@ use FHEM::Meta; use Color; -use POSIX; +#use POSIX; +use Time::HiRes qw(gettimeofday); use JSON; use SetExtensions; use Time::Local; @@ -507,10 +506,31 @@ sub HUEDevice_Undefine($$) return undef; } +sub +HUEDevice_AddJson($$@) +{ + my ($name, $obj, $json) = @_; + + my $o = eval { JSON->new->utf8(0)->decode($json) }; + if( $@ ) { + Log3 $name, 2, "$name: json error: $@ in $json"; + + } elsif( $json !~ /\{.*\}/ ) { + Log3 $name, 2, "$name: json error: $json"; + + } else { + foreach my $key ( keys %{$o} ) { + $obj->{$key} = $o->{$key}; + } + } + + return $obj; +} sub HUEDevice_SetParam($$@) { - my ($name, $obj, $cmd, $value, $value2) = @_; + my ($name, $obj, $cmd, $value, @aa) = @_; + my ($value2) = @aa; if( $cmd eq "color" ) { $value = int(1000000/$value); @@ -720,10 +740,19 @@ HUEDevice_SetParam($$@) $obj->{'hue'} = int($h*256); $obj->{'sat'} = 0+$s; $obj->{'bri'} = 0+$v; + } elsif( $cmd eq "alert" ) { $obj->{'alert'} = $value; + } elsif( $cmd eq "effect" ) { + $obj->{'on'} = JSON::true; $obj->{'effect'} = $value; + + if( defined($value2) ) { + my $json = join( ' ', @aa); + HUEDevice_AddJson( $name, $obj, $json ); + } + } elsif( $cmd eq "transitiontime" ) { $obj->{'transitiontime'} = 0+$value; } elsif( $name && $cmd eq "delayedUpdate" ) { @@ -739,10 +768,16 @@ HUEDevice_SetParam($$@) } elsif( $cmd eq 'habridgeupdate' ) { $obj->{habridgeupdate} = JSON::true; + } elsif( $cmd =~ /\{/ ) { + $value='' if( !$value ); + HUEDevice_AddJson( $name, $obj, "$cmd$value ".join( ' ', @aa) ); + } else { return 0; } + #Log3 $name, 5, "$name: ". Dumper $obj if($HUEDevice_hasDataDumper); + return 1; } sub HUEDevice_Set($@); @@ -949,8 +984,31 @@ HUEDevice_Set($@) $joined =~ s/on-till\s+[^\s]+//g; #bad workaround for: https://forum.fhem.de/index.php/topic,61636.msg728557.html#msg728557 $joined =~ s/on-till-overnight\s+[^\s]+//g; #same bad workaround for: https://forum.fhem.de/index.php/topic,61636.msg1110193 my @cmds = split(":", $joined); - for( my $i = 0; $i <= $#cmds; ++$i ) { - HUEDevice_SetParam($name, \%obj, split(" ", $cmds[$i]) ); + while( @cmds ) { + my $cmd = shift(@cmds); + + if( $cmd =~ m/{/ ) { # } for match + my $count = 0; + for my $i (0..length($cmd)-1) { + my $c = substr($cmd, $i, 1); + ++$count if( $c eq '{' ); + --$count if( $c eq '}' ); + } + + while( $cmd && $count != 0 ) { + my $next = shift(@cmds); + last if( !defined($next) ); + $cmd .= ':' . $next; + + for my $i (0..length($next)-1) { + my $c = substr($next, $i, 1); + ++$count if( $c eq '{' ); + --$count if( $c eq '}' ); + } + } + } + + HUEDevice_SetParam($name, \%obj, split(" ", $cmd) ); } } else { my ($cmd, $value, $value2, @a) = @aa; @@ -961,7 +1019,7 @@ HUEDevice_Set($@) return undef; } - HUEDevice_SetParam($name, \%obj, $cmd, $value, $value2); + HUEDevice_SetParam($name, \%obj, $cmd, $value, $value2, @a); } if( %obj ) { @@ -1410,6 +1468,8 @@ HUEDevice_Parse($$) $hash->{class} = $result->{class} if( defined($result->{class}) ); $hash->{uniqueid} = $result->{uniqueid} if( defined($result->{uniqueid}) ); + $hash->{v2_id} = $result->{v2_id} if( defined($result->{v2_id}) ); + $hash->{helper}{json} = $result; if( $hash->{helper}->{devtype} eq 'G' ) { @@ -1945,6 +2005,8 @@ HUEDevice_Attr($$$;$) 1; +__END__ + =pod =item tag cloudfree =item tag publicAPI @@ -1952,11 +2014,11 @@ HUEDevice_Attr($$$;$) =item summary_DE Geräte an einer Philips HUE Bridge, einem LIGHTIFY oder Tradfri Gateway =begin html - +

HUEDevice


- + Readings

- + Set
- + Get
- + Attributes