Working TCP subsystem
This commit is contained in:
122
main.cpp
122
main.cpp
@ -1,10 +1,128 @@
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <stdio.h>
|
||||
#include <QDir>
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
#include <QSettings>
|
||||
#include <QSignalMapper>
|
||||
#include <QTcpSocket>
|
||||
#include <QtSerialPort/QtSerialPort>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
#include <QCommandLineParser>
|
||||
|
||||
|
||||
#include "alarmtime.h"
|
||||
#include "microcontroller.h"
|
||||
#include "mainwindow.h"
|
||||
#include <QApplication>
|
||||
#include "relaydialog.h"
|
||||
#include "ampmanager.h"
|
||||
|
||||
#define BAUD QSerialPort::Baud38400
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
MainWindow w;
|
||||
|
||||
//set info
|
||||
QCoreApplication::setOrganizationName("UVOS");
|
||||
QCoreApplication::setOrganizationDomain("uvos.xyz");
|
||||
QCoreApplication::setApplicationName("SHinterface");
|
||||
QCoreApplication::setApplicationVersion("0.2");
|
||||
|
||||
QDir::setCurrent(a.applicationDirPath());
|
||||
|
||||
//parse comand line
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription("Smart Home Interface");
|
||||
parser.addHelpOption();
|
||||
parser.addVersionOption();
|
||||
QCommandLineOption tcpOption(QStringList() << "t" << "tcp", QCoreApplication::translate("main", "Use Tcp connection"));
|
||||
parser.addOption(tcpOption);
|
||||
QCommandLineOption hostOption(QStringList() << "H" << "host", QCoreApplication::translate("main", "Set server host ip addres"), "adress");
|
||||
parser.addOption(hostOption);
|
||||
QCommandLineOption portOption(QStringList() << "p" << "port", QCoreApplication::translate("main", "Set server Port in TCP mode or Serial port in serial mode"), "port");
|
||||
parser.addOption(portOption);
|
||||
QCommandLineOption serialOption(QStringList() << "s" << "serial", QCoreApplication::translate("main", "Use serial connection"));
|
||||
parser.addOption(serialOption);
|
||||
QCommandLineOption baudOption(QStringList() << "b" << "baud", QCoreApplication::translate("main", "Set Baud Rate"));
|
||||
parser.addOption(baudOption);
|
||||
QCommandLineOption secondaryOption(QStringList() << "s" << "secondary", QCoreApplication::translate("main", "Set if instance is not main instance"));
|
||||
parser.addOption(secondaryOption);
|
||||
parser.process(a);
|
||||
|
||||
QSettings settings;
|
||||
|
||||
//connect to microcontoler
|
||||
Microcontroller micro;
|
||||
if(parser.isSet(tcpOption))
|
||||
{
|
||||
QTcpSocket* microSocket = new QTcpSocket;
|
||||
|
||||
int port = 6856;
|
||||
if(parser.isSet(portOption)) port = parser.value(portOption).toInt();
|
||||
|
||||
QString host("127.0.0.1");
|
||||
if(parser.isSet(hostOption)) host = parser.value(hostOption);
|
||||
std::cout<<"connecting to "<<host.toStdString()<<':'<<port<<'\n';
|
||||
microSocket->connectToHost(host, port, QIODevice::ReadWrite);
|
||||
if(!microSocket->waitForConnected(1000))
|
||||
{
|
||||
std::cout<<"Can not connect to to Server.\n";
|
||||
return 1;
|
||||
}
|
||||
micro.setIODevice(microSocket);
|
||||
}
|
||||
else
|
||||
{
|
||||
QSerialPort* microPort = new QSerialPort;
|
||||
if(parser.isSet(portOption)) microPort->setPortName(parser.value(portOption));
|
||||
else microPort->setPortName("ttyUSB0");
|
||||
|
||||
if(parser.isSet(portOption)) microPort->setBaudRate(parser.value(baudOption).toInt());
|
||||
else microPort->setBaudRate(BAUD);
|
||||
|
||||
if(!microPort->open(QIODevice::ReadWrite)) std::cout<<"Can not open serial port "<<microPort->portName().toStdString()<<". Continueing in demo mode"<<'\n';
|
||||
else micro.setIODevice(microPort);
|
||||
}
|
||||
|
||||
AmpManager amp(µ, 0);
|
||||
|
||||
MainWindow w(&settings, µ, parser.isSet(secondaryOption));
|
||||
|
||||
RelayDialog relayDialog;
|
||||
|
||||
if(!parser.isSet(secondaryOption))
|
||||
{
|
||||
//Alarms
|
||||
AlarmTime almNight(settings.value("nightTime").toTime());
|
||||
QObject::connect(&almNight, SIGNAL(trigger()), &w, SLOT(slotSyncoff()));
|
||||
QObject::connect(&w, SIGNAL(signalAlmNightChanged(QTime)), &almNight, SLOT(changeTime(QTime)));
|
||||
QObject::connect(&w, SIGNAL(signalAlmNightStateChanged(int)), &almNight, SLOT(runOrAbort(int)));
|
||||
//QMetaObject::invokeMethod(&almNight, "run", Qt::QueuedConnection );
|
||||
|
||||
AlarmTime *almAlarm = new AlarmTime(settings.value("alarmTime").toTime());
|
||||
QSignalMapper signalMapper;
|
||||
QObject::connect(almAlarm, SIGNAL(trigger()), &signalMapper, SLOT(map()));
|
||||
signalMapper.setMapping(almAlarm, 4);
|
||||
QObject::connect(&signalMapper, SIGNAL(mapped(int)), µ, SLOT(setPattern(int)));
|
||||
QObject::connect(&w, SIGNAL(signalAlmAlarmChanged(QTime)), almAlarm, SLOT(changeTime(QTime)));
|
||||
QObject::connect(&w, SIGNAL(signalAlmAlarmStateChanged(int)), almAlarm, SLOT(runOrAbort(int)));
|
||||
//QMetaObject::invokeMethod(almAlarm, "run", Qt::QueuedConnection );
|
||||
|
||||
//Amplifyer
|
||||
QObject::connect(&w, SIGNAL(signalAmpOn()), &, SLOT(run()));
|
||||
QObject::connect(&w, SIGNAL(signalAmpOff()), &, SLOT(abort()));
|
||||
QMetaObject::invokeMethod(&, "run", Qt::QueuedConnection );
|
||||
}
|
||||
|
||||
//serial display
|
||||
QObject::connect(µ, SIGNAL(textRecived(QString)), &w, SLOT(changeHeaderLableText(QString)));
|
||||
QMetaObject::invokeMethod(µ, "run", Qt::QueuedConnection );
|
||||
|
||||
//Advanced Relays
|
||||
QObject::connect(&w, SIGNAL(showAdvRelayDialog()), &relayDialog, SLOT(show()));
|
||||
|
||||
QMetaObject::invokeMethod(&w, "postActivate", Qt::QueuedConnection );
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
|
Reference in New Issue
Block a user