UniSet  2.6.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 void sigterm( int signo ) override;
133  virtual bool deactivateObject() override;
134  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
135 
136  std::shared_ptr<SMInterface> shm;
137 
138  struct MQTTInfo
139  {
140  uniset::ObjectId sid;
141  std::string pubname;
142 
143  MQTTInfo( uniset::ObjectId id, const std::string& name ):
144  sid(id), pubname(name) {}
145  };
146 
147  typedef std::unordered_map<uniset::ObjectId, MQTTInfo> MQTTMap;
148 
149  struct RangeInfo
150  {
151  RangeInfo( long min, long max, const std::string& t ): rmin(min), rmax(max), text(t) {}
152 
153  long rmin;
154  long rmax;
155  std::string text;
156  bool check( long val ) const;
157  };
158 
160  {
161  uniset::ObjectId sid;
162  std::string pubname;
163  UniXML::iterator xmlnode;
164 
165  MQTTTextInfo( const std::string& rootsec, UniXML::iterator s, UniXML::iterator i );
166 
167  // одиночные сообщения просто имитируются min=max=val
168  std::list<RangeInfo> rlist; // список сообщений..
169 
170  void check( mosqpp::mosquittopp* serv, long value, std::shared_ptr<DebugStream>& log, const std::string& myname );
171 
172  std::string replace( RangeInfo* ri, long value );
173  };
174 
175  typedef std::unordered_map<uniset::ObjectId, MQTTTextInfo> MQTTTextMap;
176 
177  MQTTMap publist;
178  MQTTTextMap textpublist;
179 
180  private:
181 
182  std::string prefix;
183  std::string topic; // "топик" для публикации датчиков
184  bool connectOK = { false };
185  std::string host = { "localhost" };
186  int port = { 1883 };
187  int keepalive = { 60 };
188  };
189  // ----------------------------------------------------------------------------------
190 } // end of namespace uniset
191 // -----------------------------------------------------------------------------
192 #endif // _MQTTPublisher_H_
193 // -----------------------------------------------------------------------------