From ea84904c22297fd55c4b33bbb4648422258c7261 Mon Sep 17 00:00:00 2001 From: Ake Hedman Date: Thu, 8 Feb 2024 17:16:21 +0100 Subject: [PATCH] Added methods to help edit event objects --- src/vscp/common/mdf.cpp | 220 ++++++++++++++++++++++++++++++++++++++-- src/vscp/common/mdf.h | 98 ++++++++++++++++-- 2 files changed, 298 insertions(+), 20 deletions(-) diff --git a/src/vscp/common/mdf.cpp b/src/vscp/common/mdf.cpp index a5513e866..ca4f796ad 100644 --- a/src/vscp/common/mdf.cpp +++ b/src/vscp/common/mdf.cpp @@ -746,7 +746,7 @@ CMDF_Action::addActionParam(CMDF_ActionParameter *pactionparam) // deleteActionParam // -bool +bool CMDF_Action::deleteActionParam(CMDF_ActionParameter *pactionparam) { if (nullptr == pactionparam) { @@ -754,9 +754,7 @@ CMDF_Action::deleteActionParam(CMDF_ActionParameter *pactionparam) } std::deque::iterator it; - for (auto it = m_list_ActionParameter.begin(); - it != m_list_ActionParameter.end(); - ++it) { + for (auto it = m_list_ActionParameter.begin(); it != m_list_ActionParameter.end(); ++it) { if (pactionparam->getOffset() == (*it)->getOffset()) { m_list_ActionParameter.erase(it); delete pactionparam; @@ -767,12 +765,10 @@ CMDF_Action::deleteActionParam(CMDF_ActionParameter *pactionparam) return false; } -bool +bool CMDF_Action::deleteActionParam(uint8_t offset) { - for (auto it = m_list_ActionParameter.begin(); - it != m_list_ActionParameter.end(); - ++it) { + for (auto it = m_list_ActionParameter.begin(); it != m_list_ActionParameter.end(); ++it) { if (offset == (*it)->getOffset()) { m_list_ActionParameter.erase(it); delete *it; @@ -815,6 +811,16 @@ CMDF_DecisionMatrix::clearStorage() m_rowCount = 0; m_rowSize = 8; + // Cleanup action list + std::deque::iterator iterAction; + for (iterAction = m_list_action.begin(); iterAction != m_list_action.end(); ++iterAction) { + CMDF_Action *pRecordAction = *iterAction; + if (nullptr != pRecordAction) { + delete pRecordAction; + pRecordAction = nullptr; + } + } + m_list_action.clear(); } @@ -836,6 +842,66 @@ CMDF_DecisionMatrix::getAction(uint16_t code) return nullptr; } +/////////////////////////////////////////////////////////////////////////////// +// addAction +// + +bool +CMDF_DecisionMatrix::addAction(CMDF_Action *paction) +{ + // Check pointer + if (nullptr == paction) { + return false; + } + + // Offset must be unique + if (nullptr != getAction(paction->getCode())) { + return false; + } + + m_list_action.push_back(paction); + return true; +} + +/////////////////////////////////////////////////////////////////////////////// +// deleteAction +// + +bool +CMDF_DecisionMatrix::deleteAction(CMDF_Action *paction) +{ + if (nullptr == paction) { + return false; + } + + std::deque::iterator it; + for (auto it = m_list_action.begin(); it != m_list_action.end(); ++it) { + if (paction->getCode() == (*it)->getCode()) { + m_list_action.erase(it); + delete paction; + return true; + } + } + + return false; +} + +bool +CMDF_DecisionMatrix::deleteAction(uint8_t code) +{ + for (auto it = m_list_action.begin(); it != m_list_action.end(); ++it) { + if (code == (*it)->getCode()) { + m_list_action.erase(it); + delete *it; + return true; + } + } + + return false; +} + +//----------------------------------------------------------------------------- + /////////////////////////////////////////////////////////////////////////////// // Constructor/Destructor // @@ -935,6 +1001,97 @@ CMDF_Event::clearStorage() m_mapInfoURL.clear(); } +/////////////////////////////////////////////////////////////////////////////// +// getEventData +// + +CMDF_EventData * +CMDF_Event::getEventData(uint8_t offset) +{ + for (auto it = m_list_eventdata.cbegin(); it != m_list_eventdata.cend(); ++it) { + if (offset == (*it)->getOffset()) { + return *it; + } + } + + return nullptr; +} + +/////////////////////////////////////////////////////////////////////////////// +// addEventData +// + +bool +CMDF_Event::addEventData(CMDF_EventData *pEventData) +{ + if (nullptr != pEventData) { + m_list_eventdata.push_back(pEventData); + return true; + } + else { + return false; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// deleteEventData +// + +bool +CMDF_Event::deleteEventData(CMDF_EventData *pEventData) +{ + // Check pointer + if (nullptr == pEventData) { + return false; + } + + for (auto it = m_list_eventdata.cbegin(); it != m_list_eventdata.cend(); ++it) { + if (pEventData == *it) { + m_list_eventdata.erase(it); + delete pEventData; + return true; + } + } + + return false; +} + +/////////////////////////////////////////////////////////////////////////////// +// isEventDataOffsetUnique +// + +bool +CMDF_Event::isEventDataOffsetUnique(uint8_t offset) +{ + if (nullptr == getEventData(offset)) { + return true; + } + + return false; +} + +bool +CMDF_Event::isEventDataOffsetUnique(CMDF_EventData *pEventData) +{ + // Check pointer + if (nullptr == pEventData) { + return false; + } + + for (auto it = m_list_eventdata.cbegin(); it != m_list_eventdata.cend(); ++it) { + // We don't check ourself + if (pEventData == *it) { + continue; + } + + if ( pEventData->getOffset() == (*it)->getOffset()) { + return false; + } + } + + return true; +} + /////////////////////////////////////////////////////////////////////////////// // Constructor/Destructor // @@ -7876,6 +8033,7 @@ CMDF::deleteRegister(CMDF_Register *preg) for (auto it = m_list_register.cbegin(); it != m_list_register.cend(); ++it) { if (preg == *it) { m_list_register.erase(it); + delete preg; return true; } } @@ -7888,16 +8046,56 @@ CMDF::deleteRegister(CMDF_Register *preg) // bool -CMDF::deleteRemoteVariable(CMDF_RemoteVariable *pvar) +CMDF::deleteRemoteVariable(CMDF_RemoteVariable *prvar) { // Check pointer - if (nullptr == pvar) { + if (nullptr == prvar) { return false; } for (auto it = m_list_remotevar.cbegin(); it != m_list_remotevar.cend(); ++it) { - if (pvar == *it) { + if (prvar == *it) { m_list_remotevar.erase(it); + delete prvar; + return true; + } + } + + return false; +} + +/////////////////////////////////////////////////////////////////////////////// +// addEvent +// + +bool +CMDF::addEvent(CMDF_Event *pEvent) +{ + if (nullptr != pEvent) { + m_list_event.push_back(pEvent); + return true; + } + else { + return false; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// deleteEvent +// + +bool +CMDF::deleteEvent(CMDF_Event *pEvent) +{ + // Check pointer + if (nullptr == pEvent) { + return false; + } + + for (auto it = m_list_event.cbegin(); it != m_list_event.cend(); ++it) { + if (pEvent == *it) { + m_list_event.erase(it); + delete pEvent; return true; } } diff --git a/src/vscp/common/mdf.h b/src/vscp/common/mdf.h index 88bb1e831..7f9f95408 100644 --- a/src/vscp/common/mdf.h +++ b/src/vscp/common/mdf.h @@ -127,8 +127,10 @@ typedef enum mdf_record_type { mdf_type_action_param_sub_item, mdf_type_event, mdf_type_event_item, + mdf_type_event_sub_item, mdf_type_event_data, mdf_type_event_data_item, + mdf_type_event_data_sub_item, mdf_type_bootloader, mdf_type_bootloader_item, mdf_type_address, @@ -1289,7 +1291,7 @@ class CMDF_Action : public CMDF_Object { /*! Delete action parameter - @param offset Offset of action parametewr to delete. + @param offset Offset of action parameter to delete. @return true on success, false on failure. */ bool deleteActionParam(uint8_t offset); @@ -1408,12 +1410,6 @@ class CMDF_DecisionMatrix : public CMDF_Object { */ void setRowSize(uint16_t rowSize) { m_rowSize = rowSize; }; - /*! - Get decision matrix row count - @return Decision matrix action list. - */ - std::deque *getActionList(void) { return &m_list_action; }; - /*! Get pointer to action object from 6its code @param code Code for action to fetch @@ -1421,6 +1417,34 @@ class CMDF_DecisionMatrix : public CMDF_Object { */ CMDF_Action *getAction(uint16_t code); + /*! + Add action + @param paction Pointer to action to add. The code + of the action must be unique. + @return true on success, false otherwise. + */ + bool addAction(CMDF_Action *paction); + + /*! + Delete action + @param paction Pointer to action to delete + @return true on success, false on failure. + */ + bool deleteAction(CMDF_Action *paction); + + /*! + Delete action + @param offset Offset of action to delete. + @return true on success, false on failure. + */ + bool deleteAction(uint8_t code); + + /*! + Get decision matrix row count + @return Decision matrix action list. + */ + std::deque *getActionList(void) { return &m_list_action; }; + // int getRegister(uint8_t row, CMDF_DecisionMatrix__dmindex idx); private: @@ -1462,11 +1486,17 @@ class CMDF_EventData : public CMDF_Object { void clearStorage(void); /*! - Get bit array name - @return Bit array name + Get event data + @return Event data name */ std::string getName(void) { return m_name; }; + /*! + Set event data name + @param name Event data name to set + */ + void setName(const std::string &name) { m_name = name; }; + /*! Get the register description @return Register description @@ -1671,6 +1701,42 @@ class CMDF_Event : public CMDF_Object { */ std::deque *getListEventData(void) { return &m_list_eventdata; }; + /*! + Get pointer to event data object from offset + @param offset Offset of eventdata + @return Pointer to eventdata object or nullpointer if no object is found. + */ + CMDF_EventData *getEventData(uint8_t offset); + + /*! + Add event data object + @param pEventData Pointer top event data object to add. + @return true on success, false on failure. + */ + bool addEventData(CMDF_EventData *pEventData); + + /*! + Delete event data object + @param pEventData Pointer top event data object to add. + @return true on success, false on failure. + */ + bool deleteEventData(CMDF_EventData *pEventData); + + /*! + Check if offset is unique for an item + @param offset Offset to check + @return True of unique + */ + bool isEventDataOffsetUnique(uint8_t offset); + + /*! + Check if offset is unique for a event data item. + The item itself is not checked. + @param pEventData Event data item to check + @return True of unique + */ + bool isEventDataOffsetUnique(CMDF_EventData *pEventData); + private: std::string m_name; std::map m_mapDescription; @@ -3902,6 +3968,20 @@ class CMDF : public CMDF_Object { */ std::deque *getEventList(void) { return &m_list_event; }; + /*! + Add event to MDF event list + @param pEvent Pointer to event to add. + @return True on success, flase on failure + */ + bool addEvent(CMDF_Event *pEvent); + + /*! + Delete event + @param pEvent Pointer to event to add. + @return True on success, flase on failure + */ + bool deleteEvent(CMDF_Event *pEvent); + /*! Get the alarm list @return Pointer to the alarm list.