forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenergy-preference-delegate.cpp
119 lines (102 loc) · 4.21 KB
/
energy-preference-delegate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <app/clusters/energy-preference-server/energy-preference-server.h>
using namespace chip;
using namespace chip::app::Clusters::EnergyPreference;
using namespace chip::app::Clusters::EnergyPreference::Structs;
static BalanceStruct::Type gsEnergyBalances[] = {
{ .step = 0, .label = Optional<chip::CharSpan>("Efficient"_span) },
{ .step = 50, .label = Optional<chip::CharSpan>() },
{ .step = 100, .label = Optional<chip::CharSpan>("Comfort"_span) },
};
static BalanceStruct::Type gsPowerBalances[] = {
{ .step = 0, .label = Optional<chip::CharSpan>("1 Minute"_span) },
{ .step = 12, .label = Optional<chip::CharSpan>("5 Minutes"_span) },
{ .step = 24, .label = Optional<chip::CharSpan>("10 Minutes"_span) },
{ .step = 36, .label = Optional<chip::CharSpan>("15 Minutes"_span) },
{ .step = 48, .label = Optional<chip::CharSpan>("20 Minutes"_span) },
{ .step = 60, .label = Optional<chip::CharSpan>("25 Minutes"_span) },
{ .step = 70, .label = Optional<chip::CharSpan>("30 Minutes"_span) },
{ .step = 80, .label = Optional<chip::CharSpan>("60 Minutes"_span) },
{ .step = 90, .label = Optional<chip::CharSpan>("120 Minutes"_span) },
{ .step = 100, .label = Optional<chip::CharSpan>("Never"_span) },
};
// assumes it'll be the only delegate for it's lifetime.
struct EPrefDelegate : public Delegate
{
EPrefDelegate();
virtual ~EPrefDelegate();
CHIP_ERROR GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
chip::Optional<chip::MutableCharSpan> & aOutLabel) override;
CHIP_ERROR GetEnergyPriorityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, EnergyPriorityEnum & priority) override;
CHIP_ERROR GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
chip::Optional<chip::MutableCharSpan> & aOutLabel) override;
size_t GetNumEnergyBalances(chip::EndpointId aEndpoint) override;
size_t GetNumLowPowerModeSensitivities(chip::EndpointId aEndpoint) override;
};
EPrefDelegate::EPrefDelegate() : Delegate()
{
VerifyOrDie(GetDelegate() == nullptr);
SetDelegate(this);
}
EPrefDelegate::~EPrefDelegate()
{
VerifyOrDie(GetDelegate() == this);
SetDelegate(nullptr);
}
size_t EPrefDelegate::GetNumEnergyBalances(chip::EndpointId aEndpoint)
{
return (ArraySize(gsEnergyBalances));
}
size_t EPrefDelegate::GetNumLowPowerModeSensitivities(chip::EndpointId aEndpoint)
{
return (ArraySize(gsEnergyBalances));
}
CHIP_ERROR
EPrefDelegate::GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
chip::Optional<chip::MutableCharSpan> & aOutLabel)
{
if (aIndex < GetNumEnergyBalances(aEndpoint))
{
aOutStep = gsEnergyBalances[aIndex].step;
if (gsEnergyBalances[aIndex].HasValue())
{
chip::CopyCharSpanToMutableCharSpan(gsEnergyBalances[aIndex].label.Value(), aOutLabel.Value());
}
else
{
aOutLabel.ClearValue();
}
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NOT_FOUND;
}
CHIP_ERROR
EPrefDelegate::GetEnergyPriorityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, EnergyPriorityEnum & priority)
{
static EnergyPriorityEnum priorities[] = { EnergyPriorityEnum::kEfficiency, EnergyPriorityEnum::kComfort };
if (aIndex < ArraySize(priorities))
{
priority = priorities[aIndex];
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NOT_FOUND;
}
CHIP_ERROR
EPrefDelegate::GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
chip::Optional<chip::MutableCharSpan> & aOutLabel)
{
if (aIndex < GetNumLowPowerModeSensitivities(aEndpoint))
{
aOutStep = gsPowerBalances[aIndex].step;
if (gsPowerBalances[aIndex])
{
chip::CopyCharSpanToMutableCharSpan(gsPowerBalances[aIndex].label.Value(), aOutLabel.Value());
}
else
{
aOutLabel.ClearValue();
}
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NOT_FOUND;
}
static EPrefDelegate gsDelegate;