forked from vortex314/serial2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
129 lines (110 loc) · 3.22 KB
/
Copy pathmain.cpp
File metadata and controls
129 lines (110 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <Log.h>
#include <Config.h>
#include <Serial2Mqtt.h>
#include <thread>
#define DEFAULT_CONFIG "serial2mqtt.json"
void commandArguments(JsonObject, int argc, char **argv);
Log logger(2048);
//Config config;
#define MAX_PORT 20
Serial2Mqtt serial2mqtt[MAX_PORT];
void SetThreadName(std::thread *thread, const char *threadName)
{
auto handle = thread->native_handle();
pthread_setname_np(handle, threadName);
}
#include <LogFile.h>
LogFile logFile("wiringMqtt", 5, 2000000);
int main(int argc, char **argv)
{
std::thread threads[MAX_PORT];
StaticJsonDocument<10240> jsonDoc;
Sys::init();
INFO("build : " __DATE__ " " __TIME__);
char cwd[256];
getcwd(cwd, sizeof(cwd));
INFO("current directory : %s", cwd);
if (argc > 1)
{
INFO("loading config file : %s", argv[1]);
config.loadFile(argv[1]);
}
else
{
INFO("load default config : %s", DEFAULT_CONFIG);
config.loadFile(DEFAULT_CONFIG);
}
commandArguments(config.root(), argc, argv);
JsonObject logConfig = config.root()["log"];
std::string level = logConfig["level"] | "I";
logger.setLogLevel(level[0]);
if (logConfig["file"])
{
std::string prefix = logConfig["file"];
logFile.prefix(prefix.c_str());
logger.writer(
[](char *line, unsigned int length) { logFile.append(line, length); });
if (logConfig["console"])
{
bool consoleOn = logConfig["console"];
logFile.console(consoleOn);
}
}
INFO("serial2mqtt started. Build : %s ", __DATE__ " " __TIME__);
config.setNameSpace("serial");
JsonArray ports = config.root()["serial"]["ports"].as<JsonArray>();
// JsonArray ports = jsonDoc.as<JsonArray>();
if (ports.isNull())
{
jsonDoc.add("/dev/ttyUSB0");
ports = jsonDoc.as<JsonArray>();
}
for (uint32_t i = 0; i < ports.size(); i++)
{
std::string port = ports[i];
INFO("configuring port : %s", port.c_str());
serial2mqtt[i].setSerialPort(port.c_str());
serial2mqtt[i].setConfig(config);
serial2mqtt[i].init();
}
for (uint32_t i = 0; i < ports.size(); i++)
{
threads[i] = std::thread([=]() {
INFO("starting thread %d", i);
serial2mqtt[i].run();
});
SetThreadName(&threads[i], serial2mqtt[i].getSerialPortShort().c_str());
}
sleep(UINT32_MAX); // UINT32_MAX to sleep 'forever'
exit(0);
return 0;
}
void commandArguments(JsonObject config, int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "f:m:l:v:")) != -1) {
switch (opt) {
case 'm':
config["mqtt"]["connection"] = optarg;
break;
case 'f':
config["configFile"] = optarg;
break;
case 'v': {
std::string logLevel;
logLevel += optarg[0];
config["log"]["level"] = logLevel;
break;
}
case 'l':
config["log"]["file"] = optarg;
break;
default: /* '?' */
fprintf(stderr,
"Usage: %s [-v(TDIWE)] [-f configFile] [-l logFile] [-m "
"mqtt_connection]\n",
argv[0]);
exit(EXIT_FAILURE);
}
}
}