refactoring spit messages

This commit is contained in:
2025-04-04 16:30:22 +02:00
parent 74f1c31193
commit 16e7ba13f8
3 changed files with 467 additions and 597 deletions
+51 -61
View File
@@ -16,49 +16,46 @@
#include <cstdlib> #include <cstdlib>
#include <string> #include <string>
#include <optional>
#include <memory>
#include <vector>
using namespace std; using namespace std;
static const string SPITID = "#spit#"; static const string SPITID = "#spit#";
// static char stringEmpty[] = "";
static char nullChar[] = "\0";
class SPIT_Message { class SPIT_Message {
//----------- Header ----------- //----------- Header -----------
public: public:
class Header { class Header {
public: public:
static const int BYTESLENGTH = 99; static constexpr int BYTESLENGTH = 99;
virtual ~Header(); virtual ~Header();
Header(const Header &orig); Header(const Header &orig);
Header(); Header();
// Members // Members
private: private:
string m_SPITID; string m_SPITID;
int m_SPITVersion; int m_SPITVersion{SPITVERSION};
unsigned char m_MessageNumber; unsigned char m_MessageNumber{};
int m_BytesCount; int m_BytesCount{BYTESLENGTH};
string m_ServerID; string m_ServerID{};
string m_ClientID; string m_ClientID{};
int m_OPFlag; int m_OPFlag;
int m_ParseOffset = 0; int m_ParseOffset{};
void initMembers();
public: public:
const string getSPITID() const { return m_SPITID; } string getSPITID() const { return m_SPITID; }
int getSPITVersion() const { return m_SPITVersion; } int getSPITVersion() const { return m_SPITVersion; }
unsigned char getMessageNumber() const { return m_MessageNumber; } unsigned char getMessageNumber() const { return m_MessageNumber; }
int getBytesCount() const { return m_BytesCount; } int getBytesCount() const { return m_BytesCount; }
const string getServerID() const { return m_ServerID; } string getServerID() const { return m_ServerID; }
const string getClientID() const { return m_ClientID; } string getClientID() const { return m_ClientID; }
int getOPFlag() const { return m_OPFlag; } int getOPFlag() const { return m_OPFlag; }
// Receive // Receive
public:
bool parse(char *p_Bytes, int p_ByteCount); bool parse(char *p_Bytes, int p_ByteCount);
// Send // Send
public:
Header(int p_SPITVersion, int p_OPFlag); Header(int p_SPITVersion, int p_OPFlag);
void setMessageNumber(unsigned char p_MessageNumber); void setMessageNumber(unsigned char p_MessageNumber);
void setServerID(string p_ServerID); void setServerID(string p_ServerID);
@@ -86,7 +83,7 @@ class SPIT_Message {
Header *getHeader() const { return m_Header; } Header *getHeader() const { return m_Header; }
int getBytesCount() const { return m_BytesCount; } int getBytesCount() const { return m_BytesCount; }
const string getServerID() const { const string getServerID() const {
if (m_Header == NULL) if (m_Header == NULL)
return ""; return "";
return m_Header->getServerID(); return m_Header->getServerID();
@@ -135,25 +132,22 @@ class SPIT_Message {
static const char VALUEFLAG_INCREASE = 0x01; // increase the value static const char VALUEFLAG_INCREASE = 0x01; // increase the value
static const char VALUEFLAG_DECREASE = 0x02; // decrease the value static const char VALUEFLAG_DECREASE = 0x02; // decrease the value
SPIT_Message(); SPIT_Message() = default;
virtual ~SPIT_Message(); virtual ~SPIT_Message();
// SPIT_Message(const SPIT_Message& orig); // SPIT_Message(const SPIT_Message& orig);
// Members // Members
private: private:
char *m_MessageBytes; char *m_MessageBytes{};
int m_MessageByteCount; int m_MessageByteCount{};
Header *m_Header; Header *m_Header{};
OPPart *m_OPPart; OPPart * m_OPPart{};
bool m_MessageBytesCreated; bool m_MessageBytesCreated{};
void initMembers();
// Receive // Receive
public: public:
SPIT_Message(char *p_MessageBytes, int p_MessageBytesLength); SPIT_Message(char *msgBytes, int msgSize);
// SPIT_Message(const SPIT_Message& orig);
const Header *getHeader() const { return m_Header; } const Header *getHeader() const { return m_Header; }
int getOPFlag() const { int getOPFlag() const {
if (m_Header == NULL) if (m_Header == NULL)
@@ -213,7 +207,7 @@ class SPIT_Message {
void setLoginPassword(string p_LoginPassword); void setLoginPassword(string p_LoginPassword);
void setServerIP(string p_ServerIP); void setServerIP(string p_ServerIP);
void setServerPort(int p_ServerPort); void setServerPort(int p_ServerPort);
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------- ConnectAnswer ----------- //----------- ConnectAnswer -----------
@@ -251,7 +245,7 @@ class SPIT_Message {
void setServerName(string p_ContollerName); void setServerName(string p_ContollerName);
void setConnected(bool p_Connected); void setConnected(bool p_Connected);
void setStepTime(int p_StepTime_ms); void setStepTime(int p_StepTime_ms);
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------- Disconnect ----------- //----------- Disconnect -----------
@@ -274,7 +268,7 @@ class SPIT_Message {
// Send // Send
public: public:
Disconnect(); Disconnect();
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------- Watchdog ----------- //----------- Watchdog -----------
@@ -297,7 +291,7 @@ class SPIT_Message {
// Send // Send
public: public:
Watchdog(); Watchdog();
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------- WatchdogAnswer ----------- //----------- WatchdogAnswer -----------
@@ -319,7 +313,7 @@ class SPIT_Message {
// Send // Send
public: public:
WatchdogAnswer(); WatchdogAnswer();
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//------------------------------ SPITProject ---------------------------------- //------------------------------ SPITProject ----------------------------------
@@ -358,7 +352,7 @@ class SPIT_Message {
void setProjectID(string p_ClientID); void setProjectID(string p_ClientID);
void setProjectName(string p_ClientName); void setProjectName(string p_ClientName);
void setFramesPerSecond(const int p_FramesPerSecond); void setFramesPerSecond(const int p_FramesPerSecond);
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------- CloseSPITProject ----------- //----------- CloseSPITProject -----------
@@ -387,7 +381,7 @@ class SPIT_Message {
CloseSPITProject(); CloseSPITProject();
void setProjectID(string p_ClientID); void setProjectID(string p_ClientID);
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------- StartProjectTransfer ----------- //----------- StartProjectTransfer -----------
@@ -408,7 +402,7 @@ class SPIT_Message {
// Send // Send
public: public:
RequestProjectTransfer(); RequestProjectTransfer();
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------- StartProjectTransfer ----------- //----------- StartProjectTransfer -----------
@@ -430,7 +424,7 @@ class SPIT_Message {
// Send // Send
public: public:
StartProjectTransfer(); StartProjectTransfer();
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------- EndProjectTransfer ----------- //----------- EndProjectTransfer -----------
@@ -450,7 +444,7 @@ class SPIT_Message {
bool parse(char *p_Bytes, int p_ByteCount); bool parse(char *p_Bytes, int p_ByteCount);
// Send // Send
EndProjectTransfer(); EndProjectTransfer();
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//---------------------------- SPIT_Types ---------------------------------- //---------------------------- SPIT_Types ----------------------------------
@@ -522,7 +516,7 @@ class SPIT_Message {
void setDelay(long long p_Delay); void setDelay(long long p_Delay);
void setDuration(long long p_Duration); void setDuration(long long p_Duration);
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------- RemoveSPITType ----------- //----------- RemoveSPITType -----------
@@ -551,7 +545,7 @@ class SPIT_Message {
RemoveSPITType(); RemoveSPITType();
void setID(string p_ID); void setID(string p_ID);
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//---------------------------- SPITObjects ---------------------------------- //---------------------------- SPITObjects ----------------------------------
@@ -632,7 +626,7 @@ class SPIT_Message {
void setDelay(long long p_Delay); void setDelay(long long p_Delay);
void setDuration(long long p_Duration); void setDuration(long long p_Duration);
void setActionAssigned(bool p_ActionAssigned); void setActionAssigned(bool p_ActionAssigned);
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------- RemoveSPITObject ----------- //----------- RemoveSPITObject -----------
@@ -661,7 +655,7 @@ class SPIT_Message {
public: public:
RemoveSPITObject(); RemoveSPITObject();
void setID(string p_ID); void setID(string p_ID);
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//---------------------------- SPITPlayer ---------------------------------- //---------------------------- SPITPlayer ----------------------------------
@@ -704,7 +698,7 @@ class SPIT_Message {
void setPlayerType(char p_PlayerType); void setPlayerType(char p_PlayerType);
void setPlayerIsRunning(bool p_IsPlaying); void setPlayerIsRunning(bool p_IsPlaying);
void setTime(long long p_Time); void setTime(long long p_Time);
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------- PlaySPITObject ----------- //----------- PlaySPITObject -----------
@@ -746,7 +740,7 @@ class SPIT_Message {
void setPlayPrePosition(bool p_PlayOldPosition); void setPlayPrePosition(bool p_PlayOldPosition);
void setValue(int p_Value); void setValue(int p_Value);
void setValueFlag(char p_ValueFlag); void setValueFlag(char p_ValueFlag);
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------- PlaySPITEnd ----------- //----------- PlaySPITEnd -----------
@@ -772,7 +766,7 @@ class SPIT_Message {
public: public:
PlaySPITEnd(); PlaySPITEnd();
void setPlayerID(string p_PlayerID); void setPlayerID(string p_PlayerID);
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------- ReportPlayStatus ----------- //----------- ReportPlayStatus -----------
@@ -805,33 +799,29 @@ class SPIT_Message {
void setID(string p_ID); void setID(string p_ID);
void setFrameInsideObject(long long p_TimeInside); void setFrameInsideObject(long long p_TimeInside);
void setValue(int p_Value); void setValue(int p_Value);
bool fillBytes(char *p_MessageBytes, int p_ByteCount); bool fillBytes(char *msgBytes, int p_ByteCount);
}; };
//----------------------- static helper functions ---------------------------- //----------------------- static helper functions ----------------------------
public: public:
static bool isSystemLittleEndian(); static bool isSystemLittleEndian();
static int getInt(char *p_Bytes, int p_ByteCount, int p_Offset); static optional<int> getInt(char *bytes, int size, int& offset);
static int setInt(int p_Value, char *p_Bytes, int p_ByteCount, int p_Offset); static optional<long long> getLong(char *bytes, int size, int &offset);
static long long getLong(char *p_Bytes, int p_ByteCount, int p_Offset); static optional<bool> getBoolean(char *bytes, int size, int& offset);
static int setLong(long long p_Value, char *p_Bytes, int p_ByteCount, int p_Offset); static optional<char> getByte(char *bytes, int size, int& offset);
static bool getBoolean(char *p_Bytes, int p_ByteCount, int p_Offset); static int setInt(int value, char *bytes, int size, int offset);
static int setBoolean(bool p_Value, char *p_Bytes, int p_ByteCount, int p_Offset); static int setLong(long long value, char *bytes, int size, int offset);
static char getByte(char *p_Bytes, int p_ByteCount, int p_Offset); static int setBoolean(bool value, char *bytes, int size, int offset);
static int setByte(int p_Value, char *p_Bytes, int p_ByteCount, int p_Offset); static int setByte(int value, char *bytes, int size, int offset);
/** /**
* Returns the string char* of the part of the byte sequence (0x00 null terminated). * Returns the string char* of the part of the byte sequence (0x00 null terminated).
* @param p_Bytes the byte array * @param p_Bytes the byte array
* @param p_Offset the startposition of strings bytes * @param offset the startposition of strings bytes
* @param p_Length the length of the string included the null terminate byte (0x00).<br> * @param p_Length the length of the string included the null terminate byte (0x00).<br>
* The length of the string will be p_Length -1. * The length of the string will be p_Length -1.
* @return the String char*
* @throws Exception if p_Bytes == null<br>
* p_Offset less than 0<br>
* p_Bytes.length less than p_Offset + p_Length<br>
* No 0x00 null terminate byte is found
*/ */
static string getString(char *p_Bytes, int p_ByteCount, int p_Offset, int p_Length); static optional<string> getString(char *bytes, int size, int &offset, int maxLength);
/** /**
* Fill the p_String in the byte array.<br> * Fill the p_String in the byte array.<br>
@@ -856,6 +846,6 @@ class SPIT_Message {
*/ */
// static bool stringEqual(const char* p_String1, const char* p_String2); // static bool stringEqual(const char* p_String1, const char* p_String2);
// static char* stringCopy(const char* p_String); // static char* stringCopy(const char* p_String);
static char *createByteArray(const OPPart *p_OPPart, int *p_BytesCount); static char *createByteArray(const OPPart &p_OPPart, int *p_BytesCount);
}; };
#endif /* SPIT_MESSAGE_H */ #endif /* SPIT_MESSAGE_H */
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -927,7 +927,7 @@ void onPlaySpitType(string p_PlayerID, string spitTypeId, bool p_PlayerIsRunning
* @param p_Value the new value * @param p_Value the new value
*/ */
void onPlayRemoteObjectStatus(const SPITType &p_SPITType, const SPITObject &p_SPITObject, long long p_FrameInsideObject, int p_Value) { void onPlayRemoteObjectStatus(const SPITType &p_SPITType, const SPITObject &p_SPITObject, long long p_FrameInsideObject, int p_Value) {
ESP_EARLY_LOGI(TAG, "eventPlayRemoteObjectStatus RemoteSPITType: %s RemoteSPITObject: %s Position: %lld Value: %d", p_SPITType.getName().data(), p_SPITObject.getName().data(), p_FrameInsideObject, p_Value); ESP_EARLY_LOGD(TAG, "eventPlayRemoteObjectStatus RemoteSPITType: %s RemoteSPITObject: %s Position: %lld Value: %d", p_SPITType.getName().data(), p_SPITObject.getName().data(), p_FrameInsideObject, p_Value);
} }
void SpitClientApp() { void SpitClientApp() {