add min/max Warn Values and Events for temperature,fertility and moisture

This commit is contained in:
Marko Oldenburg 2017-03-16 11:58:46 +01:00
parent 8bbd9cb390
commit 4dcf8f7c37

View File

@ -35,7 +35,7 @@ use POSIX;
use JSON; use JSON;
use Blocking; use Blocking;
my $version = "0.6.0"; my $version = "0.9.9";
@ -69,8 +69,12 @@ sub XiaomiFlowerSens_Initialize($) {
$hash->{AttrFn} = "XiaomiFlowerSens_Attr"; $hash->{AttrFn} = "XiaomiFlowerSens_Attr";
$hash->{AttrList} = "interval ". $hash->{AttrList} = "interval ".
"disable:1 ". "disable:1 ".
"hciDevice:hci0,hci1,hci2 ".
"disabledForIntervals ". "disabledForIntervals ".
"hciDevice:hci0,hci1,hci2 ".
"minFertility ".
"minTemp ".
"maxTemp ".
"minMoisture ".
$readingFnAttributes; $readingFnAttributes;
@ -125,6 +129,7 @@ sub XiaomiFlowerSens_Undef($$) {
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
BlockingKill($hash->{helper}{RUNNING_PID}) if(defined($hash->{helper}{RUNNING_PID}));
delete($modules{XiaomiFlowerSens}{defptr}{$mac}); delete($modules{XiaomiFlowerSens}{defptr}{$mac});
Log3 $name, 3, "Sub XiaomiFlowerSens_Undef ($name) - delete device $name"; Log3 $name, 3, "Sub XiaomiFlowerSens_Undef ($name) - delete device $name";
@ -207,8 +212,6 @@ sub XiaomiFlowerSens_stateRequestTimer($) {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
RemoveInternalTimer($hash);
if( !IsDisabled($name) ) { if( !IsDisabled($name) ) {
readingsSingleUpdate ( $hash, "state", "active", 1 ) if( (ReadingsVal($name, "state", 0) eq "initialized" or ReadingsVal($name, "state", 0) eq "unreachable" or ReadingsVal($name, "state", 0) eq "corrupted data" or ReadingsVal($name, "state", 0) eq "disabled" or ReadingsVal($name, "state", 0) eq "Unknown" or ReadingsVal($name, "state", 0) eq "charWrite faild") ); readingsSingleUpdate ( $hash, "state", "active", 1 ) if( (ReadingsVal($name, "state", 0) eq "initialized" or ReadingsVal($name, "state", 0) eq "unreachable" or ReadingsVal($name, "state", 0) eq "corrupted data" or ReadingsVal($name, "state", 0) eq "disabled" or ReadingsVal($name, "state", 0) eq "Unknown" or ReadingsVal($name, "state", 0) eq "charWrite faild") );
@ -228,14 +231,21 @@ sub XiaomiFlowerSens_stateRequestTimer($) {
sub XiaomiFlowerSens_Set($$@) { sub XiaomiFlowerSens_Set($$@) {
my ($hash, $name, @aa) = @_; my ($hash, $name, @aa) = @_;
my ($cmd, $arg) = @aa; my ($cmd, @args) = @aa;
my $action;
if( $cmd eq 'statusRequest' ) { if( $cmd eq 'statusRequest' ) {
return "usage: statusRequest" if( @args != 0 );
XiaomiFlowerSens_stateRequest($hash); XiaomiFlowerSens_stateRequest($hash);
} elsif( $cmd eq 'clearFirmwareReading' ) {
return "usage: clearFirmwareReading" if( @args != 0 );
readingsSingleUpdate($hash,'firmware','',0);
} else { } else {
my $list = "statusRequest:noArg"; my $list = "statusRequest:noArg clearFirmwareReading:noArg";
return "Unknown argument $cmd, choose one of $list"; return "Unknown argument $cmd, choose one of $list";
} }
@ -251,13 +261,12 @@ sub XiaomiFlowerSens_Run($) {
my $wfr; my $wfr;
if( ReadingsVal($name, "firmware", 0) eq "2.6.2" ) { if( ReadingsVal($name, 'firmware', '') eq "2.6.2" ) {
$wfr = 0; $wfr = 0;
} else { } else {
$wfr = 1; $wfr = 1;
} }
BlockingKill($hash->{helper}{RUNNING_PID}) if(defined($hash->{helper}{RUNNING_PID}));
my $response_encode = XiaomiFlowerSens_forRun_encodeJSON($mac,$wfr); my $response_encode = XiaomiFlowerSens_forRun_encodeJSON($mac,$wfr);
@ -287,7 +296,8 @@ sub XiaomiFlowerSens_BlockingRun($) {
Log3 $name, 4, "Sub XiaomiFlowerSens_BlockingRun ($name) - Processing response data: $sensData"; Log3 $name, 4, "Sub XiaomiFlowerSens_BlockingRun ($name) - Processing response data: $sensData";
return "$name|$sensData" # if error in stdout the error will given to $lux variable
return "$name|Unknown Error, look at verbose 5 output" # if error in stdout the error will given to $sensData variable
unless( defined($batFwData) ); unless( defined($batFwData) );
@ -337,14 +347,15 @@ sub XiaomiFlowerSens_BlockingRun($) {
sub XiaomiFlowerSens_callGatttool($@) { sub XiaomiFlowerSens_callGatttool($@) {
my ($name,$mac,$wfr) = @_; my ($name,$mac,$wfr) = @_;
my $hci = ReadingsVal($name,"hciDevice","hci0"); my $hci = AttrVal($name,"hciDevice","hci0");
my $loop = 0; my $loop;
my $wresp; my $wresp;
my @readSensData; my @readSensData;
my @readBatFwData; my @readBatFwData;
$loop = 0;
while ( (qx(ps ax | grep -v grep | grep "gatttool -b $mac") and $loop = 0) or (qx(ps ax | grep -v grep | grep "gatttool -b $mac") and $loop < 5) ) { while ( (qx(ps ax | grep -v grep | grep "gatttool -b $mac") and $loop = 0) or (qx(ps ax | grep -v grep | grep "gatttool -b $mac") and $loop < 5) ) {
Log3 $name, 4, "Sub XiaomiFlowerSens ($name) - check gattool is running. loop: $loop"; Log3 $name, 4, "Sub XiaomiFlowerSens ($name) - check gattool is running. loop: $loop";
sleep 0.5; sleep 0.5;
@ -356,44 +367,52 @@ sub XiaomiFlowerSens_callGatttool($@) {
#### Read Sensor Data #### Read Sensor Data
## support for Firmware 2.6.6, man muß erst einen Characterwert schreiben ## support for Firmware 2.6.6, man muß erst einen Characterwert schreiben
Log3 $name, 5, "Sub XiaomiFlowerSens_callGatttool ($name) - WFR: $wfr";
if($wfr == 1) { if($wfr == 1) {
$loop = 0;
do { do {
$wresp = qx(gatttool -i $hci -b $mac --char-write-req -a 0x33 -n A01F) if($wfr == 1);
$loop++;
} while( ($loop < 10) and (not defined($wresp)) ); $wresp = qx(gatttool -i $hci -b $mac --char-write-req -a 0x33 -n A01F 2>&1 /dev/null);
$loop++;
Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - call gatttool charWrite loop $loop";
Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - charWrite wresp: $wresp" if(defined($wresp) and ($wresp) );
} while( ($loop < 10) and (not $wresp =~ /^Characteristic value was written successfully$/) );
} }
Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - run gatttool"; Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - run gatttool";
$loop = 0;
do { do {
@readSensData = split(": ",qx(gatttool -i $hci -b $mac --char-read -a 0x35 2>&1 /dev/null)); @readSensData = split(": ",qx(gatttool -i $hci -b $mac --char-read -a 0x35 2>&1 /dev/null));
Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - call gatttool charRead loop $loop";
$loop++; $loop++;
Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - call gatttool charRead loop $loop";
} while( $loop < 10 and $readSensData[0] =~ /connect error/ ); } while( $loop < 10 and not $readSensData[0] =~ /^Characteristic value\/descriptor$/ );
Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - processing gatttool response. sensData[0]: $readSensData[0]";
Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - processing gatttool response. sensData: $readSensData[1]"; Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - processing gatttool response. sensData: $readSensData[1]";
return ($readSensData[1],undef) return ($readSensData[1],undef)
unless( $readSensData[0] =~ /Characteristic value/ ); unless( $readSensData[0] =~ /^Characteristic value\/descriptor$/ );
### Read Firmware and Battery Data ### Read Firmware and Battery Data
$loop = 0;
do { do {
@readBatFwData = split(": ",qx(gatttool -i $hci -b $mac --char-read -a 0x38 2>&1 /dev/null)); @readBatFwData = split(": ",qx(gatttool -i $hci -b $mac --char-read -a 0x38 2>&1 /dev/null));
Log3 $name, 4, "Sub XiaomiFlowerSens ($name) - call gatttool readBatFw loop $loop";
$loop++; $loop++;
Log3 $name, 4, "Sub XiaomiFlowerSens ($name) - call gatttool readBatFw loop $loop";
} while( $loop < 10 and $readBatFwData[0] =~ /connect error/ ); } while( $loop < 10 and not $readBatFwData[0] =~ /^Characteristic value\/descriptor$/ );
Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - processing gatttool response. batFwData: $readBatFwData[1]"; Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - processing gatttool response. batFwData: $readBatFwData[1]";
return ($readBatFwData[1],undef) return ($readBatFwData[1],undef)
unless( $readBatFwData[0] =~ /Characteristic value/ ); unless( $readBatFwData[0] =~ /^Characteristic value\/descriptor$/ );
@ -455,6 +474,13 @@ sub XiaomiFlowerSens_BlockingDone($) {
readingsEndUpdate($hash,1); readingsEndUpdate($hash,1);
return undef; return undef;
} elsif( $response eq "Unknown Error, look at verbose 5 output" ) {
readingsBulkUpdate($hash,"lastGattError","$response");
readingsBulkUpdate($hash,"state","unreachable");
readingsEndUpdate($hash,1);
return undef;
} elsif( ref($response) eq "HASH" ) { } elsif( ref($response) eq "HASH" ) {
readingsBulkUpdate($hash,"lastGattError","$response"); readingsBulkUpdate($hash,"lastGattError","$response");
readingsBulkUpdate($hash,"state","unreachable"); readingsBulkUpdate($hash,"state","unreachable");
@ -477,6 +503,12 @@ sub XiaomiFlowerSens_BlockingDone($) {
readingsEndUpdate($hash,1); readingsEndUpdate($hash,1);
DoTrigger($name, 'minFertility ' . ($response_json->{fertility}<AttrVal($name,'minFertility',0)?'low':'ok')) if( AttrVal($name,'minFertility','none') ne 'none' );
DoTrigger($name, 'minTemp ' . ($response_json->{temp}/10<AttrVal($name,'minTemp',0)?'low':'ok')) if( AttrVal($name,'minTemp','none') ne 'none' );
DoTrigger($name, 'maxTemp ' . ($response_json->{temp}/10>AttrVal($name,'maxTemp',0)?'high':'ok')) if( AttrVal($name,'maxTemp','none') ne 'none' );
DoTrigger($name, 'minMoisture ' . ($response_json->{moisture}<AttrVal($name,'minMoisture',0)?'low':'ok')) if( AttrVal($name,'minMoisture','none') ne 'none' );
Log3 $name, 4, "Sub XiaomiFlowerSens_BlockingDone ($name) - Abschluss!"; Log3 $name, 4, "Sub XiaomiFlowerSens_BlockingDone ($name) - Abschluss!";
} }
@ -555,20 +587,29 @@ sub XiaomiFlowerSens_BlockingAborted($) {
<b>Set</b> <b>Set</b>
<ul> <ul>
<li>statusRequest - retrieves the current state of the Xiaomi Flower Monitor.</li> <li>statusRequest - retrieves the current state of the Xiaomi Flower Monitor.</li>
<li>clearFirmwareReading - clear firmware reading for new begin.</li>
<br> <br>
</ul> </ul>
<br><br> <br><br>
<a name="NUKIDeviceattribut"></a> <a name="XiaomiFlowerSensattribut"></a>
<b>Attributes</b> <b>Attributes</b>
<ul> <ul>
<li>disable - disables the Nuki device</li> <li>disable - disables the Nuki device</li>
<li>interval - interval in seconds for statusRequest</li> <li>interval - interval in seconds for statusRequest</li>
<li>minFertility - min fertility value befor low warn event</li>
<li>minMoisture - min moisture value befor low warn event</li>
<li>minTemp - min temperature value befor low warn event</li>
<li>maxTemp - max temperature value befor high warn event</li>
<br> <br>
Event Example for min/max Value's: 2017-03-16 11:08:05 XiaomiFlowerSens Dracaena minMoisture low<br>
Event Example for min/max Value's: 2017-03-16 11:08:06 XiaomiFlowerSens Dracaena maxTemp high
</ul> </ul>
</ul> </ul>
=end html =end html
=begin html_DE =begin html_DE
=end html_DE =end html_DE
=cut =cut