29 #include <QApplication>
36 #include <QMessageBox>
38 #include "trm/TunerState.h"
40 #include "TRMMonitorGUI.h"
41 #include "TRMMonitor.h"
45 #define TUNER_REFRESH_INTERVAL 5000
46 #define TOTAL_TUNER_NUMBER 4
47 #define TOTAL_DEVICE_NUMBER 5
48 #define TOTAL_LOCATOR_NUMBER 7
49 #define RESERVATION_BAR_WIDTH 550
51 #define RBW RESERVATION_BAR_WIDTH
52 #define SECOND_SCALE 1 //how many actual seconds is for each 1 second in the timespin box
54 int RESERVATION_BAR_TIME_WIDTH = 0;
56 const char *deviceNames[TOTAL_DEVICE_NUMBER] = {
64 const char *locatorNames[TOTAL_LOCATOR_NUMBER] = {
74 TRMMonitorGUI::~TRMMonitorGUI(
void)
78 TRMMonitorGUI::TRMMonitorGUI(
const QHostAddress &address, quint16 port,
const QString &clientId,
const QString &barWidth, QObject *parent) :
79 QMainWindow(), monitorUI(0), hostAddress(address), portNumber(port), clientId(clientId)
82 uint32_t connectionClientId = clientId.toUInt(&ok, 16);
83 uint32_t barWidthSeconds = barWidth.toUInt(&ok, 10);
85 qDebug() <<
"connectionClientId to " << connectionClientId;
86 qDebug() <<
"barWidthSeconds is " << barWidthSeconds;
88 RESERVATION_BAR_TIME_WIDTH = barWidthSeconds;
90 if (!ok) connectionClientId = 0;
91 monitor =
new TRMMonitor(hostAddress, portNumber, connectionClientId);
93 UNUSED_VARIABLE(parent);
94 monitorUI =
new Ui_TRMMonitor;
95 monitorUI->setupUi(
this);
101 qDebug() <<
"Setting Window Title to " << clientId;
102 setWindowTitle(clientId);
105 QWidget::setTabOrder(monitorUI->deviceListComboBox, monitorUI->startTimeHourSpinBox);
106 QWidget::setTabOrder(monitorUI->startTimeHourSpinBox, monitorUI->startTimeMinSpinBox);
107 QWidget::setTabOrder(monitorUI->startTimeMinSpinBox, monitorUI->durationHourSpinBox);
108 QWidget::setTabOrder(monitorUI->durationHourSpinBox, monitorUI->durationMinSpinBox);
109 QWidget::setTabOrder(monitorUI->durationMinSpinBox, monitorUI->locatorListComboBox);
110 QWidget::setTabOrder(monitorUI->locatorListComboBox, monitorUI->activityLiveRadioButton);
111 QWidget::setTabOrder(monitorUI->activityLiveRadioButton, monitorUI->activityRecordRadioButton);
112 QWidget::setTabOrder(monitorUI->activityRecordRadioButton, monitorUI->activityEASRadioButton);
113 QWidget::setTabOrder(monitorUI->activityEASRadioButton, monitorUI->reserveButton);
118 for (
int i = 0; i < TOTAL_DEVICE_NUMBER; i++) {
119 monitorUI->deviceListComboBox->insertItem(0, deviceNames[TOTAL_DEVICE_NUMBER - i - 1]);
124 for (
int i = 0; i < TOTAL_LOCATOR_NUMBER; i++) {
125 monitorUI->locatorListComboBox->insertItem(0, locatorNames[TOTAL_LOCATOR_NUMBER - i - 1]);
129 monitorUI->deviceListComboBox->setCurrentIndex(0);
132 QTimer *timer =
new QTimer(monitorUI->currentTimeLabel);
133 QObject::connect(timer, SIGNAL(timeout()),
this, SLOT(onTimestampTimerTimeout()));
134 onTimestampTimerTimeout();
139 QObject::connect(monitorUI->reserveButton, SIGNAL(clicked()),
this, SLOT(onReserveButtonClicked()));
144 monitor->sendGetAllTunerIds();
146 QTimer *timer =
new QTimer(
this);
147 QObject::connect(timer, SIGNAL(timeout()),
this, SLOT(onTunerStateUpdateTimerTimeout()));
148 onTunerStateUpdateTimerTimeout();
149 timer->start(TUNER_REFRESH_INTERVAL);
151 QObject::connect(monitor,SIGNAL(tunerStatesUpdated(std::map<std::string, std::string>)),
this, SLOT(onTunerStatesUpdated(std::map<std::string, std::string>)));
152 QObject::connect(monitor,SIGNAL(tunerIdsUpdated(std::list<std::string>)),
this, SLOT(onTunerIdsUpdated(std::list<std::string>)));
153 QObject::connect(monitor,SIGNAL(tunerReservationsUpdated(std::map<std::string, std::list<TunerReservation> >)),
this, SLOT(onTunerReservationsUpdated(std::map<std::string, std::list<TunerReservation> >)));
154 QObject::connect(monitor,SIGNAL(statusMessageReceived(std::string)),
this, SLOT(onStatusMessageReceived(std::string)));
155 QObject::connect(monitor,SIGNAL(conflictsReceived(ReserveTunerResponse::ConflictCT)),
this, SLOT(onConflictsReceived(ReserveTunerResponse::ConflictCT)));
162 void TRMMonitorGUI::drawReservationBar(QLabel *label,
TunerReservation *reservation)
164 int totalWidth = label->width();
165 int totalDuration = RESERVATION_BAR_TIME_WIDTH;
166 qint64 durationWidth = totalWidth;
167 QString labelText =
"";
168 qint64 now = QDateTime(QDate::currentDate(), QTime::currentTime()).toMSecsSinceEpoch();
169 QColor fillColor3(255, 153, 51);
170 QColor fillColor2(0xFF, 0xC1, 0xC1);
171 QColor fillColor1(0xBD, 0xFC, 0xC9);
172 QColor fillColor(0xE8, 0xE8, 0xE8);
173 QPixmap reservationBar;
174 if (reservation == 0) {
175 reservationBar = QPixmap(totalWidth, 30);
176 reservationBar.fill(fillColor);
179 reservationBar = QPixmap(*label->pixmap());
186 qint64 currentEpoch = QDateTime(QDate::currentDate(), QTime::currentTime()).toMSecsSinceEpoch();
189 if (reservation != 0) {
196 label->setVisible(
true);
197 if (reservation->
getActivity() == Activity::kLive) {
198 labelText =
" [" + QString(reservation->
getDevice().c_str()) +
"] - [" + QString(reservation->
getServiceLocator().c_str()) +
"]";
199 fillColor = fillColor1;
203 fillColor = fillColor2;
207 fillColor = fillColor3;
212 durationWidth = (reservation->
getDuration() * totalWidth) / (totalDuration * 1000);
227 durationWidth = (duration * label->width()) / (totalDuration * 1000);
230 qDebug() <<
"Duration Width is " << durationWidth;
233 int state = TunerState::kHybrid;
235 painter.begin(&reservationBar);
237 if (reservation != 0){
243 startX = (((reservation->
getStartTime() - now) * label->width()) / (totalDuration * 1000)) + 2;
248 painter.fillRect(startX, 0, durationWidth, 30, fillColor);
249 painter.drawText(QPoint(0, 20), labelText);
251 label->setPixmap(reservationBar);
254 void TRMMonitorGUI::setTunerStateIndicator(
const QString &tunerId,
const TunerState& state)
256 qDebug() <<
"setTunerStateIndicator " << tunerId <<
" for state " << (
const char *)(state.getState());;
258 tunerStateIndicatorLabels[tunerId]->clear();
259 QPixmap labelSquare = QPixmap(60, 60);
260 QColor fillColor = Qt::darkGray;
261 QColor fillColor2 = Qt::darkGray;
263 if (state == TunerState::kFree) {
264 fillColor = Qt::darkGray;
265 fillColor2 = Qt::darkGray;
266 }
else if (state == TunerState::kLive) {
267 fillColor = Qt::darkGreen;
268 fillColor2 = Qt::darkGreen;
269 }
else if (state == TunerState::kRecord) {
270 fillColor = Qt::darkRed;
271 fillColor2 = Qt::darkRed;
272 }
else if (state == TunerState::kHybrid) {
273 fillColor = Qt::darkGreen;
274 fillColor2 = Qt::darkRed;
275 }
else if (state == TunerState::kEAS) {
277 fillColor2 = Qt::red;
280 labelSquare.fill(fillColor);
282 painter.begin(&labelSquare);
283 painter.fillRect(0, 0, 30, 60, fillColor);
284 painter.fillRect(30, 0, 60, 60, fillColor2);
286 tunerStateIndicatorLabels[tunerId]->setPixmap(labelSquare);
289 void TRMMonitorGUI::onReserveButtonClicked(
void)
294 QTime startQTime(monitorUI->startTimeHourSpinBox->value(), monitorUI->startTimeMinSpinBox->value());
296 if (monitorUI->startTimeHourSpinBox->value() == 0 && monitorUI->startTimeMinSpinBox->value() == 0) {
297 startQTime = QTime::currentTime();
301 qint64 startTime = QDateTime(QDate::currentDate(), startQTime).toMSecsSinceEpoch();
304 qint64 startTime = QDateTime(QDate::currentDate(), QTime::currentTime()).toMSecsSinceEpoch() ;
305 startTime += ((monitorUI->startTimeHourSpinBox->value() * 60 + monitorUI->startTimeMinSpinBox->value() * 10) * SECOND_SCALE * 1000);
311 qint64 duration = (monitorUI->durationHourSpinBox->value() * 60 + monitorUI->durationMinSpinBox->value()) * SECOND_SCALE * 1000;
314 QString device = deviceNames[monitorUI->deviceListComboBox->currentIndex()];
315 QString locator = locatorNames[monitorUI->locatorListComboBox->currentIndex()];
317 if (monitorUI->activityLiveRadioButton->isChecked()) {
320 else if (monitorUI->activityRecordRadioButton->isChecked()) {
323 else if (monitorUI->activityEASRadioButton->isChecked()) {
330 qDebug() <<
"onReserveButtonClicked";
332 monitor->sendTunerReserve(device, startTime, duration, locator, activity,
"");
335 void TRMMonitorGUI::onTimestampTimerTimeout(
void)
337 monitorUI->currentTimeLabel->setText(QTime::currentTime().toString());
340 void TRMMonitorGUI::onTunerStateUpdateTimerTimeout(
void)
342 qDebug() <<
"Updating Tuner State";
343 monitor->sendGetAllTunerStates();
344 monitor->sendGetAllReservations();
347 void TRMMonitorGUI::onTunerIdsUpdated(std::list<std::string> tunerIds)
349 qDebug() <<
"onTunerIdsUpdated";
351 std::list<std::string>::iterator it = tunerIds.begin();
352 Q_ASSERT(tunerIds.size() == TOTAL_TUNER_NUMBER);
354 tunerStateIndicatorLabels.insert(QString((*it).c_str()), monitorUI->tuner1Label); it++;
355 tunerStateIndicatorLabels.insert(QString((*it).c_str()), monitorUI->tuner2Label); it++;
356 tunerStateIndicatorLabels.insert(QString((*it).c_str()), monitorUI->tuner3Label); it++;
357 tunerStateIndicatorLabels.insert(QString((*it).c_str()), monitorUI->tuner4Label); it++;
361 it = tunerIds.begin();
362 QMap<QString, QLabel *> tunerReservationBarLabel;
363 tunerReservationBarLabel.clear();
364 tunerReservationBarLabel.insert(QString((
const char *)Activity::kLive), monitorUI->tuner1LiveReservationIndicationBar);
365 tunerReservationBarLabel.insert(QString((
const char *)Activity::kRecord), monitorUI->tuner1RecordReservationIndicationBar);
366 tunerReservationBarLabels.insert(QString((*it).c_str()),tunerReservationBarLabel); it++;
368 tunerReservationBarLabel.clear();
369 tunerReservationBarLabel.insert(QString((
const char *)Activity::kLive), monitorUI->tuner2LiveReservationIndicationBar);
370 tunerReservationBarLabel.insert(QString((
const char *)Activity::kRecord), monitorUI->tuner2RecordReservationIndicationBar);
371 tunerReservationBarLabels.insert(QString((*it).c_str()),tunerReservationBarLabel); it++;
373 tunerReservationBarLabel.clear();
374 tunerReservationBarLabel.insert(QString((
const char *)Activity::kLive), monitorUI->tuner3LiveReservationIndicationBar);
375 tunerReservationBarLabel.insert(QString((
const char *)Activity::kRecord), monitorUI->tuner3RecordReservationIndicationBar);
376 tunerReservationBarLabels.insert(QString((*it).c_str()),tunerReservationBarLabel); it++;
378 tunerReservationBarLabel.clear();
379 tunerReservationBarLabel.insert((
const char *)Activity::kLive, monitorUI->tuner4LiveReservationIndicationBar);
380 tunerReservationBarLabel.insert((
const char *)Activity::kRecord, monitorUI->tuner4RecordReservationIndicationBar);
381 tunerReservationBarLabels.insert(QString((*it).c_str()),tunerReservationBarLabel); it++;
387 const std::list<std::string> &tunerIds = monitor->getTunerIds();
388 std::list<std::string>::const_iterator it = tunerIds.begin();
390 for (it = tunerIds.begin(); it != tunerIds.end(); it++) {
391 setTunerStateIndicator(QString((*it).c_str()), TunerState::kFree);
396 monitorUI->tuner1LiveReservationIndicationBar->setVisible(
true);
397 monitorUI->tuner2LiveReservationIndicationBar->setVisible(
true);
398 monitorUI->tuner3LiveReservationIndicationBar->setVisible(
true);
399 monitorUI->tuner4LiveReservationIndicationBar->setVisible(
true);
401 monitorUI->tuner1RecordReservationIndicationBar->setVisible(
true);
402 monitorUI->tuner2RecordReservationIndicationBar->setVisible(
true);
403 monitorUI->tuner3RecordReservationIndicationBar->setVisible(
true);
404 monitorUI->tuner4RecordReservationIndicationBar->setVisible(
true);
406 drawReservationBar(monitorUI->tuner1LiveReservationIndicationBar, 0);
407 drawReservationBar(monitorUI->tuner2LiveReservationIndicationBar, 0);
408 drawReservationBar(monitorUI->tuner3LiveReservationIndicationBar, 0);
409 drawReservationBar(monitorUI->tuner4LiveReservationIndicationBar, 0);
411 drawReservationBar(monitorUI->tuner1RecordReservationIndicationBar, 0);
412 drawReservationBar(monitorUI->tuner2RecordReservationIndicationBar, 0);
413 drawReservationBar(monitorUI->tuner3RecordReservationIndicationBar, 0);
414 drawReservationBar(monitorUI->tuner4RecordReservationIndicationBar, 0);
419 QList<QString> tunerIds = tunerReservationBarLabels.keys();
420 for (
int i = 0; i < tunerIds.size(); i++) {
421 QList<QString> activities = tunerReservationBarLabels[tunerIds[i]].keys();
422 for (
int j = 0; j < activities.size(); j++) {
423 QLabel *reservationIndicationBar = tunerReservationBarLabels[tunerIds[i]][activities[j]];
424 qDebug() <<
"Adding Action for " << tunerIds[i] <<
"for activity" << activities[j];
425 QAction *releaseTunerReservationAction =
new QAction(tr(
"Release"),
this);
426 releaseTunerReservationAction->setVisible(
false);
427 releaseTunerReservationAction->setEnabled(
false);
428 releaseTunerReservationAction->setData(0);
429 reservationIndicationBar->addAction(releaseTunerReservationAction);
430 QObject::connect(releaseTunerReservationAction, SIGNAL(triggered()),
this, SLOT(onReleaseTunerReservationActionTriggered()));
432 QAction *validateTunerReservationAction =
new QAction(tr(
"Validate"),
this);
433 validateTunerReservationAction->setVisible(
false);
434 validateTunerReservationAction->setEnabled(
false);
435 validateTunerReservationAction->setData(0);
436 reservationIndicationBar->addAction(validateTunerReservationAction);
437 QObject::connect(validateTunerReservationAction, SIGNAL(triggered()),
this, SLOT(onValidateTunerReservationActionTriggered()));
439 QAction *cancelRecordingAction =
new QAction(tr(
"CancelRecording"),
this);
440 cancelRecordingAction->setVisible(
false);
441 cancelRecordingAction->setEnabled(
false);
442 cancelRecordingAction->setData(0);
443 reservationIndicationBar->addAction(cancelRecordingAction);
444 QObject::connect(cancelRecordingAction, SIGNAL(triggered()),
this, SLOT(onCancelRecordingActionTriggered()));
446 QAction *renewTunerReservationAction =
new QAction(tr(
"Renew"),
this);
447 renewTunerReservationAction->setVisible(
false);
448 renewTunerReservationAction->setEnabled(
false);
449 renewTunerReservationAction->setData(0);
450 reservationIndicationBar->addAction(renewTunerReservationAction);
451 QObject::connect(renewTunerReservationAction, SIGNAL(triggered()),
this, SLOT(onRenewTunerReservationActionTriggered()));
453 reservationIndicationBar->setContextMenuPolicy(Qt::ActionsContextMenu);
461 void TRMMonitorGUI::onTunerStatesUpdated(std::map<std::string, std::string> states)
463 qDebug() <<
"onTunerStatesUpdated";
464 std::map<std::string, std::string>::iterator it = states.begin();
465 Q_ASSERT(states.size() == TOTAL_TUNER_NUMBER);
466 for (it = states.begin(); it != states.end(); it++ ) {
467 qDebug() <<
"Updating tuner [" << (*it).first.c_str() <<
"] state to " << (*it).second.c_str();
468 setTunerStateIndicator((*it).first.c_str(),
TunerState((*it).second.c_str()));
469 tunerStates[(*it).first.c_str()] = (*it).second.c_str();
473 void TRMMonitorGUI::onTunerReservationsUpdated(std::map<std::string, std::list<TunerReservation> > reservations)
476 std::map<std::string, std::list<TunerReservation> >::iterator it = reservations.begin();
478 qDebug() <<
"onTunerReservationsUpdated===========================with tuner size === " << reservations.size();
481 drawReservationBar(monitorUI->tuner1LiveReservationIndicationBar, 0);
482 drawReservationBar(monitorUI->tuner2LiveReservationIndicationBar, 0);
483 drawReservationBar(monitorUI->tuner3LiveReservationIndicationBar, 0);
484 drawReservationBar(monitorUI->tuner4LiveReservationIndicationBar, 0);
486 drawReservationBar(monitorUI->tuner1RecordReservationIndicationBar, 0);
487 drawReservationBar(monitorUI->tuner2RecordReservationIndicationBar, 0);
488 drawReservationBar(monitorUI->tuner3RecordReservationIndicationBar, 0);
489 drawReservationBar(monitorUI->tuner4RecordReservationIndicationBar, 0);
492 for (it = reservations.begin(); it != reservations.end(); it++ ) {
493 std::list<TunerReservation> & tunerReservations = (*it).second;
495 qDebug() <<
"===========================Updating tuner [" << (*it).first.c_str() <<
"]";
498 QList<QAction *> actions = tunerReservationBarLabels[(*it).first.c_str()][(
const char *)Activity::kLive]->actions();
499 for(
int j = 0; j < actions.size(); j++) {
500 actions[j]->setVisible(
false);
501 actions[j]->setEnabled(
false);
504 actions[j]->setData(0);
506 drawReservationBar(tunerReservationBarLabels[(*it).first.c_str()][(
const char *)Activity::kLive], 0);
508 actions = tunerReservationBarLabels[(*it).first.c_str()][(
const char *)Activity::kRecord]->actions();
509 for(
int j = 0; j < actions.size(); j++) {
510 actions[j]->setVisible(
false);
511 actions[j]->setEnabled(
false);
514 actions[j]->setData(0);
516 drawReservationBar(tunerReservationBarLabels[(*it).first.c_str()][(
const char *)Activity::kRecord], 0);
520 std::list<TunerReservation>::iterator it2 = tunerReservations.begin();
521 for (it2 = tunerReservations.begin(); it2 != tunerReservations.end(); it2++) {
522 qDebug() <<
" udpating action for "
523 << QString((*it2).getReservationToken().c_str())
524 <<
" WITH STAR TIME " << (*it2).getStartTime()
525 <<
" WITH DURATION " << (*it2).getDuration()
528 QLabel *reservationBar = tunerReservationBarLabels[(*it).first.c_str()][(
const char *)((*it2).getActivity().getActivity())];
529 QList<QAction *> actions = reservationBar->actions();
530 for(
int k = 0; k < actions.size(); k++) {
531 actions[k]->setVisible(
true);
532 actions[k]->setEnabled(
true);
536 *copyReservation = (*it2);
537 actions[k]->setData((
int)copyReservation);
540 actions[k]->setData(QString((*it2).getReservationToken().c_str()));
543 drawReservationBar(reservationBar, &(*it2));
551 void TRMMonitorGUI::onReleaseTunerReservationActionTriggered(
void)
553 QAction *action = qobject_cast<QAction *>(sender());
558 if (!reservationToken.isEmpty()) {
559 monitor->sendReleaseTunerReservation(reservationToken);
562 qDebug() <<
"sendReleaseTunerReservation done";
565 void TRMMonitorGUI::onValidateTunerReservationActionTriggered(
void)
567 QAction *action = qobject_cast<QAction *>(sender());
572 if (!reservationToken.isEmpty()) {
573 monitor->sendValidateTunerReservation(
"MonitorUI", reservationToken);
576 qDebug() <<
"sendValidateTunerReservation done";
579 void TRMMonitorGUI::onCancelRecordingActionTriggered(
void)
581 QAction *action = qobject_cast<QAction *>(sender());
586 if (!reservationToken.isEmpty()) {
587 monitor->sendCancelRecording(
"MonitorUI", reservationToken);
590 qDebug() <<
"sendValidateTunerReservation done";
593 void TRMMonitorGUI::onRenewTunerReservationActionTriggered(
void)
595 QAction *action = qobject_cast<QAction *>(sender());
598 monitor->sendTunerReserve(reservation->
getDevice().c_str(),
599 QDateTime(QDate::currentDate(), QTime::currentTime()).toMSecsSinceEpoch(),
605 qDebug() <<
"sendRenewTunerReservation done";
609 void TRMMonitorGUI::onStatusMessageReceived(std::string statusMessage)
612 monitorUI->logTextEdit->clear();
613 monitorUI->logTextEdit->setText(QString(statusMessage.c_str()));
616 void TRMMonitorGUI::onConflictsReceived(ReserveTunerResponse::ConflictCT conflicts)
618 QString conflictMessage;
619 ReserveTunerResponse::ConflictCT::iterator it = conflicts.begin();
621 conflictMessage += (
"<b>" + clientId +
"<br>");
622 conflictMessage +=
"<b>Requested Activity</b> is in conflict with the following group of activities\r\n\r\n<br>";
623 conflictMessage += (
"<br>");
624 conflictMessage += (
"<br>");
626 for (it = conflicts.begin(); it != conflicts.end(); it++) {
627 conflictMessage +=
" " + QString((
const char *)((*it).getActivity().getActivity())) +
" : " + QString((*it).getServiceLocator().c_str()) +
"<br>";
628 conflictMessage += (
"<br>");
631 conflictMessage += (
"<br>");
632 conflictMessage += (
"<br>");
634 conflictMessage +=
"Please cancel one group and retry your request<br>";
635 QMessageBox::information(
this,
"Conflicts", conflictMessage);