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