Extract Shade struct and create ShadeRepository

This commit is contained in:
2024-05-04 15:40:58 +10:00
parent eee6cbef46
commit a9dccae331
5 changed files with 386 additions and 123 deletions

18
lib/shade/Shade.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef SHADE_H
#define SHADE_H
#include <stdint.h>
#include <string>
struct Shade {
uint16_t ID;
std::string key;
std::string friendlyName;
int8_t lastTargetPosition;
int8_t lastPosition;
uint8_t samePositionCount;
uint8_t positionFetchCount;
void* timer;
};
#endif // SHADE_H

View File

@@ -0,0 +1,66 @@
#include "ShadeRepository.h"
#include "Shade.h"
void ShadeRepository::upsert(Shade shade)
{
for (Shade &existing_shade : shades)
{
if (existing_shade.key == shade.key)
{
existing_shade.friendlyName = shade.friendlyName;
existing_shade.ID = shade.ID;
for (auto &callback : shadeChangedCallbacks) {
callback(shade);
}
return;
}
}
// Didn't find it in the repository, add it
shades.push_back(shade);
for (auto &callback : shadeAddedCallbacks) {
callback(shade);
}
}
Shade* ShadeRepository::findById(const uint16_t id)
{
for (Shade &shade : shades)
{
if (shade.ID == id)
{
return &shade;
}
}
return nullptr;
}
Shade* ShadeRepository::findByKey(const std::string &key)
{
for (Shade &shade : shades)
{
if (shade.key == key)
{
return &shade;
}
}
return nullptr;
}
std::vector<Shade>::iterator ShadeRepository::begin()
{
return shades.begin();
}
std::vector<Shade>::iterator ShadeRepository::end()
{
return shades.end();
}
void ShadeRepository::addShadeAddedCallback(std::function<void(Shade&)> callback) {
shadeAddedCallbacks.push_back(callback);
}
void ShadeRepository::addShadeChangedCallback(std::function<void(Shade&)> callback) {
shadeChangedCallbacks.push_back(callback);
}

View File

@@ -0,0 +1,25 @@
#ifndef SHADE_REPOSITORY_H
#define SHADE_REPOSITORY_H
#include <vector>
#include <functional>
#include "Shade.h"
class ShadeRepository {
private:
std::vector<Shade> shades;
std::vector<std::function<void(Shade&)>> shadeAddedCallbacks;
std::vector<std::function<void(Shade&)>> shadeChangedCallbacks;
public:
void upsert(Shade shade);
std::vector<Shade>::iterator begin();
std::vector<Shade>::iterator end();
Shade* findById(const uint16_t id);
Shade* findByKey(const std::string& key);
void addShadeAddedCallback(std::function<void(Shade&)> callback);
void addShadeChangedCallback(std::function<void(Shade&)> callback);
};
#endif // SHADE_REPOSITORY_H