tinymqtt-test : new commands

This commit is contained in:
Francois BIOT
2022-12-04 02:03:28 +01:00
parent 7af8e46b59
commit c913bc61bb
2 changed files with 73 additions and 14 deletions

View File

@@ -13,6 +13,7 @@
#endif
#include <sstream>
#include <string>
#include <map>
bool echo_on = true;
@@ -29,6 +30,21 @@ auto erase_to_end = TinyConsole::erase_to_end;
const char* ssid = "";
const char* password = "";
struct free_broker
{
public:
free_broker(const char* s, uint16_t p, const char* comment) : url(s), port(p) {}
std::string url;
uint16_t port;
};
const std::map<std::string, free_broker> list =
{
{ "mqtthq", { "public.mqtthq.com" , 8083, "publish/subscribe" }},
{ "hivemq", { "broker.hivemq.com", 1883, "" }}
};
/** Very complex example
* Console allowing to make any kind of test,
* even some stress tests.
@@ -330,6 +346,26 @@ void onCommand(const std::string& command)
std::string cmd=command;
if (cmd.substr(0,3)!="set") replaceVars(cmd);
eval(cmd);
Console << endl;
Console.prompt();
}
void clientConnect(MqttClient* client, std::string& cmd)
{
std::string remote = getword(cmd);
uint16_t port;
auto it=list.find(remote);
if (it != list.end())
{
Console << "Connecting to free broker: " << remote << endl;
remote = it->second.url;
port=it->second.port;
}
else
port=getint(cmd);
client->connect(remote.c_str(), port, getint(cmd, 60));
Console << (client->connected() ? "connected." : "not connected") << endl;
}
void eval(std::string& cmd)
@@ -374,6 +410,14 @@ void eval(std::string& cmd)
{
TinyMqtt::debug = getint(cmd);
}
else if (compare(s, "list"))
{
Console << "List of free servers" << endl;
for(const auto& fb: list)
{
Console << " " << fb.first << " : " << fb.second.url << ":" << fb.second.port << endl;
}
}
else if (compare(s, "delete"))
{
if (client==nullptr && broker==nullptr)
@@ -437,8 +481,7 @@ void eval(std::string& cmd)
{
if (compare(s,"connect"))
{
client->connect(getip(cmd,"192.168.1.40").c_str(), getint(cmd, 1883), getint(cmd, 60));
Console << (client->connected() ? "connected." : "not connected") << endl;
clientConnect(client, cmd);
}
else if (compare(s,"publish"))
{
@@ -623,15 +666,19 @@ void eval(std::string& cmd)
}
else if (id.length() or clients.find(id)!=clients.end())
{
s=getword(cmd); // broker name
if (s=="" or brokers.find(s) != brokers.end())
s=getword(cmd); // broker
if (s=="" or brokers.find(s) != brokers.end() or list.find(s) != list.end())
{
MqttBroker* broker = nullptr;
if (s.length()) broker = brokers[s];
MqttClient* client = new MqttClient(broker);
client->id(id);
MqttClient* client = new MqttClient(broker, id);
clients[id]=client;
client->setCallback(onPublish);
if (list.find(s) != list.end())
{
cmd=s+' '+cmd;
clientConnect(client, cmd);
}
Console << "new client (" << id.c_str() << ", " << s.c_str() << ')' << endl;
}
else if (s.length())
@@ -707,17 +754,21 @@ void eval(std::string& cmd)
Console << "syntax:" << endl;
Console << " MqttBroker:" << endl;
Console << " broker {broker_name} {port} : create a new broker" << endl;
Console << " broker_name can be one of 'list'" << endl;
Console << " broker_name.delete : delete a broker (buggy)" << endl;
Console << " broker_name.view : dump a broker" << endl;
Console << endl;
Console << " MqttClient:" << endl;
Console << " client {name} {parent broker} : create a client then" << endl;
Console << " client {name} {broker} : create a client then" << endl;
Console << " name.connect [ip] [port] [alive]" << endl;
Console << " name.[un]subscribe topic" << endl;
Console << " name.publish topic [payload]" << endl;
Console << " name.view" << endl;
Console << " name.delete" << endl;
Console << endl;
Console << " list : list of free brokers (debug 1 advised)" << endl;
Console << " debug #" << endl;
Console << " list : get list of free brokers" << endl;
Console << " blink [Dx on_ms off_ms] : make pin blink" << endl;
Console << " ls / ip / reset" << endl;
Console << " set [name][value]" << endl;

View File

@@ -36,6 +36,7 @@ MqttBroker::~MqttBroker()
MqttClient::MqttClient(MqttBroker* local_broker, TcpClient* new_client)
: local_broker(local_broker)
{
debug("MqttClient private with broker");
#ifdef TINY_MQTT_ASYNC
client = new_client;
client->onData(onData, this);
@@ -89,20 +90,19 @@ void MqttClient::close(bool bSendDisconnect)
void MqttClient::connect(MqttBroker* local)
{
debug("MqttClient::connect_1");
debug("MqttClient::connect_local");
close();
local_broker = local;
}
void MqttClient::connect(std::string broker, uint16_t port, uint16_t ka)
{
debug("MqttClient::connect_3");
debug("MqttClient::connect_to_host " << broker << ':' << port);
keep_alive = ka;
close();
if (client) delete client;
client = new TcpClient;
debug("Trying to connect to " << broker.c_str() << ':' << port);
#ifdef TINY_MQTT_ASYNC
client->onData(onData, this);
client->onConnect(onConnect, this);
@@ -110,8 +110,13 @@ void MqttClient::connect(std::string broker, uint16_t port, uint16_t ka)
#else
if (client->connect(broker.c_str(), port))
{
debug("link established");
onConnect(this, client);
}
else
{
debug("unable to connect.");
}
#endif
}
@@ -123,7 +128,7 @@ void MqttBroker::addClient(MqttClient* client)
void MqttBroker::connect(const std::string& host, uint16_t port)
{
debug("MqttBroker::connect_2");
debug("MqttBroker::connect");
if (broker == nullptr) broker = new MqttClient;
broker->connect(host, port);
broker->local_broker = this; // Because connect removed the link
@@ -483,7 +488,7 @@ void MqttClient::processMessage(MqttMessage* mesg)
}
#ifdef TINY_MQTT_DEBUG
Console << yellow << "Client connected :" << clientId.c_str() << ", keep alive=" << keep_alive << '.' << white << endl;
Console << yellow << "Client " << clientId << " connected : keep alive=" << keep_alive << '.' << white << endl;
#endif
bclose = false;
mqtt_connected=true;
@@ -602,8 +607,11 @@ void MqttClient::processMessage(MqttMessage* mesg)
if (local_broker==nullptr or client==nullptr) // internal MqttClient receives publish
{
#ifdef TINY_MQTT_DEBUG
if (TinyMqtt::debug >= 2)
{
Console << (isSubscribedTo(published) ? "not" : "") << " subscribed.\n";
Console << "has " << (callback ? "" : "no ") << " callback.\n";
}
#endif
if (callback and isSubscribedTo(published))
{