-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
145 lines (134 loc) · 4.4 KB
/
mainwindow.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
#include "mainwindow.h"
#include <QtWidgets/QAction>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QApplication>
#include <QNetworkReply>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QAction *quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
notifyAction = new QAction("", this);
connect(notifyAction, SIGNAL(triggered()), this, SLOT(onNotifyClick()));
QMenu *trayIconMenu = new QMenu(this);
trayIconMenu->addAction(notifyAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
this->setState(OFFLINE);
trayIcon->show();
connect(&webSocket, &QWebSocket::connected, this, &MainWindow::onConnected);
connect(&webSocket, &QWebSocket::disconnected, this, &MainWindow::onDisconnect);
connect(&webSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(onError(QAbstractSocket::SocketError)));
connect(&netManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(httpFinished(QNetworkReply*)));
connect(&pingTimer, SIGNAL(timeout()), this, SLOT(sendPing()));
connect(&reconnectTimer, SIGNAL(timeout()), this, SLOT(openConnection()));
reconnectTimer.setSingleShot(true);
this->openConnection();
}
MainWindow::~MainWindow()
{
}
void MainWindow::openConnection(){
qDebug() << "Http request";
QNetworkRequest request;
request.setUrl(QUrl("http://isthetoiletfree.fastmonkeys.com"));
request.setRawHeader("User-Agent", "ToiletWatch 1.0");
netManager.get(request);
}
void MainWindow::httpFinished(QNetworkReply*r){
r->deleteLater();
if(r->error() == 0){
qDebug() << "Http request ok";
QByteArray data=r->readAll();
QString str(data);
if(str.contains(">no<"))
this->setState(OCCUPIED);
else if(str.contains(">yes<"))
this->setState(FREE);
else {
qDebug() << "Http content parse error " << str;
goto error;
}
qDebug() << "WebSocket connection";
webSocket.open(QUrl(QStringLiteral("ws://isthetoiletfree.fastmonkeys.com/hasfreesocket")));
return;
}
else {
qDebug() << "Http request failed " << r->error();
}
error:
this->setState(OFFLINE);
reconnectTimer.start(10000);
}
void MainWindow::onConnected()
{
qDebug() << "WebSocket connected";
connect(&webSocket, &QWebSocket::textMessageReceived,
this, &MainWindow::onTextMessageReceived);
pingTimer.start(20000);
}
void MainWindow::onDisconnect()
{
qDebug() << "WebSocket disconnected";
this->setState(OFFLINE);
pingTimer.stop();
}
void MainWindow::onError(QAbstractSocket::SocketError socketError)
{
qDebug() << "WebSocket error " << socketError;
this->setState(OFFLINE);
webSocket.abort();
reconnectTimer.start(10000);
}
void MainWindow::onTextMessageReceived(QString message)
{
qDebug() << "WebSocket received:" << message;
if(message.contains("yes"))
this->setState(FREE);
else if(message.contains("no"))
this->setState(OCCUPIED);
else{
qDebug() << message;
this->setState(OFFLINE);
}
}
void MainWindow::sendPing()
{
qDebug() << "Sending ping";
//Avoid Heroku conection timeouts
webSocket.sendTextMessage("Ping");
}
void MainWindow::onNotifyClick(){
setState(this->state == WAITING?OCCUPIED:WAITING);
}
void MainWindow::setState(states s){
switch (s) {
case FREE:
if(this->state == WAITING)
trayIcon->showMessage("The toilet is now free", "", QSystemTrayIcon::Information);
notifyAction->setEnabled(false);
notifyAction->setText(tr("&Notify when free"));
trayIcon->setIcon(QIcon(":/toilet.png"));
break;
case OCCUPIED:
notifyAction->setEnabled(true);
notifyAction->setText(tr("&Notify when free"));
trayIcon->setIcon(QIcon(":/toilet_red.png"));
break;
case WAITING:
notifyAction->setEnabled(true);
notifyAction->setText(tr("&Remove notification"));
trayIcon->setIcon(QIcon(":/toilet_red.png"));
break;
case OFFLINE:
notifyAction->setEnabled(false);
notifyAction->setText(tr("&Notify when free"));
trayIcon->setIcon(QIcon(":/toilet_transparent.png"));
break;
}
this->state = s;
}