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