123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- #include <algorithm>
- #include "mqttvar.h"
- #include "mqttdbg.h"
- /////////////////////////////////////////////////////////////////////////////
- static bool _fncomp(const char *p1, const char *p2)
- {
- return strcmp(p1, p2) < 0;
- }
- /////////////////////////////////////////////////////////////////////////////
- CMqttVarTable::CMqttVarTable(void) : m_map(_fncomp), m_pub(_fncomp)
- {
- }
- CMqttVarTable::~CMqttVarTable(void)
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- void CMqttVarTable::AddVar(CMqttVar *pVar)
- {
- m_map[pVar->GetPath()] = pVar;
- AddToPubTable(pVar);
- }
- CMqttVar* CMqttVarTable::Find(const char *key) const
- {
- std::map<const char*, CMqttVar*>::const_iterator it = m_map.find(key);
- if(it == m_map.end())
- return NULL;
- return it->second;
- }
- bool CMqttVarTable::AddToPubTable(CMqttVar *pv)
- {
- if(pv->PublishEnabled())
- {
- if(m_pub.find(pv->GetPath()) == m_pub.end())
- {
- m_pub[pv->GetPath()] = pv;
- return true;
- }
- }
- return false;
- }
- bool CMqttVarTable::RemoveFromPubTable(CMqttVar *pv)
- {
- if(!pv->PublishEnabled())
- {
- if(m_pub.find(pv->GetPath()) != m_pub.end())
- {
- m_pub.erase(pv->GetPath());
- return true;
- }
- }
- return false;
- }
- void CMqttVarTable::CheckShmAndPublish(const char *pszTopicDevID, const char *pszTopicPrefix, CMqttMessageQueue &rmq, int &nLocks)
- {
- for(auto i = m_pub.begin(); i != m_pub.end(); ++i)
- {
- CMqttVar *pVar = i->second;
- pVar->CheckShmAndPublish(pszTopicDevID, pszTopicPrefix, rmq, nLocks);
- }
- }
- void CMqttVarTable::DumpPubEnabled(uint32_t nPubMask)
- {
- #ifdef _DUMP_ENABLED_VARS
- TRACE("\nList of published variables:\n");
- for(auto i = m_pub.begin(); i != m_pub.end(); ++i)
- {
- CMqttVar *pVar = i->second;
- uint32_t nMask = pVar->GetPublishMask();
-
- if(nMask & nPubMask)
- {
- TracePubVar(pVar->GetPath(), nMask & nPubMask, pVar->GetQoS(), pVar->GetRetained());
- }
- }
- TRACE("\n");
- #endif // _DUMP_ENABLED_VARS
- }
- void CMqttVarTable::TracePubVar(const char *pszPath, uint32_t nMask, int nQos, bool bRetained)
- {
- #ifdef _DUMP_ENABLED_VARS
- if(pszPath && nMask)
- {
- int nCount = 0;
- TRACE("%s - [", pszPath);
- if(nMask & MQTT_VALUE_BINLE)
- {
- TRACE("%s", MQTT_TOPIC_VALUE_BINLE);
- ++nCount;
- }
- if(nMask & MQTT_VALUE_BINBE)
- {
- if(nCount)
- TRACE(", ");
- TRACE("%s", MQTT_TOPIC_VALUE_BINBE);
- ++nCount;
- }
- if(nMask & MQTT_VALUE_JSON)
- {
- if(nCount)
- TRACE(", ");
- TRACE("%s", MQTT_TOPIC_VALUE_JSON);
- ++nCount;
- }
- if(nMask & MQTT_VALUE_PBUF)
- {
- if(nCount)
- TRACE(", ");
- TRACE("%s", MQTT_TOPIC_VALUE_PBUF);
- ++nCount;
- }
- TRACE("] - [QOS: %d] - [Retained: %s]\n", nQos, bRetained ? "true" : "false");
- }
- #endif // _DUMP_ENABLED_VARS
- }
|