-
Notifications
You must be signed in to change notification settings - Fork 0
/
remoteom.cpp
785 lines (673 loc) · 19.2 KB
/
remoteom.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/****************************************************************************
**
** Copyright 2010 Adminotech Ltd
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
*****************************************************************************/
#include <QtGui>
#include <QTimer>
#include "remoteom.h"
#include "ui_remoteom.h"
#include "settings.h"
RemoteOM::RemoteOM(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::RemoteOM)
{
mapResponse[48] = "No fault";
mapResponse[49] = "IP. Plate current too high.";
mapResponse[50] = "HV. Plate/anode voltage too low.";
mapResponse[51] = "SWR. Excessive reflected power.";
mapResponse[52] = "Ig2. Screen limit exceeded.";
mapResponse[53] = "Overdrive. Too much drive from exciter.";
mapResponse[54] = "FWD=0, Power output is zero with drive power higher than zero.";
mapResponse[55] = "Igmax. 1st grid current limit exceeded.";
mapResponse[56] = "Tune. Insufficient tuning - retune PA";
mapResponse[62] = "PA turned on.";
mapResponse[60] = "PA turned off.";
mapResponse[83] = "PA turned to stand by mode.";
mapResponse[79] = "PA turned to operating mode.";
mapResponse[76] = "List 20 last fault codes.";
mapResponse[71] = "Reset PA succeeded.";
mapResponse[87] = "PA is heating up. Please wait.";
mapResponse[90] = "Operating is not possible.";
mapResponse[84] = "Fatal fault.";
dialog = new SettingsDialog();
ui->setupUi(this);
createActions();
//Init settings if startprofile is empty
if (startProfile.isEmpty())
{
dialog->FirstChoice(startProfile.remove("\""));
startProfile = "";
}
QTimer *timer = new QTimer(this);
timer->setInterval(1000);
timer->start();
connect(timer, SIGNAL(timeout()), this, SLOT(TimeOut()));
ui->menu->addAction(configureAct);
ui->menu->addAction(exitAct);
ui->ledDrive->hide();
ui->ledGrid->hide();
ui->ledOperate->hide();
ui->ledPA->hide();
ui->ledPower->hide();
ui->ledStdby->hide();
ui->ledSwr->hide();
ui->ledTune->hide();
ui->ledFault->hide();
connect(ui->buttonConnect, SIGNAL(clicked()), this, SLOT(connectSocket()));
socket = new QTcpSocket(this);
ui->buttonPower->setStatusTip(tr("Turn PA ON/OFF."));
ui->buttonOperate->setStatusTip(tr("Turn Operate ON/OFF."));
ui->buttonConnect->setStatusTip(tr("Connect/Disconnect to Remote OM"));
setInfoText("Connecting to Remote OM. Please wait.");
receiveFaults = false;
faultTimer = new QTimer(this);
waitTimer = new QTimer(this);
connect(faultTimer, SIGNAL(timeout()), this, SLOT(blinkFault()));
connect(waitTimer, SIGNAL(timeout()), this, SLOT(blinkWait()));
//Get settings
if (!startProfile.isEmpty())
{
/* dialog->SetInitialSettings();
QString show_ip = dialog->set_ipaddress;
QString show_port = dialog->set_port;
ui->txtCurrentSettings->setText("Current settings: "+show_ip+":"+show_port);*/
}
}
RemoteOM::~RemoteOM()
{
closeSocket();
delete ui;
}
void RemoteOM::closeEvent(QCloseEvent *event)
{
closeSocket();
event->accept();
}
void RemoteOM::createActions()
{
exitAct = new QAction(tr("Exit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
configureAct = new QAction(tr("Settings"), this);
configureAct->setStatusTip(tr("Settings"));
connect(configureAct, SIGNAL(triggered()), this, SLOT(configure()));
}
void RemoteOM::configure()
{
if (dialog)
{
dialog->Init();
dialog->show();
}
else
{
dialog = new SettingsDialog();
dialog->show();
}
}
void RemoteOM::setInfoText(QString text)
{
if (infoText.length() == 0)
infoText = text;
if (infoText != text)
{
infoText = text;
QString temp = ui->txtInfo->text();
temp = text + "\n"+ temp;
if (temp.length() > 200)
{
temp.remove(200, (temp.length() - 200));
}
ui->txtInfo->setText(temp);
}
}
//Socket calls
void RemoteOM::on_buttonPower_clicked()
{
//Try connect socket if not connected
//qDebug() << "Socket state power button clicked " << socket->state();
if (socket->state() != QAbstractSocket::ConnectedState)
{
connectSocket();
if (socket && socket->state() == QAbstractSocket::ConnectedState)
{
setInfoText("Connected to Remote OM.");
ui->ledPower->show();
}
else
{
setInfoText("The host was not found. Please check the host name and port settings.");
return;
}
}
//Turn PA on
if(ui->ledPA->isHidden())
{
setInfoText("Turning PA on.");
QByteArray temp;
temp.append(62);
sendData(temp);
ui->ledPA->show();
}
else
{
setInfoText("Turning PA off.");
QByteArray temp;
temp.append(60);
sendData(temp);
ui->ledPA->hide();
}
}
void RemoteOM::on_buttonOperate_clicked()
{
//Turn to opearate mode
if (ui->ledOperate->isHidden())
{
setInfoText("Turn to operating mode.");
QByteArray temp;
temp.append(79);
sendData(temp);
}
else
{
setInfoText("Turn to stand by mode.");
QByteArray temp;
temp.append(83);
sendData(temp);
}
}
void RemoteOM::on_buttonFaults_clicked()
{
setInfoText("Listing 20 last fault codes.");
QByteArray temp;
temp.append(70);
sendData(temp);
}
void RemoteOM::on_buttonReset_clicked()
{
setInfoText("Resetting PA.");
QByteArray temp;
temp.append(82);
sendData(temp);
}
//Socket functions
void RemoteOM::readyRead()
{
QByteArray bytes = socket->readAll();
QString read(bytes);
socket->flush();
if (bytes.isNull() || bytes.isEmpty())
return;
//Read password
if (read.contains("Password"))
{
QString password = dialog->set_password;
if ( password.length() <= 0)
{
QMessageBox::information(this, tr("Remote OM"),
tr("The connection requires a password. Please, define password in settings."));
return;
}
setInfoText("Authenticating connection.");
for(int i = 0; i < password.length(); i++)
{
QChar c = password.at(i).toAscii();
QByteArray parr;
parr.append(c);
sendData(parr);
}
QByteArray temp;
temp.append(13);
sendData(temp);
authenticate = true;
return;
}
//Read faults.
if (read.contains("LAST 20 FAULTS:"))
{
receiveFaults = true;
}
//Read all to faultBuffer until ASCII space is received
if (receiveFaults)
{
for(int i = 0; i < bytes.length(); i++)
{
uchar t = (uchar)bytes.at(i);
if (faultBuffer.size() > 20)
{
receiveFaults = false;
}
else
{
faultBuffer.append(t);
return;
}
}
}
//Read faultBuffer first and then continue normally
if (!receiveFaults && faultBuffer.size() > 1)
{
for(int i = 0; i < faultBuffer.length(); i++)
{
uchar t = (uchar)faultBuffer.at(i);
//Receive faults
switch (t)
{
case 48:
setInfoText(mapResponse[48]);
break;
case 49:
setInfoText(mapResponse[49]);
break;
case 50:
setInfoText(mapResponse[50]);
break;
case 51:
setInfoText(mapResponse[51]);
break;
case 52:
setInfoText(mapResponse[52]);
break;
case 53:
setInfoText(mapResponse[53]);
break;
case 54:
setInfoText(mapResponse[54]);
break;
case 55:
setInfoText(mapResponse[55]);
break;
case 56:
setInfoText(mapResponse[56]);
break;
default:
break;
}
}
faultBuffer.clear();
}
for(int i = 0; i < bytes.length(); i++)
{
uchar t = (uchar)bytes.at(i);
if (t > 127)
{
int d = (int)t;
d = d - 128;
ui->progressBar->setValue(d*1.4);
return;
}
switch (t)
{
case 10:
authBuffer.append(t);
break;
case 13:
if (authenticate && authBuffer.at(0) == 10 && socket->state() == QAbstractSocket::ConnectedState)
{
setInfoText("Authentication succeeded");
}
else
{
setInfoText("Authentication failed.");
}
authenticate = false;
authBuffer.clear();
break;
case 62:
setInfoText(mapResponse[62]);
ui->ledPA->show();
sent = "";
break;
case 60:
setInfoText(mapResponse[60]);
ui->ledPA->hide();
sent = "";
stopFaultBlinking();
stopWaitBlinking();
break;
case 83:
setInfoText(mapResponse[83]);
if (ui->ledStdby->isHidden())
ui->ledStdby->show();
if (!ui->ledOperate->isHidden())
ui->ledOperate->hide();
if (ui->ledPA->isHidden())
ui->ledPA->show();
stopWaitBlinking();
sent = "";
break;
case 79:
setInfoText(mapResponse[79]);
if (ui->ledOperate->isHidden())
ui->ledOperate->show();
if (!ui->ledStdby->isHidden())
ui->ledStdby->hide();
if (ui->ledPA->isHidden())
ui->ledPA->show();
stopWaitBlinking();
sent = "";
break;
case 76:
setInfoText(mapResponse[76]);
sent = "";
receiveFaults = true;
break;
case 71:
setInfoText(mapResponse[71]);
sent = "";
stopFaultBlinking();
break;
case 87:
setInfoText(mapResponse[87]);
if (ui->ledPA->isHidden())
ui->ledPA->show();
waitTimer->start(500);
break;
case 90:
setInfoText(mapResponse[90]);
ui->ledOperate->hide();
stopWaitBlinking();
ui->ledGrid->hide();
ui->ledDrive->hide();
ui->ledTune->hide();
ui->ledSwr->hide();
ui->ledStdby->show();
break;
case 84:
setInfoText(mapResponse[84]);
if (ui->ledFault->isHidden())
ui->ledFault->show();
break;
case 49:
setInfoText(mapResponse[49]);
if (ui->ledFault->isHidden())
{
ui->ledFault->show();
}
else
{
ui->ledFault->hide();
}
startFaultTimer();
break;
case 50:
setInfoText(mapResponse[50]);
if (ui->ledFault->isHidden())
{
ui->ledFault->show();
}
else
{
ui->ledFault->hide();
}
startFaultTimer();
break;
case 51:
setInfoText(mapResponse[51]);
if (ui->ledFault->isHidden())
{
ui->ledFault->show();
ui->ledSwr->show();
}
else
{
ui->ledFault->hide();
ui->ledSwr->hide();
}
startFaultTimer();
break;
case 52:
setInfoText(mapResponse[52]);
if (ui->ledFault->isHidden())
{
ui->ledFault->show();
ui->ledGrid->show();
}
else
{
ui->ledFault->hide();
ui->ledGrid->hide();
}
startFaultTimer();
break;
case 53:
setInfoText(mapResponse[53]);
if (ui->ledFault->isHidden())
{
ui->ledFault->show();
ui->ledDrive->show();
}
else
{
ui->ledFault->hide();
ui->ledDrive->hide();
}
startFaultTimer();
break;
case 54:
setInfoText(mapResponse[54]);
if (ui->ledFault->isHidden())
{
ui->ledFault->show();
}
else
{
ui->ledFault->hide();
}
startFaultTimer();
break;
case 55:
setInfoText(mapResponse[55]);
if (ui->ledFault->isHidden())
{
ui->ledFault->show();
ui->ledGrid->show();
}
else
{
ui->ledFault->hide();
ui->ledGrid->hide();
}
startFaultTimer();
break;
case 56:
setInfoText(mapResponse[56]);
if (ui->ledFault->isHidden())
{
ui->ledFault->show();
ui->ledTune->show();
}
else
{
ui->ledFault->hide();
ui->ledTune->hide();
}
startFaultTimer();
break;
default:
break;
}
}
//Send not replied. Do one re-send.
if (sent.length() > 0)
{
QByteArray temp;
temp.append(sent);
sendData(temp);
sent = "";
}
}
//"S" - enters PA into STBY - reply of PA: "S"
//"O" - enters PA into OPERATE - reply of PA: "O"
//"R" - reset fault - reply of PA: "G" folowed by "S" or "O"
//">" - Turn PA ON - reply of PA: ">"
//"<" - Turn PA OFF - reply of PA: "<"
//"F" - Sends last 20 fault codes. Reply is list of all 20 fault codes.
void RemoteOM::sendData(QByteArray input)
{
//Try connect socket
if (socket->state() == QAbstractSocket::ConnectedState)
{
socket->write(input);
sent = input.at(0);
}
else
{
setInfoText("The host was not found. Please check the host name and port settings.");
}
}
void RemoteOM::TimeOut()
{
if (!startProfile.isEmpty())
{
dialog->FirstChoice(startProfile.remove("\""));
startProfile = "";
}
QString show_ip = dialog->set_ipaddress;
QString show_port = dialog->set_port;
ui->txtCurrentSettings->setText("Current settings: "+show_ip+":"+show_port);
}
void RemoteOM::connectSocket()
{
if (socket && socket->state() == QTcpSocket::ConnectedState)
{
closeSocket();
ui->ledPower->hide();
setInfoText("Connection closed");
return;
}
if (socket->state() != QAbstractSocket::ConnectedState)
{
QString ipaddress = "";
QString port = "";
ipaddress = dialog->set_ipaddress;
port = dialog->set_port;
if (ipaddress.isEmpty() || port.isEmpty())
{
QMessageBox::information(this, tr("Remote OM"), tr("Please select settings."));
return;
}
socket->connectToHost(ipaddress, port.toInt());
if (!socket->waitForConnected(5000)) {
emit error(socket->error());
return;
}
if (socket && socket->state() == QAbstractSocket::ConnectedState)
{
setInfoText("Connected to Remote OM.");
ui->ledPower->show();
}
else
{
setInfoText("The host was not found. Please check the host name and port settings.");
}
}
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
Qt::DirectConnection);
}
void RemoteOM::closeSocket()
{
if (socket && socket->state() == QAbstractSocket::ConnectedState)
{
//TURN PA OFF
QByteArray temp;
temp.append(60);
sendData(temp);
ui->ledPA->hide();
socket->waitForBytesWritten();
setInfoText("Closing connection and software, please wait.");
socket->close();
}
}
void RemoteOM::proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth)
{
auth->setUser("qsockstest");
auth->setPassword("password");
}
void RemoteOM::error(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Remote OM"),
tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Remote OM"),
tr("The connection was refused by the peer. "
"Make sure the ip address and port are valid."));
break;
default:
QMessageBox::information(this, tr("Remote OM"),
tr("The following error occurred: %1.")
.arg(socket->errorString()));
}
}
//Ui fault and wait led blinking
void RemoteOM::blinkFault()
{
faultTimer->stop();
ui->ledFault->hide();
ui->ledSwr->hide();
ui->ledTune->hide();
ui->ledGrid->hide();
ui->ledDrive->hide();
}
void RemoteOM::blinkWait()
{
if (ui->ledGrid->isHidden())
{
ui->ledGrid->show();
ui->ledDrive->show();
ui->ledTune->show();
ui->ledSwr->show();
}
else
{
ui->ledGrid->hide();
ui->ledDrive->hide();
ui->ledTune->hide();
ui->ledSwr->hide();
}
}
void RemoteOM::stopFaultBlinking()
{
if (faultTimer->isActive())
faultTimer->stop();
ui->ledGrid->hide();
ui->ledDrive->hide();
ui->ledTune->hide();
ui->ledSwr->hide();
}
void RemoteOM::stopWaitBlinking()
{
if (waitTimer->isActive())
{
waitTimer->stop();
ui->ledFault->hide();
}
}
void RemoteOM::startFaultTimer()
{
if (!faultTimer->isActive())
faultTimer->start(3000);
}