Skip to content

Commit e5ed77c

Browse files
authored
Merge pull request #2618 from nebuohyrrah/feature_Supervision_Switch_MultiLevel
Supervision CC support for SwitchMultiLevel
2 parents 9cb4621 + 5eab991 commit e5ed77c

4 files changed

Lines changed: 78 additions & 31 deletions

File tree

cpp/src/command_classes/SwitchMultilevel.cpp

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "command_classes/CommandClasses.h"
2929
#include "command_classes/SwitchMultilevel.h"
3030
#include "command_classes/WakeUp.h"
31+
#include "command_classes/Supervision.h"
3132
#include "Defs.h"
3233
#include "Msg.h"
3334
#include "Driver.h"
@@ -226,6 +227,29 @@ namespace OpenZWave
226227
return false;
227228
}
228229

230+
void SwitchMultilevel::SupervisionSessionSuccess(uint8 _session_id, uint32 const _instance)
231+
{
232+
if (Node* node = GetNodeUnsafe())
233+
{
234+
uint32 index = node->GetSupervisionIndex(_session_id);
235+
236+
if (index != Internal::CC::Supervision::StaticNoIndex())
237+
{
238+
if (Internal::VC::ValueByte* value = static_cast<Internal::VC::ValueByte*>(GetValue(_instance, ValueID_Index_SwitchMultiLevel::Level)))
239+
{
240+
value->ConfirmNewValue();
241+
242+
Log::Write(LogLevel_Info, GetNodeId(), "Confirmed switch multi level index %d to value=%d",
243+
index, value->GetValue());
244+
}
245+
}
246+
else
247+
{
248+
Log::Write(LogLevel_Info, GetNodeId(), "Ignore unknown supervision session %d", _session_id);
249+
}
250+
}
251+
}
252+
229253
//-----------------------------------------------------------------------------
230254
// <SwitchMultilevel::SetValue>
231255
// Set the level on a device
@@ -394,40 +418,54 @@ namespace OpenZWave
394418
//-----------------------------------------------------------------------------
395419
bool SwitchMultilevel::SetLevel(uint8 const _instance, uint8 const _level)
396420
{
397-
Log::Write(LogLevel_Info, GetNodeId(), "SwitchMultilevel::Set - Setting to level %d", _level);
398-
Msg* msg = new Msg("SwitchMultilevelCmd_Set", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true);
399-
msg->SetInstance(this, _instance);
400-
msg->Append(GetNodeId());
401421

402-
if (GetVersion() >= 2)
403-
{
404-
Internal::VC::ValueInt* durationValue = static_cast<Internal::VC::ValueInt*>(GetValue(_instance, ValueID_Index_SwitchMultiLevel::Duration));
405-
uint32 duration = durationValue->GetValue();
406-
durationValue->Release();
407-
if (duration > 7620)
408-
Log::Write(LogLevel_Info, GetNodeId(), " Duration: Device Default");
409-
else if (duration > 0x7F)
410-
Log::Write(LogLevel_Info, GetNodeId(), " Rouding to %d Minutes (over 127 seconds)", encodeDuration(duration)-0x79);
411-
else
412-
Log::Write(LogLevel_Info, GetNodeId(), " Duration: %d seconds", duration);
413-
414-
msg->Append(4);
415-
msg->Append(GetCommandClassId());
416-
msg->Append(SwitchMultilevelCmd_Set);
417-
msg->Append(_level);
418-
msg->Append(encodeDuration(duration));
419-
}
420-
else
422+
if (Node* node = GetNodeUnsafe())
421423
{
422-
msg->Append(3);
423-
msg->Append(GetCommandClassId());
424-
msg->Append(SwitchMultilevelCmd_Set);
425-
msg->Append(_level);
426-
}
424+
//add supervision encapsulation if supported
425+
uint8 index = ValueID_Index_SwitchMultiLevel::Level;
426+
uint8 supervision_session_id = node->CreateSupervisionSession(StaticGetCommandClassId(), index);
427+
if (supervision_session_id == Internal::CC::Supervision::StaticNoSessionId())
428+
{
429+
Log::Write(LogLevel_Debug, GetNodeId(), "Supervision not supported, fall back to setpoint set/get");
430+
}
427431

428-
msg->Append(GetDriver()->GetTransmitOptions());
429-
GetDriver()->SendMsg(msg, Driver::MsgQueue_Send);
430-
return true;
432+
Log::Write(LogLevel_Info, GetNodeId(), "SwitchMultilevel::Set - Setting to level %d", _level);
433+
Msg* msg = new Msg("SwitchMultilevelCmd_Set", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true);
434+
msg->SetInstance(this, _instance);
435+
msg->SetSupervision(supervision_session_id);
436+
msg->Append(GetNodeId());
437+
438+
if (GetVersion() >= 2)
439+
{
440+
Internal::VC::ValueInt* durationValue = static_cast<Internal::VC::ValueInt*>(GetValue(_instance, ValueID_Index_SwitchMultiLevel::Duration));
441+
uint32 duration = durationValue->GetValue();
442+
durationValue->Release();
443+
if (duration > 7620)
444+
Log::Write(LogLevel_Info, GetNodeId(), " Duration: Device Default");
445+
else if (duration > 0x7F)
446+
Log::Write(LogLevel_Info, GetNodeId(), " Rouding to %d Minutes (over 127 seconds)", encodeDuration(duration)-0x79);
447+
else
448+
Log::Write(LogLevel_Info, GetNodeId(), " Duration: %d seconds", duration);
449+
450+
msg->Append(4);
451+
msg->Append(GetCommandClassId());
452+
msg->Append(SwitchMultilevelCmd_Set);
453+
msg->Append(_level);
454+
msg->Append(encodeDuration(duration));
455+
}
456+
else
457+
{
458+
msg->Append(3);
459+
msg->Append(GetCommandClassId());
460+
msg->Append(SwitchMultilevelCmd_Set);
461+
msg->Append(_level);
462+
}
463+
464+
msg->Append(GetDriver()->GetTransmitOptions());
465+
GetDriver()->SendMsg(msg, Driver::MsgQueue_Send);
466+
return true;
467+
}
468+
return false;
431469
}
432470

433471
//-----------------------------------------------------------------------------

cpp/src/command_classes/SwitchMultilevel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ namespace OpenZWave
7979
{
8080
return 4;
8181
}
82+
virtual void SupervisionSessionSuccess(uint8 _session_id, uint32 const _instance);
8283

8384
protected:
8485
virtual void CreateVars(uint8 const _instance) override;

cpp/src/value_classes/ValueByte.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ namespace OpenZWave
120120
ValueByte* tempValue = new ValueByte(*this);
121121
tempValue->m_value = _value;
122122

123+
// Save the new value to be stored when the device confirms the value was set successfully,
124+
m_newValue = _value;
125+
123126
// Set the value in the device.
124127
bool ret = ((Value*) tempValue)->Set();
125128

cpp/src/value_classes/ValueByte.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ namespace OpenZWave
5555

5656
bool Set(uint8 const _value);
5757
void OnValueRefreshed(uint8 const _value);
58+
void ConfirmNewValue()
59+
{
60+
OnValueRefreshed(m_newValue);
61+
};
5862
void SetTargetValue(uint8 const _target, uint32 _duration = 0);
5963

6064
// From Value
@@ -72,6 +76,7 @@ namespace OpenZWave
7276
uint8 m_value; // the current value
7377
uint8 m_valueCheck; // the previous value (used for double-checking spurious value reads)
7478
uint8 m_targetValue; // the Target Value, if the CC support it
79+
uint8 m_newValue; // a new value to be set on the device (used by Supervision CC)
7580
};
7681
} // namespace VC
7782
} // namespace Internal

0 commit comments

Comments
 (0)