Extract Shade struct and create ShadeRepository
This commit is contained in:
18
lib/shade/Shade.h
Normal file
18
lib/shade/Shade.h
Normal 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
|
||||
66
lib/shade_repository/ShadeRepository.cpp
Normal file
66
lib/shade_repository/ShadeRepository.cpp
Normal 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);
|
||||
}
|
||||
25
lib/shade_repository/ShadeRepository.h
Normal file
25
lib/shade_repository/ShadeRepository.h
Normal 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
|
||||
Reference in New Issue
Block a user