00001 #include <algorithm>
00002 using namespace std;
00003
00004 #include "signalmonitorvalue.h"
00005 #include "mythlogging.h"
00006
00007 bool SignalMonitorValue::run_static_init = true;
00008 QStringList SignalMonitorValue::ERROR_NO_CHANNEL;
00009 QStringList SignalMonitorValue::ERROR_NO_LINK;
00010 QStringList SignalMonitorValue::SIGNAL_LOCK;
00011
00012 #define DEBUG_SIGNAL_MONITOR_VALUE 0
00013
00019 void SignalMonitorValue::Init()
00020 {
00021 if (run_static_init)
00022 {
00023 run_static_init = false;
00024 ERROR_NO_CHANNEL<<"error"<<QObject::tr("Could not open tuner device");
00025 ERROR_NO_LINK<<"error"<<QObject::tr("Bad connection to backend");
00026
00027 SignalMonitorValue slock(
00028 QObject::tr("Signal Lock"), "slock", 0, true, 0, 1, 0);
00029 slock.SetValue(1);
00030 SIGNAL_LOCK<<slock.GetName()<<slock.GetStatus();
00031 }
00032 }
00033
00034 SignalMonitorValue::SignalMonitorValue(const QString& _name,
00035 const QString& _noSpaceName,
00036 int _threshold,
00037 bool _high_threshold,
00038 int _min, int _max,
00039 int _timeout) :
00040 name(_name),
00041 noSpaceName(_noSpaceName),
00042 value(0),
00043 threshold(_threshold),
00044 minval(_min), maxval(_max), timeout(_timeout),
00045 high_threshold(_high_threshold), set(false)
00046 {
00047 name.detach();
00048 noSpaceName.detach();
00049
00050 Init();
00051 #if DEBUG_SIGNAL_MONITOR_VALUE
00052 LOG(VB_GENERAL, LOG_DEBUG,
00053 QString("SignalMonitorValue(%1, %2, %3, %4, %5, %6, %7, %8, %9)")
00054 .arg(name) .arg(noSpaceName) .arg(value) .arg(threshold)
00055 .arg(minval) .arg(maxval) .arg(timeout) .arg(high_threshold)
00056 .arg((set ? "true" : "false")));
00057 #endif
00058 }
00059
00060 SignalMonitorValue::SignalMonitorValue(const QString& _name,
00061 const QString& _noSpaceName,
00062 int _value, int _threshold,
00063 bool _high_threshold,
00064 int _min, int _max,
00065 int _timeout, bool _set) :
00066 name(_name),
00067 noSpaceName(_noSpaceName),
00068 value(_value),
00069 threshold(_threshold),
00070 minval(_min), maxval(_max), timeout(_timeout),
00071 high_threshold(_high_threshold), set(_set)
00072 {
00073 name.detach();
00074 noSpaceName.detach();
00075
00076 Init();
00077 #if DEBUG_SIGNAL_MONITOR_VALUE
00078 LOG(VB_GENERAL, LOG_DEBUG,
00079 QString("SignalMonitorValue(%1, %2, %3, %4, %5, %6, %7, %8, %9)")
00080 .arg(name) .arg(noSpaceName) .arg(value) .arg(threshold)
00081 .arg(minval) .arg(maxval) .arg(timeout) .arg(high_threshold)
00082 .arg((set ? "true" : "false")));
00083 #endif
00084 }
00085
00086 QString SignalMonitorValue::GetName(void) const
00087 {
00088 if (name.isNull())
00089 return QString::null;
00090
00091 QString ret = name;
00092 ret.detach();
00093 return ret;
00094 }
00095
00096 QString SignalMonitorValue::GetShortName(void) const
00097 {
00098 if (noSpaceName.isNull())
00099 return QString::null;
00100
00101 QString ret = noSpaceName;
00102 ret.detach();
00103 return ret;
00104 }
00105
00106 bool SignalMonitorValue::Set(const QString& _name, const QString& _longString)
00107 {
00108 name = _name;
00109 QString longString = _longString;
00110
00111 if (QString::null == name || QString::null == longString)
00112 return false;
00113
00114 if (("message" == name) || ("error" == name))
00115 {
00116 SetRange(0, 1);
00117 SetValue(0);
00118 SetThreshold( ("message" == name) ? 0 : 1, true );
00119 SetTimeout( ("message" == name) ? 0 : -1 );
00120 noSpaceName = name;
00121 name = longString;
00122
00123 return true;
00124 }
00125
00126 QStringList vals = longString.split(" ", QString::SkipEmptyParts);
00127
00128 if (8 != vals.size() || "(null)" == vals[0])
00129 return false;
00130
00131 noSpaceName = vals[0];
00132 SetRange(vals[3].toInt(), vals[4].toInt());
00133 SetValue(vals[1].toInt());
00134 SetThreshold(vals[2].toInt(), (bool) vals[6].toInt());
00135 SetTimeout(vals[5].toInt());
00136
00137 set = (bool) vals[7].toInt();
00138
00139 name.detach();
00140 noSpaceName.detach();
00141
00142 return true;
00143 }
00144
00145 SignalMonitorValue* SignalMonitorValue::Create(const QString& _name,
00146 const QString& _longString)
00147 {
00148 SignalMonitorValue *smv = new SignalMonitorValue();
00149 if (!smv->Set(_name, _longString))
00150 {
00151 delete smv;
00152 return NULL;
00153 }
00154 return smv;
00155 }
00156
00161 SignalMonitorList SignalMonitorValue::Parse(const QStringList& slist)
00162 {
00163 SignalMonitorValue smv;
00164 SignalMonitorList monitor_list;
00165 for (int i=0; i+1<slist.size(); i+=2)
00166 {
00167 #if DEBUG_SIGNAL_MONITOR_VALUE
00168 LOG(VB_GENERAL, LOG_DEBUG,
00169 "Parse(" + slist[i] + ", (" + slist[i+1] + "))");
00170 #endif
00171 if (smv.Set(slist[i], slist[i+1]))
00172 monitor_list.push_back(smv);
00173 else
00174 {
00175 LOG(VB_GENERAL, LOG_ERR,
00176 QString("SignalMonitorValue::Parse(): Error, "
00177 "unable to parse (%1, (%2))")
00178 .arg(slist[i]).arg(slist[i+1]));
00179 }
00180 }
00181 return monitor_list;
00182 }
00183
00188 bool SignalMonitorValue::AllGood(const SignalMonitorList& slist)
00189 {
00190 bool good = true;
00191 SignalMonitorList::const_iterator it = slist.begin();
00192 for (; it != slist.end(); ++it)
00193 good &= it->IsGood();
00194 #if DEBUG_SIGNAL_MONITOR_VALUE
00195 if (!good)
00196 {
00197 QString msg("AllGood failed on ");
00198 SignalMonitorList::const_iterator it = slist.begin();
00199 for (; it != slist.end(); ++it)
00200 if (!it->IsGood())
00201 {
00202 msg += it->noSpaceName;
00203 msg += QString("(%1%2%3) ")
00204 .arg(it->GetValue())
00205 .arg(it->high_threshold ? "<" : ">")
00206 .arg(it->GetThreshold());
00207 }
00208 LOG(VB_GENERAL, LOG_DEBUG, msg);
00209 }
00210 #endif
00211 return good;
00212 }
00213
00218 int SignalMonitorValue::MaxWait(const SignalMonitorList& slist)
00219 {
00220 int wait = 0, minWait = 0;
00221 SignalMonitorList::const_iterator it = slist.begin();
00222 for (; it != slist.end(); ++it)
00223 {
00224 wait = max(wait, it->GetTimeout());
00225 minWait = min(minWait, it->GetTimeout());
00226 }
00227 return (minWait<0) ? -1 : wait;
00228 }
00229