|
UniSet
2.6.0
|
00001 /* 00002 * Copyright (c) 2015 Pavel Vainerman. 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as 00006 * published by the Free Software Foundation, version 2.1. 00007 * 00008 * This program is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * Lesser General Lesser Public License for more details. 00012 * 00013 * You should have received a copy of the GNU Lesser General Public License 00014 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00015 */ 00016 // ----------------------------------------------------------------------------- 00017 #ifndef _MQTTPublisher_H_ 00018 #define _MQTTPublisher_H_ 00019 // ----------------------------------------------------------------------------- 00020 #include <unordered_map> 00021 #include <list> 00022 #include <memory> 00023 #include <mosquittopp.h> 00024 #include "UObject_SK.h" 00025 #include "SMInterface.h" 00026 #include "SharedMemory.h" 00027 #include "extensions/Extensions.h" 00028 // ------------------------------------------------------------------------- 00029 namespace uniset 00030 { 00031 // ----------------------------------------------------------------------------- 00106 class MQTTPublisher: 00107 protected mosqpp::mosquittopp, 00108 public UObject_SK 00109 { 00110 public: 00111 MQTTPublisher( uniset::ObjectId objId, xmlNode* cnode, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr, 00112 const std::string& prefix = "mqtt" ); 00113 virtual ~MQTTPublisher(); 00114 00116 static std::shared_ptr<MQTTPublisher> init_mqttpublisher( int argc, const char* const* argv, 00117 uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr, 00118 const std::string& prefix = "mqtt" ); 00119 00121 static void help_print( int argc, const char* const* argv ); 00122 00123 virtual void on_connect(int rc) override; 00124 virtual void on_message(const struct mosquitto_message* message) override; 00125 virtual void on_subscribe(int mid, int qos_count, const int* granted_qos) override; 00126 00127 protected: 00128 MQTTPublisher(); 00129 00130 virtual void askSensors( UniversalIO::UIOCommand cmd ) override; 00131 virtual void sensorInfo( const uniset::SensorMessage* sm ) override; 00132 virtual void sigterm( int signo ) override; 00133 virtual bool deactivateObject() override; 00134 virtual void sysCommand( const uniset::SystemMessage* sm ) override; 00135 00136 std::shared_ptr<SMInterface> shm; 00137 00138 struct MQTTInfo 00139 { 00140 uniset::ObjectId sid; 00141 std::string pubname; 00142 00143 MQTTInfo( uniset::ObjectId id, const std::string& name ): 00144 sid(id), pubname(name) {} 00145 }; 00146 00147 typedef std::unordered_map<uniset::ObjectId, MQTTInfo> MQTTMap; 00148 00149 struct RangeInfo 00150 { 00151 RangeInfo( long min, long max, const std::string& t ): rmin(min), rmax(max), text(t) {} 00152 00153 long rmin; 00154 long rmax; 00155 std::string text; 00156 bool check( long val ) const; 00157 }; 00158 00159 struct MQTTTextInfo 00160 { 00161 uniset::ObjectId sid; 00162 std::string pubname; 00163 UniXML::iterator xmlnode; 00164 00165 MQTTTextInfo( const std::string& rootsec, UniXML::iterator s, UniXML::iterator i ); 00166 00167 // одиночные сообщения просто имитируются min=max=val 00168 std::list<RangeInfo> rlist; // список сообщений.. 00169 00170 void check( mosqpp::mosquittopp* serv, long value, std::shared_ptr<DebugStream>& log, const std::string& myname ); 00171 00172 std::string replace( RangeInfo* ri, long value ); 00173 }; 00174 00175 typedef std::unordered_map<uniset::ObjectId, MQTTTextInfo> MQTTTextMap; 00176 00177 MQTTMap publist; 00178 MQTTTextMap textpublist; 00179 00180 private: 00181 00182 std::string prefix; 00183 std::string topic; // "топик" для публикации датчиков 00184 bool connectOK = { false }; 00185 std::string host = { "localhost" }; 00186 int port = { 1883 }; 00187 int keepalive = { 60 }; 00188 }; 00189 // ---------------------------------------------------------------------------------- 00190 } // end of namespace uniset 00191 // ----------------------------------------------------------------------------- 00192 #endif // _MQTTPublisher_H_ 00193 // -----------------------------------------------------------------------------
1.7.6.1