-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
125 lines (108 loc) · 4.99 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
#include <QCoreApplication>
#include "readweather.h"
#include <QTest>
#include <QtSql>
#include <QDebug>
#include <QProcessEnvironment>
#include <stdio.h>
#include "../SupportRoutines/supportfunctions.h"
int main(int argc, char *argv[])
{
/*
* DON'T ShowDiagnosticsSince OR DumpDebugInfo OR FlushDiagnostics
* DIAGNOSTICS TILL AFTER OPTIONS PROCESSED.
*
* THIS MAY AVOID TRYING TO WRITE DIAGNOSTICS TO MAIN DATABASE SINCE
* IT MAY NOT HAVE A DebugInfo TABLE.
*/
QCoreApplication a(argc, argv);
StartTime = QDateTime::currentDateTime();
// Silent to terminal till options processed.
qInstallMessageHandler(saveMessageOutput);
qInfo() << "Begin";
DetermineCommitTag();
// Get environment to use for default values.
QProcessEnvironment env=QProcessEnvironment::systemEnvironment();
/*
* Local variable declarations
*/
QStringList driverList = QSqlDatabase::drivers();
if (driverList.isEmpty())
{
qCritical("No database drivers found.");
return 1;
}
qInfo() << "Available database drivers are:" << driverList;
/*
* Process command line options
*/
QCommandLineParser parser;
parser.setApplicationDescription("\nWeather\n"
" Program to read data from the weather station, store in database, and publish to MQTT.");
parser.addHelpOption();
QCommandLineOption serialDeviceOption(QStringList() << "s" << "serial-name", "The name of the serial device. [cu.SLAB_USBtoUART]", "Name", "cu.SLAB_USBtoUART");
QCommandLineOption mqttWeatherTopic(QStringList() << "t" << "weather-topic", "The MQTT topic for weather data.", "Path", "demay_farm/Weather");
QCommandLineOption mqttStatusTopic(QStringList() << "T" << "status-topic", "The MQTT topic for program status messages.", "Path", "demay_farm/Weather/Status");
QCommandLineOption databaseOption(QStringList() << "d" << "database", "Database id string.", "URL"
, env.value("DatabaseProgramAccess", "").remove(QChar('"')));
QCommandLineOption debugDatabaseOption(QStringList() << "B" << "debug-database", "Database id string for debug info.", "URL"
, "");
QCommandLineOption showDiagnosticsOption(QStringList() << "D" << "show-diagnostics"
, "Print saved diagnostics to terminal at runtime.");
QCommandLineOption immediateDiagnosticsOption(QStringList() << "S" << "immediate-diagnostics"
, "Print diagnostic info to terminal immediately.");
QCommandLineOption dontWriteDatabaseOption(QStringList() << "W" << "dont-write"
, "If specified, don't actually write to the database.");
parser.addOption(serialDeviceOption);
parser.addOption(mqttWeatherTopic);
parser.addOption(mqttStatusTopic);
parser.addOption(databaseOption);
parser.addOption(debugDatabaseOption);
parser.addOption(showDiagnosticsOption);
parser.addOption(immediateDiagnosticsOption);
parser.addOption(dontWriteDatabaseOption);
parser.process(a);
ShowDiagnostics = parser.isSet(showDiagnosticsOption);
ImmediateDiagnostics = parser.isSet(immediateDiagnosticsOption);
if (ImmediateDiagnostics)
qInstallMessageHandler(terminalMessageOutput);
else
qInstallMessageHandler(saveMessageOutput);
DontActuallyWriteDatabase = parser.isSet(dontWriteDatabaseOption);
qDebug() << "DontActuallyWriteDatabase: " << DontActuallyWriteDatabase;
QString serialDevice = parser.value(serialDeviceOption);
qDebug() << "Using serial device" << serialDevice;
MQTT_WEATHER_TOPIC = parser.value(mqttWeatherTopic);
qInfo() << "MQTT_WEATHER_TOPIC" << MQTT_WEATHER_TOPIC;
MQTT_STATUS_TOPIC = parser.value(mqttStatusTopic);
qInfo() << "MQTT_STATUS_TOPIC" << MQTT_STATUS_TOPIC;
QString databaseConnString = parser.value(databaseOption);
qDebug() << "Using database connection string: " << databaseConnString;
addConnectionFromString(databaseConnString);
databaseConnString = parser.value(debugDatabaseOption);
if (!databaseConnString.isEmpty())
{
qDebug() << "Using database connection string for debug info: " << databaseConnString;
addConnectionFromString(databaseConnString, true);
}
FlushDiagnostics();
if (QSqlDatabase::connectionNames().isEmpty())
{
qCritical("No database connection for weather data.");
qInfo() << "Return" << 1;
return 1;
}
// qInfo() << "Available time zones are: " << QTimeZone::availableTimeZoneIds();
ReadWeather w(serialDevice);
if (w.connected())
{
int retVal = a.exec(); // Process event loop till quit called.
DumpDebugInfo();
qInfo("Normal termination, Returning %d", retVal);
return retVal;
}
qCritical() << "ReadWeather failed to connect.";
DumpDebugInfo();
qInfo() << "Return" << -1;
return -1;
}