AlarmNotifications
PANDA Slow Control Alarm Daemon
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
desktopalarmwidget.cpp
Go to the documentation of this file.
1 
34 #include "desktopalarmwidget.h"
35 
36 #include <limits>
37 
38 #include <QApplication>
39 #include <QIcon>
40 #include <QInputDialog>
41 #include <QMessageBox>
42 #include <QtConcurrentRun>
43 
44 #include "alarmconfiguration.h"
45 #include "alarmserverconnector.h"
46 #include "beedo.h"
47 #include "oldgcccompat.h"
48 
49 using namespace AlarmNotifications;
50 
51 DesktopAlarmWidget::DesktopAlarmWidget ( const bool activateBeedo )
52  : _activateBeedo ( activateBeedo ),
53  _asc ( nullptr ),
54  _run ( true ),
55  _alarmActive ( false )
56 {
57  if ( _activateBeedo )
58  Beedo::instance(); // Initialize Beedo instance from main thread
60  _iconThread = QtConcurrent::run ( this, &DesktopAlarmWidget::observeAlarmStatus );
61  connect ( this, SIGNAL ( alarmStatusChanged() ), this, SLOT ( changeTrayIcon() ) );
62  connect ( this, SIGNAL ( notificationSwitchChanged ( bool ) ), this, SLOT ( notificationSwitchChange ( bool ) ) );
63 }
64 
66 {
67  _run = false;
68  _iconThread.waitForFinished();
69  delete _asc; // Do not need to check for nullptr, deleting nullptr is always safe in C++
70  if ( _activateBeedo )
72 }
73 
75 {
76  // No access to the object _asc points to, therefore no locking of the mutex
77  // This avoids dead-locks if getStatus() is called within a method already protected by the mutex
78  if ( _asc == nullptr )
79  {
80  return Disabled;
81  }
82  else
83  {
84  if ( _alarmActive )
85  return ActiveAlarm;
86  else
87  return ActiveOK;
88  }
89 }
90 
92 {
93  boost::lock_guard<boost::mutex> concurrency_lock ( _ascmutex );
94  if ( _asc == nullptr )
95  {
97  emit notificationSwitchChanged ( true );
98  }
99  else
100  {
101  delete _asc;
102  _asc = nullptr;
103  emit notificationSwitchChanged ( false );
104  }
105 }
106 
108 {
109  bool ok = false;
110  const int rawanswer = QInputDialog::getInteger (
111  nullptr,
112  QString::fromUtf8 ( "Configure notification timeout" ),
113  QString::fromUtf8 ( "Plese enter the time in seconds that should pass between\nthe reception of an alarm from the CSS Alarm Server\nand the display of a desktop notification:" ),
114  AlarmConfiguration::instance().getDesktopNotificationTimeout(),
115  1, // AlarmServerConnector would read a value of 0 as disabling desktop notifications
116  std::numeric_limits<int>::max(),
117  1,
118  &ok
119  );
120  if ( !ok )
121  return;
122  if ( rawanswer < 1 )
123  return;
124  const unsigned int answer = ( unsigned int ) rawanswer; // Any non-negative int will fit into unsigned int and negative ints are excluded above
128 }
129 
131 {
132  QString messagetext;
133  QString messagetitle = QString::fromUtf8 ( "Alarm notifications desktop widget" );
134  unsigned short messagetype; // 0 = info, 1 = warning, 2 = critical
135  {
136  boost::lock_guard<boost::mutex> concurrency_lock ( _ascmutex );
137  if ( _asc == nullptr )
138  {
139  messagetype = 1;
140  messagetext = QString::fromUtf8 ( "The Alarm notifications desktop widget is currently disabled, so informations about alarms are not recevied from the alarm server!" );
141  }
142  else
143  {
144  if ( _asc->getNumberOfAlarms() == 0 )
145  {
146  messagetype = 0;
147  messagetext = QString::fromUtf8 ( "No alarms are known to the Alarm notifications desktop widget.\n\nPlease note that alarms triggered before the start or re-activation of this widget have not been received, so if you want to be sure that nothing is wrong, look at the alarm display in CSS." );
148  }
149  else
150  {
151  messagetype = 2;
152  messagetext = QString::fromUtf8 ( "ATTENTION!\n\nThere are %1 alarm(s) active in the Detector Control System! For detailed information look at the alarm display in CSS!" ).arg ( _asc->getNumberOfAlarms() );
153  }
154  }
155  }
156  switch ( messagetype )
157  {
158  case 2:
159  QMessageBox::critical (
160  nullptr,
161  messagetitle,
162  messagetext
163  );
164  break;
165  case 1:
166  QMessageBox::warning (
167  nullptr,
168  messagetitle,
169  messagetext
170  );
171  break;
172  case 0:
173  default:
174  QMessageBox::information (
175  nullptr,
176  messagetitle,
177  messagetext
178  );
179  }
180 }
181 
183 {
184  QApplication::exit ( 0 );
185 }
186 
188 {
189  while ( _run )
190  {
191  {
192  boost::lock_guard<boost::mutex> concurrency_lock ( _ascmutex );
193  if ( _asc != nullptr )
194  {
195  if ( _alarmActive && _asc->getNumberOfAlarms() == 0 )
196  {
197  _alarmActive = false;
198  emit alarmStatusChanged();
199  }
200  if ( !_alarmActive && _asc->getNumberOfAlarms() != 0 )
201  {
202  _alarmActive = true;
203  emit alarmStatusChanged();
204  }
205  }
206  }
207  usleep ( 500*1000 ); // Check status every 0.5 seconds
208  }
209 }
210 
211 
212 
213 #include "desktopalarmwidget.moc"
void exitApplication()
Close the desktop widget.
void configureNotificationTimeout()
Change timespan between alarm trigger and notification display.
void observeAlarmStatus()
Thread to periodically check the status.
void destroy()
Destroy all media objects.
Definition: beedo.cpp:132
DesktopAlarmWidgetStatus
Status of the desktop widget.
void toggleNotifications()
Toggles the "main switch".
void alarmStatusChanged()
Alarm has been triggered or acknowledged.
virtual void notificationSwitchChange(bool enabled)=0
React on widget enable/disable.
#define nullptr
Allow using the nullptr keyword with GCC < 4.6.
Definition: oldgcccompat.h:56
void ReReadConfiguration()
Read configuration again from disk.
Compatibility macros for older versions of the GCC.
boost::mutex _ascmutex
Protect access to the _asc pointer.
static Beedo & instance()
Get singleton instance.
Definition: beedo.cpp:53
void showStatusMessage()
Show status dialog box.
void WriteConfiguration()
Write configuration to disk.
Provide an opto-acoustic alarm notification on a control room PC.
Namespace for Alarm Notifications application.
void notificationSwitchChanged(bool enabled)
Widget has been enabled or disabled.
DesktopAlarmWidgetStatus getStatus() const noexcept
Query alarm status.
QFuture< void > _iconThread
Alarm status observer thread.
Provides connectivity to the CSS Alarm Server.
size_t getNumberOfAlarms() const noexcept
Query number of active alarms.
#define noexcept
Allow using the noexcept keyword with GCC < 4.6.
Definition: oldgcccompat.h:52
virtual void changeTrayIcon()=0
Change the tray icon due to status change.
void setDesktopNotificationTimeout(const unsigned int newSetting)
Change the timeout for sending a desktop notification.
Provide a tray icon and desktop notifications in case of an alarm.
DesktopAlarmWidget(const bool activateBeedo=false)
Constructor.
Singleton to read and change the configuration of this application.
static AlarmConfiguration & instance() noexcept
Get singleton instance.
AlarmServerConnector * _asc
Pointer to the AlarmServerConnector instance.
const bool _activateBeedo
Beedo activation flag.