Bugfix and code cleaning

fix: unkown command at device set.
fix : updateallreadings now using sub functions
change: device readings now with real names
This commit is contained in:
2017-05-01 13:09:07 +02:00
parent c0e41a0a52
commit 49c6a159ea
3 changed files with 78 additions and 98 deletions

View File

@@ -50,7 +50,7 @@ sub XiaomiSmartHome_Notify($$);
sub XiaomiSmartHome_updateSingleReading($$);
sub XiaomiSmartHome_updateAllReadings($);
my $iv="\x17\x99\x6d\x09\x3d\x28\xdd\xb3\xba\x69\x5a\x2e\x6f\x58\x56\x2e";
my $version = "0.18";
my $version = "0.20";
my %XiaomiSmartHome_gets = (
"getDevices" => ["get_id_list", '^.+get_id_list_ack' ],
@@ -117,12 +117,13 @@ sub XiaomiSmartHome_Read($) {
{
Log3 $name, 5, "$name: Read> " . $buf;
readingsBeginUpdate( $hash );
if ($decoded->{'cmd'} eq 'read_ack' || $decoded->{'cmd'} eq 'report' && $decoded->{'model'} eq 'gateway' || $decoded->{'cmd'} eq 'heartbeat' && $decoded->{'model'} eq 'gateway' || $decoded->{'cmd'} eq 'write_ack'){
if ($decoded->{'cmd'} eq 'report' && $decoded->{'model'} eq 'gateway' || $decoded->{'cmd'} eq 'heartbeat' && $decoded->{'model'} eq 'gateway' || $decoded->{'cmd'} eq 'write_ack'){
readingsBeginUpdate( $hash );
if ($decoded->{'model'} && $decoded->{'model'} eq 'gateway' ){
if ($decoded->{'cmd'} eq 'report'){
my $data = decode_json($decoded->{data});
if (defined $data->{rgb}){
Log3 $name, 4, "$name: Read>" . " SID: " . $decoded->{'sid'} . " Type: Gateway" . " RGB: " . $data->{rgb} ;
Log3 $name, 3, "$name: Read>" . " SID: " . $decoded->{'sid'} . " Type: Gateway" . " RGB: " . $data->{rgb} ;
readingsBulkUpdate($hash, "RGB", $data->{rgb} , 1 );
}
}
@@ -147,12 +148,23 @@ sub XiaomiSmartHome_Read($) {
readingsBulkUpdate($hash, 'heartbeat', "Write_OK", 1 );
}
}
readingsEndUpdate( $hash, 1 );
return;
}
else {
Log3 $name, 4, "$name: Read> dispatch " . $buf;
Dispatch($hash, $buf, undef);
}
readingsEndUpdate( $hash, 1 );
if ($decoded->{'cmd'} eq 'get_id_list_ack'){
my @sensors = @{decode_json($decoded->{data})};
foreach my $sensor (@sensors)
{
Log3 $name, 4, "$name: Read> PushRead:" . $sensor;
XiaomiSmartHome_Write($hash, 'read', $sensor );
}
return;
}
Log3 $name, 4, "$name: Read> dispatch " . $buf;
Dispatch($hash, $buf, undef);
}
}
#####################################
@@ -199,10 +211,8 @@ sub XiaomiSmartHome_getGatewaySID($){
{
if ($decoded->{'sid'} ne '')
{
Log3 $name, 3, "$name: Find SID for Gateway: $decoded->{'sid'}";
Log3 $name, 4, "$name: Find SID for Gateway: $decoded->{'sid'}";
return $decoded->{'sid'};
# XiaomiSmartHome_connect($hash) if( $init_done);
}
}
else {
@@ -277,9 +287,8 @@ sub XiaomiSmartHome_Write($@)
XiaomiSmartHome_disconnect($hash);
$p->close();
return undef;
}
}
}
}
my $GATEWAY = inet_ntoa(inet_aton($hash->{GATEWAY}));
$hash->{GATEWAY_IP} = $GATEWAY;
my $msg;
@@ -287,31 +296,33 @@ sub XiaomiSmartHome_Write($@)
{
$msg = '{"cmd":"' .$cmd . '","sid":"' . $val . '"}';
}
else {
if ( $hash->{READINGS}{password}{VAL} !~ /^[a-zA-Z0-9]{16}$/ )
{
Log3 $name, 1, "$name: Write> Password not SET!";
readingsSingleUpdate($hash, "password", "giveaPassword!", 1);
return "for $cmd, wrong password, it must be hex and 16 characters";
}
else {
if ($cmd eq 'rgb')
{
$msg = '{"cmd":"write","model":"gateway","sid":"' . $hash->{SID} . '","short_id":0,"key":"8","data":"{\"rgb\":' . $val . ',\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }';
}
if ($cmd eq 'pct')
{
$msg = '{"cmd":"write","model":"gateway","sid":"' . $hash->{SID} . '","short_id":0,"key":"8","data":"{\"rgb\":' . $val . ',\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }';
}
if ($cmd eq 'power')
{
$msg = '{"cmd":"write","model":"plug","sid":"' . $iohash->{SID} . '","data":"{\"status\":\"' . $val . '\",\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }';
}
if ($cmd eq 'get_id_list')
{
Log3 $name, 5, "$name: Write> Get all Sensors";
$msg = '{"cmd" : "get_id_list"}';
}
if ( $hash->{READINGS}{password}{VAL} !~ /^[a-zA-Z0-9]{16}$/ )
{
Log3 $name, 1, "$name: Write> Password not SET!";
readingsSingleUpdate($hash, "password", "giveaPassword!", 1);
return "for $cmd, wrong password, it must be hex and 16 characters";
}
else {
if ($cmd eq 'rgb')
{
$msg = '{"cmd":"write","model":"gateway","sid":"' . $hash->{SID} . '","short_id":0,"key":"8","data":"{\"rgb\":' . $val . ',\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }';
}
if ($cmd eq 'pct')
{
$msg = '{"cmd":"write","model":"gateway","sid":"' . $hash->{SID} . '","short_id":0,"key":"8","data":"{\"rgb\":' . $val . ',\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }';
}
if ($cmd eq 'power')
{
$msg = '{"cmd":"write","model":"plug","sid":"' . $iohash->{SID} . '","data":"{\"status\":\"' . $val . '\",\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }';
}
}
}
return Log3 $name, 4, "$name: Write> - socket not connected" unless($hash->{CD});
Log3 $name, 3, "$name: Write> $msg " . $GATEWAY;
Log3 $name, 4, "$name: Write> $msg " . $GATEWAY;
my $sock = $hash->{CD};
my $MAXLEN = 1024;
$sock->mcast_send($msg,$GATEWAY .':9898') or die "send: $!";
@@ -593,7 +604,7 @@ sub XiaomiSmartHome_updateAllReadings($)
{
my $hash = shift;
my $name = $hash->{NAME};
Log3 $name, 5, "name> updateAllReadings> Starting UpdateALLReadings";
Log3 $name, 5, "$name> updateAllReadings> Starting UpdateALLReadings";
my $GATEWAY;
my $p = Net::Ping->new();
if ( ! $p->ping($hash->{GATEWAY})){
@@ -626,37 +637,8 @@ sub XiaomiSmartHome_updateAllReadings($)
XiaomiSmartHome_connect($hash);
return undef;
}
my $sock = $hash->{CD};
my $MAXLEN = 1024;
my $msg = '{"cmd" : "get_id_list"}';
$sock->mcast_send($msg, $GATEWAY .':9898') or die "send: $!";
eval {
$sock->recv($msg, $MAXLEN) or die "recv: $!";
Log3 $name, 5, "$name: updateAllReadings>" . $msg;
my $json = $hash->{helper}{JSON}->incr_parse($msg);
my $decoded = decode_json($msg);
if ($json){
Log3 $name, 5, "$name: updateAllReadings> Read:" . $msg;
if ($decoded->{'cmd'} eq 'get_id_list_ack'){
my @sensors = @{decode_json($decoded->{data})};
foreach my $sensor (@sensors)
{
$msg = '{"cmd":"read","sid":"' . $sensor . '" }';
Log3 $name, 4, "$name: updateAllReadings> PushRead:" . $sensor;
my $msg = '{"cmd":"read","sid":"' . $sensor . '" }';
$sock->mcast_send($msg, $GATEWAY .':9898') or die "send: $!";
eval {
$sock->recv($msg, $MAXLEN) or die "recv: $!";
Log3 $name, 5, "$name: updateAllReadings> " . $msg;
Dispatch($hash, $msg, undef);
}
}
}
}
}
#return undef;
XiaomiSmartHome_Write($hash, 'get_id_list');
}
#####################################

View File

@@ -25,7 +25,7 @@ package main;
use strict;
use warnings;
my $version = "0.19";
my $version = "0.20";
sub XiaomiSmartHome_Device_updateSReading($);
@@ -52,8 +52,6 @@ sub XiaomiSmartHome_Device_mot($$)
my ($hash, $mot) = @_;
InternalTimer(gettimeofday()+$mot, "XiaomiSmartHome_Device_on_timeout",$hash, 0);
# on-for-timer is now a on.
}
#####################################
@@ -74,26 +72,27 @@ sub XiaomiSmartHome_Device_Set($@)
if($args[0] eq "on")
{
IOWrite($hash,"power","on",$hash);
return;
}
elsif($args[0] eq "off")
{
IOWrite($hash,"power","off",$hash);
return;
}
}
if($cmd eq "open")
{
readingsSingleUpdate($hash, "state", "open", 1 );
return;
}
# if($cmd eq "open")
# {
# readingsSingleUpdate($hash, "state", "open", 1 );
# return;
# }
if($cmd eq "motionOffTimer")
{
readingsSingleUpdate($hash, "motionOffTimer", "$args[0]", 1 );;
return;
}
else
{
return "Unknown argument $cmd, choose one of $setlist";
}
return "Unknown argument $cmd, choose one of $setlist";
}
@@ -117,7 +116,7 @@ sub XiaomiSmartHome_Device_Read($$$){
my $data = decode_json($decoded->{data});
readingsBeginUpdate( $hash );
if (defined $data->{status}){
Log3 $name, 3, "$name: DEV_Read>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Status: " . $data->{status};
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Status: " . $data->{status};
readingsBulkUpdate($hash, "state", "$data->{status}", 1 );
if ($data->{status} eq 'motion' && $hash->{MODEL} eq 'motion'){
readingsBulkUpdate($hash, "no_motion", "0", 1 );
@@ -127,33 +126,33 @@ sub XiaomiSmartHome_Device_Read($$$){
}
}
if (defined $data->{no_motion}){
Log3 $name, 3, "$name: DEV_Read>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_motion: " . $data->{no_motion};
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_motion: " . $data->{no_motion};
readingsBulkUpdate($hash, "no_motion", "$data->{no_motion}", 1 );
}
if (defined $data->{no_close}){
Log3 $name, 3, "$name: DEV_Read>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_close: " . $data->{no_close};
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_close: " . $data->{no_close};
readingsBulkUpdate($hash, "no_close", "$data->{no_close}", 1 );
}
if (defined $data->{voltage}){
my $bat = ($data->{voltage}/1000);
Log3 $name, 3, "$name: DEV_Read>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Voltage: " . $data->{voltage};
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Voltage: " . $data->{voltage};
readingsBulkUpdate($hash, "battery", $bat, 1 );
}
if (defined $data->{temperature}){
my $temp = $data->{temperature};
$temp =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{2}(?=\d))/$1./g;
Log3 $name, 3, "$name: DEV_Read>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Temperature: " . $temp;
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Temperature: " . $temp;
readingsBulkUpdate($hash, "temperature", "$temp", 1 );
}
if (defined $data->{humidity}){
my $hum = $data->{humidity};
$hum =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{2}(?=\d))/$1./g;
Log3 $name, 3, "$name: DEV_Read>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Humidity: " . $hum;
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Humidity: " . $hum;
readingsBulkUpdate($hash, "humidity", "$hum", 1 );
}
#86sw2 start
if (defined $data->{channel_0}){
Log3 $name, 3, "$name: DEV_Read>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Channel_0: " . $data->{channel_0};
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Channel_0: " . $data->{channel_0};
readingsBulkUpdate($hash, "channel_0", "$data->{channel_0}", 1 );
}
if (defined $data->{channel_1}){
@@ -163,25 +162,25 @@ sub XiaomiSmartHome_Device_Read($$$){
#86sw2 end
#plug start
if (defined $data->{load_voltage}){
Log3 $name, 3, "$name: DEV_Read>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " LOAD_Voltage: " . $data->{load_voltage};
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " LOAD_Voltage: " . $data->{load_voltage};
readingsBulkUpdate($hash, "LOAD_Voltage", "$data->{load_voltage}", 1 );
}
if (defined $data->{load_power}){
Log3 $name, 3, "$name: DEV_Read>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " LOAD_Power: " . $data->{load_power};
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " LOAD_Power: " . $data->{load_power};
readingsBulkUpdate($hash, "LOAD_Power", "$data->{load_power}", 1 );
}
if (defined $data->{power_consumed}){
Log3 $name, 3, "$name:" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " POWER_Consumed: " . $data->{power_consumed};
Log3 $name, 3, "$name:" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " POWER_Consumed: " . $data->{power_consumed};
readingsBulkUpdate($hash, "POWER_Consumed", "$data->{power_consumed}", 1 );
}
if (defined $data->{inuse}){
Log3 $name, 3, "$name: DEV_Read>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " InUse: " . $data->{inuse};
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " InUse: " . $data->{inuse};
readingsBulkUpdate($hash, "inuse", "$data->{inuse}", 1 );
}
#plug end
#cube start
if (defined $data->{rotate}){
Log3 $name, 3, "$name: DEV_Read>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Rotate: " . $data->{rotate};
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Rotate: " . $data->{rotate};
readingsBulkUpdate($hash, "rotate", "$data->{rotate}", 1 );
readingsBulkUpdate($hash, "state", "rotate", 1 );
}
@@ -244,7 +243,6 @@ sub XiaomiSmartHome_Device_update($){
sub XiaomiSmartHome_Device_Define($$) {
my ($hash, $def) = @_;
my ($name, $modul, $sid, $type, $iodev) = split("[ \t]+", $def);
#Log3 "test", 3, "Define status = " . $status;
$hash->{TYPE} = $modul;
$hash->{MODEL} = $type;
$hash->{SID} = $sid;
@@ -256,7 +254,7 @@ sub XiaomiSmartHome_Device_Define($$) {
if(defined($hash->{IODev}->{NAME})) {
my $IOname = $hash->{IODev}->{NAME};
Log3 $name, 3, $IOname . ": DEV_Define> " .$name. ": " . $type . " I/O device is " . $hash->{IODev}->{NAME};
Log3 $name, 4, $IOname . ": DEV_Define> " .$name. ": " . $type . " I/O device is " . $hash->{IODev}->{NAME};
} else {
Log3 $name, 1, "$name DEV_Define> $type - no I/O device";
}
@@ -266,7 +264,7 @@ sub XiaomiSmartHome_Device_Define($$) {
return "XiaomiSmartHome device $hash->{SID} on XiaomiSmartHome $iodev already defined as $d->{NAME}." if( defined($d) && $d->{IODev} == $hash->{IODev} && $d->{NAME} ne $name );
Log3 $name, 3, $iodev . ": DEV_Define> " . $name . ": defined as ". $hash->{MODEL};
Log3 $name, 4, $iodev . ": DEV_Define> " . $name . ": defined as ". $hash->{MODEL};
$attr{$name}{room} = "MiSmartHome" if( !defined( $attr{$name}{room} ) );
if( $type eq 'motion') {
$attr{$name}{devStateIcon} = 'motion:motion_detector@red off:motion_detector@green no_motion:motion_detector@green' if( !defined( $attr{$name}{devStateIcon} ) );

View File

@@ -1,2 +1,2 @@
UPD 2017-04-28_20:38:35 22335 FHEM/71_XiaomiSmartHome.pm
UPD 2017-04-28_23:29:58 11418 FHEM/71_XiaomiSmartHome_Device.pm
UPD 2017-05-01_12:08:08 22360 FHEM/71_XiaomiSmartHome.pm
UPD 2017-05-01_11:32:24 11699 FHEM/71_XiaomiSmartHome_Device.pm