This commit is contained in:
Marko Oldenburg 2017-09-29 07:50:05 +02:00
commit b272a0cd7c

View File

@ -51,9 +51,9 @@ sub XiaomiFlowerSens_stateRequestTimer($);
sub XiaomiFlowerSens_Set($$@); sub XiaomiFlowerSens_Set($$@);
sub XiaomiFlowerSens_Run($); sub XiaomiFlowerSens_Run($);
sub XiaomiFlowerSens_BlockingRun($); sub XiaomiFlowerSens_BlockingRun($);
sub XiaomiFlowerSens_callGatttool($@); sub XiaomiFlowerSens_gattSensorData($);
sub XiaomiFlowerSens_forRun_encodeJSON($$); sub XiaomiFlowerSens_gattBatFwData($);
sub XiaomiFlowerSens_forDone_encodeJSON($$$$$$); sub XiaomiFlowerSens_forDone_encodeJSON(@);
sub XiaomiFlowerSens_BlockingDone($); sub XiaomiFlowerSens_BlockingDone($);
sub XiaomiFlowerSens_BlockingAborted($); sub XiaomiFlowerSens_BlockingAborted($);
@ -262,13 +262,29 @@ sub XiaomiFlowerSens_Set($$@) {
sub XiaomiFlowerSens_Run($) { sub XiaomiFlowerSens_Run($) {
my ( $hash, $cmd ) = @_; my $hash = shift;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
$hash->{helper}{RUNNING_PID} = BlockingCall("XiaomiFlowerSens_BlockingRun", $name, "XiaomiFlowerSens_BlockingDone", 30, "XiaomiFlowerSens_BlockingAborted", $hash) unless(exists($hash->{helper}{RUNNING_PID}));
Log3 $name, 4, "Sub XiaomiFlowerSens_Run ($name) - start blocking call";
readingsSingleUpdate ( $hash, "state", "read data", 1 ) if( ReadingsVal($name, "state", 0) eq "active" );
}
sub XiaomiFlowerSens_BlockingRun($) {
my $name = shift;
my $hci = AttrVal($name,"hciDevice","hci0");
my $sshHost = AttrVal($name,"sshHost","none");
my $mac = $hash->{BTMAC}; my $mac = $hash->{BTMAC};
my $wresp;
my @readSensData;
my @readBatFwData;
my $wfr; my $wfr;
if( ReadingsVal($name, 'firmware', '') eq "2.6.2" ) { if( ReadingsVal($name, 'firmware', '') eq "2.6.2" ) {
$wfr = 0; $wfr = 0;
} else { } else {
@ -276,30 +292,12 @@ sub XiaomiFlowerSens_Run($) {
} }
my $response_encode = XiaomiFlowerSens_forRun_encodeJSON($mac,$wfr);
$hash->{helper}{RUNNING_PID} = BlockingCall("XiaomiFlowerSens_BlockingRun", $name."|".$response_encode, "XiaomiFlowerSens_BlockingDone", 30, "XiaomiFlowerSens_BlockingAborted", $hash) unless(exists($hash->{helper}{RUNNING_PID}));
Log3 $name, 4, "Sub XiaomiFlowerSens_Run ($name) - start blocking call";
readingsSingleUpdate ( $hash, "state", "call data", 1 ) if( ReadingsVal($name, "state", 0) eq "active" );
}
sub XiaomiFlowerSens_BlockingRun($) {
my ($string) = @_;
my ($name,$data) = split("\\|", $string);
my $data_json = decode_json($data);
my $mac = $data_json->{mac};
my $wfr = $data_json->{wfr};
Log3 $name, 4, "Sub XiaomiFlowerSens_BlockingRun ($name) - Running nonBlocking"; Log3 $name, 4, "Sub XiaomiFlowerSens_BlockingRun ($name) - Running nonBlocking";
##### call sensor data ##### call sensor data
my ($sensData,$batFwData) = XiaomiFlowerSens_callGatttool($name,$mac,$wfr); my ($sensData,$batFwData) = XiaomiFlowerSens_callGatttool($name);
Log3 $name, 4, "Sub XiaomiFlowerSens_BlockingRun ($name) - Processing response data: $sensData"; Log3 $name, 4, "Sub XiaomiFlowerSens_BlockingRun ($name) - Processing response data: $sensData";
@ -352,16 +350,12 @@ sub XiaomiFlowerSens_BlockingRun($) {
return "$name|$response_encode"; return "$name|$response_encode";
} }
sub XiaomiFlowerSens_callGatttool($@) { sub XiaomiFlowerSens_gattSensorData($) {
my ($name,$mac,$wfr) = @_; my $name = shift;
my $hci = AttrVal($name,"hciDevice","hci0");
my $loop; my $loop;
my $wresp; my $wresp;
my $sshHost = AttrVal($name,"sshHost","none");
my @readSensData;
my @readBatFwData;
$loop = 0; $loop = 0;
@ -385,7 +379,6 @@ 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
@ -421,12 +414,12 @@ sub XiaomiFlowerSens_callGatttool($@) {
if( $sshHost ne 'none' ) { if( $sshHost ne 'none' ) {
@readSensData = split(": ",qx(ssh $sshHost 'gatttool -i $hci -b $mac --char-read -a 0x35 2>&1 /dev/null')); @readSensData = split(": ",qx(ssh $sshHost 'gatttool -i $hci -b $mac --char-read -a 0x35 2>&1 /dev/null'));
Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - call data from host $sshHost"; Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - read data from host $sshHost";
} else { } else {
@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 data from local host"; Log3 $name, 4, "Sub XiaomiFlowerSens_callGatttool ($name) - read data from local host";
} }
$loop++; $loop++;
@ -441,6 +434,16 @@ sub XiaomiFlowerSens_callGatttool($@) {
unless( $readSensData[0] =~ /^Characteristic value\/descriptor$/ ); unless( $readSensData[0] =~ /^Characteristic value\/descriptor$/ );
### no Error in data string
return $readSensData[1];
}
sub XiaomiFlowerSens_gattBatFwData($) {
my $name = shift;
my $loop;
### Read Firmware and Battery Data ### Read Firmware and Battery Data
$loop = 0; $loop = 0;
do { do {
@ -467,25 +470,11 @@ sub XiaomiFlowerSens_callGatttool($@) {
unless( $readBatFwData[0] =~ /^Characteristic value\/descriptor$/ ); unless( $readBatFwData[0] =~ /^Characteristic value\/descriptor$/ );
### no Error in data string ### no Error in data string
return ($readSensData[1],$readBatFwData[1]) return $readBatFwData[1];
} }
sub XiaomiFlowerSens_forRun_encodeJSON($$) { sub XiaomiFlowerSens_forDone_encodeJSON(@) {
my ($mac,$wfr) = @_;
my %data = (
'mac' => $mac,
'wfr' => $wfr
);
return encode_json \%data;
}
sub XiaomiFlowerSens_forDone_encodeJSON($$$$$$) {
my ($temp,$lux,$moisture,$fertility,$blevel,$fw) = @_; my ($temp,$lux,$moisture,$fertility,$blevel,$fw) = @_;
@ -550,7 +539,7 @@ sub XiaomiFlowerSens_BlockingDone($) {
readingsBulkUpdate($hash, "moisture", $response_json->{moisture}); readingsBulkUpdate($hash, "moisture", $response_json->{moisture});
readingsBulkUpdate($hash, "fertility", $response_json->{fertility}); readingsBulkUpdate($hash, "fertility", $response_json->{fertility});
readingsBulkUpdate($hash, "firmware", $response_json->{firmware}); readingsBulkUpdate($hash, "firmware", $response_json->{firmware});
readingsBulkUpdate($hash, "state", "active") if( ReadingsVal($name,"state", 0) eq "call data" or ReadingsVal($name,"state", 0) eq "unreachable" or ReadingsVal($name,"state", 0) eq "corrupted data" ); readingsBulkUpdate($hash, "state", "active") if( ReadingsVal($name,"state", 0) eq "read data" or ReadingsVal($name,"state", 0) eq "unreachable" or ReadingsVal($name,"state", 0) eq "corrupted data" );
readingsEndUpdate($hash,1); readingsEndUpdate($hash,1);