/
main.cpp
148 lines (124 loc) · 5.49 KB
/
main.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <QCoreApplication>
#include <QLoggingCategory>
#include <QCommandLineParser>
#include "qtozwdaemon.h"
#include "mqttpublisher.h"
#include <qt-openzwave/qt-openzwavedatabase.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QCoreApplication::setApplicationName("ozwdaemon");
QCoreApplication::setApplicationVersion("0.1");
QCoreApplication::setOrganizationName("OpenZWave");
QCoreApplication::setOrganizationDomain("openzwave.com");
QCommandLineParser parser;
parser.setApplicationDescription("QT OpenZWave Remote Daemon");
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption serialPort(QStringList() << "s" << "serial-port",
"Serial Port of USB Stick",
"serialPort"
);
parser.addOption(serialPort);
QCommandLineOption configDir(QStringList() << "c" << "config-dir",
"Directory containing the OZW Config Files",
"configDir"
);
parser.addOption(configDir);
QCommandLineOption userDir(QStringList() << "u" << "user-dir",
"Directory for the OZW User Files",
"userDir"
);
parser.addOption(userDir);
QCommandLineOption MQTTServer(QStringList() << "mqtt-server",
"MQTT Server Hostname/IP Address",
"IP/Hostname"
);
parser.addOption(MQTTServer);
QCommandLineOption MQTTPort(QStringList() << "mqtt-port",
"MQTT Server Port",
"Port"
);
parser.addOption(MQTTPort);
parser.process(a);
if (!parser.isSet(serialPort)) {
fputs(qPrintable("Serial Port is Required\n"), stderr);
fputs("\n\n", stderr);
fputs(qPrintable(parser.helpText()), stderr);
exit(-1);
}
QSettings settings;
if (parser.isSet(MQTTServer)) {
settings.setValue("MQTTServer", parser.value(MQTTServer));
}
if (parser.isSet(MQTTPort)) {
settings.setValue("MQTTPort", parser.value(MQTTPort).toInt());
}
#if 1
QLoggingCategory::setFilterRules("qt.remoteobjects.debug=true\n"
"qt.remoteobjects.warning=true\n"
"qt.remoteobjects.models.debug=true\n"
"qt.remoteobjects.models.debug=true\n"
"qt.remoteobjects.io.debug=true\n"
"default.debug=true");
#else
QLoggingCategory::setFilterRules("default.debug=true");
#endif
QStringList PossibleDBPaths;
if (parser.isSet(configDir))
PossibleDBPaths << parser.value(configDir);
PossibleDBPaths << "./config/";
PossibleDBPaths << settings.value("openzwave/ConfigPath", QDir::toNativeSeparators("../../../config/")).toString().append("/");
PossibleDBPaths << QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
QString path, dbPath, userPath;
foreach(path, PossibleDBPaths) {
qDebug() << "Checking " << QFileInfo(QDir::toNativeSeparators(path+"/config/manufacturer_specific.xml")).absoluteFilePath() << " for manufacturer_specific.xml";
if (QFileInfo(QDir::toNativeSeparators(path+"/config/manufacturer_specific.xml")).exists()) {
dbPath = QFileInfo(QDir::toNativeSeparators(path+"/config/manufacturer_specific.xml")).absoluteFilePath();
break;
}
qDebug() << "Checking " << QFileInfo(QDir::toNativeSeparators(path+"../config/manufacturer_specific.xml")).absoluteFilePath() << " for manufacturer_specific.xml";
if (QFile(QDir::toNativeSeparators(path+"/../config/manufacturer_specific.xml")).exists()) {
dbPath = QFileInfo(QDir::toNativeSeparators(path+"/../config/manufacturer_specific.xml")).absoluteFilePath();
break;
}
}
PossibleDBPaths.clear();
if (parser.isSet(userDir))
PossibleDBPaths << parser.value(userDir);
PossibleDBPaths << "./config/";
PossibleDBPaths << settings.value("openzwave/UserPath", QDir::toNativeSeparators("../../../config/")).toString().append("/");
PossibleDBPaths << QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
foreach(path, PossibleDBPaths) {
qDebug() << "Checking " << QFileInfo(QDir::toNativeSeparators(path+"/config/Options.xml")).absoluteFilePath() << " for options.xml";
if (QFileInfo(QDir::toNativeSeparators(path+"/config/options.xml")).exists()) {
userPath = QFileInfo(QDir::toNativeSeparators(path+"/config/options.xml")).absoluteFilePath();
break;
}
qDebug() << "Checking " << QFileInfo(QDir::toNativeSeparators(path+"/../config/options.xml")).absoluteFilePath() << " for options.xml";
if (QFile(QDir::toNativeSeparators(path+"/../config/options.xml")).exists()) {
userPath = QFileInfo(QDir::toNativeSeparators(path+"/../config/options.xml")).absoluteFilePath();
break;
}
}
// if (userPath.isEmpty()) {
// fputs(qPrintable("userPath is Not Set or Missing\n"), stderr);
// exit(-1);
// }
if (dbPath.isEmpty()) {
copyConfigDatabase(QFileInfo("./").absoluteFilePath().append("/"));
dbPath = "./config/";
userPath = "./config/";
}
qDebug() << "DBPath: " << dbPath;
qDebug() << "userPath: " << userPath;
QTOZWOptions options(QTOZWOptions::Local);
options.setUserPath(userPath);
options.setConfigPath(dbPath);
qtozwdaemon daemon;
mqttpublisher mqttpublisher;
mqttpublisher.setOZWDaemon(&daemon);
daemon.setSerialPort(parser.value(serialPort));
daemon.startOZW();
return a.exec();
}