UniSet  2.8.0
MQTTPublisher.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // -----------------------------------------------------------------------------
17 #ifndef _MQTTPublisher_H_
18 #define _MQTTPublisher_H_
19 // -----------------------------------------------------------------------------
20 #include <unordered_map>
21 #include <list>
22 #include <memory>
23 #include <mosquittopp.h>
24 #include "UObject_SK.h"
25 #include "SMInterface.h"
26 #include "SharedMemory.h"
27 #include "extensions/Extensions.h"
28 // -------------------------------------------------------------------------
29 namespace uniset
30 {
31  // -----------------------------------------------------------------------------
107  protected mosqpp::mosquittopp,
108  public UObject_SK
109  {
110  public:
111  MQTTPublisher( uniset::ObjectId objId, xmlNode* cnode, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
112  const std::string& prefix = "mqtt" );
113  virtual ~MQTTPublisher();
114 
116  static std::shared_ptr<MQTTPublisher> init_mqttpublisher( int argc, const char* const* argv,
117  uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
118  const std::string& prefix = "mqtt" );
119 
121  static void help_print( int argc, const char* const* argv );
122 
123  virtual void on_connect(int rc) override;
124  virtual void on_message(const struct mosquitto_message* message) override;
125  virtual void on_subscribe(int mid, int qos_count, const int* granted_qos) override;
126 
127  protected:
128  MQTTPublisher();
129 
130  virtual void askSensors( UniversalIO::UIOCommand cmd ) override;
131  virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
132  virtual bool deactivateObject() override;
133  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
134 
135  std::shared_ptr<SMInterface> shm;
136 
137  struct MQTTInfo
138  {
139  uniset::ObjectId sid;
140  std::string pubname;
141 
142  MQTTInfo( uniset::ObjectId id, const std::string& name ):
143  sid(id), pubname(name) {}
144  };
145 
146  typedef std::unordered_map<uniset::ObjectId, MQTTInfo> MQTTMap;
147 
148  struct RangeInfo
149  {
150  RangeInfo( long min, long max, const std::string& t ): rmin(min), rmax(max), text(t) {}
151 
152  long rmin;
153  long rmax;
154  std::string text;
155  bool check( long val ) const;
156  };
157 
159  {
160  uniset::ObjectId sid;
161  std::string pubname;
162  UniXML::iterator xmlnode;
163 
164  MQTTTextInfo( const std::string& rootsec, UniXML::iterator s, UniXML::iterator i );
165 
166  // одиночные сообщения просто имитируются min=max=val
167  std::list<RangeInfo> rlist; // список сообщений..
168 
169  void check( mosqpp::mosquittopp* serv, long value, std::shared_ptr<DebugStream>& log, const std::string& myname );
170 
171  std::string replace( RangeInfo* ri, long value );
172  };
173 
174  typedef std::unordered_map<uniset::ObjectId, MQTTTextInfo> MQTTTextMap;
175 
176  MQTTMap publist;
177  MQTTTextMap textpublist;
178 
179  private:
180 
181  std::string prefix;
182  std::string topic; // "топик" для публикации датчиков
183  bool connectOK = { false };
184  std::string host = { "localhost" };
185  int port = { 1883 };
186  int keepalive = { 60 };
187  };
188  // ----------------------------------------------------------------------------------
189 } // end of namespace uniset
190 // -----------------------------------------------------------------------------
191 #endif // _MQTTPublisher_H_
192 // -----------------------------------------------------------------------------
Definition: CommonEventLoop.h:14
Definition: MQTTPublisher.h:137
Definition: UniXML.h:43
static void help_print(int argc, const char *const *argv)
Definition: MQTTPublisher.cc:164
Definition: MessageType.h:170
Definition: MQTTPublisher.h:158
Definition: MQTTPublisher.h:106
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition: MQTTPublisher.cc:142
Definition: MessageType.h:126
Definition: MQTTPublisher.h:148
Definition: UObject_SK.h:28
long ObjectId
Definition: UniSetTypes_i.idl:30
static std::shared_ptr< MQTTPublisher > init_mqttpublisher(int argc, const char *const *argv, uniset::ObjectId shmID, const std::shared_ptr< SharedMemory > &ic=nullptr, const std::string &prefix="mqtt")
Definition: MQTTPublisher.cc:218