MythTV  master
signalmonitorvalue.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <utility>
3 
4 #include "signalmonitorvalue.h"
5 #include "mythlogging.h"
6 
11 
12 #define DEBUG_SIGNAL_MONITOR_VALUE 1
13 
20 {
21  if (run_static_init)
22  {
23  run_static_init = false;
24  ERROR_NO_CHANNEL<<"error"<<tr("Could not open tuner device");
25  ERROR_NO_LINK<<"error"<<tr("Bad connection to backend");
26 
27  SignalMonitorValue slock(
28  QCoreApplication::translate("(Common)", "Signal Lock"),
29  "slock", 0, true, 0, 1, 0ms);
30  slock.SetValue(1);
31  SIGNAL_LOCK<<slock.GetName()<<slock.GetStatus();
32  }
33 }
34 
36  QString _noSpaceName,
37  int _threshold,
38  bool _high_threshold,
39  int _min, int _max,
40  std::chrono::milliseconds _timeout) :
41  m_name(std::move(_name)),
42  m_noSpaceName(std::move(_noSpaceName)),
43  m_value(0),
44  m_threshold(_threshold),
45  m_minVal(_min), m_maxVal(_max), m_timeout(_timeout),
46  m_highThreshold(_high_threshold)
47 {
48  Init();
49 #if DEBUG_SIGNAL_MONITOR_VALUE
50  LOG(VB_GENERAL, LOG_DEBUG,
51  QString("SignalMonitorValue(%1, %2, %3, %4, %5, %6, %7, %8, %9)")
52  .arg(m_name) .arg(m_noSpaceName) .arg(m_value) .arg(m_threshold)
53  .arg(m_minVal) .arg(m_maxVal) .arg(m_timeout.count()) .arg(m_highThreshold)
54  .arg((m_set ? "true" : "false")));
55 #endif
56 }
57 
59  QString _noSpaceName,
60  int _value, int _threshold,
61  bool _high_threshold,
62  int _min, int _max,
63  std::chrono::milliseconds _timeout,
64  bool _set) :
65  m_name(std::move(_name)),
66  m_noSpaceName(std::move(_noSpaceName)),
67  m_value(_value),
68  m_threshold(_threshold),
69  m_minVal(_min), m_maxVal(_max), m_timeout(_timeout),
70  m_highThreshold(_high_threshold), m_set(_set)
71 {
72  Init();
73 #if DEBUG_SIGNAL_MONITOR_VALUE
74  LOG(VB_GENERAL, LOG_DEBUG,
75  QString("SignalMonitorValue(%1, %2, %3, %4, %5, %6, %7, %8, %9)")
76  .arg(m_name) .arg(m_noSpaceName) .arg(m_value) .arg(m_threshold)
77  .arg(m_minVal) .arg(m_maxVal) .arg(m_timeout.count()) .arg(m_highThreshold)
78  .arg((m_set ? "true" : "false")));
79 #endif
80 }
81 
82 QString SignalMonitorValue::GetName(void) const
83 {
84  if (m_name.isNull())
85  return QString();
86 
87  return m_name;
88 }
89 
91 {
92  if (m_noSpaceName.isNull())
93  return QString();
94 
95  return m_noSpaceName;
96 }
97 
98 bool SignalMonitorValue::Set(const QString& _name, const QString& _longString)
99 {
100  m_name = _name;
101  const QString& longString = _longString;
102 
103  if (m_name.isEmpty() || longString.isEmpty())
104  return false;
105 
106  if (("message" == m_name) || ("error" == m_name))
107  {
108  SetRange(0, 1);
109  SetValue(0);
110  SetThreshold( ("message" == m_name) ? 0 : 1, true );
111  SetTimeout( ("message" == m_name) ? 0ms : -1ms );
113  m_name = longString;
114 
115  return true;
116  }
117 
118 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
119  QStringList vals = longString.split(" ", QString::SkipEmptyParts);
120 #else
121  QStringList vals = longString.split(" ", Qt::SkipEmptyParts);
122 #endif
123 
124  if (8 != vals.size() || "(null)" == vals[0])
125  return false;
126 
127  m_noSpaceName = vals[0];
128  SetRange(vals[3].toInt(), vals[4].toInt());
129  SetValue(vals[1].toInt());
130  SetThreshold(vals[2].toInt(), (bool) vals[6].toInt());
131  SetTimeout(std::chrono::milliseconds(vals[5].toInt()));
132 
133  m_set = (bool) vals[7].toInt();
134  return true;
135 }
136 
138  const QString& _longString)
139 {
140  auto *smv = new SignalMonitorValue();
141  if (!smv->Set(_name, _longString))
142  {
143  delete smv;
144  return nullptr;
145  }
146  return smv;
147 }
148 
154 {
155  SignalMonitorValue smv;
156  SignalMonitorList monitor_list;
157  for (int i=0; i+1<slist.size(); i+=2)
158  {
159 #if DEBUG_SIGNAL_MONITOR_VALUE
160  LOG(VB_GENERAL, LOG_DEBUG,
161  "Parse(" + slist[i] + ", (" + slist[i+1] + "))");
162 #endif
163  if (smv.Set(slist[i], slist[i+1]))
164  monitor_list.push_back(smv);
165  else
166  {
167  LOG(VB_GENERAL, LOG_ERR,
168  QString("SignalMonitorValue::Parse(): Error, "
169  "unable to parse (%1, (%2))")
170  .arg(slist[i]).arg(slist[i+1]));
171  }
172  }
173  return monitor_list;
174 }
175 
181 {
182  auto isgood = [](const auto & smv){ return smv.IsGood(); };
183  bool good = std::all_of(slist.cbegin(), slist.cend(), isgood);
184 #if DEBUG_SIGNAL_MONITOR_VALUE
185  if (!good)
186  {
187  QString msg("AllGood failed on ");
188  for (const auto & smv : slist)
189  {
190  if (!smv.IsGood())
191  {
192  msg += smv.m_noSpaceName;
193  msg += QString("(%1%2%3) ")
194  .arg(smv.GetValue())
195  .arg(smv.m_highThreshold ? "<" : ">")
196  .arg(smv.GetThreshold());
197  }
198  }
199  LOG(VB_GENERAL, LOG_DEBUG, msg);
200  }
201 #endif
202  return good;
203 }
204 
209 std::chrono::milliseconds SignalMonitorValue::MaxWait(const SignalMonitorList& slist)
210 {
211  std::chrono::milliseconds wait = 0ms;
212  std::chrono::milliseconds minWait = 0s;
213  for (const auto & smv : slist)
214  {
215  wait = std::max(wait, smv.GetTimeout());
216  minWait = std::min(minWait, smv.GetTimeout());
217  }
218  return (minWait < 0ms) ? -1ms : wait;
219 }
SignalMonitorValue::Parse
static SignalMonitorList Parse(const QStringList &slist)
Converts a list of strings to SignalMonitorValue classes.
Definition: signalmonitorvalue.cpp:153
bool
bool
Definition: pxsup2dast.c:30
SignalMonitorValue::SetValue
void SetValue(int _value)
Definition: signalmonitorvalue.h:74
SignalMonitorValue::SetThreshold
void SetThreshold(int _threshold)
Definition: signalmonitorvalue.h:84
SignalMonitorValue::GetStatus
QString GetStatus() const
Returns a signal monitor value as one long string.
Definition: signalmonitorvalue.h:32
SignalMonitorValue::Create
static SignalMonitorValue * Create(const QString &_name, const QString &_longString)
Definition: signalmonitorvalue.cpp:137
SignalMonitorValue::m_value
int m_value
Definition: signalmonitorvalue.h:139
signalmonitorvalue.h
SignalMonitorValue::MaxWait
static std::chrono::milliseconds MaxWait(const SignalMonitorList &slist)
Returns the maximum timeout value in the signal monitor list.
Definition: signalmonitorvalue.cpp:209
SignalMonitorValue::SIGNAL_LOCK
static QStringList SIGNAL_LOCK
Definition: signalmonitorvalue.h:116
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
SignalMonitorValue::m_set
bool m_set
Definition: signalmonitorvalue.h:145
vals
static float * vals
Definition: tentacle3d.cpp:18
SignalMonitorValue::GetName
QString GetName(void) const
Returns the long name of this value.
Definition: signalmonitorvalue.cpp:82
SignalMonitorValue::m_maxVal
int m_maxVal
Definition: signalmonitorvalue.h:142
SignalMonitorValue::SetTimeout
void SetTimeout(std::chrono::milliseconds _timeout)
Definition: signalmonitorvalue.h:98
SignalMonitorValue::GetShortName
QString GetShortName(void) const
Returns a space free name of the value. Used by GetStatus().
Definition: signalmonitorvalue.cpp:90
mythlogging.h
SignalMonitorValue::run_static_init
static bool run_static_init
Definition: signalmonitorvalue.h:118
SignalMonitorValue::ERROR_NO_CHANNEL
static QStringList ERROR_NO_CHANNEL
Definition: signalmonitorvalue.h:114
SignalMonitorValue
Definition: signalmonitorvalue.h:13
SignalMonitorValue::ERROR_NO_LINK
static QStringList ERROR_NO_LINK
Definition: signalmonitorvalue.h:115
SignalMonitorValue::SetRange
void SetRange(int _min, int _max)
Sets the minimum and maximum values.
Definition: signalmonitorvalue.h:93
SignalMonitorValue::m_threshold
int m_threshold
Definition: signalmonitorvalue.h:140
SignalMonitorValue::m_minVal
int m_minVal
Definition: signalmonitorvalue.h:141
SignalMonitorValue::SignalMonitorList
std::vector< SignalMonitorValue > SignalMonitorList
Definition: signalmonitorvalue.h:17
SignalMonitorValue::m_noSpaceName
QString m_noSpaceName
Definition: signalmonitorvalue.h:138
SignalMonitorValue::m_timeout
std::chrono::milliseconds m_timeout
Definition: signalmonitorvalue.h:143
std
Definition: mythchrono.h:23
SignalMonitorValue::m_highThreshold
bool m_highThreshold
Definition: signalmonitorvalue.h:144
SignalMonitorValue::m_name
QString m_name
Definition: signalmonitorvalue.h:137
SignalMonitorValue::AllGood
static bool AllGood(const SignalMonitorList &slist)
Returns true if all the values in the list return true on IsGood().
Definition: signalmonitorvalue.cpp:180
SignalMonitorValue::SignalMonitorValue
SignalMonitorValue()=default
SignalMonitorValue::Set
bool Set(const QString &_name, const QString &_longString)
Definition: signalmonitorvalue.cpp:98
SignalMonitorValue::Init
static void Init()
Initializes the some static constants needed by SignalMonitorValue.
Definition: signalmonitorvalue.cpp:19