From 3f3ec6bd5fdaefb55cd3f953fb3800a8e63cb2ab Mon Sep 17 00:00:00 2001 From: LeonGaultier Date: Mon, 4 Nov 2019 14:10:02 +0000 Subject: [PATCH] 74_XiaomiBTLESens: code implementation for ClearGrassSens git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@20454 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- CHANGED | 1 + FHEM/74_XiaomiBTLESens.pm | 151 +++++++++++++++++++++++++++++++++++--- 2 files changed, 143 insertions(+), 9 deletions(-) diff --git a/CHANGED b/CHANGED index c0fb8e661..d9258af6e 100644 --- a/CHANGED +++ b/CHANGED @@ -1,5 +1,6 @@ # 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: 74_XiaomiBTLESens: code implementation for ClearGrassSens - bugfix: 73_AutoShuttersControl: fix window close fn then Mode_Up off - change: 93_DbLog: change SQL-Statement for SVG delta-h and delta-d plots, Forum: 65860.msg982171.html#msg982171 diff --git a/FHEM/74_XiaomiBTLESens.pm b/FHEM/74_XiaomiBTLESens.pm index 48dde1fae..fedc1dc12 100644 --- a/FHEM/74_XiaomiBTLESens.pm +++ b/FHEM/74_XiaomiBTLESens.pm @@ -152,6 +152,7 @@ BEGIN { GP_Export( qw( Initialize + stateRequestTimer ) ); @@ -172,6 +173,15 @@ my %XiaomiModels = ( 'firmware' => '0x24', 'devicename' => '0x3' }, + clearGrassSens => { + 'rdata' => '0x1e', + 'wdata' => '0x10', + 'wdataValue' => '0100', + 'wdatalisten' => 2, + 'battery' => '0x3b', + 'firmware' => '0x2a', + 'devicename' => '0x3' + }, ); my %CallBatteryAge = ( @@ -208,7 +218,7 @@ sub Initialize($) { . "minLux " . "maxLux " . "sshHost " - . "model:flowerSens,thermoHygroSens " + . "model:flowerSens,thermoHygroSens,clearGrassSens " . "blockingCallLoglevel:2,3,4,5 " . $readingFnAttributes; @@ -427,7 +437,9 @@ sub stateRequest($) { ) ); - if ( $hash->{helper}{CallSensDataCounter} < 1 ) { + if ( $hash->{helper}{CallSensDataCounter} < 1 + and AttrVal( $name, 'model', '' ) ne 'clearGrassSens' ) + { CreateParamGatttool( $hash, 'write', @@ -438,13 +450,21 @@ sub stateRequest($) { $hash->{helper}{CallSensDataCounter} + 1; } + elsif ( $hash->{helper}{CallSensDataCounter} < 1 + and AttrVal( $name, 'model', '' ) eq 'clearGrassSens' ) + { + CreateParamGatttool( $hash, 'read', + $XiaomiModels{ AttrVal( $name, 'model', '' ) }{rdata}, + ); + $hash->{helper}{CallSensDataCounter} = + $hash->{helper}{CallSensDataCounter} + 1; + } else { $readings{'lastGattError'} = 'charWrite faild'; WriteReadings( $hash, \%readings ); $hash->{helper}{CallSensDataCounter} = 0; return; } - } else { @@ -468,7 +488,7 @@ sub stateRequestTimer($) { stateRequest($hash); InternalTimer( gettimeofday() + $hash->{INTERVAL} + int( rand(300) ), - "FHEM::XiaomiBTLESens::stateRequestTimer", $hash ); + "XiaomiBTLESens_stateRequestTimer", $hash ); Log3 $name, 4, "XiaomiBTLESens ($name) - stateRequestTimer: Call Request Timer"; @@ -827,7 +847,6 @@ sub ProcessingNotification($@) { $readings = ThermoHygroSensHandle0x18( $hash, $notification ); } - elsif ( $handle eq '0x10' ) { ### Thermo/Hygro Sens - Read Sensor Data Log3 $name, 4, @@ -835,7 +854,6 @@ sub ProcessingNotification($@) { $readings = ThermoHygroSensHandle0x10( $hash, $notification ); } - elsif ( $handle eq '0x24' ) { ### Thermo/Hygro Sens - Read Firmware Data Log3 $name, 4, @@ -843,7 +861,6 @@ sub ProcessingNotification($@) { $readings = ThermoHygroSensHandle0x24( $hash, $notification ); } - elsif ( $handle eq '0x3' ) { ### Thermo/Hygro Sens - Read and Write Devicename Log3 $name, 4, @@ -855,6 +872,39 @@ sub ProcessingNotification($@) { $readings = ThermoHygroSensHandle0x3( $hash, $notification ); } } + elsif ( AttrVal( $name, 'model', 'none' ) eq 'clearGrassSens' ) { + if ( $handle eq '0x3b' ) { + ### Clear Grass Sens - Read Battery Data + Log3 $name, 4, + "XiaomiBTLESens ($name) - ProcessingNotification: handle 0x3b"; + + $readings = ClearGrassSensHandle0x3b( $hash, $notification ); + } + elsif ( $handle eq '0x1e' ) { + ### Clear Grass Sens - Read Sensor Data + Log3 $name, 4, + "XiaomiBTLESens ($name) - ProcessingNotification: handle 0x1e"; + + $readings = ClearGrassSensHandle0x1e( $hash, $notification ); + } + elsif ( $handle eq '0x2a' ) { + ### Clear Grass Sens - Read Firmware Data + Log3 $name, 4, + "XiaomiBTLESens ($name) - ProcessingNotification: handle 0x2a"; + + $readings = ClearGrassSensHandle0x2a( $hash, $notification ); + } + elsif ( $handle eq '0x3' ) { + ### Clear Grass Sens - Read and Write Devicename + Log3 $name, 4, + "XiaomiBTLESens ($name) - ProcessingNotification: handle 0x3"; + + return CreateParamGatttool( $hash, 'read', + $XiaomiModels{ AttrVal( $name, 'model', '' ) }{devicename} ) + unless ( $gattCmd eq 'read' ); + $readings = ClearGrassSensHandle0x3( $hash, $notification ); + } + } WriteReadings( $hash, $readings ); } @@ -1022,6 +1072,87 @@ sub ThermoHygroSensHandle0x3($$) { return \%readings; } +sub ClearGrassSensHandle0x3b($$) { + ### Clear Grass Sens - Battery Data + my ( $hash, $notification ) = @_; + + my $name = $hash->{NAME}; + my %readings; + + Log3 $name, 4, "XiaomiBTLESens ($name) - Clear Grass Sens Handle0x3b"; + + chomp($notification); + $notification =~ s/\s+//g; + + ### neue Vereinheitlichung für Batteriereadings Forum #800017 + $readings{'batteryPercent'} = hex( substr( $notification, 14, 2 ) ); + $readings{'batteryState'} = + ( hex( substr( $notification, 14, 2 ) ) > 15 ? "ok" : "low" ); + + $hash->{helper}{CallBattery} = 1; + CallBattery_Timestamp($hash); + return \%readings; +} + +sub ClearGrassSensHandle0x1e($$) { + ### Clear Grass Sens - Read Sensor Data + my ( $hash, $notification ) = @_; + + my $name = $hash->{NAME}; + my %readings; + + Log3 $name, 4, "XiaomiBTLESens ($name) - Clear Grass Sens Handle0x1e"; + + return stateRequest($hash) + unless ( $notification =~ /^([0-9a-f]{2}(\s?))*$/ ); + + my @numberOfHex = split( ' ', $notification ); + + $notification =~ s/\s+//g; + + $readings{'temperature'} = hex( substr( $notification, 4, 2 ) ) / 10; + $readings{'humidity'} = + hex( substr( $notification, 11, 1 ) . substr( $notification, 8, 2 ) ) / + 10; + + $hash->{helper}{CallBattery} = 0; + return \%readings; +} + +sub ClearGrassSensHandle0x2a($$) { + ### Clear Grass Sens - Read Firmware Data + my ( $hash, $notification ) = @_; + + my $name = $hash->{NAME}; + my %readings; + + Log3 $name, 4, "XiaomiBTLESens ($name) - Clear Grass Sens Handle0x2a"; + + $notification =~ s/\s+//g; + + $readings{'firmware'} = pack( 'H*', $notification ); + + $hash->{helper}{CallBattery} = 0; + return \%readings; +} + +sub ClearGrassSensHandle0x3($$) { + ### Clear Grass Sens - Read and Write Devicename + my ( $hash, $notification ) = @_; + + my $name = $hash->{NAME}; + my %readings; + + Log3 $name, 4, "XiaomiBTLESens ($name) - Clear Grass Sens Handle0x3"; + + $notification =~ s/\s+//g; + + $readings{'devicename'} = pack( 'H*', $notification ); + + $hash->{helper}{CallBattery} = 0; + return \%readings; +} + sub WriteReadings($$) { my ( $hash, $readings ) = @_; @@ -1045,7 +1176,9 @@ sub WriteReadings($$) { . ReadingsVal( $name, 'temperature', 0 ) . ' H: ' . ReadingsVal( $name, 'humidity', 0 ) ) - ) if ( AttrVal( $name, 'model', 'none' ) eq 'thermoHygroSens' ); + ) + if ( AttrVal( $name, 'model', 'none' ) eq 'thermoHygroSens' + or AttrVal( $name, 'model', 'none' ) eq 'clearGrassSens' ); readingsEndUpdate( $hash, 1 ); @@ -1402,7 +1535,7 @@ sub CometBlueBTLE_CmdlinePreventGrepFalsePositive($) { ], "release_status": "stable", "license": "GPL_2", - "version": "v2.6.0", + "version": "v2.8.0", "author": [ "Marko Oldenburg " ],