refactor: concentrating repo functions into repo class

This commit is contained in:
2025-04-07 00:12:29 +02:00
parent 3231120a1e
commit 407c155c4d
8 changed files with 197 additions and 392 deletions
+7 -37
View File
@@ -16,47 +16,17 @@
#include "SPITType.h"
#include "SPIT_Message.h"
#include <string>
#include <unordered_map>
#include <memory>
#include "SpitRepo.hpp"
using namespace std;
class SPITObjectListener;
class SPITObject {
private:
static unordered_map<string, unique_ptr<SPITObject>> sSpitObjects;
static unordered_map<string, unique_ptr<SPITObject>> sSpitObjectsRemote;
public:
static SPITObject *getSPITObject(const string &id);
static const SPITObject &getSPITObjectRef(const string &id);
static void addSPITObject(unique_ptr<SPITObject> spitObject);
static bool removeSPITObject(const SPITObject &spitObject);
static bool removeSPITObject(const string & spitObjectID);
static int getSPITObjectCount() {
return sSpitObjects.size();
}
static SPITObject *getSPITObject(int p_Index);
static const SPITObject &getSPITObjectRef(int p_Index);
static void clearSPITObjects() { sSpitObjects.clear(); }
static void clearUnconfirmedSPITObjects();
static void setConfirmedFalse();
static SPITObject *getSPITObjectRemote(const string &id);
static const SPITObject &getSPITObjectRemoteRef(const string &id);
static void addSPITObjectRemote(unique_ptr<SPITObject> spitObject);
static bool removeSPITObjectRemote(const SPITObject &spitObject);
static bool removeSPITObjectRemote(const string & spitObjectID);
static int getSPITObjectRemoteCount() {
return sSpitObjectsRemote.size();
}
static SPITObject *getSPITObjectRemote(int p_Index);
static const SPITObject &getSPITObjectRemoteRef(int p_Index);
static void clearSPITObjectsRemote(){ sSpitObjectsRemote.clear(); }
static void eventProjectClosed();
static void clearUnconfirmedSPITObjects(SpitRepo<SPITObject> &repo);
static void setConfirmedFalse(SpitRepo<SPITObject> &repo);
static void eventProjectClosed(SpitRepo<SPITObject> &repo, SpitRepo<SPITObject> &remoteRepo);
SPITObject(string p_ID);
@@ -133,15 +103,15 @@ class SPITObject {
long long getDelay() const { return m_Delay; }
long long getDuration() const { return m_Duration; }
bool getProjectIndepended() const {
bool getProjectIndepended(SpitRepo<SPITType> &repo, SpitRepo<SPITType> &remoteRepo) const {
SPITType *ptrSPITType;
if (m_ServerRemote == false) {
ptrSPITType = SPITType::getSPITType(m_SPITTypeID);
ptrSPITType = repo.getEntry(m_SPITTypeID);
if (ptrSPITType == nullptr)
return true;
return ptrSPITType->getProjectIndepended();
} else {
ptrSPITType = SPITType::getSPITTypeRemote(m_SPITTypeID);
ptrSPITType = remoteRepo.getEntry(m_SPITTypeID);
if (ptrSPITType == nullptr)
return true;
return ptrSPITType->getProjectIndepended();
+3 -27
View File
@@ -16,40 +16,16 @@
#include "SPITSocket.h"
#include "SPIT_Message.h"
#include <string>
#include <unordered_map>
#include <memory>
#include "SpitRepo.hpp"
using namespace std;
class SPITTypeListener;
class SPITType {
private:
static unordered_map<string, unique_ptr<SPITType>> sSpitTypes;
static unordered_map<string, unique_ptr<SPITType>> sSpitTypesRemote;
public:
static SPITType *getSPITType(const string &id);
static const SPITType &getSPITTypeRef(const string &id);
static void addSPITType( unique_ptr<SPITType> spitType);
static bool removeSPITType(const SPITType &spitType);
static int getSPITTypeCount() { return sSpitTypes.size(); }
static SPITType *getSPITType(int p_Index);
static const SPITType &getSPITTypeRef(int p_Index);
static void clearSPITTypes() { sSpitTypes.clear(); }
static void setConfirmedFalse();
static SPITType *getSPITTypeRemote(const string &id);
static const SPITType &getSPITTypeRemoteRef(const string &id);
static void addSPITTypeRemote(unique_ptr<SPITType> spitType);
static bool removeSPITTypeRemote(const SPITType &spitType);
static int getSPITTypeRemoteCount() { return sSpitTypesRemote.size(); }
static SPITType *getSPITTypeRemote(int p_Index);
static const SPITType &getSPITTypeRemoteRef(int p_Index);
static void clearSPITTypesRemote() { sSpitTypesRemote.clear(); }
static void eventProjectClosed();
static void setConfirmedFalse(SpitRepo<SPITType> &repo);
static void eventProjectClosed(SpitRepo<SPITType> &repo, SpitRepo<SPITType> &remoteRepo);
SPITType(string p_ID);
SPITType(string p_ID, string p_GroupName, string p_Name, bool p_Fixed, bool p_ProjectIndepended, long long p_DefaultDelay, long long p_DefaultDuration);
+7
View File
@@ -20,6 +20,7 @@
#include "SPITSocket.h"
#include "SPITType.h"
#include "SPIT_Message.h"
#include "SpitRepo.hpp"
#include <memory>
#include <vector>
@@ -345,6 +346,12 @@ class SPIT_Client : I_SAD_Client {
void answerWatchdog() {
sAnswered = true;
}
SpitRepo<SPITObject> mSpitObjects;
SpitRepo<SPITObject> mRemoteSpitObjects;
SpitRepo<SPITType> mSpitTypes;
SpitRepo<SPITType> mRemoteSpitTypes;
};
#endif /* SPIT_CLIENT_H */
+64
View File
@@ -0,0 +1,64 @@
#pragma once
#include <memory>
#include <string>
#include <unordered_map>
template <typename T>
class SpitRepo {
public:
T *getEntry(const string &id) {
auto i = mEntries.find(id);
if (i == mEntries.end()) {
return nullptr;
}
return i->second.get();
}
const T &getEntryRef(const string &id) {
return *mEntries.at(id);
}
T *getEntry(int index) {
if (index >= 0 && index < mEntries.size()) {
return next(mEntries.begin(), index)->second.get();
}
return nullptr;
}
const T &getEntryRef(int index) {
if (index >= 0 && index < mEntries.size()) {
auto i = next(mEntries.begin(), index);
return *i->second;
} else {
throw "SPITObject index out of range";
}
}
void addEntry(unique_ptr<T> entry) {
mEntries[entry->getID()] = move(entry);
}
bool removeEntry(const T &entry) {
return removeEntry(entry.getID());
}
bool removeEntry(const string &id) {
auto i = mEntries.find(id);
if (i == mEntries.end()) {
return false;
}
i = mEntries.erase(i);
return true;
}
int getEntryCount() {
return mEntries.size();
}
unordered_map<string, unique_ptr<T>> &getRepo() {
return mEntries;
}
void clearEntries() {
mEntries.clear();
}
private:
unordered_map<string, unique_ptr<T>> mEntries;
};
+12 -120
View File
@@ -16,147 +16,39 @@
#include "SPITSocket.h"
#include "esp_log.h"
unordered_map<string, unique_ptr<SPITObject>> SPITObject::sSpitObjects;
unordered_map<string, unique_ptr<SPITObject>> SPITObject::sSpitObjectsRemote;
SPITObject *SPITObject::getSPITObject(const string &id) {
auto i = sSpitObjects.find(id);
if (i == sSpitObjects.end()) {
return nullptr;
}
return i->second.get();
}
/**
* Returns the reference of the SPITObject with ID == id.
* Throws an exception if no SPITObject is not found.
* @param id the ID
* @return reference of the SPITType with ID == id
*/
const SPITObject &SPITObject::getSPITObjectRef(const string &id) {
return *sSpitObjects.at(id);
}
SPITObject *SPITObject::getSPITObjectRemote(const string &id) {
auto i = sSpitObjectsRemote.find(id);
if (i == sSpitObjectsRemote.end()) {
return nullptr;
}
return i->second.get();
}
/**
* Returns the reference of the remote SPITObject with ID == id.
* Throws an exception if no SPITObject is not found.
* @param id the ID
* @return reference of the SPITType with ID == id
*/
const SPITObject &SPITObject::getSPITObjectRemoteRef(const string &id) {
return *sSpitObjectsRemote.at(id);
}
void SPITObject::addSPITObject(unique_ptr<SPITObject> spitObject) {
ESP_LOGD("OBJ", "adding spitObject %s/%s", spitObject->getID().c_str(), spitObject->getName().c_str());
sSpitObjects[spitObject->getID()] = move(spitObject);
}
void SPITObject::addSPITObjectRemote(unique_ptr<SPITObject> spitObject) {
ESP_LOGD("OBJ", "adding spitObject %s/%s", spitObject->getID().c_str(), spitObject->getName().c_str());
sSpitObjectsRemote[spitObject->getID()] = move(spitObject);
}
bool SPITObject::removeSPITObject(const SPITObject &spitObject) {
return removeSPITObject(spitObject.getID());
}
bool SPITObject::removeSPITObject(const string &spitObjectID) {
auto i = sSpitObjects.find(spitObjectID);
if (i == sSpitObjects.end()) {
return false;
}
i = sSpitObjects.erase(i);
return true;
}
bool SPITObject::removeSPITObjectRemote(const SPITObject &spitObject) {
return removeSPITObjectRemote(spitObject.getID());
}
bool SPITObject::removeSPITObjectRemote(const string &spitObjectID) {
auto i = sSpitObjectsRemote.find(spitObjectID);
if (i == sSpitObjectsRemote.end()) {
return false;
}
i = sSpitObjectsRemote.erase(i);
return true;
}
SPITObject *SPITObject::getSPITObject(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitObjects.size()) {
auto i = next(sSpitObjects.begin(), p_Index);
return i->second.get();
}
return nullptr;
}
/**
* Returns a reference to the SPITObject at position p_Index.
* Throws an exception if p_Index is out of range.
* @param p_Index position
* @return the reference of the SPITObject at position.
*/
const SPITObject &SPITObject::getSPITObjectRef(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitObjects.size()) {
auto i = next(sSpitObjects.begin(), p_Index);
return *i->second;
} else {
throw "SPITObject index out of range";
}
}
SPITObject *SPITObject::getSPITObjectRemote(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitObjectsRemote.size()) {
auto i = next(sSpitObjectsRemote.begin(), p_Index);
return i->second.get();
} else {
return nullptr;
}
}
/**
* Returns a reference to the remote SPITObject at position p_Index.
* Throws an exception if p_Index is out of range.
* @param p_Index position
* @return the reference of the SPITObject at position.
*/
const SPITObject &SPITObject::getSPITObjectRemoteRef(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitObjectsRemote.size()) {
auto i = next(sSpitObjectsRemote.begin(), p_Index);
return *i->second;
} else {
throw "Remote SPITObject index out of range";
}
}
/**
* clears all unconfirmed SPITObjects not created by this SPIT_Client.
*/
void SPITObject::clearUnconfirmedSPITObjects() {
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
void SPITObject::clearUnconfirmedSPITObjects(SpitRepo<SPITObject>& repo) {
auto& entries = repo.getRepo();
for (auto i = entries.begin(); i != entries.end(); i++) {
SPITObject *ptrSPITObject = i->second.get();
if (ptrSPITObject->getCreatedByClient() == false && ptrSPITObject->getConfirmed() == false) {
i = sSpitObjects.erase(i);
i = entries.erase(i);
}
}
}
void SPITObject::eventProjectClosed() {
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
void SPITObject::eventProjectClosed(SpitRepo<SPITObject>& repo, SpitRepo<SPITObject>& remoteRepo) {
auto& entries = repo.getRepo();
for (auto i = entries.begin(); i != entries.end(); i++) {
SPITObject *ptrSPITObject = i->second.get();
if (ptrSPITObject->getCreatedByClient() == true) {
continue;
}
i = sSpitObjects.erase(i);
i = entries.erase(i);
}
// Remote
sSpitObjectsRemote.clear();
remoteRepo.clearEntries();
}
/**
* sets confirmed = false for all SPITObjects.
* This should be called when the client has loaded a project.
*/
void SPITObject::setConfirmedFalse() {
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
void SPITObject::setConfirmedFalse(SpitRepo<SPITObject>& repo) {
auto& entries = repo.getRepo();
for (auto i = entries.begin(); i != entries.end(); i++) {
i->second->setConfirmed(false);
}
}
+8 -112
View File
@@ -17,130 +17,26 @@
static const char *TAG = "SPIT";
unordered_map<string, unique_ptr<SPITType>> SPITType::sSpitTypes;
unordered_map<string, unique_ptr<SPITType>> SPITType::sSpitTypesRemote;
SPITType *SPITType::getSPITType(const string &id) {
auto i = sSpitTypes.find(id);
if (i == sSpitTypes.end()) {
return nullptr;
}
return i->second.get();
}
/**
* Returns the reference of the SPITType with ID == id.
* Throws an exception if no SPITType is not found.
* @param id the ID
* @return reference of the SPITType with ID == id
*/
const SPITType &SPITType::getSPITTypeRef(const string &id) {
return *sSpitTypes.at(id);
}
SPITType *SPITType::getSPITTypeRemote(const string &id) {
auto i = sSpitTypesRemote.find(id);
if (i == sSpitTypesRemote.end()) {
return nullptr;
}
return i->second.get();
}
/**
* Returns the reference of the remote SPITType with ID == id.
* Throws an exception if no SPITType is not found.
* @param id the ID
* @return reference of the SPITType with ID == id
*/
const SPITType &SPITType::getSPITTypeRemoteRef(const string &id) {
return *sSpitTypesRemote.at(id);
}
void SPITType::addSPITType(unique_ptr<SPITType> spitType) {
ESP_LOGD("OBJ", "adding spitObject %s/%s", spitType->getID().c_str(), spitType->getName().c_str());
sSpitTypes[spitType->getID()] = move(spitType);
}
void SPITType::addSPITTypeRemote(unique_ptr<SPITType> spitType) {
ESP_LOGD("OBJ", "adding spitObject %s/%s", spitType->getID().c_str(), spitType->getName().c_str());
sSpitTypesRemote[spitType->getID()] = move(spitType);
}
bool SPITType::removeSPITType(const SPITType &spitType) {
auto i = sSpitTypes.find(spitType.getID());
if (i == sSpitTypes.end()) {
return false;
}
i = sSpitTypes.erase(i);
return true;
}
bool SPITType::removeSPITTypeRemote(const SPITType &spitType) {
auto i = sSpitTypesRemote.find(spitType.getID());
if (i == sSpitTypesRemote.end()) {
return false;
}
i = sSpitTypesRemote.erase(i);
return true;
}
SPITType *SPITType::getSPITType(int p_Index) {
if (p_Index < sSpitTypes.size()) {
auto i = next(sSpitTypes.begin(), p_Index);
return i->second.get();
}
return nullptr;
}
/**
* Returns a reference to the SPITType at position p_Index.
* Throws an exception if p_Index is out of range.
* @param p_Index position
* @return the reference of the SPITType at position.
*/
const SPITType &SPITType::getSPITTypeRef(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitTypes.size()) {
auto i = next(sSpitTypes.begin(), p_Index);
return *i->second;
} else {
throw "SPITType index out of range";
}
}
SPITType *SPITType::getSPITTypeRemote(int p_Index) {
if (p_Index < sSpitTypesRemote.size()) {
auto i = next(sSpitTypesRemote.begin(), p_Index);
return i->second.get();
}
return nullptr;
}
/**
* Returns a reference to the remote SPITType at position p_Index.
* Throws an exception if p_Index is out of range.
* @param p_Index position
* @return the reference of the SPITType at position.
*/
const SPITType &SPITType::getSPITTypeRemoteRef(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitTypesRemote.size()) {
auto i = next(sSpitTypesRemote.begin(), p_Index);
return *i->second;
} else {
throw "Remote SPITType index out of range";
}
}
void SPITType::eventProjectClosed() {
for (auto i = sSpitTypes.begin(); i != sSpitTypes.end(); i++) {
void SPITType::eventProjectClosed(SpitRepo<SPITType> &repo, SpitRepo<SPITType> &remoteRepo) {
auto &entries = repo.getRepo();
for (auto i = entries.begin(); i != entries.end(); i++) {
SPITType *ptrSPITType = i->second.get();
if (ptrSPITType->getCreatedByClient() == true) {
continue;
}
i = sSpitTypes.erase(i);
i = entries.erase(i);
}
// Remote
sSpitTypesRemote.clear();
remoteRepo.clearEntries();
}
/**
* Sets confirmed = false for all SPITTypes.
* This should be called when the client has loaded a project.
*/
void SPITType::setConfirmedFalse() {
for (auto i = sSpitTypes.begin(); i != sSpitTypes.end(); i++) {
void SPITType::setConfirmedFalse(SpitRepo<SPITType> &repo) {
auto &entries = repo.getRepo();
for (auto i = entries.begin(); i != entries.end(); i++) {
i->second->setConfirmed(false);
}
}
+57 -57
View File
@@ -52,8 +52,8 @@ void SPIT_Client::clientProjectClose() {
void SPIT_Client::clientProjectLoaded() {
// set confirmed = false for all SPITTypes and SPITObjects
SPITType::setConfirmedFalse();
SPITObject::setConfirmedFalse();
SPITType::setConfirmedFalse(mSpitTypes);
SPITObject::setConfirmedFalse(mSpitObjects);
if (m_ConnectionStatus == CONNECTION_OK) {
// send ReportSPITProject to the server
@@ -80,14 +80,14 @@ void SPIT_Client::clientProjectLoaded() {
* @param p_ActionAssigned
*/
void SPIT_Client::setActionSPITType(string p_SPITTypeID, bool p_ActionAssigned) {
SPITType *ptrSPITType = SPITType::getSPITType(p_SPITTypeID);
SPITType *ptrSPITType = mSpitTypes.getEntry(p_SPITTypeID);
if (ptrSPITType == nullptr)
return;
if (ptrSPITType->setActionAssigned(p_ActionAssigned) == true) {
sendSPITTypeInternal(*ptrSPITType);
for (int i = 0; i < SPITObject::getSPITObjectCount(); i++) {
SPITObject *ptrSPITObject = SPITObject::getSPITObject(i);
for (int i = 0; i < mSpitObjects.getEntryCount(); i++) {
SPITObject *ptrSPITObject = mSpitObjects.getEntry(i);
if (ptrSPITObject == nullptr)
continue;
if (ptrSPITObject->getSPITTypeID() == ptrSPITType->getID()) {
@@ -107,7 +107,7 @@ void SPIT_Client::setActionSPITType(string p_SPITTypeID, bool p_ActionAssigned)
void SPIT_Client::setActionSPITObject(string p_SPITObjectID, bool p_ActionAssigned) {
SPITObject *ptrSPITObject;
{
ptrSPITObject = SPITObject::getSPITObject(p_SPITObjectID);
ptrSPITObject = mSpitObjects.getEntry(p_SPITObjectID);
if (ptrSPITObject == nullptr)
return;
if (ptrSPITObject->setActionAssigned(p_ActionAssigned) == true) {
@@ -686,8 +686,8 @@ void SPIT_Client::parseReportSPITProject(SPIT_Message::ReportSPITProject *p_Repo
mCallbacks.onProjectUpdate(ProjUpdate::ServerLoaded, m_ServerProjectID);
// Set all SPITTypes and SPITObjects confirmed = false
SPITType::setConfirmedFalse();
SPITObject::setConfirmedFalse();
SPITType::setConfirmedFalse(mSpitTypes);
SPITObject::setConfirmedFalse(mSpitObjects);
// Send RequestProjectTransfer
auto sMRequestProjectTransfer = make_unique<SPIT_Message::RequestProjectTransfer>();
@@ -727,18 +727,18 @@ void SPIT_Client::parseRequestProjectTransfer(SPIT_Message::RequestProjectTransf
}
// send all listes SPITTypes
int cntT = SPITType::getSPITTypeCount();
int cntT = mSpitTypes.getEntryCount();
for (int i = 0; i < cntT; i++) {
SPITType *ptrSPITType = SPITType::getSPITType(i);
SPITType *ptrSPITType = mSpitTypes.getEntry(i);
if (ptrSPITType == nullptr)
break;
sendSPITTypeInternal(*ptrSPITType);
fos::Task::delayMs(10);
}
int cntO = SPITObject::getSPITObjectCount();
int cntO = mSpitObjects.getEntryCount();
for (int i = 0; i < cntO; i++) {
SPITObject *ptrSPITObject = SPITObject::getSPITObject(i);
SPITObject *ptrSPITObject = mSpitObjects.getEntry(i);
if (ptrSPITObject == nullptr)
break;
sendSPITObjectInternal(ptrSPITObject);
@@ -782,7 +782,7 @@ void SPIT_Client::parseRequestProjectTransfer(SPIT_Message::RequestProjectTransf
// unique_lock<recursive_mutex> o_LockType(*SPITType::getMutex());
// intCount = SPITType::getSPITTypeCount();
// for (int i = 0; i < intCount; i++) {
// ptrSPITType = SPITType::getSPITType(i);
// ptrSPITType = mSpitTypes.getEntry(i);
// if (ptrSPITType == nullptr) continue;
// intResult = sendSPITTypeInternal(ptrSPITType);
// if (intResult < 0) return;
@@ -793,9 +793,9 @@ void SPIT_Client::parseRequestProjectTransfer(SPIT_Message::RequestProjectTransf
// {
// unique_lock<recursive_mutex> o_LockObject(*SPITObject::getMutex());
// intCount = SPITObject::getSPITObjectCount();
// intCount = mSpitObjects.getEntryCount();
// for (int i = 0; i < intCount; i++) {
// ptrSPITObject = SPITObject::getSPITObject(i);
// ptrSPITObject = mSpitObjects.getEntry(i);
// if (ptrSPITObject == nullptr) continue;
// intResult = sendSPITObjectInternal(ptrSPITObject);
// if (intResult < 0) return;
@@ -842,7 +842,7 @@ void SPIT_Client::sendProject(SPIT_Client *p_PtrSPIT_Client) {
// // unique_lock<recursive_mutex> o_LockType(*SPITType::getMutex());
// intCount = SPITType::getSPITTypeCount();
// for (int i = 0; i < intCount; i++) {
// ptrSPITType = SPITType::getSPITType(i);
// ptrSPITType = mSpitTypes.getEntry(i);
// if (ptrSPITType == nullptr) continue;
// if (ptrSPITType->getCreatedByClient() == false) continue;
// intResult = p_PtrSPIT_Client->sendSPITTypeInternal(ptrSPITType);
@@ -854,9 +854,9 @@ void SPIT_Client::sendProject(SPIT_Client *p_PtrSPIT_Client) {
// // {
// // unique_lock<recursive_mutex> o_LockObject(*SPITObject::getMutex());
// intCount = SPITObject::getSPITObjectCount();
// intCount = mSpitObjects.getEntryCount();
// for (int i = 0; i < intCount; i++) {
// ptrSPITObject = SPITObject::getSPITObject(i);
// ptrSPITObject = mSpitObjects.getEntry(i);
// if (ptrSPITObject == nullptr) continue;
// if (ptrSPITObject->getCreatedByClient() == false) continue;
// intResult = p_PtrSPIT_Client->sendSPITObjectInternal(ptrSPITObject);
@@ -883,7 +883,7 @@ void SPIT_Client::parseStartProjectTransfer(SPIT_Message::StartProjectTransfer *
mCallbacks.onProjectUpdate(ProjUpdate::StartTransfer, m_ServerProjectID);
}
void SPIT_Client::parseEndProjectTransfer(SPIT_Message::EndProjectTransfer *p_EndProjectTransfer) {
SPITObject::clearUnconfirmedSPITObjects();
SPITObject::clearUnconfirmedSPITObjects(mSpitObjects);
mCallbacks.onProjectUpdate(ProjUpdate::EndTransfer, m_ServerProjectID);
// Send SPIT_Message::ReportSPITProject after the m_SPITClientInterface is notfied of EndProjectTransfer
@@ -904,7 +904,7 @@ void SPIT_Client::parseReportSPITType(SPIT_Message::ReportSPITType *p_ReportSPIT
bool boolChanged = false;
{
if (p_ReportSPITType->getServerRemote() == false) {
ptrSPITType = SPITType::getSPITType(p_ReportSPITType->getID());
ptrSPITType = mSpitTypes.getEntry(p_ReportSPITType->getID());
if (ptrSPITType == nullptr) {
// The user's app my veto the SPITType
if (mCallbacks.onCheckSpitTypeVeto(p_ReportSPITType) == false) {
@@ -912,15 +912,15 @@ void SPIT_Client::parseReportSPITType(SPIT_Message::ReportSPITType *p_ReportSPIT
}
auto spitType = make_unique<SPITType>(p_ReportSPITType->getID());
ptrSPITType = spitType.get();
SPITType::addSPITType(move(spitType));
mSpitTypes.addEntry(move(spitType));
boolNew = true;
}
} else {
ptrSPITType = SPITType::getSPITTypeRemote(p_ReportSPITType->getID());
ptrSPITType = mRemoteSpitTypes.getEntry(p_ReportSPITType->getID());
if (ptrSPITType == nullptr) {
auto spitType = make_unique<SPITType>(p_ReportSPITType->getID());
ptrSPITType = spitType.get();
SPITType::addSPITTypeRemote(move(spitType));
mRemoteSpitTypes.addEntry(move(spitType));
boolNew = true;
}
}
@@ -949,11 +949,11 @@ void SPIT_Client::parseRemoveSPITType(SPIT_Message::RemoveSPITType *p_RemoveSPIT
bool boolRemote = false;
string stringSPITTypeID = p_RemoveSPITType->getID();
SPITType *ptrSPITType = SPITType::getSPITTypeRemote(stringSPITTypeID);
SPITType *ptrSPITType = mRemoteSpitTypes.getEntry(stringSPITTypeID);
if (!ptrSPITType) {
boolRemote = true;
} else {
ptrSPITType = SPITType::getSPITType(stringSPITTypeID);
ptrSPITType = mSpitTypes.getEntry(stringSPITTypeID);
if (!ptrSPITType) {
boolRemote = false;
} else {
@@ -971,9 +971,9 @@ void SPIT_Client::parseRemoveSPITType(SPIT_Message::RemoveSPITType *p_RemoveSPIT
mCallbacks.onSpitType(ObjectEvent::Deleted, boolRemote, *ptrSPITType);
if (boolRemote == false) {
SPITType::removeSPITType(*ptrSPITType);
mSpitTypes.removeEntry(*ptrSPITType);
} else {
SPITType::removeSPITTypeRemote(*ptrSPITType);
mRemoteSpitTypes.removeEntry(*ptrSPITType);
}
}
@@ -997,7 +997,7 @@ int SPIT_Client::sendSPITTypeInternal(SPITType &p_SPITType) {
bool SPIT_Client::changeSPITType(SPITType *p_SPITType) {
if (p_SPITType == nullptr)
return false;
SPITType *ptrSPITType = SPITType::getSPITType(p_SPITType->getID());
SPITType *ptrSPITType = mSpitTypes.getEntry(p_SPITType->getID());
if (ptrSPITType == nullptr)
return false;
@@ -1027,13 +1027,13 @@ int SPIT_Client::removeSPITType(string p_SPITTypeID) {
return -1;
SPITType *ptrSPITType;
{
ptrSPITType = SPITType::getSPITType(p_SPITTypeID);
ptrSPITType = mSpitTypes.getEntry(p_SPITTypeID);
if (ptrSPITType == nullptr)
return -1;
auto sMRemoveSPITType = make_unique<SPIT_Message::RemoveSPITType>();
ptrSPITType->fillMessageOP(*sMRemoveSPITType);
mCallbacks.onSpitType(ObjectEvent::Deleted, false, *ptrSPITType);
SPITType::removeSPITType(*ptrSPITType);
mSpitTypes.removeEntry(*ptrSPITType);
return sendSPITMessage(move(sMRemoveSPITType));
}
return -1;
@@ -1045,11 +1045,11 @@ SPITType *SPIT_Client::createSPITTypeFixed(string p_ID, string p_GroupName, stri
SPITType *SPIT_Client::createSPITType(string p_ID, string p_GroupName, string p_Name, long long p_DefaultDelay, long long p_DefaultDuration, bool p_FIxed, bool p_ProjectIndepended) {
bool boolNew;
SPITType *ptrSPITType = SPITType::getSPITType(p_ID);
SPITType *ptrSPITType = mSpitTypes.getEntry(p_ID);
if (ptrSPITType == nullptr) {
auto spitType = make_unique<SPITType>(p_ID, p_GroupName, p_Name, p_FIxed, p_ProjectIndepended, p_DefaultDelay, p_DefaultDuration);
ptrSPITType = spitType.get();
SPITType::addSPITType(move(spitType));
mSpitTypes.addEntry(move(spitType));
ptrSPITType->setCreatedByClient(true);
boolNew = true;
} else {
@@ -1083,15 +1083,15 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
ESP_LOGD(TAG, "parseReportSPITObject %s / %s", p_ReportSPITObject->getID().c_str(), p_ReportSPITObject->getName().c_str());
if (p_ReportSPITObject->getServerRemote() == false) {
boolRemote = false;
ptrSPITObject = SPITObject::getSPITObject(p_ReportSPITObject->getID());
ptrSPITType = SPITType::getSPITType(p_ReportSPITObject->getSPITTypeID());
ptrSPITObject = mSpitObjects.getEntry(p_ReportSPITObject->getID());
ptrSPITType = mSpitTypes.getEntry(p_ReportSPITObject->getSPITTypeID());
if (ptrSPITType == nullptr) {
// No associated SPITType found
if (ptrSPITObject != nullptr) {
// SpitObject exists but has no corresponding SPITType -> remove the SPITObject without sending a message
// Maybe the SPITObject has changed the SPITTypeID to a SPITTypeID of another board
SPITObject::removeSPITObject(*ptrSPITObject);
mSpitObjects.removeEntry(*ptrSPITObject);
}
return;
}
@@ -1103,21 +1103,21 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
}
auto spitObj = make_unique<SPITObject>(p_ReportSPITObject->getID());
ptrSPITObject = spitObj.get();
SPITObject::addSPITObject(move(spitObj));
mSpitObjects.addEntry(move(spitObj));
boolNew = true;
} else {
// The user's app my veto the SPITType
if (mCallbacks.onCheckSpitObjectVeto(p_ReportSPITObject) == false) {
// SpitObject exists and User APp denied the SPITObject -> remove the SPITObject without sending a message
SPITObject::removeSPITObject(*ptrSPITObject);
mSpitObjects.removeEntry(*ptrSPITObject);
ESP_LOGD(TAG, "TypeVeto %s / %s", p_ReportSPITObject->getID().c_str(), p_ReportSPITObject->getName().c_str());
return;
}
}
} else {
boolRemote = true;
ptrSPITObject = SPITObject::getSPITObjectRemote(p_ReportSPITObject->getID());
ptrSPITType = SPITType::getSPITTypeRemote(p_ReportSPITObject->getSPITTypeID());
ptrSPITObject = mRemoteSpitObjects.getEntry(p_ReportSPITObject->getID());
ptrSPITType = mRemoteSpitTypes.getEntry(p_ReportSPITObject->getSPITTypeID());
if (ptrSPITType == nullptr) {
// No associated SPITType found
return;
@@ -1125,7 +1125,7 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
if (ptrSPITObject == nullptr) {
auto spitObj = make_unique<SPITObject>(p_ReportSPITObject->getID());
ptrSPITObject = spitObj.get();
SPITObject::addSPITObjectRemote(move(spitObj));
mRemoteSpitObjects.addEntry(move(spitObj));
boolNew = true;
}
}
@@ -1157,13 +1157,13 @@ void SPIT_Client::parseRemoveSPITObject(SPIT_Message::RemoveSPITObject *p_Remove
return;
}
SPITObject *ptrSPITObject = SPITObject::getSPITObjectRemote(p_RemoveSPITObject->getID());
SPITObject *ptrSPITObject = mRemoteSpitObjects.getEntry(p_RemoveSPITObject->getID());
if (ptrSPITObject != nullptr) {
SPITType *ptrSPITType = SPITType::getSPITTypeRemote(ptrSPITObject->getSPITTypeID());
SPITType *ptrSPITType = mRemoteSpitTypes.getEntry(ptrSPITObject->getSPITTypeID());
if (ptrSPITType != nullptr) {
mCallbacks.onSpitObject(ObjectEvent::Deleted, true, *ptrSPITType, *ptrSPITObject);
}
SPITObject::removeSPITObjectRemote(*ptrSPITObject);
mRemoteSpitObjects.removeEntry(*ptrSPITObject);
} else {
// call removeSPITObject, this will notify the server that the object is removed
removeSPITObject(p_RemoveSPITObject->getID());
@@ -1192,10 +1192,10 @@ bool SPIT_Client::changeSPITObject(SPITObject *p_SPITObject) {
SPITObject *ptrSPITObject;
SPITType *ptrSPITType;
{
ptrSPITObject = SPITObject::getSPITObject(p_SPITObject->getID());
ptrSPITObject = mSpitObjects.getEntry(p_SPITObject->getID());
if (ptrSPITObject == nullptr)
return false;
ptrSPITType = SPITType::getSPITType(p_SPITObject->getID());
ptrSPITType = mSpitTypes.getEntry(p_SPITObject->getID());
if (ptrSPITObject == nullptr)
return false;
@@ -1239,15 +1239,15 @@ int SPIT_Client::removeSPITObject(string p_SPITObjectID) {
{
auto sMRemoveSPITObject = make_unique<SPIT_Message::RemoveSPITObject>();
ptrSPITObject = SPITObject::getSPITObject(p_SPITObjectID);
ptrSPITObject = mSpitObjects.getEntry(p_SPITObjectID);
if (ptrSPITObject == nullptr)
return -1;
ptrSPITType = SPITType::getSPITType(ptrSPITObject->getSPITTypeID());
ptrSPITType = mSpitTypes.getEntry(ptrSPITObject->getSPITTypeID());
if (ptrSPITType == nullptr)
return -1;
ptrSPITObject->fillMessageOP(*sMRemoveSPITObject);
mCallbacks.onSpitObject(ObjectEvent::Deleted, false, *ptrSPITType, *ptrSPITObject);
SPITObject::removeSPITObject(*ptrSPITObject);
mSpitObjects.removeEntry(*ptrSPITObject);
return sendSPITMessage(move(sMRemoveSPITObject));
}
return -1;
@@ -1269,7 +1269,7 @@ void SPIT_Client::parsePlaySPITStart(SPIT_Message::PlaySPITStart *p_PlaySPITStar
void SPIT_Client::parsePlaySPITObject(SPIT_Message::PlaySPITObject *p_PlaySPITObject) {
SPITPlayer &ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITObject->getPlayerID());
SPITObject *ptrSPITObject = SPITObject::getSPITObject(p_PlaySPITObject->getSPITObjectID());
SPITObject *ptrSPITObject = mSpitObjects.getEntry(p_PlaySPITObject->getSPITObjectID());
if (ptrSPITObject == nullptr) {
return;
@@ -1300,11 +1300,11 @@ void SPIT_Client::parsePlaySPITEnd(SPIT_Message::PlaySPITEnd *p_PlaySPITEnd) {
// boolStopped = true;
// }
// {
// ptrSPITObject = SPITObject::getSPITObject(ptrPlayObject->getSPITObjectID());
// ptrSPITObject = mSpitObjects.getEntry(ptrPlayObject->getSPITObjectID());
// if (ptrSPITObject == nullptr) {
// return;
// }
// SPITType *ptrSPITType = SPITType::getSPITType(ptrSPITObject->getSPITTypeID());
// SPITType *ptrSPITType = mSpitTypes.getEntry(ptrSPITObject->getSPITTypeID());
// if (ptrSPITType == nullptr) {
// return;
// }
@@ -1321,7 +1321,7 @@ void SPIT_Client::parsePlaySPITEnd(SPIT_Message::PlaySPITEnd *p_PlaySPITEnd) {
SPITPlayer::PlaySPITType *ptrPlaySPITType = i->second;
bool boolStarted = ptrPlaySPITType->getIsNew();
bool boolStopped = (ptrPlaySPITType->getIsPlayed() == false);
SPITType *ptrSPITType = SPITType::getSPITType(ptrPlaySPITType->getSPITTypeID());
SPITType *ptrSPITType = mSpitTypes.getEntry(ptrPlaySPITType->getSPITTypeID());
if (ptrSPITType == nullptr) {
// set isplayed to false, the ptrPlayObject will be removed by ptrSPITPlayer.endPlaySequence()
ptrPlaySPITType->setIsPlayed(false);
@@ -1335,17 +1335,17 @@ void SPIT_Client::parsePlaySPITEnd(SPIT_Message::PlaySPITEnd *p_PlaySPITEnd) {
void SPIT_Client::parseReportPlayStatus(SPIT_Message::ReportPlayStatus *p_ReportPlayStatus) {
SPITType *ptrSPITType = NULL;
SPITObject *ptrSPITObject = SPITObject::getSPITObjectRemote(p_ReportPlayStatus->getID());
SPITObject *ptrSPITObject = mRemoteSpitObjects.getEntry(p_ReportPlayStatus->getID());
if (ptrSPITObject == nullptr) {
ptrSPITObject = SPITObject::getSPITObject(p_ReportPlayStatus->getID());
ptrSPITObject = mSpitObjects.getEntry(p_ReportPlayStatus->getID());
if (ptrSPITObject == nullptr) {
return;
}
}
if (ptrSPITObject->getServerRemote() == false) {
ptrSPITType = SPITType::getSPITType(ptrSPITObject->getSPITTypeID());
ptrSPITType = mSpitTypes.getEntry(ptrSPITObject->getSPITTypeID());
} else {
ptrSPITType = SPITType::getSPITTypeRemote(ptrSPITObject->getSPITTypeID());
ptrSPITType = mRemoteSpitTypes.getEntry(ptrSPITObject->getSPITTypeID());
}
if (ptrSPITType == nullptr) {
@@ -1373,7 +1373,7 @@ void SPIT_Client::parseReportPlayStatus(SPIT_Message::ReportPlayStatus *p_Report
void SPIT_Client::sendRemoteObject(string p_SPITObjectID, int p_Valueppm, char p_ValueFlag) {
SPITObject *ptrRemoteSPITObject;
{
ptrRemoteSPITObject = SPITObject::getSPITObjectRemote(p_SPITObjectID);
ptrRemoteSPITObject = mRemoteSpitObjects.getEntry(p_SPITObjectID);
if (ptrRemoteSPITObject == nullptr)
return;