2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-17 23:46:03 +00:00

10_MYSENSORS_DEVICE.pm: added heartbeat, alive check, deep sleep and RSSI - thanks to Beta-User

git-svn-id: https://svn.fhem.de/fhem/trunk@16895 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
Hauswart 2018-06-22 07:23:44 +00:00
parent 24e3210969
commit 9850953741
3 changed files with 288 additions and 144 deletions

View File

@ -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: 10_MYSENSORS_DEVICE: heartbeat, alive check, sleep and RSSI
- change: 74_AMADtasker: AMAD Taskerproject change to new battery states
- feature: 49_SSCamSTRM: new attr hideDisplayName regarding to Forum #88667
- bugfix: 49_SSCam: global vars

View File

@ -28,7 +28,10 @@ use strict;
use warnings;
my %gets = (
"version" => "",
# "version" => 1,
"heartbeat" => 1, #request node to send hearbeat, MySensorsCore.cpp L 429
# "presentation" => 1, #request node to redo presentation, MySensorsCore.cpp L 426
"RSSI" => 1, #request RSSI values (available only for recent versions, not for all transceiver types
);
sub MYSENSORS_DEVICE_Initialize($) {
@ -39,6 +42,7 @@ sub MYSENSORS_DEVICE_Initialize($) {
$hash->{DefFn} = "MYSENSORS::DEVICE::Define";
$hash->{UndefFn} = "MYSENSORS::DEVICE::UnDefine";
$hash->{SetFn} = "MYSENSORS::DEVICE::Set";
$hash->{GetFn} = "MYSENSORS::DEVICE::Get";
$hash->{AttrFn} = "MYSENSORS::DEVICE::Attr";
$hash->{AttrList} =
@ -114,8 +118,8 @@ my %static_types = (
S_CUSTOM => { receives => [V_VAR1,V_VAR2,V_VAR3,V_VAR4,V_VAR5], sends => [V_VAR1,V_VAR2,V_VAR3,V_VAR4,V_VAR5] }, # Use this for custom sensors where no other fits.
S_DUST => { receives => [], sends => [V_LEVEL,V_UNIT_PREFIX] }, # Dust level sensor
S_SCENE_CONTROLLER => { receives => [], sends => [V_SCENE_ON,V_SCENE_OFF] }, # Scene controller device
S_RGB_LIGHT => { receives => [V_RGB,V_WATT], sends => [V_RGB,V_WATT] }, # RGB light
S_RGBW_LIGHT => { receives => [V_RGBW,V_WATT], sends => [V_RGBW,V_WATT] }, # RGBW light (with separate white component)
S_RGB_LIGHT => { receives => [V_RGB,V_WATT,V_PERCENTAGE], sends => [V_RGB,V_WATT,V_PERCENTAGE] }, # RGB light
S_RGBW_LIGHT => { receives => [V_RGBW,V_WATT,V_PERCENTAGE], sends => [V_RGBW,V_WATT,V_PERCENTAGE] }, # RGBW light (with separate white component)
S_COLOR_SENSOR => { receives => [V_RGB], sends => [V_RGB] }, # Color sensor
S_HVAC => { receives => [], sends => [V_STATUS,V_TEMP,V_HVAC_SETPOINT_HEAT,V_HVAC_SETPOINT_COOL,V_HVAC_FLOW_STATE,V_HVAC_FLOW_MODE,V_HVAC_SPEED] }, # Thermostat/HVAC device
S_MULTIMETER => { receives => [], sends => [V_VOLTAGE,V_CURRENT,V_IMPEDANCE] }, # Multimeter device
@ -270,6 +274,35 @@ sub Set($@) {
}
}
sub Get($@) {
my ($hash, @a) = @_;
my $type = $hash->{TYPE};
return "\"get $type\" needs at least one parameter" if(@a < 2);
if(!defined($gets{$a[1]})) {
my @cList = map { $_ =~ m/^(file|raw)$/ ? $_ : "$_:noArg" } sort keys %gets;
return "Unknown argument $a[1], choose one of " . join(" ", @cList);
}
my $command = $a[1];
COMMAND_HANDLER: {
$command eq "version" and do {
sendMessage($hash->{IODev}, radioId => $hash->{radioId}, cmd => C_INTERNAL, ack => 0, subType => I_VERSION);
last;
};
$command eq "heartbeat" and do {
sendMessage($hash->{IODev}, radioId => $hash->{radioId}, cmd => C_INTERNAL, ack => 0, subType => I_HEARTBEAT_REQUEST);
last;
};
$command eq "presentation" and do {
sendMessage($hash->{IODev}, radioId => $hash->{radioId}, cmd => C_INTERNAL, ack => 0, subType => I_PRESENTATION);
last;
};
$command eq "RSSI" and do {
sendMessage($hash->{IODev}, radioId => $hash->{radioId}, cmd => C_INTERNAL, ack => 0, subType => I_SIGNAL_REPORT_REQUEST);
last;
};
}
}
sub Attr($$$$) {
my ($command,$name,$attribute,$value) = @_;
@ -480,7 +513,7 @@ sub onSetMessage($$) {
readingsSingleUpdate($hash, $reading, $value, 1);
};
Log3 ($hash->{NAME}, 4, "MYSENSORS_DEVICE $hash->{NAME}: ignoring C_SET-message ".GP_Catch($@)) if $@;
refreshInternalMySTimer($hash,"Alive") if $hash->{timeoutAlive};
refreshInternalMySTimer($hash,"Alive") if $hash->{timeoutAlive}; #deactivate in case of wanted reduction of alive to internal (heartbeat/battery/smartsleep) messages
} else {
Log3 ($hash->{NAME}, 5, "MYSENSORS_DEVICE $hash->{NAME}: ignoring C_SET-message without payload");
};
@ -508,8 +541,11 @@ sub onInternalMessage($$) {
my $typeStr = internalMessageTypeToStr($type);
INTERNALMESSAGE: {
$type == I_BATTERY_LEVEL and do {
readingsSingleUpdate($hash, "batterylevel", $msg->{payload}, 1);
Log3 ($name, 4, "MYSENSORS_DEVICE $name: batterylevel $msg->{payload}");
# readingsSingleUpdate($hash, "batterylevel", $msg->{payload}, 1);
# Log3 ($name, 3, "MYSENSORS_DEVICE $name: batterylevel is deprecated and will be removed soon, use batteryPercent instead (Forum #87575)");
readingsSingleUpdate($hash, "batteryPercent", $msg->{payload}, 1);
refreshInternalMySTimer($hash,"Alive") if $hash->{timeoutAlive};
Log3 ($name, 4, "MYSENSORS_DEVICE $name: batteryPercent $msg->{payload}");
last;
};
$type == I_TIME and do {
@ -565,17 +601,17 @@ sub onInternalMessage($$) {
last;
};
$type == I_SKETCH_NAME and do {
$hash->{$typeStr} = $msg->{payload};
#$hash->{$typeStr} = $msg->{payload};
readingsSingleUpdate($hash, "SKETCH_NAME", $msg->{payload}, 1);
last;
};
$type == I_SKETCH_VERSION and do {
$hash->{$typeStr} = $msg->{payload};
#$hash->{$typeStr} = $msg->{payload};
readingsSingleUpdate($hash, "SKETCH_VERSION", $msg->{payload}, 1);
last;
};
$type == I_REBOOT and do {
$hash->{$typeStr} = $msg->{payload};
#$hash->{$typeStr} = $msg->{payload};
last;
};
$type == I_GATEWAY_READY and do {
@ -594,6 +630,77 @@ sub onInternalMessage($$) {
$hash->{$typeStr} = $msg->{payload};
last;
};
$type == I_HEARTBEAT_REQUEST and do {
#$hash->{$typeStr} = $msg->{payload};
eval {
sendMessage($hash->{IODev},radioId => 0, childId => 0, cmd => C_INTERNAL, ack => 0, subType => I_HEARTBEAT_RESPONSE);
};
refreshInternalMySTimer($hash,"Alive") if $hash->{timeoutAlive};
last;
};
$type == I_PRESENTATION and do {
#$hash->{$typeStr} = $msg->{payload};
last;
};
$type == I_DISCOVER_REQUEST and do {
#$hash->{$typeStr} = $msg->{payload};
last;
};
$type == I_DISCOVER_RESPONSE and do {
#$hash->{$typeStr} = $msg->{payload};
last;
};
$type == I_HEARTBEAT_RESPONSE and do {
readingsSingleUpdate($hash, "heartbeat", "last", 0);
refreshInternalMySTimer($hash,"Alive") if $hash->{timeoutAlive};
$hash->{$typeStr} = $msg->{payload};
last;
};
$type == I_LOCKED and do {
$hash->{$typeStr} = $msg->{payload};
last;
};
$type == I_REGISTRATION_REQUEST and do {
$hash->{$typeStr} = $msg->{payload};
last;
};
$type == I_REGISTRATION_RESPONSE and do {
$hash->{$typeStr} = $msg->{payload};
last;
};
$type == I_DEBUG and do {
$hash->{$typeStr} = $msg->{payload};
last;
};
$type == I_SIGNAL_REPORT_REVERSE and do {
#$hash->{$typeStr} = $msg->{payload};
readingsSingleUpdate($hash, "rssi_DEVICE", $msg->{payload}, 1) if ($msg->{payload} > -256 );
last;
};
$type == I_SIGNAL_REPORT_RESPONSE and do {
#$hash->{$typeStr} = $msg->{payload};
readingsSingleUpdate($hash, "rssi_at_IODev", $msg->{payload}, 1) if ($msg->{payload} > -256 );
last;
};
$type == I_PRE_SLEEP_NOTIFICATION and do {
#$hash->{$typeStr} = $msg->{payload};
readingsSingleUpdate($hash, "nowSleeping", "1", 0);
refreshInternalMySTimer($hash,"Alive") if $hash->{timeoutAlive};
last;
};
$type == I_POST_SLEEP_NOTIFICATION and do {
#$hash->{$typeStr} = $msg->{payload};
readingsSingleUpdate($hash, "nowSleeping", "0", 0);
refreshInternalMySTimer($hash,"Alive") if $hash->{timeoutAlive};
#here we can send out retained and outstanding messages
$hash->{nexttry} = -1;
MYSENSORS::Timer($hash);
my $retainedMsg;
while (defined($retainedMsg = shift @{$hash->{retainedMessagesForRadioId}})) {
sendClientMessage($hash,$retainedMsg);
};
last;
};
}
}
@ -601,8 +708,26 @@ sub sendClientMessage($%) {
my ($hash,%msg) = @_;
$msg{radioId} = $hash->{radioId};
$msg{ack} = $hash->{ack} unless defined $msg{ack};
unless ($hash->{nowSleeping}) {
sendMessage($hash->{IODev},%msg);
refreshInternalMySTimer($hash,"Ack") if (($hash->{ack} or $hash->{IODev}->{ack}) and $hash->{timeoutAck}) ;
refreshInternalMySTimer($hash,"Ack") if (($hash->{ack} or $hash->{IODev}->{ack}) and $hash->{timeoutAck});
} else {
#write to queue if node is asleep
my $retainedMessagesForRadioId = $hash->{retainedMessagesForRadioId}->{$msg{radioId}};
unless (defined $retainedMessagesForRadioId) {
$retainedMessagesForRadioId = {
messages => [],
};
$hash->{retainedMessagesForRadioId}->{$msg{radioId}} = $retainedMessagesForRadioId;
}
my $messages = $retainedMessagesForRadioId->{messages};
@$messages = grep {
$_->{childId} != $msg{childId}
or $_->{cmd} != $msg{cmd}
or $_->{subType} != $msg{subType}
} @$messages;
push @$messages,\%msg;
}
}
sub rawToMappedReading($$$$) {

View File

@ -115,12 +115,30 @@ use constant {
I_REQUEST_SIGNING => 15,
I_GET_NONCE => 16,
I_GET_NONCE_RESPONSE => 17,
I_HEARTBEAT_REQUEST => 18,
I_PRESENTATION => 19,
I_DISCOVER_REQUEST => 20,
I_DISCOVER_RESPONSE => 21,
I_HEARTBEAT_RESPONSE => 22,
I_LOCKED => 23, # Node is locked (reason in string-payload)
I_PING => 24, # Ping sent to node, payload incremental hop counter
I_PONG => 25, # In return to ping, sent back to sender, payload incremental hop counter
I_REGISTRATION_REQUEST => 26, # Register request to GW
I_REGISTRATION_RESPONSE => 27, # Register response from GW
I_DEBUG => 28,
I_SIGNAL_REPORT_REQUEST => 29,
I_SIGNAL_REPORT_REVERSE => 30,
I_SIGNAL_REPORT_RESPONSE => 31,
I_PRE_SLEEP_NOTIFICATION => 32,
I_POST_SLEEP_NOTIFICATION => 33,
};
use constant internalMessageTypes => qw{ I_BATTERY_LEVEL I_TIME I_VERSION I_ID_REQUEST I_ID_RESPONSE
I_INCLUSION_MODE I_CONFIG I_FIND_PARENT I_FIND_PARENT_RESPONSE
I_LOG_MESSAGE I_CHILDREN I_SKETCH_NAME I_SKETCH_VERSION
I_REBOOT I_GATEWAY_READY I_REQUEST_SIGNING I_GET_NONCE I_GET_NONCE_RESPONSE };
I_REBOOT I_GATEWAY_READY I_REQUEST_SIGNING I_GET_NONCE I_GET_NONCE_RESPONSE I_HEARTBEAT_REQUEST I_PRESENTATION I_DISCOVER_REQUEST
I_DISCOVER_RESPONSE I_HEARTBEAT_RESPONSE I_LOCKED I_PING I_PONG I_REGISTRATION_REQUEST I_REGISTRATION_RESPONSE I_DEBUG
I_SIGNAL_REPORT_REQUEST I_SIGNAL_REPORT_REVERSE I_SIGNAL_REPORT_RESPONSE I_PRE_SLEEP_NOTIFICATION I_POST_SLEEP_NOTIFICATION };
sub internalMessageTypeToStr($) {
(internalMessageTypes)[shift];
@ -174,7 +192,7 @@ use constant sensorTypes => qw{ S_DOOR S_MOTION S_SMOKE S_BINARY S_DIMMER S_COVE
S_RAIN S_UV S_WEIGHT S_POWER S_HEATER S_DISTANCE S_LIGHT_LEVEL S_ARDUINO_NODE
S_ARDUINO_REPEATER_NODE S_LOCK S_IR S_WATER S_AIR_QUALITY S_CUSTOM S_DUST S_SCENE_CONTROLLER
S_RGB_LIGHT S_RGBW_LIGHT S_COLOR_SENSOR S_HVAC S_MULTIMETER S_SPRINKLER S_WATER_LEAK S_SOUND S_VIBRATION
S_MOISTURE S_INFO S_GAS S_GPS S_WATER_QUALITY};
S_MOISTURE S_INFO S_GAS S_GPS S_WATER_QUALITY };
sub sensorTypeToStr($) {
(sensorTypes)[shift];