diff options
author | daniel-Jones <daniel@danieljon.es> | 2017-07-06 19:04:48 +0930 |
---|---|---|
committer | daniel-Jones <daniel@danieljon.es> | 2017-07-06 19:04:48 +0930 |
commit | d3624454f67e3afc026d7e16546f5549e67f6102 (patch) | |
tree | e370fc7bd48c58f9d4a523d9eb7c554ebf6b461e /qt/consoleserver | |
parent | 9834782c287b1dc6df59a03e5992911d8fb70a83 (diff) | |
download | RGB-Controller-d3624454f67e3afc026d7e16546f5549e67f6102.tar.gz RGB-Controller-d3624454f67e3afc026d7e16546f5549e67f6102.zip |
Added console based server that allows clients to connect and send commands to the microcontroller, does not require X to use, includes the IRC bot. Also included my beginning works on a c ncurses based client.
Diffstat (limited to 'qt/consoleserver')
-rw-r--r-- | qt/consoleserver/.gitignore | 73 | ||||
-rwxr-xr-x | qt/consoleserver/consoleserver | bin | 0 -> 55320 bytes | |||
-rw-r--r-- | qt/consoleserver/consoleserver.pro | 31 | ||||
-rw-r--r-- | qt/consoleserver/irc.cpp | 114 | ||||
-rw-r--r-- | qt/consoleserver/irc.h | 38 | ||||
-rw-r--r-- | qt/consoleserver/main.cpp | 30 | ||||
-rw-r--r-- | qt/consoleserver/serial.cpp | 67 | ||||
-rw-r--r-- | qt/consoleserver/serial.h | 27 | ||||
-rw-r--r-- | qt/consoleserver/server.cpp | 57 | ||||
-rw-r--r-- | qt/consoleserver/server.h | 26 |
10 files changed, 463 insertions, 0 deletions
diff --git a/qt/consoleserver/.gitignore b/qt/consoleserver/.gitignore new file mode 100644 index 0000000..fab7372 --- /dev/null +++ b/qt/consoleserver/.gitignore @@ -0,0 +1,73 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/qt/consoleserver/consoleserver b/qt/consoleserver/consoleserver Binary files differnew file mode 100755 index 0000000..97dacfc --- /dev/null +++ b/qt/consoleserver/consoleserver diff --git a/qt/consoleserver/consoleserver.pro b/qt/consoleserver/consoleserver.pro new file mode 100644 index 0000000..606fe10 --- /dev/null +++ b/qt/consoleserver/consoleserver.pro @@ -0,0 +1,31 @@ +QT += core network serialport +QT -= gui + +CONFIG += c++11 + +TARGET = consoleserver +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +SOURCES += main.cpp \ + serial.cpp \ + server.cpp \ + irc.cpp + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which as been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +HEADERS += \ + serial.h \ + server.h \ + irc.h diff --git a/qt/consoleserver/irc.cpp b/qt/consoleserver/irc.cpp new file mode 100644 index 0000000..3a2ed79 --- /dev/null +++ b/qt/consoleserver/irc.cpp @@ -0,0 +1,114 @@ +#include "irc.h" + +irc::irc(QObject *parent) : QObject(parent) +{ + qDebug() << "irc loaded but not started"; +} + +void irc::setup(QString srv, int p, QString c, QString n) +{ + qDebug() << "irc started"; + server = srv; + port = p; + channel = c; + name = n; + /* setup socket, connect signal/slot */ + socket = new QTcpSocket(this); + connect(socket, SIGNAL(readyRead()), this, SLOT(read())); + pingcheck = new QTimer(this); + connect(pingcheck, SIGNAL(timeout()), this, SLOT(pingcheckfunc())); + /* connect */ + con(); +} + +void irc::read() +{ + QString line = socket->readLine(); + if (line.contains("PING :")) + { + pinged = true; + QString sline = "PONG :" + line.split(":")[1]; + socket->write(sline.toLatin1()); + } + QString tmp = name + " MODE " + name + " :+"; + if (line.contains(tmp)) + { + buf = "JOIN " + channel + "\r\n"; + socket->write(buf.toUtf8()); + } + if (line.contains("!")) /* most likely a message to handle */ + handle(line); + if(socket->canReadLine()) + read(); +} + +void irc::con() +{ + pinged = false; + socket->connectToHost(server, port); + buf = "NICK " + name + "\r\n"; + socket->write(buf.toUtf8()); + buf = "USER " + name + " 8 * :" + name + "\r\n"; + socket->write(buf.toUtf8()); + pingcheck->start(300000); +} + +void irc::discon() +{ + socket->write("QUIT :elegant quit \r\n"); + socket->close(); + pingcheck->stop(); +} + +void irc::handle(QString str) +{ + /* :Scruffy!Scruff@i.am.huskehhh.com PRIVMSG #csgo :You a vim faggot? */ + if (str.contains("PRIVMSG")) + { + + QString usr = str.split("!")[0].replace(":", ""); + QString msg = str.split("PRIVMSG")[1].split(":")[1]; + if (usr == "daniel_j") + { + if (msg.contains("!send")) + { + + msg.remove(QRegExp("[\\n\\t\\r]")); + msg.replace("!send ", ""); + if (msg.contains(";")) + { + QStringList tmp = msg.split(";"); + for (int x = 0; x < tmp.size(); x++) + { + emit sendcmd(tmp.at(x)); + } + } + else + { + emit sendcmd(msg); + } + } + } + } +} + +void irc::sendmsg(QString msg) +{ + buf = "PRIVMSG " + channel + " :" + msg + " \r\n"; + socket->write(buf.toUtf8()); +} + +void irc::pingcheckfunc() +{ + if (pinged) + { + pinged = false; + } + else + { + qDebug() << "reconnection needed"; + name = name + "_"; + discon(); + con(); + } +} diff --git a/qt/consoleserver/irc.h b/qt/consoleserver/irc.h new file mode 100644 index 0000000..733bebf --- /dev/null +++ b/qt/consoleserver/irc.h @@ -0,0 +1,38 @@ +#ifndef IRC_H +#define IRC_H + +#include <QtNetwork/QTcpSocket> +#include <QDebug> +#include <QTimer> + +class irc : public QObject +{ + Q_OBJECT + public: + explicit irc(QObject *parent = 0); + void setup(QString srv, int p, QString c, QString n); + void discon(); + void sendmsg(QString msg); + private: + QTcpSocket *socket; + QString server; + int port; + QString channel; + QString name; + QString buf; + bool pinged; + QTimer *pingcheck; + + private slots: + void read(); + void con(); + void handle(QString str); + void pingcheckfunc(); + + signals: + void sendcmd(QString cmd); + + public slots: +}; + +#endif // IRC_H diff --git a/qt/consoleserver/main.cpp b/qt/consoleserver/main.cpp new file mode 100644 index 0000000..c944e38 --- /dev/null +++ b/qt/consoleserver/main.cpp @@ -0,0 +1,30 @@ +#include <QCoreApplication> +#include <QObject> +#include "serial.h" +#include "server.h" +#include "irc.h" + +void worker(QString arg1); + +serial ser; +server srv; +irc ircbot; + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + if (argc > 1) + worker(argv[1]); + else + worker("none"); + return a.exec(); +} + +void worker(QString arg1) +{ + srv.server_start(); + ser.start(arg1); + ircbot.setup("irc.danieljon.es", 6667, "#csgo", "LightBot"); + QObject::connect(&srv, SIGNAL(sendcmd(QString)), &ser, SLOT(send(QString))); + QObject::connect(&ircbot, SIGNAL(sendcmd(QString)), &ser, SLOT(send(QString))); +} diff --git a/qt/consoleserver/serial.cpp b/qt/consoleserver/serial.cpp new file mode 100644 index 0000000..aea46bb --- /dev/null +++ b/qt/consoleserver/serial.cpp @@ -0,0 +1,67 @@ +#include "serial.h" +#include <QThread> + +serial::serial() +{ + qDebug() << "serial loaded but not started"; +} + +void serial::start(QString port) +{ + qDebug() << "serial started"; + if (port != "none") + { + qDebug() << "using port" << port; + if (!connect(port)) + { + qDebug() << "fatal error connecting to the serial port"; + qDebug() << "not bothering to continue"; + exit(1); + } + } + else + { + qDebug() << "you need to provide a serial port as an argument."; + qDebug() << "available ports:"; + show_ports(); + qDebug() << "quitting because there is no point continuing"; + exit(1); + } +} + +void serial::show_ports() +{ + for (int x = 0; x < QSerialPortInfo::availablePorts().size(); x++) + { + if (!s.availablePorts().at(x).description().isEmpty()) + qDebug() << QString("%1").arg(QSerialPortInfo::availablePorts().at(x).portName()); + } +} + +bool serial::connect(QString port) +{ + if (!ser.isOpen()) + { + ser.setPortName(port); + ser.setBaudRate(QSerialPort::Baud9600); + ser.setDataBits(QSerialPort::Data8); + ser.setParity(QSerialPort::NoParity); + ser.setStopBits(QSerialPort::OneStop); + ser.setFlowControl(QSerialPort::NoFlowControl); + ser.open(QIODevice::ReadWrite); + ser.waitForBytesWritten(9000); + if (ser.isWritable()) { + qDebug() << "serial connected"; + return true; + } + } + return false; +} + +void serial::send(QString com) +{ + data = ""; + data.append(com + "\n"); + if (ser.isOpen()) + ser.write(data); +} diff --git a/qt/consoleserver/serial.h b/qt/consoleserver/serial.h new file mode 100644 index 0000000..3f0386a --- /dev/null +++ b/qt/consoleserver/serial.h @@ -0,0 +1,27 @@ +#ifndef SERIAL_H +#define SERIAL_H + +#include <QObject> +#include <QSerialPort> +#include <QSerialPortInfo> +#include <QDebug> + + +class serial : public QObject +{ + Q_OBJECT +public: + serial(); + void start(QString port); + public slots: + void send(QString com); +private: + bool connect(QString port); + void show_ports(); + QSerialPort ser; + QSerialPortInfo s; + QByteArray serdata; + QByteArray data; +}; + +#endif // SERIAL_H diff --git a/qt/consoleserver/server.cpp b/qt/consoleserver/server.cpp new file mode 100644 index 0000000..401ee8d --- /dev/null +++ b/qt/consoleserver/server.cpp @@ -0,0 +1,57 @@ +#include "server.h" + +server::server(QObject *parent) : QObject(parent) +{ + srv = new QTcpServer(); + connect(srv, SIGNAL(newConnection()), this, SLOT(new_connection())); + qDebug() << "server loaded but not started"; +} + +bool server::server_start() +{ + qDebug() << "server started"; + if (srv->listen(QHostAddress::Any, 3001)) + { + qDebug() << "listening on 3001"; + return true; + } + else + { + return false; + } +} + +bool server::server_stop() +{ + srv->close(); + if (srv->isListening()) + return false; + return true; +} + +void server::new_connection() +{ + QTcpSocket *socket; + socket = srv->nextPendingConnection(); + socket->waitForReadyRead(3000); + handle(QString(socket->readAll()).remove(QRegExp("[\\n\\t\\r]"))); + socket->close(); + delete socket; +} + +void server::handle(QString msg) +{ + qDebug() << "received" << msg; + if (msg.contains(";")) + { + QStringList tmp = msg.split(";"); + for (int x = 0; x < tmp.size(); x++) + { + emit sendcmd(tmp.at(x)); + } + } + else + { + emit sendcmd(msg); + } +} diff --git a/qt/consoleserver/server.h b/qt/consoleserver/server.h new file mode 100644 index 0000000..9fd84dc --- /dev/null +++ b/qt/consoleserver/server.h @@ -0,0 +1,26 @@ +#ifndef SERVER_H +#define SERVER_H + +#include <QTcpServer> +#include <QTcpSocket> + +class server : public QObject +{ + Q_OBJECT + public: + explicit server(QObject *parent = 0); + bool server_start(); + bool server_stop(); + private: + QTcpServer *srv; + void handle(QString msg); + private slots: + void new_connection(); +signals: + void sendcmd(QString); + + + public slots: +}; + +#endif // SERVER_H |