Further refactoring for better safety and maintainability

This commit is contained in:
2025-04-06 22:46:06 +02:00
parent 47694df8a3
commit 3231120a1e
11 changed files with 291 additions and 406 deletions
+11
View File
@@ -0,0 +1,11 @@
# SPIT
Original implementation from Hag seem to be not suited for Embedded devices.
- Supports IPv4 only
- Reduced Threads to a minimum
- Then reduced usage of Mutexes to a minimum
- Reduced Sockets to a minimum
- Introduced containers and removed plain new and delete
- Removed UUID-Generation because SPIT-Types never change on embedded devices
- Callbacks instead of interfaces where suitable
Binary file not shown.
+8 -7
View File
@@ -18,6 +18,7 @@
#include "SPIT_Message.h" #include "SPIT_Message.h"
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <memory>
using namespace std; using namespace std;
@@ -25,13 +26,13 @@ class SPITObjectListener;
class SPITObject { class SPITObject {
private: private:
static unordered_map<string, SPITObject *> sSpitObjects; static unordered_map<string, unique_ptr<SPITObject>> sSpitObjects;
static unordered_map<string, SPITObject *> sSpitObjectsRemote; static unordered_map<string, unique_ptr<SPITObject>> sSpitObjectsRemote;
public: public:
static SPITObject *getSPITObject(const string &id); static SPITObject *getSPITObject(const string &id);
static const SPITObject &getSPITObjectRef(const string &id); static const SPITObject &getSPITObjectRef(const string &id);
static void addSPITObject(SPITObject &spitObject); static void addSPITObject(unique_ptr<SPITObject> spitObject);
static bool removeSPITObject(const SPITObject &spitObject); static bool removeSPITObject(const SPITObject &spitObject);
static bool removeSPITObject(const string & spitObjectID); static bool removeSPITObject(const string & spitObjectID);
static int getSPITObjectCount() { static int getSPITObjectCount() {
@@ -39,13 +40,13 @@ class SPITObject {
} }
static SPITObject *getSPITObject(int p_Index); static SPITObject *getSPITObject(int p_Index);
static const SPITObject &getSPITObjectRef(int p_Index); static const SPITObject &getSPITObjectRef(int p_Index);
static void clearSPITObjects(); static void clearSPITObjects() { sSpitObjects.clear(); }
static void clearUnconfirmedSPITObjects(); static void clearUnconfirmedSPITObjects();
static void setConfirmedFalse(); static void setConfirmedFalse();
static SPITObject *getSPITObjectRemote(const string &id); static SPITObject *getSPITObjectRemote(const string &id);
static const SPITObject &getSPITObjectRemoteRef(const string &id); static const SPITObject &getSPITObjectRemoteRef(const string &id);
static void addSPITObjectRemote(SPITObject &spitObject); static void addSPITObjectRemote(unique_ptr<SPITObject> spitObject);
static bool removeSPITObjectRemote(const SPITObject &spitObject); static bool removeSPITObjectRemote(const SPITObject &spitObject);
static bool removeSPITObjectRemote(const string & spitObjectID); static bool removeSPITObjectRemote(const string & spitObjectID);
static int getSPITObjectRemoteCount() { static int getSPITObjectRemoteCount() {
@@ -53,11 +54,11 @@ class SPITObject {
} }
static SPITObject *getSPITObjectRemote(int p_Index); static SPITObject *getSPITObjectRemote(int p_Index);
static const SPITObject &getSPITObjectRemoteRef(int p_Index); static const SPITObject &getSPITObjectRemoteRef(int p_Index);
static void clearSPITObjectsRemote(); static void clearSPITObjectsRemote(){ sSpitObjectsRemote.clear(); }
static void eventProjectClosed(); static void eventProjectClosed();
SPITObject(); SPITObject(string p_ID);
private: private:
string m_ID; string m_ID;
+1 -1
View File
@@ -58,7 +58,7 @@ class SPITPlayer {
public: public:
// list the SPITPlayers // list the SPITPlayers
static unordered_map<string, SPITPlayer *> s_SPITPlayers; // all actual played objects static unordered_map<string, SPITPlayer *> s_SPITPlayers; // all actual played objects
static SPITPlayer *getSPITPlayer(string p_PlayerID); // if a player does not exist creates a new one static SPITPlayer &getSPITPlayer(string p_PlayerID); // if a player does not exist creates a new one
static void clearPlayers(); static void clearPlayers();
SPITPlayer(string p_PlayerID); SPITPlayer(string p_PlayerID);
+8 -8
View File
@@ -18,6 +18,7 @@
#include "SPIT_Message.h" #include "SPIT_Message.h"
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <memory>
using namespace std; using namespace std;
@@ -25,32 +26,32 @@ class SPITTypeListener;
class SPITType { class SPITType {
private: private:
static unordered_map<string, SPITType *> sSpitTypes; static unordered_map<string, unique_ptr<SPITType>> sSpitTypes;
static unordered_map<string, SPITType *> sSpitTypesRemote; static unordered_map<string, unique_ptr<SPITType>> sSpitTypesRemote;
public: public:
static SPITType *getSPITType(const string &id); static SPITType *getSPITType(const string &id);
static const SPITType &getSPITTypeRef(const string &id); static const SPITType &getSPITTypeRef(const string &id);
static void addSPITType(SPITType &spitType); static void addSPITType( unique_ptr<SPITType> spitType);
static bool removeSPITType(const SPITType &spitType); static bool removeSPITType(const SPITType &spitType);
static int getSPITTypeCount() { return sSpitTypes.size(); } static int getSPITTypeCount() { return sSpitTypes.size(); }
static SPITType *getSPITType(int p_Index); static SPITType *getSPITType(int p_Index);
static const SPITType &getSPITTypeRef(int p_Index); static const SPITType &getSPITTypeRef(int p_Index);
static void clearSPITTypes(); static void clearSPITTypes() { sSpitTypes.clear(); }
static void setConfirmedFalse(); static void setConfirmedFalse();
static SPITType *getSPITTypeRemote(const string &id); static SPITType *getSPITTypeRemote(const string &id);
static const SPITType &getSPITTypeRemoteRef(const string &id); static const SPITType &getSPITTypeRemoteRef(const string &id);
static void addSPITTypeRemote(SPITType &spitType); static void addSPITTypeRemote(unique_ptr<SPITType> spitType);
static bool removeSPITTypeRemote(const SPITType &spitType); static bool removeSPITTypeRemote(const SPITType &spitType);
static int getSPITTypeRemoteCount() { return sSpitTypesRemote.size(); } static int getSPITTypeRemoteCount() { return sSpitTypesRemote.size(); }
static SPITType *getSPITTypeRemote(int p_Index); static SPITType *getSPITTypeRemote(int p_Index);
static const SPITType &getSPITTypeRemoteRef(int p_Index); static const SPITType &getSPITTypeRemoteRef(int p_Index);
static void clearSPITTypesRemote(); static void clearSPITTypesRemote() { sSpitTypesRemote.clear(); }
static void eventProjectClosed(); static void eventProjectClosed();
SPITType(); 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); SPITType(string p_ID, string p_GroupName, string p_Name, bool p_Fixed, bool p_ProjectIndepended, long long p_DefaultDelay, long long p_DefaultDuration);
private: private:
@@ -80,7 +81,6 @@ class SPITType {
* true = the action is triggered on the server. The server waits for the client to play objects of this SPITType. * true = the action is triggered on the server. The server waits for the client to play objects of this SPITType.
*/ */
bool getServerRemote() const { return m_ServerRemote; } bool getServerRemote() const { return m_ServerRemote; }
bool getProjectIndepended() const { return m_ServerProjectIndepended; } bool getProjectIndepended() const { return m_ServerProjectIndepended; }
/** /**
* This is used by the server and client to tell if the other side has registered the changes. * This is used by the server and client to tell if the other side has registered the changes.
+5 -8
View File
@@ -52,11 +52,8 @@ class SPIT_Client : I_SAD_Client {
void (*onServerEvent)(ServerEvent event, const SAD_DetectedServer &p_DetectedServer){}; void (*onServerEvent)(ServerEvent event, const SAD_DetectedServer &p_DetectedServer){};
void (*onConnectionStatus)(string p_ServerID, string p_ServerComputerName, string p_ServerName, int p_ServerStepTime, int p_ConnectionStatus, int p_OldConnectionStatus){}; void (*onConnectionStatus)(string p_ServerID, string p_ServerComputerName, string p_ServerName, int p_ServerStepTime, int p_ConnectionStatus, int p_OldConnectionStatus){};
void (*onProjectUpdate)(ProjUpdate state, string projectId){}; void (*onProjectUpdate)(ProjUpdate state, string projectId){};
void (*onSpitType)(ObjectEvent event, bool isRemote, const SPITType &p_SPITType){};
void (*onSpitType)(ObjectEvent event, const SPITType &p_SPITType){}; void (*onSpitObject)(ObjectEvent event, bool isRemote, const SPITType &p_SPITType, const SPITObject &p_SPITObject){};
void (*onSpitTypeRemote)(ObjectEvent event, const SPITType &p_SPITType){};
void (*onSpitObject)(ObjectEvent event, const SPITType &p_SPITType, const SPITObject &p_SPITObject){};
void (*onSpitObjectRemote)(ObjectEvent event, const SPITType &p_SPITType, const SPITObject &p_SPITObject){};
void (*onPlay)(string p_PlayerID, string p_PlayerName, char p_PlayerType, bool p_PlayerIsRunning, long long p_TimeFrames){}; void (*onPlay)(string p_PlayerID, string p_PlayerName, char p_PlayerType, bool p_PlayerIsRunning, long long p_TimeFrames){};
void (*onPlaySpitType)(string p_PlayerID, string p_SPITTypeID, bool p_PlayerIsRunning, bool p_Started, bool p_Ended, double p_Value){}; void (*onPlaySpitType)(string p_PlayerID, string p_SPITTypeID, bool p_PlayerIsRunning, bool p_Started, bool p_Ended, double p_Value){};
@@ -96,12 +93,12 @@ class SPIT_Client : I_SAD_Client {
static constexpr char VALUEFLAG_INCREASE = 0x01; static constexpr char VALUEFLAG_INCREASE = 0x01;
/**Decrease the existing value by the given value.<br>This is usefull e.g. when instead of a fader a endless wheel is used*/ /**Decrease the existing value by the given value.<br>This is usefull e.g. when instead of a fader a endless wheel is used*/
static constexpr char VALUEFLAG_DECREASE = 0x02; static constexpr char VALUEFLAG_DECREASE = 0x02;
static constexpr int MAX_OVERTAKING_SPIT_MESSAGES = 10;
//----- SPIT_Client ----- //----- SPIT_Client -----
public: public:
SPIT_Client(string name, string projName, Callbacks calls); SPIT_Client(string name, string projName, Callbacks calls);
virtual ~SPIT_Client(); virtual ~SPIT_Client();
static int s_MAX_OVERTAKING_SPIT_MESSAGES;
private: private:
Callbacks mCallbacks; Callbacks mCallbacks;
@@ -120,7 +117,7 @@ class SPIT_Client : I_SAD_Client {
unsigned char m_MessageNumber_Send; unsigned char m_MessageNumber_Send;
int m_MessageNumber_Received; int m_MessageNumber_Received;
vector<SPIT_Message *> m_OvertakingSPIT_Messages; vector<unique_ptr<SPIT_Message>> m_OvertakingSPIT_Messages;
// Server's info filled by parseConnectAnswer // Server's info filled by parseConnectAnswer
string m_ServerID{}; string m_ServerID{};
@@ -220,7 +217,7 @@ class SPIT_Client : I_SAD_Client {
void resetMessageNumberReceived(); void resetMessageNumberReceived();
void parseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage); void parseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage);
void checkParseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage); void checkParseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage);
void addOvertakingSPIT_Message(SPIT_Message *p_SPIT_Message); void addOvertakingSPIT_Message(unique_ptr<SPIT_Message> p_SPIT_Message);
void parseConnectAnswer(SPIT_Message::ConnectAnswer *p_ConnectAnswer); void parseConnectAnswer(SPIT_Message::ConnectAnswer *p_ConnectAnswer);
void parseDisconnect(SPIT_Message::Disconnect *p_Disconnect); void parseDisconnect(SPIT_Message::Disconnect *p_Disconnect);
void parseWatchdog(SPIT_Message::Watchdog *p_Watchdog); void parseWatchdog(SPIT_Message::Watchdog *p_Watchdog);
+26 -51
View File
@@ -14,16 +14,17 @@
#include "SPITObject.h" #include "SPITObject.h"
#include "SPITSocket.h" #include "SPITSocket.h"
#include "esp_log.h"
unordered_map<string, SPITObject *> SPITObject::sSpitObjects; unordered_map<string, unique_ptr<SPITObject>> SPITObject::sSpitObjects;
unordered_map<string, SPITObject *> SPITObject::sSpitObjectsRemote; unordered_map<string, unique_ptr<SPITObject>> SPITObject::sSpitObjectsRemote;
SPITObject *SPITObject::getSPITObject(const string &id) { SPITObject *SPITObject::getSPITObject(const string &id) {
auto i = sSpitObjects.find(id); auto i = sSpitObjects.find(id);
if (i == sSpitObjects.end()) { if (i == sSpitObjects.end()) {
return nullptr; return nullptr;
} }
return i->second; return i->second.get();
} }
/** /**
* Returns the reference of the SPITObject with ID == id. * Returns the reference of the SPITObject with ID == id.
@@ -40,7 +41,7 @@ SPITObject *SPITObject::getSPITObjectRemote(const string &id) {
if (i == sSpitObjectsRemote.end()) { if (i == sSpitObjectsRemote.end()) {
return nullptr; return nullptr;
} }
return i->second; return i->second.get();
} }
/** /**
* Returns the reference of the remote SPITObject with ID == id. * Returns the reference of the remote SPITObject with ID == id.
@@ -52,16 +53,18 @@ const SPITObject &SPITObject::getSPITObjectRemoteRef(const string &id) {
return *sSpitObjectsRemote.at(id); return *sSpitObjectsRemote.at(id);
} }
void SPITObject::addSPITObject(SPITObject &spitObject) { void SPITObject::addSPITObject(unique_ptr<SPITObject> spitObject) {
sSpitObjects[spitObject.getID()] = &spitObject; ESP_LOGD("OBJ", "adding spitObject %s/%s", spitObject->getID().c_str(), spitObject->getName().c_str());
sSpitObjects[spitObject->getID()] = move(spitObject);
} }
void SPITObject::addSPITObjectRemote(SPITObject &spitObject) { void SPITObject::addSPITObjectRemote(unique_ptr<SPITObject> spitObject) {
sSpitObjectsRemote[spitObject.getID()] = &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) { bool SPITObject::removeSPITObject(const SPITObject &spitObject) {
return removeSPITObject(spitObject.getID()); return removeSPITObject(spitObject.getID());
} }
bool SPITObject::removeSPITObject(const string & spitObjectID) { bool SPITObject::removeSPITObject(const string &spitObjectID) {
auto i = sSpitObjects.find(spitObjectID); auto i = sSpitObjects.find(spitObjectID);
if (i == sSpitObjects.end()) { if (i == sSpitObjects.end()) {
return false; return false;
@@ -72,7 +75,7 @@ bool SPITObject::removeSPITObject(const string & spitObjectID) {
bool SPITObject::removeSPITObjectRemote(const SPITObject &spitObject) { bool SPITObject::removeSPITObjectRemote(const SPITObject &spitObject) {
return removeSPITObjectRemote(spitObject.getID()); return removeSPITObjectRemote(spitObject.getID());
} }
bool SPITObject::removeSPITObjectRemote(const string & spitObjectID) { bool SPITObject::removeSPITObjectRemote(const string &spitObjectID) {
auto i = sSpitObjectsRemote.find(spitObjectID); auto i = sSpitObjectsRemote.find(spitObjectID);
if (i == sSpitObjectsRemote.end()) { if (i == sSpitObjectsRemote.end()) {
return false; return false;
@@ -83,8 +86,8 @@ bool SPITObject::removeSPITObjectRemote(const string & spitObjectID) {
SPITObject *SPITObject::getSPITObject(int p_Index) { SPITObject *SPITObject::getSPITObject(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitObjects.size()) { if (p_Index >= 0 && p_Index < sSpitObjects.size()) {
unordered_map<string, SPITObject *>::iterator i = next(sSpitObjects.begin(), p_Index); auto i = next(sSpitObjects.begin(), p_Index);
return i->second; return i->second.get();
} }
return nullptr; return nullptr;
} }
@@ -96,7 +99,7 @@ SPITObject *SPITObject::getSPITObject(int p_Index) {
*/ */
const SPITObject &SPITObject::getSPITObjectRef(int p_Index) { const SPITObject &SPITObject::getSPITObjectRef(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitObjects.size()) { if (p_Index >= 0 && p_Index < sSpitObjects.size()) {
unordered_map<string, SPITObject *>::iterator i = next(sSpitObjects.begin(), p_Index); auto i = next(sSpitObjects.begin(), p_Index);
return *i->second; return *i->second;
} else { } else {
throw "SPITObject index out of range"; throw "SPITObject index out of range";
@@ -104,8 +107,8 @@ const SPITObject &SPITObject::getSPITObjectRef(int p_Index) {
} }
SPITObject *SPITObject::getSPITObjectRemote(int p_Index) { SPITObject *SPITObject::getSPITObjectRemote(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitObjectsRemote.size()) { if (p_Index >= 0 && p_Index < sSpitObjectsRemote.size()) {
unordered_map<string, SPITObject *>::iterator i = next(sSpitObjectsRemote.begin(), p_Index); auto i = next(sSpitObjectsRemote.begin(), p_Index);
return i->second; return i->second.get();
} else { } else {
return nullptr; return nullptr;
} }
@@ -118,76 +121,48 @@ SPITObject *SPITObject::getSPITObjectRemote(int p_Index) {
*/ */
const SPITObject &SPITObject::getSPITObjectRemoteRef(int p_Index) { const SPITObject &SPITObject::getSPITObjectRemoteRef(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitObjectsRemote.size()) { if (p_Index >= 0 && p_Index < sSpitObjectsRemote.size()) {
unordered_map<string, SPITObject *>::iterator i = next(sSpitObjectsRemote.begin(), p_Index); auto i = next(sSpitObjectsRemote.begin(), p_Index);
return *i->second; return *i->second;
} else { } else {
throw "Remote SPITObject index out of range"; throw "Remote SPITObject index out of range";
} }
} }
void SPITObject::clearSPITObjects() {
SPITObject *ptrSPITObject;
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
ptrSPITObject = i->second;
i = sSpitObjects.erase(i);
delete ptrSPITObject;
}
}
void SPITObject::clearSPITObjectsRemote() {
SPITObject *ptrSPITObject;
for (auto i = sSpitObjectsRemote.begin(); i != sSpitObjectsRemote.end(); i++) {
ptrSPITObject = i->second;
i = sSpitObjectsRemote.erase(i);
delete ptrSPITObject;
}
}
/** /**
* clears all unconfirmed SPITObjects not created by this SPIT_Client. * clears all unconfirmed SPITObjects not created by this SPIT_Client.
*/ */
void SPITObject::clearUnconfirmedSPITObjects() { void SPITObject::clearUnconfirmedSPITObjects() {
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) { for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
SPITObject *ptrSPITObject = i->second; SPITObject *ptrSPITObject = i->second.get();
if (ptrSPITObject->getCreatedByClient() == false && ptrSPITObject->getConfirmed() == false) { if (ptrSPITObject->getCreatedByClient() == false && ptrSPITObject->getConfirmed() == false) {
i = sSpitObjects.erase(i); i = sSpitObjects.erase(i);
delete ptrSPITObject;
} }
} }
} }
void SPITObject::eventProjectClosed() { void SPITObject::eventProjectClosed() {
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) { for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
SPITObject *ptrSPITObject = i->second; SPITObject *ptrSPITObject = i->second.get();
if (ptrSPITObject->getCreatedByClient() == true) { if (ptrSPITObject->getCreatedByClient() == true) {
continue; continue;
} }
i = sSpitObjects.erase(i); i = sSpitObjects.erase(i);
delete ptrSPITObject;
} }
// Remote // Remote
for (auto i = sSpitObjectsRemote.begin(); i != sSpitObjectsRemote.end(); i++) { sSpitObjectsRemote.clear();
SPITObject *ptrSPITObject = i->second;
i = sSpitObjectsRemote.erase(i);
delete ptrSPITObject;
}
} }
/** /**
* sets confirmed = false for all SPITObjects. * sets confirmed = false for all SPITObjects.
* This should be called when the client has loaded a project. * This should be called when the client has loaded a project.
*/ */
void SPITObject::setConfirmedFalse() { void SPITObject::setConfirmedFalse() {
SPITObject *ptrSPITObject;
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) { for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
ptrSPITObject = i->second; i->second->setConfirmed(false);
ptrSPITObject->setConfirmed(false);
} }
} }
SPITObject::SPITObject() { SPITObject::SPITObject(string p_ID) {
m_ID = createGUID(); m_ID = p_ID;
} }
bool SPITObject::setMessageOP(SPIT_Message::ReportSPITObject *p_ReportSPITObject) { bool SPITObject::setMessageOP(SPIT_Message::ReportSPITObject *p_ReportSPITObject) {
@@ -404,7 +379,7 @@ bool SPITObject::setServerRemote(bool p_BelongstoServer) {
} }
/** /**
* This is a virtual construct - ActionAssignedType is not implemented in the SPITProtocol. * This is a virtual construct - ActionAssignedType is not implemented in the SPITProtocol.
* This is used when an action is associated with a SPITType. * This is used when an action is associated with a SPITType.
* Then this action is done for every SPITObject with the SPITType. * Then this action is done for every SPITObject with the SPITType.
* @return true = an action is assigned for the SPITType. * @return true = an action is assigned for the SPITType.
*/ */
+5 -10
View File
@@ -17,17 +17,12 @@
unordered_map<string, SPITPlayer *> SPITPlayer::s_SPITPlayers; unordered_map<string, SPITPlayer *> SPITPlayer::s_SPITPlayers;
SPITPlayer *SPITPlayer::getSPITPlayer(string p_PlayerID) { SPITPlayer &SPITPlayer::getSPITPlayer(string p_PlayerID) {
{ auto i = s_SPITPlayers.find(p_PlayerID);
auto i = s_SPITPlayers.find(p_PlayerID); if (i == s_SPITPlayers.end()) {
if (i == s_SPITPlayers.end()) { s_SPITPlayers[p_PlayerID] = new SPITPlayer(p_PlayerID);
s_SPITPlayers[p_PlayerID] = new SPITPlayer(p_PlayerID);
}
i = s_SPITPlayers.find(p_PlayerID);
if (i == s_SPITPlayers.end())
return NULL;
return i->second;
} }
return *s_SPITPlayers.at(p_PlayerID);
} }
void SPITPlayer::clearPlayers() { void SPITPlayer::clearPlayers() {
+22 -43
View File
@@ -17,15 +17,15 @@
static const char *TAG = "SPIT"; static const char *TAG = "SPIT";
unordered_map<string, SPITType *> SPITType::sSpitTypes; unordered_map<string, unique_ptr<SPITType>> SPITType::sSpitTypes;
unordered_map<string, SPITType *> SPITType::sSpitTypesRemote; unordered_map<string, unique_ptr<SPITType>> SPITType::sSpitTypesRemote;
SPITType *SPITType::getSPITType(const string &id) { SPITType *SPITType::getSPITType(const string &id) {
auto i = sSpitTypes.find(id); auto i = sSpitTypes.find(id);
if (i == sSpitTypes.end()) { if (i == sSpitTypes.end()) {
return nullptr; return nullptr;
} }
return i->second; return i->second.get();
} }
/** /**
* Returns the reference of the SPITType with ID == id. * Returns the reference of the SPITType with ID == id.
@@ -42,7 +42,7 @@ SPITType *SPITType::getSPITTypeRemote(const string &id) {
if (i == sSpitTypesRemote.end()) { if (i == sSpitTypesRemote.end()) {
return nullptr; return nullptr;
} }
return i->second; return i->second.get();
} }
/** /**
@@ -55,11 +55,13 @@ const SPITType &SPITType::getSPITTypeRemoteRef(const string &id) {
return *sSpitTypesRemote.at(id); return *sSpitTypesRemote.at(id);
} }
void SPITType::addSPITType(SPITType &spitType) { void SPITType::addSPITType(unique_ptr<SPITType> spitType) {
sSpitTypes[spitType.getID()] = &spitType; ESP_LOGD("OBJ", "adding spitObject %s/%s", spitType->getID().c_str(), spitType->getName().c_str());
sSpitTypes[spitType->getID()] = move(spitType);
} }
void SPITType::addSPITTypeRemote(SPITType &spitType) { void SPITType::addSPITTypeRemote(unique_ptr<SPITType> spitType) {
sSpitTypesRemote[spitType.getID()] = &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) { bool SPITType::removeSPITType(const SPITType &spitType) {
auto i = sSpitTypes.find(spitType.getID()); auto i = sSpitTypes.find(spitType.getID());
@@ -79,12 +81,11 @@ bool SPITType::removeSPITTypeRemote(const SPITType &spitType) {
} }
SPITType *SPITType::getSPITType(int p_Index) { SPITType *SPITType::getSPITType(int p_Index) {
SPITType *pSpitType = nullptr;
if (p_Index < sSpitTypes.size()) { if (p_Index < sSpitTypes.size()) {
unordered_map<string, SPITType *>::iterator i = next(sSpitTypes.begin(), p_Index); auto i = next(sSpitTypes.begin(), p_Index);
pSpitType = i->second; return i->second.get();
} }
return pSpitType; return nullptr;
} }
/** /**
* Returns a reference to the SPITType at position p_Index. * Returns a reference to the SPITType at position p_Index.
@@ -94,7 +95,7 @@ SPITType *SPITType::getSPITType(int p_Index) {
*/ */
const SPITType &SPITType::getSPITTypeRef(int p_Index) { const SPITType &SPITType::getSPITTypeRef(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitTypes.size()) { if (p_Index >= 0 && p_Index < sSpitTypes.size()) {
unordered_map<string, SPITType *>::iterator i = next(sSpitTypes.begin(), p_Index); auto i = next(sSpitTypes.begin(), p_Index);
return *i->second; return *i->second;
} else { } else {
throw "SPITType index out of range"; throw "SPITType index out of range";
@@ -102,8 +103,8 @@ const SPITType &SPITType::getSPITTypeRef(int p_Index) {
} }
SPITType *SPITType::getSPITTypeRemote(int p_Index) { SPITType *SPITType::getSPITTypeRemote(int p_Index) {
if (p_Index < sSpitTypesRemote.size()) { if (p_Index < sSpitTypesRemote.size()) {
unordered_map<string, SPITType *>::iterator i = next(sSpitTypesRemote.begin(), p_Index); auto i = next(sSpitTypesRemote.begin(), p_Index);
return i->second; return i->second.get();
} }
return nullptr; return nullptr;
} }
@@ -115,43 +116,23 @@ SPITType *SPITType::getSPITTypeRemote(int p_Index) {
*/ */
const SPITType &SPITType::getSPITTypeRemoteRef(int p_Index) { const SPITType &SPITType::getSPITTypeRemoteRef(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitTypesRemote.size()) { if (p_Index >= 0 && p_Index < sSpitTypesRemote.size()) {
unordered_map<string, SPITType *>::iterator i = next(sSpitTypesRemote.begin(), p_Index); auto i = next(sSpitTypesRemote.begin(), p_Index);
return *i->second; return *i->second;
} else { } else {
throw "Remote SPITType index out of range"; throw "Remote SPITType index out of range";
} }
} }
void SPITType::clearSPITTypes() {
for (auto i = sSpitTypes.begin(); i != sSpitTypes.end(); i++) {
SPITType *pSpitType = i->second;
i = sSpitTypes.erase(i);
delete pSpitType;
}
}
void SPITType::clearSPITTypesRemote() {
for (auto i = sSpitTypesRemote.begin(); i != sSpitTypesRemote.end(); i++) {
SPITType *ptrSPITType = i->second;
i = sSpitTypesRemote.erase(i);
delete ptrSPITType;
}
}
void SPITType::eventProjectClosed() { void SPITType::eventProjectClosed() {
for (auto i = sSpitTypes.begin(); i != sSpitTypes.end(); i++) { for (auto i = sSpitTypes.begin(); i != sSpitTypes.end(); i++) {
SPITType *ptrSPITType = i->second; SPITType *ptrSPITType = i->second.get();
if (ptrSPITType->getCreatedByClient() == true) { if (ptrSPITType->getCreatedByClient() == true) {
continue; continue;
} }
i = sSpitTypes.erase(i); i = sSpitTypes.erase(i);
delete ptrSPITType;
} }
// Remote // Remote
for (auto i = sSpitTypesRemote.begin(); i != sSpitTypesRemote.end(); i++) { sSpitTypesRemote.clear();
SPITType *ptrSPITType = i->second;
i = sSpitTypesRemote.erase(i);
delete ptrSPITType;
}
} }
/** /**
@@ -160,17 +141,15 @@ void SPITType::eventProjectClosed() {
*/ */
void SPITType::setConfirmedFalse() { void SPITType::setConfirmedFalse() {
for (auto i = sSpitTypes.begin(); i != sSpitTypes.end(); i++) { for (auto i = sSpitTypes.begin(); i != sSpitTypes.end(); i++) {
SPITType *ptrSPITType = i->second; i->second->setConfirmed(false);
ptrSPITType->setConfirmed(false);
} }
} }
SPITType::SPITType() { SPITType::SPITType(string p_ID) {
m_ID = createGUID(); m_ID = p_ID;
} }
SPITType::SPITType(string id, string grpName, string name, bool fixed, bool projectIndependent, long long defaultDelay, long long defaultDuration) { SPITType::SPITType(string id, string grpName, string name, bool fixed, bool projectIndependent, long long defaultDelay, long long defaultDuration) {
m_ID = createGUID();
m_ActionAssigned = false; m_ActionAssigned = false;
m_Confirmed = false; m_Confirmed = false;
m_ID = id; m_ID = id;
+125 -184
View File
@@ -264,7 +264,7 @@ int SPIT_Client::sendSPITMessage(unique_ptr<SPIT_Message::OPPart> spitMessageOPP
m_MessageNumber_Send = 1; m_MessageNumber_Send = 1;
else else
m_MessageNumber_Send++; m_MessageNumber_Send++;
opPart.getHeader()->setMessageNumber(m_MessageNumber_Send); opPart.getHeader()->setMessageNumber(m_MessageNumber_Send);
int opFlag = 0; int opFlag = 0;
@@ -296,7 +296,7 @@ bool SPIT_Client::startReceiveSPITMessages() {
return true; return true;
} }
void SPIT_Client::stopReceiveSPITMessages() { void SPIT_Client::stopReceiveSPITMessages() {
m_StopReceiveSPITMessages = true; m_StopReceiveSPITMessages = true;
} }
void SPIT_Client::receiveSPITMessage(SPIT_Client &rM) { void SPIT_Client::receiveSPITMessage(SPIT_Client &rM) {
@@ -318,14 +318,7 @@ void SPIT_Client::receiveSPITMessage(SPIT_Client &rM) {
} }
void SPIT_Client::resetMessageNumberReceived() { void SPIT_Client::resetMessageNumberReceived() {
SPIT_Message *ptrOvertakingSPIT_Message;
m_MessageNumber_Received = -1; m_MessageNumber_Received = -1;
for (auto it = m_OvertakingSPIT_Messages.begin(); it != m_OvertakingSPIT_Messages.end(); it++) {
ptrOvertakingSPIT_Message = *it;
delete ptrOvertakingSPIT_Message;
// m_OvertakingSPIT_Messages.erase(it);
}
m_OvertakingSPIT_Messages.clear(); m_OvertakingSPIT_Messages.clear();
} }
@@ -344,11 +337,7 @@ void SPIT_Client::checkParseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage)
return; return;
} }
int intActualMessageNumber; int intActualMessageNumber = (int)(p_SPITMessage->getHeader()->getMessageNumber() & 0xFF);
int intNextMessageNumber;
SPIT_Message *ptrOvertakingSPIT_Message;
intActualMessageNumber = (int)(p_SPITMessage->getHeader()->getMessageNumber() & 0xFF);
if (intActualMessageNumber == 0) { if (intActualMessageNumber == 0) {
parseSPIT_Message(std::move(p_SPITMessage)); parseSPIT_Message(std::move(p_SPITMessage));
@@ -360,7 +349,7 @@ void SPIT_Client::checkParseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage)
return; return;
} }
intNextMessageNumber = m_MessageNumber_Received + 1; int intNextMessageNumber = m_MessageNumber_Received + 1;
if (intNextMessageNumber > 255 || intNextMessageNumber < 1) if (intNextMessageNumber > 255 || intNextMessageNumber < 1)
intNextMessageNumber = 1; intNextMessageNumber = 1;
@@ -376,12 +365,12 @@ void SPIT_Client::checkParseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage)
if (intNextMessageNumber > 255 || intNextMessageNumber < 1) if (intNextMessageNumber > 255 || intNextMessageNumber < 1)
intNextMessageNumber = 1; intNextMessageNumber = 1;
ptrOvertakingSPIT_Message = *it; auto &ptrOvertakingSPIT_Message = *it;
intActualMessageNumber = (int)(ptrOvertakingSPIT_Message->getHeader()->getMessageNumber() & 0xFF); intActualMessageNumber = (int)(ptrOvertakingSPIT_Message->getHeader()->getMessageNumber() & 0xFF);
if (intActualMessageNumber == intNextMessageNumber) { if (intActualMessageNumber == intNextMessageNumber) {
m_MessageNumber_Received = intActualMessageNumber; m_MessageNumber_Received = intActualMessageNumber;
// parseSPIT_Message(ptrOvertakingSPIT_Message); parseSPIT_Message(move(ptrOvertakingSPIT_Message));
m_OvertakingSPIT_Messages.erase(it); m_OvertakingSPIT_Messages.erase(it);
it = m_OvertakingSPIT_Messages.begin(); it = m_OvertakingSPIT_Messages.begin();
@@ -392,33 +381,28 @@ void SPIT_Client::checkParseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage)
} else { } else {
// p_SPITMessage has overtaken another message // p_SPITMessage has overtaken another message
addOvertakingSPIT_Message(p_SPITMessage.get()); addOvertakingSPIT_Message(move(p_SPITMessage));
} }
} }
int SPIT_Client::s_MAX_OVERTAKING_SPIT_MESSAGES = 10; void SPIT_Client::addOvertakingSPIT_Message(unique_ptr<SPIT_Message> p_SPIT_Message) {
if (!p_SPIT_Message) {
void SPIT_Client::addOvertakingSPIT_Message(SPIT_Message *p_SPIT_Message) {
if (p_SPIT_Message == nullptr)
return; return;
int intMessageNumber; }
SPIT_Message *ptrOvertakingSPIT_Message; if (m_OvertakingSPIT_Messages.size() > MAX_OVERTAKING_SPIT_MESSAGES) {
int intOvertakingMessageNumber;
if (m_OvertakingSPIT_Messages.size() > s_MAX_OVERTAKING_SPIT_MESSAGES) {
ESP_LOGD(TAG, "SPIT_Client receiveing: perhaps message lost !!!!!"); ESP_LOGD(TAG, "SPIT_Client receiveing: perhaps message lost !!!!!");
} }
intMessageNumber = (int)(p_SPIT_Message->getHeader()->getMessageNumber() & 0xFF); int intMessageNumber = (int)(p_SPIT_Message->getHeader()->getMessageNumber() & 0xFF);
for (auto it = m_OvertakingSPIT_Messages.begin(); it != m_OvertakingSPIT_Messages.end(); it++) { for (auto it = m_OvertakingSPIT_Messages.begin(); it != m_OvertakingSPIT_Messages.end(); ++it) {
ptrOvertakingSPIT_Message = *it; const auto &overtaking = *it;
intOvertakingMessageNumber = (int)(ptrOvertakingSPIT_Message->getHeader()->getMessageNumber() & 0xFF); int intOvertakingMessageNumber = (int)(overtaking->getHeader()->getMessageNumber() & 0xFF);
if (intMessageNumber < intOvertakingMessageNumber) { if (intMessageNumber < intOvertakingMessageNumber) {
m_OvertakingSPIT_Messages.insert(it, p_SPIT_Message); m_OvertakingSPIT_Messages.insert(it, move(p_SPIT_Message));
return; return;
} }
} }
m_OvertakingSPIT_Messages.push_back(p_SPIT_Message); m_OvertakingSPIT_Messages.push_back(move(p_SPIT_Message));
} }
void SPIT_Client::parseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage) { void SPIT_Client::parseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage) {
@@ -612,8 +596,7 @@ void SPIT_Client::disconnectInternal(int p_Cause) {
} }
void SPIT_Client::parseConnectAnswer(SPIT_Message::ConnectAnswer *p_ConnectAnswer) { void SPIT_Client::parseConnectAnswer(SPIT_Message::ConnectAnswer *p_ConnectAnswer) {
int intOldConnectionStatus; int intOldConnectionStatus = m_ConnectionStatus;
intOldConnectionStatus = m_ConnectionStatus;
if (p_ConnectAnswer->getClientID() != m_ID) if (p_ConnectAnswer->getClientID() != m_ID)
return; return;
@@ -726,6 +709,7 @@ void SPIT_Client::parseCloseSPITProject(SPIT_Message::CloseSPITProject *p_CloseS
return; return;
string stringProjectID; string stringProjectID;
mCallbacks.onProjectUpdate(ProjUpdate::ServerClosed, p_CloseSPITProject->getProjectID()); mCallbacks.onProjectUpdate(ProjUpdate::ServerClosed, p_CloseSPITProject->getProjectID());
SPITPlayer::clearPlayers();
} }
string SPIT_Client::getServerProjectID() { return m_ServerProjectID; } string SPIT_Client::getServerProjectID() { return m_ServerProjectID; }
@@ -926,13 +910,17 @@ void SPIT_Client::parseReportSPITType(SPIT_Message::ReportSPITType *p_ReportSPIT
if (mCallbacks.onCheckSpitTypeVeto(p_ReportSPITType) == false) { if (mCallbacks.onCheckSpitTypeVeto(p_ReportSPITType) == false) {
return; return;
} }
ptrSPITType = new SPITType(); auto spitType = make_unique<SPITType>(p_ReportSPITType->getID());
ptrSPITType = spitType.get();
SPITType::addSPITType(move(spitType));
boolNew = true; boolNew = true;
} }
} else { } else {
ptrSPITType = SPITType::getSPITTypeRemote(p_ReportSPITType->getID()); ptrSPITType = SPITType::getSPITTypeRemote(p_ReportSPITType->getID());
if (ptrSPITType == nullptr) { if (ptrSPITType == nullptr) {
ptrSPITType = new SPITType(); auto spitType = make_unique<SPITType>(p_ReportSPITType->getID());
ptrSPITType = spitType.get();
SPITType::addSPITTypeRemote(move(spitType));
boolNew = true; boolNew = true;
} }
} }
@@ -944,13 +932,7 @@ void SPIT_Client::parseReportSPITType(SPIT_Message::ReportSPITType *p_ReportSPIT
auto eventType = boolNew ? ObjectEvent::Created : ObjectEvent::Changed; auto eventType = boolNew ? ObjectEvent::Created : ObjectEvent::Changed;
// add or change SPITType // add or change SPITType
if (ptrSPITType->getServerRemote() == false) { mCallbacks.onSpitType(eventType, ptrSPITType->getServerRemote(), *ptrSPITType);
SPITType::addSPITType(*ptrSPITType);
mCallbacks.onSpitType(eventType, *ptrSPITType);
} else {
SPITType::addSPITTypeRemote(*ptrSPITType);
mCallbacks.onSpitTypeRemote(eventType, *ptrSPITType);
}
if (ptrSPITType != nullptr) { if (ptrSPITType != nullptr) {
if (ptrSPITType->setConfirmed(true) == true || boolChanged == true) { if (ptrSPITType->setConfirmed(true) == true || boolChanged == true) {
@@ -961,42 +943,37 @@ void SPIT_Client::parseReportSPITType(SPIT_Message::ReportSPITType *p_ReportSPIT
} }
void SPIT_Client::parseRemoveSPITType(SPIT_Message::RemoveSPITType *p_RemoveSPITType) { void SPIT_Client::parseRemoveSPITType(SPIT_Message::RemoveSPITType *p_RemoveSPITType) {
// printf("parse RemoveSPITType %s\n", p_RemoveSPITType->getID()); // printf("parse RemoveSPITType %s\n", p_RemoveSPITType->getID());
if (p_RemoveSPITType == nullptr) if (!p_RemoveSPITType) {
return; return;
string stringSPITTypeID; }
bool boolRemote = false; bool boolRemote = false;
SPITType *ptrSPITType; string stringSPITTypeID = p_RemoveSPITType->getID();
stringSPITTypeID = p_RemoveSPITType->getID();
{ SPITType *ptrSPITType = SPITType::getSPITTypeRemote(stringSPITTypeID);
ptrSPITType = SPITType::getSPITTypeRemote(stringSPITTypeID); if (!ptrSPITType) {
if (ptrSPITType != nullptr) { boolRemote = true;
boolRemote = true; } else {
ptrSPITType = SPITType::getSPITType(stringSPITTypeID);
if (!ptrSPITType) {
boolRemote = false;
} else { } else {
ptrSPITType = SPITType::getSPITType(stringSPITTypeID); // No SPITType with stringSPITTypeID is listet
if (ptrSPITType != nullptr) {
boolRemote = false;
} else {
// No SPITType with stringSPITTypeID is listet
return;
}
}
if (ptrSPITType->getCreatedByClient() && ptrSPITType->getFixed()) {
// the SPITyte is created by client himself and it is fixed -> don't remove the SPITType!!
// Send the SPITTyp back to the server
sendSPITTypeInternal(*ptrSPITType);
return; return;
} }
}
if (boolRemote == false) { if (ptrSPITType->getCreatedByClient() && ptrSPITType->getFixed()) {
mCallbacks.onSpitType(ObjectEvent::Deleted, *ptrSPITType); // the SPITyte is created by client himself and it is fixed -> don't remove the SPITType!!
SPITType::removeSPITType(*ptrSPITType); // Send the SPITTyp back to the server
} else { sendSPITTypeInternal(*ptrSPITType);
mCallbacks.onSpitTypeRemote(ObjectEvent::Deleted, *ptrSPITType); return;
SPITType::removeSPITTypeRemote(*ptrSPITType); }
}
delete ptrSPITType; mCallbacks.onSpitType(ObjectEvent::Deleted, boolRemote, *ptrSPITType);
if (boolRemote == false) {
SPITType::removeSPITType(*ptrSPITType);
} else {
SPITType::removeSPITTypeRemote(*ptrSPITType);
} }
} }
@@ -1031,7 +1008,7 @@ bool SPIT_Client::changeSPITType(SPITType *p_SPITType) {
ptrSPITType->setName(p_SPITType->getName()); ptrSPITType->setName(p_SPITType->getName());
ptrSPITType->setProjectIndepended(p_SPITType->getProjectIndepended()); ptrSPITType->setProjectIndepended(p_SPITType->getProjectIndepended());
mCallbacks.onSpitType(ObjectEvent::Changed, *ptrSPITType); mCallbacks.onSpitType(ObjectEvent::Changed, false, *ptrSPITType);
sendSPITTypeInternal(*ptrSPITType); sendSPITTypeInternal(*ptrSPITType);
return true; return true;
@@ -1055,9 +1032,8 @@ int SPIT_Client::removeSPITType(string p_SPITTypeID) {
return -1; return -1;
auto sMRemoveSPITType = make_unique<SPIT_Message::RemoveSPITType>(); auto sMRemoveSPITType = make_unique<SPIT_Message::RemoveSPITType>();
ptrSPITType->fillMessageOP(*sMRemoveSPITType); ptrSPITType->fillMessageOP(*sMRemoveSPITType);
mCallbacks.onSpitType(ObjectEvent::Deleted, *ptrSPITType); mCallbacks.onSpitType(ObjectEvent::Deleted, false, *ptrSPITType);
SPITType::removeSPITType(*ptrSPITType); SPITType::removeSPITType(*ptrSPITType);
delete ptrSPITType;
return sendSPITMessage(move(sMRemoveSPITType)); return sendSPITMessage(move(sMRemoveSPITType));
} }
return -1; return -1;
@@ -1071,7 +1047,9 @@ SPITType *SPIT_Client::createSPITType(string p_ID, string p_GroupName, string p_
bool boolNew; bool boolNew;
SPITType *ptrSPITType = SPITType::getSPITType(p_ID); SPITType *ptrSPITType = SPITType::getSPITType(p_ID);
if (ptrSPITType == nullptr) { if (ptrSPITType == nullptr) {
ptrSPITType = new SPITType(p_ID, p_GroupName, p_Name, p_FIxed, p_ProjectIndepended, p_DefaultDelay, p_DefaultDuration); 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));
ptrSPITType->setCreatedByClient(true); ptrSPITType->setCreatedByClient(true);
boolNew = true; boolNew = true;
} else { } else {
@@ -1086,8 +1064,7 @@ SPITType *SPIT_Client::createSPITType(string p_ID, string p_GroupName, string p_
} }
auto eventType = boolNew ? ObjectEvent::Created : ObjectEvent::Changed; auto eventType = boolNew ? ObjectEvent::Created : ObjectEvent::Changed;
SPITType::addSPITType(*ptrSPITType); mCallbacks.onSpitType(eventType, false, *ptrSPITType);
mCallbacks.onSpitType(eventType, *ptrSPITType);
sendSPITTypeInternal(*ptrSPITType); sendSPITTypeInternal(*ptrSPITType);
return ptrSPITType; return ptrSPITType;
} }
@@ -1103,6 +1080,7 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
bool boolRemote = false; bool boolRemote = false;
bool boolChanged = false; bool boolChanged = false;
{ {
ESP_LOGD(TAG, "parseReportSPITObject %s / %s", p_ReportSPITObject->getID().c_str(), p_ReportSPITObject->getName().c_str());
if (p_ReportSPITObject->getServerRemote() == false) { if (p_ReportSPITObject->getServerRemote() == false) {
boolRemote = false; boolRemote = false;
ptrSPITObject = SPITObject::getSPITObject(p_ReportSPITObject->getID()); ptrSPITObject = SPITObject::getSPITObject(p_ReportSPITObject->getID());
@@ -1114,7 +1092,6 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
// SpitObject exists but has no corresponding SPITType -> remove the SPITObject without sending a message // 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 // Maybe the SPITObject has changed the SPITTypeID to a SPITTypeID of another board
SPITObject::removeSPITObject(*ptrSPITObject); SPITObject::removeSPITObject(*ptrSPITObject);
delete ptrSPITObject;
} }
return; return;
} }
@@ -1124,8 +1101,9 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
ESP_LOGD(TAG, "ObjectVeto %s / %s", p_ReportSPITObject->getID().c_str(), p_ReportSPITObject->getName().c_str()); ESP_LOGD(TAG, "ObjectVeto %s / %s", p_ReportSPITObject->getID().c_str(), p_ReportSPITObject->getName().c_str());
return; return;
} }
auto spitObj = make_unique<SPITObject>(p_ReportSPITObject->getID());
ptrSPITObject = new SPITObject(); ptrSPITObject = spitObj.get();
SPITObject::addSPITObject(move(spitObj));
boolNew = true; boolNew = true;
} else { } else {
// The user's app my veto the SPITType // The user's app my veto the SPITType
@@ -1133,7 +1111,6 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
// SpitObject exists and User APp denied the SPITObject -> remove the SPITObject without sending a message // SpitObject exists and User APp denied the SPITObject -> remove the SPITObject without sending a message
SPITObject::removeSPITObject(*ptrSPITObject); SPITObject::removeSPITObject(*ptrSPITObject);
ESP_LOGD(TAG, "TypeVeto %s / %s", p_ReportSPITObject->getID().c_str(), p_ReportSPITObject->getName().c_str()); ESP_LOGD(TAG, "TypeVeto %s / %s", p_ReportSPITObject->getID().c_str(), p_ReportSPITObject->getName().c_str());
delete ptrSPITObject;
return; return;
} }
} }
@@ -1146,12 +1123,15 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
return; return;
} }
if (ptrSPITObject == nullptr) { if (ptrSPITObject == nullptr) {
ptrSPITObject = new SPITObject(); auto spitObj = make_unique<SPITObject>(p_ReportSPITObject->getID());
ptrSPITObject = spitObj.get();
SPITObject::addSPITObjectRemote(move(spitObj));
boolNew = true; boolNew = true;
} }
} }
if (!ptrSPITObject->setMessageOP(p_ReportSPITObject)) { if (!ptrSPITObject->setMessageOP(p_ReportSPITObject)) {
ESP_LOGE(TAG, "Change not allowed");
// setMessageOP error - the SPITObject can't be changed, it may be fixed and crated by this client // setMessageOP error - the SPITObject can't be changed, it may be fixed and crated by this client
sendSPITObjectInternal(ptrSPITObject); sendSPITObjectInternal(ptrSPITObject);
return; return;
@@ -1160,17 +1140,10 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
if (boolRemote == false) { if (boolRemote == false) {
// set the virtual action assignment // set the virtual action assignment
ptrSPITObject->setActionAssignedType(ptrSPITType->getActionAssigned()); ptrSPITObject->setActionAssignedType(ptrSPITType->getActionAssigned());
SPITObject::addSPITObject(*ptrSPITObject);
} else {
SPITObject::addSPITObjectRemote(*ptrSPITObject);
} }
auto eventType = boolNew ? ObjectEvent::Created : ObjectEvent::Changed; auto eventType = boolNew ? ObjectEvent::Created : ObjectEvent::Changed;
if (boolRemote) { mCallbacks.onSpitObject(eventType, boolRemote, *ptrSPITType, *ptrSPITObject);
mCallbacks.onSpitObjectRemote(eventType, *ptrSPITType, *ptrSPITObject);
} else {
mCallbacks.onSpitObject(eventType, *ptrSPITType, *ptrSPITObject);
}
if (ptrSPITObject->setConfirmed(true) == true || boolChanged == true) { if (ptrSPITObject->setConfirmed(true) == true || boolChanged == true) {
// Confirmation has changed // Confirmation has changed
@@ -1180,33 +1153,20 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
} }
void SPIT_Client::parseRemoveSPITObject(SPIT_Message::RemoveSPITObject *p_RemoveSPITObject) { void SPIT_Client::parseRemoveSPITObject(SPIT_Message::RemoveSPITObject *p_RemoveSPITObject) {
// printf("parse RemoveSPITObject %s\n", p_RemoveSPITObject->getID()); // printf("parse RemoveSPITObject %s\n", p_RemoveSPITObject->getID());
if (p_RemoveSPITObject == nullptr) if (!p_RemoveSPITObject) {
return; return;
bool boolRemote = false; }
SPITObject *ptrSPITObject;
SPITType *ptrSPITType;
{ SPITObject *ptrSPITObject = SPITObject::getSPITObjectRemote(p_RemoveSPITObject->getID());
ptrSPITObject = SPITObject::getSPITObjectRemote(p_RemoveSPITObject->getID()); if (ptrSPITObject != nullptr) {
if (ptrSPITObject != nullptr) { SPITType *ptrSPITType = SPITType::getSPITTypeRemote(ptrSPITObject->getSPITTypeID());
boolRemote = true; if (ptrSPITType != nullptr) {
ptrSPITType = SPITType::getSPITTypeRemote(ptrSPITObject->getSPITTypeID()); mCallbacks.onSpitObject(ObjectEvent::Deleted, true, *ptrSPITType, *ptrSPITObject);
if (ptrSPITType != nullptr) {
if (boolRemote) {
mCallbacks.onSpitObjectRemote(ObjectEvent::Deleted, *ptrSPITType, *ptrSPITObject);
} else {
mCallbacks.onSpitObject(ObjectEvent::Deleted, *ptrSPITType, *ptrSPITObject);
}
} else {
// No associated SPITType found
// delete the SPITObject without a notification
}
SPITObject::removeSPITObjectRemote(*ptrSPITObject);
delete ptrSPITObject;
} else {
// call removeSPITObject, this will notify the server that the object is removed
removeSPITObject(p_RemoveSPITObject->getID());
} }
SPITObject::removeSPITObjectRemote(*ptrSPITObject);
} else {
// call removeSPITObject, this will notify the server that the object is removed
removeSPITObject(p_RemoveSPITObject->getID());
} }
} }
@@ -1256,7 +1216,7 @@ bool SPIT_Client::changeSPITObject(SPITObject *p_SPITObject) {
ptrSPITObject->setValueFactor(p_SPITObject->getValueFactor()); ptrSPITObject->setValueFactor(p_SPITObject->getValueFactor());
ptrSPITObject->setValueUse(p_SPITObject->getValueUse()); ptrSPITObject->setValueUse(p_SPITObject->getValueUse());
mCallbacks.onSpitObject(ObjectEvent::Changed, *ptrSPITType, *ptrSPITObject); mCallbacks.onSpitObject(ObjectEvent::Changed, false, *ptrSPITType, *ptrSPITObject);
sendSPITObjectInternal(ptrSPITObject); sendSPITObjectInternal(ptrSPITObject);
} }
return true; return true;
@@ -1286,9 +1246,8 @@ int SPIT_Client::removeSPITObject(string p_SPITObjectID) {
if (ptrSPITType == nullptr) if (ptrSPITType == nullptr)
return -1; return -1;
ptrSPITObject->fillMessageOP(*sMRemoveSPITObject); ptrSPITObject->fillMessageOP(*sMRemoveSPITObject);
mCallbacks.onSpitObject(ObjectEvent::Deleted, *ptrSPITType, *ptrSPITObject); mCallbacks.onSpitObject(ObjectEvent::Deleted, false, *ptrSPITType, *ptrSPITObject);
SPITObject::removeSPITObject(*ptrSPITObject); SPITObject::removeSPITObject(*ptrSPITObject);
delete ptrSPITObject;
return sendSPITMessage(move(sMRemoveSPITObject)); return sendSPITMessage(move(sMRemoveSPITObject));
} }
return -1; return -1;
@@ -1298,38 +1257,33 @@ int SPIT_Client::removeSPITObject(string p_SPITObjectID) {
void SPIT_Client::parsePlaySPITStart(SPIT_Message::PlaySPITStart *p_PlaySPITStart) { void SPIT_Client::parsePlaySPITStart(SPIT_Message::PlaySPITStart *p_PlaySPITStart) {
ESP_LOGD(TAG, "parse PlaySPITStart ID: %s Name: %s Running: %d", p_PlaySPITStart->getPlayerID().c_str(), p_PlaySPITStart->getPlayerName().c_str(), (int)p_PlaySPITStart->getPlayerIsRunning()); ESP_LOGD(TAG, "parse PlaySPITStart ID: %s Name: %s Running: %d", p_PlaySPITStart->getPlayerID().c_str(), p_PlaySPITStart->getPlayerName().c_str(), (int)p_PlaySPITStart->getPlayerIsRunning());
SPITPlayer *ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITStart->getPlayerID()); SPITPlayer &spitPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITStart->getPlayerID());
if (ptrSPITPlayer == nullptr) spitPlayer.setIsRunning(p_PlaySPITStart->getPlayerIsRunning());
return; spitPlayer.setName(p_PlaySPITStart->getPlayerName());
ptrSPITPlayer->setIsRunning(p_PlaySPITStart->getPlayerIsRunning()); spitPlayer.setType(p_PlaySPITStart->getPlayerType());
ptrSPITPlayer->setName(p_PlaySPITStart->getPlayerName()); spitPlayer.setTime(p_PlaySPITStart->getTime());
ptrSPITPlayer->setType(p_PlaySPITStart->getPlayerType()); spitPlayer.startPlaySequence();
ptrSPITPlayer->setTime(p_PlaySPITStart->getTime());
ptrSPITPlayer->startPlaySequence();
mCallbacks.onPlay(ptrSPITPlayer->getID(), ptrSPITPlayer->getName(), ptrSPITPlayer->getType(), ptrSPITPlayer->getIsRunning(), ptrSPITPlayer->getTime()); mCallbacks.onPlay(spitPlayer.getID(), spitPlayer.getName(), spitPlayer.getType(), spitPlayer.getIsRunning(), spitPlayer.getTime());
} }
void SPIT_Client::parsePlaySPITObject(SPIT_Message::PlaySPITObject *p_PlaySPITObject) { void SPIT_Client::parsePlaySPITObject(SPIT_Message::PlaySPITObject *p_PlaySPITObject) {
SPITPlayer *ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITObject->getPlayerID());
if (ptrSPITPlayer == nullptr)
return;
SPITPlayer &ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITObject->getPlayerID());
SPITObject *ptrSPITObject = SPITObject::getSPITObject(p_PlaySPITObject->getSPITObjectID()); SPITObject *ptrSPITObject = SPITObject::getSPITObject(p_PlaySPITObject->getSPITObjectID());
if (ptrSPITObject == nullptr) { if (ptrSPITObject == nullptr) {
return; return;
} }
SPITPlayer::PlaySPITType *ptrPlaySPITType = ptrSPITPlayer->addSPITType_Playing(ptrSPITObject->getSPITTypeID()); SPITPlayer::PlaySPITType *ptrPlaySPITType = ptrSPITPlayer.addSPITType_Playing(ptrSPITObject->getSPITTypeID());
if (ptrPlaySPITType == nullptr) { if (ptrPlaySPITType == nullptr) {
return; return;
} }
ptrPlaySPITType->setPlayerIsRunning(ptrSPITPlayer->getIsRunning()); ptrPlaySPITType->setPlayerIsRunning(ptrSPITPlayer.getIsRunning());
ptrPlaySPITType->addValue(((double)p_PlaySPITObject->getValue() / 1000000), ((double)ptrSPITObject->getValueFactor() / 1000000)); ptrPlaySPITType->addValue(((double)p_PlaySPITObject->getValue() / 1000000), ((double)ptrSPITObject->getValueFactor() / 1000000));
} }
void SPIT_Client::parsePlaySPITEnd(SPIT_Message::PlaySPITEnd *p_PlaySPITEnd) {
SPITPlayer *ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITEnd->getPlayerID());
if (ptrSPITPlayer == nullptr)
return;
void SPIT_Client::parsePlaySPITEnd(SPIT_Message::PlaySPITEnd *p_PlaySPITEnd) {
SPITPlayer &ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITEnd->getPlayerID());
// unordered_map<string, SPITPlayer::PlaySPITType*>* ptrPlayObjects; // unordered_map<string, SPITPlayer::PlaySPITType*>* ptrPlayObjects;
// bool boolStarted; // bool boolStarted;
// bool boolStopped; // bool boolStopped;
@@ -1358,59 +1312,46 @@ void SPIT_Client::parsePlaySPITEnd(SPIT_Message::PlaySPITEnd *p_PlaySPITEnd) {
// mCallbacks.onPlaySpitType(ptrPlayObject->getPlayerID(), boolStarted, boolStopped, *ptrSPITType, *ptrSPITObject, *ptrPlayObject); // mCallbacks.onPlaySpitType(ptrPlayObject->getPlayerID(), boolStarted, boolStopped, *ptrSPITType, *ptrSPITObject, *ptrPlayObject);
// } // }
// } // }
// ptrSPITPlayer->endPlaySequence(); // ptrSPITPlayer.endPlaySequence();
// SPITPlayer *ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITEnd->getPlayerID()); // SPITPlayer &ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITEnd->getPlayerID());
// if (ptrSPITPlayer == nullptr)
// return;
unordered_map<string, SPITPlayer::PlaySPITType *> *ptrPlaySPITTypes = ptrSPITPlayer->getPlaySPITTypes(); unordered_map<string, SPITPlayer::PlaySPITType *> *ptrPlaySPITTypes = ptrSPITPlayer.getPlaySPITTypes();
for (auto i = ptrPlaySPITTypes->begin(); i != ptrPlaySPITTypes->end(); i++) { for (auto i = ptrPlaySPITTypes->begin(); i != ptrPlaySPITTypes->end(); i++) {
SPITPlayer::PlaySPITType *ptrPlaySPITType = i->second; SPITPlayer::PlaySPITType *ptrPlaySPITType = i->second;
bool boolStarted = false; bool boolStarted = ptrPlaySPITType->getIsNew();
bool boolStopped = false; bool boolStopped = (ptrPlaySPITType->getIsPlayed() == false);
if (ptrPlaySPITType->getIsNew()) { SPITType *ptrSPITType = SPITType::getSPITType(ptrPlaySPITType->getSPITTypeID());
boolStarted = true; if (ptrSPITType == nullptr) {
} // set isplayed to false, the ptrPlayObject will be removed by ptrSPITPlayer.endPlaySequence()
if (ptrPlaySPITType->getIsPlayed() == false) { ptrPlaySPITType->setIsPlayed(false);
boolStopped = true; continue;
}
{
SPITType *ptrSPITType = SPITType::getSPITType(ptrPlaySPITType->getSPITTypeID());
if (ptrSPITType == nullptr) {
// set isplayed to false, the ptrPlayObject will be removed by ptrSPITPlayer->endPlaySequence()
ptrPlaySPITType->setIsPlayed(false);
continue;
}
mCallbacks.onPlaySpitType(ptrPlaySPITType->getPlayerID(), ptrPlaySPITType->getSPITTypeID(), ptrPlaySPITType->getPlayerIsRunning(), boolStarted, boolStopped, ptrPlaySPITType->getValue());
} }
mCallbacks.onPlaySpitType(ptrPlaySPITType->getPlayerID(), ptrPlaySPITType->getSPITTypeID(), ptrPlaySPITType->getPlayerIsRunning(), boolStarted, boolStopped, ptrPlaySPITType->getValue());
} }
ptrSPITPlayer->endPlaySequence(); ptrSPITPlayer.endPlaySequence();
} }
void SPIT_Client::parseReportPlayStatus(SPIT_Message::ReportPlayStatus *p_ReportPlayStatus) { void SPIT_Client::parseReportPlayStatus(SPIT_Message::ReportPlayStatus *p_ReportPlayStatus) {
ESP_LOGD(TAG, "parseReportPlayStatus");
SPITType *ptrSPITType = NULL; SPITType *ptrSPITType = NULL;
{
SPITObject *ptrSPITObject = SPITObject::getSPITObjectRemote(p_ReportPlayStatus->getID()); SPITObject *ptrSPITObject = SPITObject::getSPITObjectRemote(p_ReportPlayStatus->getID());
if (ptrSPITObject == nullptr) {
ptrSPITObject = SPITObject::getSPITObject(p_ReportPlayStatus->getID());
if (ptrSPITObject == nullptr) { if (ptrSPITObject == nullptr) {
ptrSPITObject = SPITObject::getSPITObject(p_ReportPlayStatus->getID());
if (ptrSPITObject == nullptr) {
return;
}
}
if (ptrSPITObject->getServerRemote() == false) {
ptrSPITType = SPITType::getSPITType(ptrSPITObject->getSPITTypeID());
} else {
ptrSPITType = SPITType::getSPITTypeRemote(ptrSPITObject->getSPITTypeID());
}
if (ptrSPITType == nullptr) {
return; return;
} }
mCallbacks.onPlayRemoteObjectStatus(*ptrSPITType, *ptrSPITObject, p_ReportPlayStatus->getFrameInsideObject(), p_ReportPlayStatus->getValue());
} }
if (ptrSPITObject->getServerRemote() == false) {
ptrSPITType = SPITType::getSPITType(ptrSPITObject->getSPITTypeID());
} else {
ptrSPITType = SPITType::getSPITTypeRemote(ptrSPITObject->getSPITTypeID());
}
if (ptrSPITType == nullptr) {
return;
}
mCallbacks.onPlayRemoteObjectStatus(*ptrSPITType, *ptrSPITObject, p_ReportPlayStatus->getFrameInsideObject(), p_ReportPlayStatus->getValue());
} }
//--------- Remote ---------- //--------- Remote ----------
@@ -1517,15 +1458,15 @@ void SPIT_Client::processSocketTask(SPIT_Client &instance) {
strParts[intIndex] = stringPart; strParts[intIndex] = stringPart;
intIndex++; intIndex++;
} }
auto server = make_unique<SAD_DetectedServer> ( auto server = make_unique<SAD_DetectedServer>(
strParts[SAD_DetectedServer::PARAMINDEX_NAME], strParts[SAD_DetectedServer::PARAMINDEX_NAME],
strParts[SAD_DetectedServer::PARAMINDEX_ID], strParts[SAD_DetectedServer::PARAMINDEX_ID],
strParts[SAD_DetectedServer::PARAMINDEX_COMPUTERNAME], strParts[SAD_DetectedServer::PARAMINDEX_COMPUTERNAME],
stringServerIP, stringServerIP,
strParts[SAD_DetectedServer::PARAMINDEX_PORT], strParts[SAD_DetectedServer::PARAMINDEX_PORT],
strParts[SAD_DetectedServer::PARAMINDEX_CLIENTIP], strParts[SAD_DetectedServer::PARAMINDEX_CLIENTIP],
strParts[SAD_DetectedServer::PARAMINDEX_ADDITIONS], strParts[SAD_DetectedServer::PARAMINDEX_ADDITIONS],
strParts[SAD_DetectedServer::PARAMINDEX_SERVERPROJECTNAME]); strParts[SAD_DetectedServer::PARAMINDEX_SERVERPROJECTNAME]);
SAD_DetectedServer::addDetectedServer(move(server)); SAD_DetectedServer::addDetectedServer(move(server));
+80 -94
View File
@@ -733,13 +733,13 @@ void onProjectUpdate(SPIT_Client::ProjUpdate state, string projectId) {
bool onCheckSpitTypeVeto(SPIT_Message::ReportSPITType *p_ReportSPITType) { bool onCheckSpitTypeVeto(SPIT_Message::ReportSPITType *p_ReportSPITType) {
return true; // Bypass veto return true; // Bypass veto
if (p_ReportSPITType == NULL) if (p_ReportSPITType == nullptr)
return true; return true;
SPITType *ptrSPITType; SPITType *ptrSPITType;
ptrSPITType = SPITType::getSPITType(p_ReportSPITType->getID()); ptrSPITType = SPITType::getSPITType(p_ReportSPITType->getID());
// Don't allow foreign SPITTypes // Don't allow foreign SPITTypes
if (ptrSPITType == NULL) { if (ptrSPITType == nullptr) {
// ESP_LOGW(TAG, "ObjectVeto NoSpitType"); // ESP_LOGW(TAG, "ObjectVeto NoSpitType");
return false; return false;
} }
@@ -753,10 +753,10 @@ bool onCheckSpitTypeVeto(SPIT_Message::ReportSPITType *p_ReportSPITType) {
bool onCheckSpitObjectVeto(SPIT_Message::ReportSPITObject *p_ReportSPITObject) { bool onCheckSpitObjectVeto(SPIT_Message::ReportSPITObject *p_ReportSPITObject) {
return true; // Bypass veto return true; // Bypass veto
if (p_ReportSPITObject == NULL) if (p_ReportSPITObject == nullptr)
return true; return true;
SPITType *ptrSPITType = SPITType::getSPITType(p_ReportSPITObject->getSPITTypeID()); SPITType *ptrSPITType = SPITType::getSPITType(p_ReportSPITObject->getSPITTypeID());
if (ptrSPITType == NULL) { if (ptrSPITType == nullptr) {
// ESP_LOGW(TAG, "ObjectVeto NoSpitObj"); // ESP_LOGW(TAG, "ObjectVeto NoSpitObj");
return false; return false;
} }
@@ -768,7 +768,7 @@ bool onCheckSpitObjectVeto(SPIT_Message::ReportSPITObject *p_ReportSPITObject) {
} }
SPITObject *ptrSPITObject = SPITObject::getSPITObject(p_ReportSPITObject->getID()); SPITObject *ptrSPITObject = SPITObject::getSPITObject(p_ReportSPITObject->getID());
// Don't allow foreign SPITTypes // Don't allow foreign SPITTypes
if (ptrSPITObject == NULL) { if (ptrSPITObject == nullptr) {
// ESP_LOGE(TAG, "Unknown ID"); // ESP_LOGE(TAG, "Unknown ID");
return false; return false;
} }
@@ -776,102 +776,90 @@ bool onCheckSpitObjectVeto(SPIT_Message::ReportSPITObject *p_ReportSPITObject) {
return true; return true;
} }
// Called when the server has created, loaded, changed or deleted a SPITType. // Called when the server has created, loaded, changed or deleted a (Remote) SPITType.
// Remote SPITType are used in Remote SPITObjects. A Remote SPITObject is used to remote control the SPITServer.
// May also occur when the server confirms a SPITType created or changed by the client. // May also occur when the server confirms a SPITType created or changed by the client.
void onSpitType(SPIT_Client::ObjectEvent event, const SPITType &p_SPITType) { void onSpitType(SPIT_Client::ObjectEvent event, bool isRemote, const SPITType &p_SPITType) {
if (event == SPIT_Client::ObjectEvent::Created) { if (isRemote) {
// Whenever you provide an action for this SPITType take this over in your code. if (event == SPIT_Client::ObjectEvent::Created) {
// call _SpitClient.setActionSPITType(p_SPITType.getID(), true); ESP_LOGI(TAG, "NEW SPIT-REMOTE-Type ID: %s GroupName: %s Name: %s confirmed: %d", p_SPITType.getID().data(), p_SPITType.getGroupName().data(), p_SPITType.getName().data(), p_SPITType.getConfirmed());
ESP_LOGI(TAG, "NEW SPIT-Type ID: %s GroupName: %s Name: %s confirmed: %d", p_SPITType.getID().data(), p_SPITType.getGroupName().data(), p_SPITType.getName().data(), p_SPITType.getConfirmed()); auto typeName = p_SPITType.getName();
} else if (event == SPIT_Client::ObjectEvent::Changed) { if (ExpectedObjects.contains(typeName)) {
ESP_LOGI(TAG, "SPIT-Type CHANGED ID: %s GroupName: %s Name: %s confirmed: %d", p_SPITType.getID().data(), p_SPITType.getGroupName().data(), p_SPITType.getName().data(), p_SPITType.getConfirmed()); ExpectedObjects[typeName] = p_SPITType.getID();
} else if (event == SPIT_Client::ObjectEvent::Deleted) {
ESP_LOGI(TAG, "SPIT-Type Removed ID: %s GroupName: %s Name: %s\n", p_SPITType.getID().data(), p_SPITType.getGroupName().data(), p_SPITType.getName().data());
}
}
/**
* Called when the server has created or changed a Remote SPITType.
* Remote SPITType are used in Remote SPITObjects. A Remote SPITObject is used to remote control the SPITServer.
* @param p_New true = server created a new SPITType / false = server changed a existing SPITType
*/
void onSpitTypeRemote(SPIT_Client::ObjectEvent event, const SPITType &p_SPITType) {
if (event == SPIT_Client::ObjectEvent::Created) {
ESP_LOGI(TAG, "NEW SPIT-REMOTE-Type ID: %s GroupName: %s Name: %s confirmed: %d", p_SPITType.getID().data(), p_SPITType.getGroupName().data(), p_SPITType.getName().data(), p_SPITType.getConfirmed());
auto typeName = p_SPITType.getName();
if (ExpectedObjects.contains(typeName)) {
ExpectedObjects[typeName] = p_SPITType.getID();
}
} else if (event == SPIT_Client::ObjectEvent::Changed) {
// we had to inform all Remote SPITObjects using the p_SPITType
// perhaps the SPITObject uses information from the SPITType
SPITObject *ptrSPITObject;
int intSPITObjectCount;
intSPITObjectCount = SPITObject::getSPITObjectRemoteCount();
for (int intObject = 0; intObject < intSPITObjectCount; intObject++) {
ptrSPITObject = SPITObject::getSPITObjectRemote(intObject);
if (ptrSPITObject == NULL)
continue;
if (ptrSPITObject->getSPITTypeID() == p_SPITType.getID()) {
// call ecentSPITObjectRemote
// onSpitObjectRemote(SPIT_Client::ObjectEvent::Changed, p_SPITType, *ptrSPITObject);
} }
} else if (event == SPIT_Client::ObjectEvent::Changed) {
// we had to inform all Remote SPITObjects using the p_SPITType
// perhaps the SPITObject uses information from the SPITType
SPITObject *ptrSPITObject;
int intSPITObjectCount;
intSPITObjectCount = SPITObject::getSPITObjectRemoteCount();
for (int intObject = 0; intObject < intSPITObjectCount; intObject++) {
ptrSPITObject = SPITObject::getSPITObjectRemote(intObject);
if (ptrSPITObject == nullptr)
continue;
if (ptrSPITObject->getSPITTypeID() == p_SPITType.getID()) {
// call ecentSPITObjectRemote
// onSpitObjectRemote(SPIT_Client::ObjectEvent::Changed, p_SPITType, *ptrSPITObject);
}
}
ESP_LOGI(TAG, "SPIT-REMOTE-Type CHANGED ID: %s GroupName: %s Name: %s confirmed: %d", p_SPITType.getID().data(), p_SPITType.getGroupName().data(), p_SPITType.getName().data(), p_SPITType.getConfirmed());
} else if (event == SPIT_Client::ObjectEvent::Deleted) {
ESP_LOGD(TAG, "SPIT-REMOTE-Type Removed ID: %s GroupName: %s Name: %s\n", p_SPITType.getID().data(), p_SPITType.getGroupName().data(), p_SPITType.getName().data());
}
} else {
if (event == SPIT_Client::ObjectEvent::Created) {
// Whenever you provide an action for this SPITType take this over in your code.
// call _SpitClient.setActionSPITType(p_SPITType.getID(), true);
ESP_LOGI(TAG, "NEW SPIT-Type ID: %s GroupName: %s Name: %s confirmed: %d", p_SPITType.getID().data(), p_SPITType.getGroupName().data(), p_SPITType.getName().data(), p_SPITType.getConfirmed());
} else if (event == SPIT_Client::ObjectEvent::Changed) {
ESP_LOGI(TAG, "SPIT-Type CHANGED ID: %s GroupName: %s Name: %s confirmed: %d", p_SPITType.getID().data(), p_SPITType.getGroupName().data(), p_SPITType.getName().data(), p_SPITType.getConfirmed());
} else if (event == SPIT_Client::ObjectEvent::Deleted) {
ESP_LOGI(TAG, "SPIT-Type Removed ID: %s GroupName: %s Name: %s\n", p_SPITType.getID().data(), p_SPITType.getGroupName().data(), p_SPITType.getName().data());
} }
ESP_LOGI(TAG, "SPIT-REMOTE-Type CHANGED ID: %s GroupName: %s Name: %s confirmed: %d", p_SPITType.getID().data(), p_SPITType.getGroupName().data(), p_SPITType.getName().data(), p_SPITType.getConfirmed());
} else if (event == SPIT_Client::ObjectEvent::Deleted) {
ESP_LOGD(TAG, "SPIT-REMOTE-Type Removed ID: %s GroupName: %s Name: %s\n", p_SPITType.getID().data(), p_SPITType.getGroupName().data(), p_SPITType.getName().data());
} }
} }
/** // Called when the server has created, loaded or changed a SPITObject.
* Called when the server has created, loaded or changed a SPITObject. // A Remote SPITObject is used to remote control the SPITServer.
* May also occur when the server confirms a SPITObject created by the client, what the client should never do!!. // May also occur when the server confirms a SPITObject created by the client, what the client should never do!!.
* false = server changed a existing SPITObject // false = server changed a existing SPITObject
* @param p_SPITType a reference to a copy of the associated SPITType instance. // @param p_SPITType a reference to a copy of the associated SPITType instance.
* @param p_SPITObject a reference to a copy of the SPITObject instance. // @param p_SPITObject a reference to a copy of the SPITObject instance.
*/ void onSpitObject(SPIT_Client::ObjectEvent event, bool isRemote, const SPITType &p_SPITType, const SPITObject &p_SPITObject) {
void onSpitObject(SPIT_Client::ObjectEvent event, const SPITType &p_SPITType, const SPITObject &p_SPITObject) { if (isRemote) {
if (event == SPIT_Client::ObjectEvent::Created) { if (event == SPIT_Client::ObjectEvent::Created) {
ESP_LOGI(TAG, "NEW SPIT-Object ID: %s Name: %s -> SPITType: %s", p_SPITObject.getID().data(), p_SPITObject.getName().data(), p_SPITType.getName().data()); ESP_LOGI(TAG, "NEW SPIT-REMOTE-Object ID: %s Name: %s -> SPITType: %s", p_SPITObject.getID().data(), p_SPITObject.getName().data(), p_SPITType.getName().data());
// check your actions // This is a remote SPITObject which can be used to remote control the server.
// Whenever you provide an action for this SPITObject take this over in your code. // If you want you can list this for later use
// call _SpitClient.setActionSPITObject(p_SPITObject.getID(), true); } else if (event == SPIT_Client::ObjectEvent::Changed) {
ESP_LOGI(TAG, "SPIT-REMOTE-Object CHANGED ID: %s Name: %s -> SPITType: %s", p_SPITObject.getID().data(), p_SPITObject.getName().data(), p_SPITType.getName().data());
} else if (event == SPIT_Client::ObjectEvent::Deleted) {
ESP_LOGD(TAG, "SPIT-REMOTE-Object Removed ID: %s Name: %s\n", p_SPITObject.getID().data(), p_SPITObject.getName().data());
// This is a remote SPITObject
// If you have listet this prior you should remove it from the list
}
} else {
if (event == SPIT_Client::ObjectEvent::Created) {
ESP_LOGI(TAG, "NEW SPIT-Object ID: %s Name: %s -> SPITType: %s", p_SPITObject.getID().data(), p_SPITObject.getName().data(), p_SPITType.getName().data());
// check your actions
// Whenever you provide an action for this SPITObject take this over in your code.
// call _SpitClient.setActionSPITObject(p_SPITObject.getID(), true);
// for our little excample as described in loadClientProject: // for our little excample as described in loadClientProject:
// We have only actions for SPITTypes - means every SPITObject with the associated "Actionx"-SPITTypes will trigger the action when it is played // We have only actions for SPITTypes - means every SPITObject with the associated "Actionx"-SPITTypes will trigger the action when it is played
//-> no need to do something here, because setActionSPITType has been set prior and the SPITObject will automatically inherits this //-> no need to do something here, because setActionSPITType has been set prior and the SPITObject will automatically inherits this
// SPITObjects with other SPITTypes asscciated will trigger no actions // SPITObjects with other SPITTypes asscciated will trigger no actions
} else if (event == SPIT_Client::ObjectEvent::Changed) { } else if (event == SPIT_Client::ObjectEvent::Changed) {
ESP_LOGI(TAG, "SPIT-Object CHANGED ID: %s Name: %s -> SPITType: %s", p_SPITObject.getID().data(), p_SPITObject.getName().data(), p_SPITType.getName().data()); ESP_LOGI(TAG, "SPIT-Object CHANGED ID: %s Name: %s -> SPITType: %s", p_SPITObject.getID().data(), p_SPITObject.getName().data(), p_SPITType.getName().data());
} else if (event == SPIT_Client::ObjectEvent::Deleted) { } else if (event == SPIT_Client::ObjectEvent::Deleted) {
ESP_LOGD(TAG, "SPIT-Object Removed ID: %s Name: %s\n", p_SPITObject.getID().data(), p_SPITObject.getName().data()); ESP_LOGD(TAG, "SPIT-Object Removed ID: %s Name: %s\n", p_SPITObject.getID().data(), p_SPITObject.getName().data());
// Check your actions // Check your actions
// take the removal of the SPITObject over in your code // take the removal of the SPITObject over in your code
// CAUTION: Deleted SPITObjects can be added later again (perhaps per UNDO on the SPITServer) // CAUTION: Deleted SPITObjects can be added later again (perhaps per UNDO on the SPITServer)
//-> don't remove your assigned action immediately - keep it to reassign it later //-> don't remove your assigned action immediately - keep it to reassign it later
} }
}
/**
* Called when the server has created or changed a Remote SPITObject.
* A Remote SPITObject is used to remote control the SPITServer.
* @param p_New true = server created a new remote SPITObject
* false = server changed a existing remote SPITObject
* @param p_SPITType a reference to a copy of the associated SPITType instance.
* @param p_SPITObject a reference to a copy of the SPITObject instance.
*/
void onSpitObjectRemote(SPIT_Client::ObjectEvent event, const SPITType &p_SPITType, const SPITObject &p_SPITObject) {
if (event == SPIT_Client::ObjectEvent::Created) {
ESP_LOGI(TAG, "NEW SPIT-REMOTE-Object ID: %s Name: %s -> SPITType: %s", p_SPITObject.getID().data(), p_SPITObject.getName().data(), p_SPITType.getName().data());
// This is a remote SPITObject which can be used to remote control the server.
// If you want you can list this for later use
} else if (event == SPIT_Client::ObjectEvent::Changed) {
ESP_LOGI(TAG, "SPIT-REMOTE-Object CHANGED ID: %s Name: %s -> SPITType: %s", p_SPITObject.getID().data(), p_SPITObject.getName().data(), p_SPITType.getName().data());
} else if (event == SPIT_Client::ObjectEvent::Deleted) {
ESP_LOGD(TAG, "SPIT-REMOTE-Object Removed ID: %s Name: %s\n", p_SPITObject.getID().data(), p_SPITObject.getName().data());
// This is a remote SPITObject
// If you have listet this prior you should remove it from the list
} }
} }
@@ -951,9 +939,7 @@ void SpitClientApp() {
.onConnectionStatus = onConnectionStatus, .onConnectionStatus = onConnectionStatus,
.onProjectUpdate = onProjectUpdate, .onProjectUpdate = onProjectUpdate,
.onSpitType = onSpitType, .onSpitType = onSpitType,
.onSpitTypeRemote = onSpitTypeRemote,
.onSpitObject = onSpitObject, .onSpitObject = onSpitObject,
.onSpitObjectRemote = onSpitObjectRemote,
.onPlay = onPlay, .onPlay = onPlay,
.onPlaySpitType = onPlaySpitType, .onPlaySpitType = onPlaySpitType,
.onPlayRemoteObjectStatus = onPlayRemoteObjectStatus, .onPlayRemoteObjectStatus = onPlayRemoteObjectStatus,