119std::vector<std::string>
static myformat(
const std::string& prefix,
120 const std::string& headLine,
121 const std::string& message,
122 std::size_t initialPrefixLength = 0) {
124 const size_t prefixLen = prefix.size();
125 const size_t indentCount = prefixLen + 1;
126 const std::string indent(indentCount,
' ');
128 std::vector<std::string> lines;
132 size_t avail = (termWidth >
int(indentCount + 2)) ?
static_cast<std::size_t>(termWidth) - (indentCount + 2) : 20u;
134 auto wrapped =
wrapParagraph(prefix +
" ┬ " + headLine
, avail - (prefix.length() + 2)
);
136 if (wrapped.empty()) {
137 wrapped.push_back(
"");
143 for (
const auto& line : wrapped) {
144 lines.emplace_back((first ?
"" : indent +
"│ ") + line);
150 auto j =
nlohmann::json::parse(message);
152 std::string pretty = j.dump(2);
154 std::istringstream prettyIStringStream(pretty);
156 for (
auto [line, lineNumnber] = std::tuple{std::string(
""), 0}; std::getline(prettyIStringStream, line); lineNumnber++) {
157 if (lineNumnber == 0 && !prettyIStringStream.eof()) {
158 lines.push_back(indent +
"├ " + line);
159 }
else if (prettyIStringStream.eof()) {
160 lines.push_back(indent +
"└ " + line);
162 lines.push_back(indent +
"│ " + line);
165 }
catch (
nlohmann::json::parse_error&) {
169 std::istringstream messageIStringStream(message);
170 std::vector<std::string> allLines;
171 for (std::string line; std::getline(messageIStringStream, line);) {
174 if (wrapped.empty()) {
175 wrapped.push_back(
"");
178 allLines.insert(allLines.end(), wrapped.begin(), wrapped.end());
181 if (!allLines.empty() && allLines.back().empty()) {
186 for (std::size_t lineNumber = 0; lineNumber < allLines.size(); ++lineNumber) {
187 if (lineNumber == 0 && lineNumber + 1 != allLines.size()) {
188 lines.push_back(indent +
"├ " + allLines[lineNumber]);
189 }
else if (lineNumber + 1 == allLines.size()) {
190 lines.push_back(indent +
"└ " + allLines[lineNumber]);
192 lines.push_back(indent +
"│ " + allLines[lineNumber]);
218 const std::string& clientId,
222 const std::string& willTopic,
223 const std::string& willMessage,
226 const std::string& username,
227 const std::string& password,
228 const std::list<std::string>& subTopics,
229 const std::string& pubTopic,
230 const std::string& pubMessage,
232 const std::string& sessionStoreFileName)
233 : iot::
mqtt::client::Mqtt(connectionName, clientId, keepAlive, sessionStoreFileName)
246 VLOG(1) <<
"Client Id: " << clientId;
247 VLOG(1) <<
" Keep Alive: " << keepAlive;
248 VLOG(1) <<
" Clean Session: " << cleanSession;
249 VLOG(1) <<
" Will Topic: " << willTopic;
250 VLOG(1) <<
" Will Message: " << willMessage;
251 VLOG(1) <<
" Will QoS: " <<
static_cast<uint16_t>(willQoS);
252 VLOG(1) <<
" Will Retain " << willRetain;
253 VLOG(1) <<
" Username: " << username;
254 VLOG(1) <<
" Password: " << password;
283 if (connack.getReturnCode() == 0) {
284 bool sendDisconnectFlag =
true;
287 VLOG(0) <<
"MQTT Subscribe";
290 std::list<iot::
mqtt::Topic> topicList;
293 std::back_inserter(topicList),
294 [qoSDefault =
this->qoSDefault](
const std::string& compositTopic) -> iot::
mqtt::Topic {
295 std::size_t pos = compositTopic.rfind(
"##");
297 const std::string topic = compositTopic.substr(0, pos);
298 uint8_t qoS = qoSDefault;
300 if (pos != std::string::npos) {
302 qoS =
getQos(compositTopic.substr(pos + 2)
);
303 }
catch (
const std::logic_error& error) {
304 VLOG(0) <<
"[" << Color::Code::FG_RED <<
"Error" << Color::Code::FG_DEFAULT
305 <<
"] Malformed composit topic: " << compositTopic <<
"\n"
310 VLOG(0) <<
" t: " <<
static_cast<
int>(qoS) <<
" | " << topic;
311 return iot::
mqtt::Topic(topic, qoS);
313 sendSubscribe(topicList);
315 sendDisconnectFlag =
false;
316 }
catch (
const std::logic_error&) {
321 VLOG(0) <<
"MQTT Publish";
323 std::size_t pos =
pubTopic.rfind(
"##");
325 const std::string topic =
pubTopic.substr(0, pos);
330 if (pos != std::string::npos) {
333 }
catch (
const std::logic_error& error) {
334 VLOG(0) <<
"[" << Color::Code::FG_RED <<
"Error" << Color::Code::FG_DEFAULT
335 <<
"] Malformed composit topic: " <<
pubTopic <<
"\n"
342 sendDisconnectFlag = qoS > 0 ?
false : sendDisconnectFlag;
343 }
catch (
const std::logic_error&) {
346 if (sendDisconnectFlag) {
Mqtt(const std::string &connectionName, const std::string &clientId, uint8_t qoSDefault, uint16_t keepAlive, bool cleanSession, const std::string &willTopic, const std::string &willMessage, uint8_t willQoS, bool willRetain, const std::string &username, const std::string &password, const std::list< std::string > &subTopics, const std::string &pubTopic, const std::string &pubMessage, bool pubRetain=false, const std::string &sessionStoreFileName="")
static std::vector< std::string > myformat(const std::string &prefix, const std::string &headLine, const std::string &message, std::size_t initialPrefixLength=0)