UniSet  2.24.2
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  // -----------------------------------------------------------------------------
110  protected mosqpp::mosquittopp,
111  public UObject_SK
112  {
113  public:
114  MQTTPublisher( uniset::ObjectId objId, xmlNode* cnode, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
115  const std::string& prefix = "mqtt" );
116  virtual ~MQTTPublisher();
117 
119  static std::shared_ptr<MQTTPublisher> init_mqttpublisher( int argc, const char* const* argv,
120  uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
121  const std::string& prefix = "mqtt" );
122 
124  static void help_print( int argc, const char* const* argv );
125 
126  virtual void on_connect(int rc) override;
127  virtual void on_message(const struct mosquitto_message* message) override;
128  virtual void on_subscribe(int mid, int qos_count, const int* granted_qos) override;
129 
130  protected:
131  MQTTPublisher();
132 
133  virtual void askSensors( UniversalIO::UIOCommand cmd ) override;
134  virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
135  virtual bool deactivateObject() override;
136  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
137 
138  std::shared_ptr<SMInterface> shm;
139 
140  struct MQTTInfo
141  {
142  uniset::ObjectId sid;
143  std::string pubname;
144 
145  MQTTInfo( uniset::ObjectId id, const std::string& name ):
146  sid(id), pubname(name) {}
147  };
148 
149  typedef std::unordered_map<uniset::ObjectId, MQTTInfo> MQTTMap;
150 
151  struct RangeInfo
152  {
153  RangeInfo( long min, long max, const std::string& t ): rmin(min), rmax(max), text(t) {}
154 
155  long rmin;
156  long rmax;
157  std::string text;
158  bool check( long val ) const;
159  };
160 
162  {
163  uniset::ObjectId sid;
164  std::string pubname;
165  UniXML::iterator xmlnode;
166 
167  MQTTTextInfo( const std::string& rootsec, UniXML::iterator s, UniXML::iterator i );
168 
169  // одиночные сообщения просто имитируются min=max=val
170  std::list<RangeInfo> rlist; // список сообщений..
171 
172  void check( mosqpp::mosquittopp* serv, long value, std::shared_ptr<DebugStream>& log, const std::string& myname );
173 
174  std::string replace( RangeInfo* ri, long value );
175  };
176 
177  typedef std::unordered_map<uniset::ObjectId, MQTTTextInfo> MQTTTextMap;
178 
179  MQTTMap publist;
180  MQTTTextMap textpublist;
181 
182  private:
183 
184  std::string prefix;
185  std::string topic; // "топик" для публикации датчиков
186  bool connectOK = { false };
187  std::string host = { "localhost" };
188  int port = { 1883 };
189  int keepalive = { 60 };
190  };
191  // ----------------------------------------------------------------------------------
192 } // end of namespace uniset
193 // -----------------------------------------------------------------------------
194 #endif // _MQTTPublisher_H_
195 // -----------------------------------------------------------------------------
Definition: UObject_SK.h:30
Definition: MQTTPublisher.h:112
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
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition: MQTTPublisher.cc:142
static void help_print(int argc, const char *const *argv)
Definition: MQTTPublisher.cc:164
Definition: MessageType.h:127
Definition: MessageType.h:171
Definition: UniXML.h:44
Definition: CommonEventLoop.h:15
long ObjectId
Definition: UniSetTypes_i.idl:30
Definition: MQTTPublisher.h:141
Definition: MQTTPublisher.h:162
Definition: MQTTPublisher.h:152