Skip to content

Commit 0773a0d

Browse files
committed
Refactoring
1 parent 90a776c commit 0773a0d

8 files changed

+260
-560
lines changed

examples/all-clusters-app/all-clusters-common/include/meter-identification-delegate.h

-95
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include <app/clusters/meter-identification-server/meter-identification-server.h>
4+
#include <json/value.h>
5+
6+
namespace chip {
7+
namespace app {
8+
namespace Clusters {
9+
namespace MeterIdentification {
10+
11+
Instance * GetInstance();
12+
13+
CHIP_ERROR LoadJson(const Json::Value & root);
14+
15+
void Shutdown();
16+
17+
} // namespace MeterIdentification
18+
} // namespace Clusters
19+
} // namespace app
20+
} // namespace chip

examples/all-clusters-app/all-clusters-common/src/MeterIdentificationEventTriggers.cpp

+48-44
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include <meter-identification-delegate.h>
19+
#include <meter-identification-instance.h>
2020

2121
#include <app/clusters/meter-identification-server/MeterIdentificationTestEventTriggerHandler.h>
2222

@@ -37,7 +37,7 @@ class OldMeterIdentificationAttributes
3737

3838
private:
3939

40-
DataModel::Nullable<MeterIdentificationDelegate *> mDelegate;
40+
Instance * mInstance = nullptr;
4141

4242
static void SetCharSpan(DataModel::Nullable<CharSpan> & charSpan, const DataModel::Nullable<CharSpan> && value)
4343
{
@@ -50,15 +50,12 @@ class OldMeterIdentificationAttributes
5050
if (!value.IsNull())
5151
{
5252
const size_t len = value.Value().size();
53-
auto * str = static_cast<char *>(chip::Platform::MemoryAlloc(1 + len));
54-
if (nullptr == str)
53+
if (auto * str = static_cast<char *>(chip::Platform::MemoryAlloc(1 + len)))
5554
{
56-
return;
55+
memcpy(str, value.Value().data(), len);
56+
str[len] = 0;
57+
charSpan = DataModel::MakeNullable(CharSpan(str, len));
5758
}
58-
59-
memcpy(str, value.Value().data(), len);
60-
str[len] = 0;
61-
charSpan = DataModel::MakeNullable(CharSpan(str, len));
6259
}
6360
}
6461

@@ -71,8 +68,14 @@ class OldMeterIdentificationAttributes
7168
}
7269
}
7370

71+
/**
72+
* Lexicographically increments a string.
73+
*
74+
* @param string A string to lexicographically increment
75+
*/
7476
static std::string IncrementString(const std::string &&string)
7577
{
78+
//The lexicographically smallest and largest characters
7679
constexpr char minC = ' ', maxC = '~';
7780

7881
std::string ret{minC};
@@ -102,13 +105,13 @@ class OldMeterIdentificationAttributes
102105

103106
void SaveAttributes()
104107
{
105-
mDelegate.SetNonNull(GetDelegate());
106-
VerifyOrDieWithMsg(mDelegate.Value() != nullptr, AppServer, "Meter Identification Delegate is null");
107-
mMeterType = mDelegate.Value()->GetMeterType();
108-
SetCharSpan(mPointOfDelivery, mDelegate.Value()->GetPointOfDelivery());
109-
SetCharSpan(mMeterSerialNumber, mDelegate.Value()->GetMeterSerialNumber());
110-
SetCharSpan(mProtocolVersion, mDelegate.Value()->GetProtocolVersion());
111-
const auto && powerThreshold = mDelegate.Value()->GetPowerThreshold();
108+
mInstance = GetInstance();
109+
VerifyOrDieWithMsg(mInstance, AppServer, "Meter Identification instance is null");
110+
mMeterType = mInstance->GetMeterType();
111+
SetCharSpan(mPointOfDelivery, mInstance->GetPointOfDelivery());
112+
SetCharSpan(mMeterSerialNumber, mInstance->GetMeterSerialNumber());
113+
SetCharSpan(mProtocolVersion, mInstance->GetProtocolVersion());
114+
const auto && powerThreshold = mInstance->GetPowerThreshold();
112115
if (!powerThreshold.IsNull())
113116
{
114117
mPowerThreshold.SetNonNull(powerThreshold.Value());
@@ -122,84 +125,85 @@ class OldMeterIdentificationAttributes
122125
CleanCharSpan(mProtocolVersion);
123126
mMeterType.SetNull();
124127
mPowerThreshold.SetNull();
125-
mDelegate.SetNull();
128+
mInstance = nullptr;
126129
}
127130

128131
void RestoreAttributes() const
129132
{
130-
if (!mDelegate.IsNull())
133+
if (mInstance)
131134
{
132-
mDelegate.Value()->SetMeterType(mMeterType);
133-
mDelegate.Value()->SetPointOfDelivery(mPointOfDelivery);
134-
mDelegate.Value()->SetMeterSerialNumber(mMeterSerialNumber);
135-
mDelegate.Value()->SetProtocolVersion(mProtocolVersion);
136-
mDelegate.Value()->SetPowerThreshold(mPowerThreshold);
135+
mInstance->SetMeterType(mMeterType);
136+
mInstance->SetPointOfDelivery(mPointOfDelivery);
137+
mInstance->SetMeterSerialNumber(mMeterSerialNumber);
138+
mInstance->SetProtocolVersion(mProtocolVersion);
139+
mInstance->SetPowerThreshold(mPowerThreshold);
137140
}
138141
}
139142

140143
void IncreaseAttributes()
141144
{
142-
if (mDelegate.Value()->GetMeterType().IsNull())
145+
VerifyOrDieWithMsg(mInstance, AppServer, "Meter Identification instance is null");
146+
if (mInstance->GetMeterType().IsNull())
143147
{
144-
mDelegate.Value()->SetMeterType(DataModel::MakeNullable(static_cast<MeterTypeEnum>(0)));
148+
mInstance->SetMeterType(DataModel::MakeNullable(static_cast<MeterTypeEnum>(0)));
145149
}
146150
else
147151
{
148-
mDelegate.Value()->SetMeterType(DataModel::MakeNullable(static_cast<MeterTypeEnum>(1 +
149-
static_cast<std::underlying_type<MeterTypeEnum>::type>(mDelegate.Value()->GetMeterType().Value()))));
152+
mInstance->SetMeterType(DataModel::MakeNullable(static_cast<MeterTypeEnum>(1 +
153+
static_cast<std::underlying_type<MeterTypeEnum>::type>(mInstance->GetMeterType().Value()))));
150154
}
151155

152-
if (mDelegate.Value()->GetPointOfDelivery().IsNull())
156+
if (mInstance->GetPointOfDelivery().IsNull())
153157
{
154-
mDelegate.Value()->SetPointOfDelivery(DataModel::MakeNullable(CharSpan::fromCharString("")));
158+
mInstance->SetPointOfDelivery(DataModel::MakeNullable(CharSpan::fromCharString("")));
155159
}
156160
else
157161
{
158-
mDelegate.Value()->SetPointOfDelivery(DataModel::MakeNullable(CharSpan::fromCharString(IncrementString(
159-
mDelegate.Value()->GetPointOfDelivery().Value().data()).c_str())));
162+
mInstance->SetPointOfDelivery(DataModel::MakeNullable(CharSpan::fromCharString(IncrementString(
163+
mInstance->GetPointOfDelivery().Value().data()).c_str())));
160164
}
161165

162-
if (mDelegate.Value()->GetMeterSerialNumber().IsNull())
166+
if (mInstance->GetMeterSerialNumber().IsNull())
163167
{
164-
mDelegate.Value()->SetMeterSerialNumber(DataModel::MakeNullable(CharSpan::fromCharString("")));
168+
mInstance->SetMeterSerialNumber(DataModel::MakeNullable(CharSpan::fromCharString("")));
165169
}
166170
else
167171
{
168-
mDelegate.Value()->SetMeterSerialNumber(DataModel::MakeNullable(CharSpan::fromCharString(IncrementString(
169-
mDelegate.Value()->GetMeterSerialNumber().Value().data()).c_str())));
172+
mInstance->SetMeterSerialNumber(DataModel::MakeNullable(CharSpan::fromCharString(IncrementString(
173+
mInstance->GetMeterSerialNumber().Value().data()).c_str())));
170174
}
171175

172-
if (mDelegate.Value()->GetProtocolVersion().IsNull())
176+
if (mInstance->GetProtocolVersion().IsNull())
173177
{
174-
mDelegate.Value()->SetProtocolVersion(DataModel::MakeNullable(CharSpan::fromCharString("")));
178+
mInstance->SetProtocolVersion(DataModel::MakeNullable(CharSpan::fromCharString("")));
175179
}
176180
else
177181
{
178-
mDelegate.Value()->SetProtocolVersion(DataModel::MakeNullable(CharSpan::fromCharString(IncrementString(
179-
mDelegate.Value()->GetProtocolVersion().Value().data()).c_str())));
182+
mInstance->SetProtocolVersion(DataModel::MakeNullable(CharSpan::fromCharString(IncrementString(
183+
mInstance->GetProtocolVersion().Value().data()).c_str())));
180184
}
181185

182-
if (mDelegate.Value()->GetPowerThreshold().IsNull())
186+
if (mInstance->GetPowerThreshold().IsNull())
183187
{
184-
mDelegate.Value()->SetPowerThreshold(DataModel::MakeNullable((Structs::PowerThresholdStruct::Type){Optional<int64_t>(0),
188+
mInstance->SetPowerThreshold(DataModel::MakeNullable((Structs::PowerThresholdStruct::Type){Optional<int64_t>(0),
185189
Optional<int64_t>(0), static_cast<PowerThresholdSourceEnum>(0)}));
186190
}
187191
else
188192
{
189-
auto powerThreshold = mDelegate.Value()->GetPowerThreshold();
193+
auto powerThreshold = mInstance->GetPowerThreshold();
190194
++powerThreshold.Value().powerThreshold.Value();
191195
++powerThreshold.Value().apparentPowerThreshold.Value();
192196
powerThreshold.Value().powerThresholdSource.Value() = static_cast<PowerThresholdSourceEnum>(1 +
193197
static_cast<std::underlying_type<PowerThresholdSourceEnum>::type>(powerThreshold.Value().powerThresholdSource.Value()));
194-
mDelegate.Value()->SetPowerThreshold(std::move(powerThreshold));
198+
mInstance->SetPowerThreshold(std::move(powerThreshold));
195199
}
196200
}
197201

198202
public:
199203

200204
void Update()
201205
{
202-
if (mDelegate.IsNull())
206+
if (!mInstance)
203207
{
204208
SaveAttributes();
205209
}

0 commit comments

Comments
 (0)