Better client creation

This commit is contained in:
hsaturn
2021-03-22 01:19:50 +01:00
parent e71a4d5e87
commit 18b5f0c27b

View File

@@ -250,37 +250,6 @@ void loop()
} }
s = getword(cmd); s = getword(cmd);
if (broker)
{
if (compare(s,"connect"))
{
Serial << "NYI" << endl;
}
else if (compare(s, "view"))
{
broker->dump();
}
}
else if (client)
{
if (compare(s,"connect"))
{
client->connect(getword(cmd,"192.168.1.40").c_str(), 1883);
Serial << (client->connected() ? "connected." : "not connected") << endl;
}
else if (compare(s,"publish"))
{
client->publish(getword(cmd, topic.c_str()));
}
else if (compare(s,"subscribe"))
{
client->subscribe(getword(cmd, topic.c_str()));
}
else if (compare(s, "view"))
{
client->dump();
}
}
if (compare(s, "delete")) if (compare(s, "delete"))
{ {
if (client==nullptr && broker==nullptr) if (client==nullptr && broker==nullptr)
@@ -324,6 +293,41 @@ void loop()
else else
Serial << "Nothing to delete" << endl; Serial << "Nothing to delete" << endl;
} }
else if (broker)
{
if (compare(s,"connect"))
{
Serial << "NYI" << endl;
}
else if (compare(s, "view"))
{
broker->dump();
}
}
else if (client)
{
if (compare(s,"connect"))
{
client->connect(getword(cmd,"192.168.1.40").c_str(), 1883);
Serial << (client->connected() ? "connected." : "not connected") << endl;
}
else if (compare(s,"publish"))
{
auto ok=client->publish(getword(cmd, topic.c_str()));
if (ok != MqttOk)
{
Serial << "## ERROR " << ok << endl;
}
}
else if (compare(s,"subscribe"))
{
client->subscribe(getword(cmd, topic.c_str()));
}
else if (compare(s, "view"))
{
client->dump();
}
}
else if (compare(s, "auto")) else if (compare(s, "auto"))
{ {
automatic::command(client, cmd); automatic::command(client, cmd);
@@ -356,19 +360,23 @@ void loop()
std::string id=getword(cmd); std::string id=getword(cmd);
if (id.length() or clients.find(id)!=clients.end()) if (id.length() or clients.find(id)!=clients.end())
{ {
MqttBroker* broker = nullptr;
// TODO cmd line broker name s=getword(cmd); // broker name
if (brokers.size()==1) broker = brokers.begin()->second; if (s=="" or brokers.find(s) != brokers.end())
Serial << "broker=" << (int32_t)broker << endl; {
delay(500); MqttBroker* broker = nullptr;
if (s.length()) broker = brokers[s];
MqttClient* client = new MqttClient(broker); MqttClient* client = new MqttClient(broker);
client->id(id); client->id(id);
clients[id]=client; clients[id]=client;
client->setCallback(onPublish); client->setCallback(onPublish);
client->subscribe(topic); client->subscribe(topic);
Serial << "new client (" << id.c_str() << ")" << endl; Serial << "new client (" << id.c_str() << ", " << s.c_str() << ')' << endl;
}
else if (s.length())
{
Serial << " not found." << endl;
}
} }
else else
Serial << "Missing or existing client name" << endl; Serial << "Missing or existing client name" << endl;
@@ -400,7 +408,7 @@ void loop()
Serial << " broker {name} {port} : create a new broker" << endl; Serial << " broker {name} {port} : create a new broker" << endl;
Serial << endl; Serial << endl;
Serial << " MqttClient:" << endl; Serial << " MqttClient:" << endl;
Serial << " client {name} : create a client then" << endl; Serial << " client {name} {parent broker} : create a client then" << endl;
Serial << " name.connect [ip]" << endl; Serial << " name.connect [ip]" << endl;
Serial << " name.subscribe [topic]" << endl; Serial << " name.subscribe [topic]" << endl;
Serial << " name.publish [topic]" << endl; Serial << " name.publish [topic]" << endl;