mqttvartbl.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <algorithm>
  2. #include "mqttvar.h"
  3. #include "mqttdbg.h"
  4. /////////////////////////////////////////////////////////////////////////////
  5. static bool _fncomp(const char *p1, const char *p2)
  6. {
  7. return strcmp(p1, p2) < 0;
  8. }
  9. /////////////////////////////////////////////////////////////////////////////
  10. CMqttVarTable::CMqttVarTable(void) : m_map(_fncomp), m_pub(_fncomp)
  11. {
  12. }
  13. CMqttVarTable::~CMqttVarTable(void)
  14. {
  15. }
  16. /////////////////////////////////////////////////////////////////////////////
  17. void CMqttVarTable::AddVar(CMqttVar *pVar)
  18. {
  19. m_map[pVar->GetPath()] = pVar;
  20. AddToPubTable(pVar);
  21. }
  22. CMqttVar* CMqttVarTable::Find(const char *key) const
  23. {
  24. std::map<const char*, CMqttVar*>::const_iterator it = m_map.find(key);
  25. if(it == m_map.end())
  26. return NULL;
  27. return it->second;
  28. }
  29. bool CMqttVarTable::AddToPubTable(CMqttVar *pv)
  30. {
  31. if(pv->PublishEnabled())
  32. {
  33. if(m_pub.find(pv->GetPath()) == m_pub.end())
  34. {
  35. m_pub[pv->GetPath()] = pv;
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. bool CMqttVarTable::RemoveFromPubTable(CMqttVar *pv)
  42. {
  43. if(!pv->PublishEnabled())
  44. {
  45. if(m_pub.find(pv->GetPath()) != m_pub.end())
  46. {
  47. m_pub.erase(pv->GetPath());
  48. return true;
  49. }
  50. }
  51. return false;
  52. }
  53. void CMqttVarTable::CheckShmAndPublish(const char *pszTopicDevID, const char *pszTopicPrefix, CMqttMessageQueue &rmq, int &nLocks)
  54. {
  55. for(auto i = m_pub.begin(); i != m_pub.end(); ++i)
  56. {
  57. CMqttVar *pVar = i->second;
  58. pVar->CheckShmAndPublish(pszTopicDevID, pszTopicPrefix, rmq, nLocks);
  59. }
  60. }
  61. void CMqttVarTable::DumpPubEnabled(uint32_t nPubMask)
  62. {
  63. #ifdef _DUMP_ENABLED_VARS
  64. TRACE("\nList of published variables:\n");
  65. for(auto i = m_pub.begin(); i != m_pub.end(); ++i)
  66. {
  67. CMqttVar *pVar = i->second;
  68. uint32_t nMask = pVar->GetPublishMask();
  69. if(nMask & nPubMask)
  70. {
  71. TracePubVar(pVar->GetPath(), nMask & nPubMask, pVar->GetQoS(), pVar->GetRetained());
  72. }
  73. }
  74. TRACE("\n");
  75. #endif // _DUMP_ENABLED_VARS
  76. }
  77. void CMqttVarTable::TracePubVar(const char *pszPath, uint32_t nMask, int nQos, bool bRetained)
  78. {
  79. #ifdef _DUMP_ENABLED_VARS
  80. if(pszPath && nMask)
  81. {
  82. int nCount = 0;
  83. TRACE("%s - [", pszPath);
  84. if(nMask & MQTT_VALUE_BINLE)
  85. {
  86. TRACE("%s", MQTT_TOPIC_VALUE_BINLE);
  87. ++nCount;
  88. }
  89. if(nMask & MQTT_VALUE_BINBE)
  90. {
  91. if(nCount)
  92. TRACE(", ");
  93. TRACE("%s", MQTT_TOPIC_VALUE_BINBE);
  94. ++nCount;
  95. }
  96. if(nMask & MQTT_VALUE_JSON)
  97. {
  98. if(nCount)
  99. TRACE(", ");
  100. TRACE("%s", MQTT_TOPIC_VALUE_JSON);
  101. ++nCount;
  102. }
  103. if(nMask & MQTT_VALUE_PBUF)
  104. {
  105. if(nCount)
  106. TRACE(", ");
  107. TRACE("%s", MQTT_TOPIC_VALUE_PBUF);
  108. ++nCount;
  109. }
  110. TRACE("] - [QOS: %d] - [Retained: %s]\n", nQos, bRetained ? "true" : "false");
  111. }
  112. #endif // _DUMP_ENABLED_VARS
  113. }