xref: /OK3568_Linux_fs/app/forlinx/flapp/src/plugins/watchdog/watchdogwidgt.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include "watchdogwidgt.h"
2 #include "ui_watchdogwidgt.h"
3 #include "iwatchdog.h"
4 #include <QValidator>
5 #include "messagebox.h"
6 #include <QTimer>
7 #include <QDoubleValidator>
8 #include <QIntValidator>
9 #include <QCoreApplication>
10 
WatchDogWidgt(QWidget * parent)11 WatchDogWidgt::WatchDogWidgt(QWidget *parent) :
12     IWidget(parent),
13     ui(new Ui::WatchDogWidgt),
14     m_wathcdog(Q_NULLPTR)
15 {
16     ui->setupUi(this);
17     ui->checkBox->setEnabled(false);
18 
19     QIntValidator *pIntV = new QIntValidator;
20     pIntV->setRange(1, 10);
21     ui->timeoutEdt->setValidator(pIntV);
22 
23     QDoubleValidator *pDoubleV = new QDoubleValidator;
24     pDoubleV->setRange(0.1, 10.0);
25     ui->feedEdt->setValidator(pDoubleV);
26 }
27 
~WatchDogWidgt()28 WatchDogWidgt::~WatchDogWidgt()
29 {
30     if(m_wathcdog->isOpen())
31     {
32         ui->openBtn->click();
33     }
34     delete ui;
35 }
36 
id()37 QString WatchDogWidgt::id()
38 {
39     return "watchdog";
40 }
41 
loadData(const QDomElement & head,const QDomElement & body)42 void WatchDogWidgt::loadData(const QDomElement &head, const QDomElement &body)
43 {
44     Q_UNUSED(body)
45     QString devName;
46     QString type;
47     QDomElement e = head.toElement();
48     devName = e.attribute("devName");
49     type = e.attribute("type");
50     m_wathcdog = new IWatchDog(devName, this);
51     connect(m_wathcdog, &IWatchDog::feedCount, this, [=](int count){
52         ui->widget->setValue(QString::number(count));
53     });
54 
55     if(devName.isEmpty())
56     {
57         ui->widget->setTips("watchdog not config!");
58     }
59     qDebug() << QString("dveName=%1, type=%2").arg(devName).arg(type);
60 
61     ui->setBtn->setEnabled(false);
62 
63     connect(qApp, &QCoreApplication::aboutToQuit, this, [=](){
64         if(ui->openBtn->isChecked())
65         {
66             ui->openBtn->click();
67         }
68     });
69 }
70 
on_openBtn_clicked()71 void WatchDogWidgt::on_openBtn_clicked()
72 {
73     auto  setCheck = [=](bool bCheck){
74         ui->checkBox->setEnabled(bCheck);
75         ui->checkBox->blockSignals(true);
76         ui->checkBox->setChecked(bCheck);
77         ui->checkBox->blockSignals(false);
78     };
79 
80     int timeoutTime = ui->timeoutEdt->text().toInt();
81     float feedtime = ui->feedEdt->text().toFloat();
82 
83     if(!m_wathcdog->isOpen()&&  checkoutInput() && m_wathcdog->open(timeoutTime, feedtime))
84     {
85         setCheck(true);
86         ui->openBtn->setText("stop");
87         ui->setBtn->setEnabled(true);
88     }else{
89         ui->setBtn->setEnabled(false);
90         m_wathcdog->close();
91         ui->openBtn->setText("start");
92         setCheck(false);
93         ui->widget->setValue("0");
94     }
95 }
96 
on_checkBox_clicked()97 void WatchDogWidgt::on_checkBox_clicked()
98 {
99     qDebug() <<"start time wathcgod";
100     if(m_wathcdog->isOpen())
101     {
102         if(!ui->checkBox->isChecked())
103         {
104             ui->widget->startDownTime(ui->timeoutEdt->text().toInt());
105         }else{
106             ui->widget->startDownTime(0);
107         }
108         m_wathcdog->setFeed(ui->checkBox->isChecked());
109     }
110 }
111 
on_setBtn_clicked()112 void WatchDogWidgt::on_setBtn_clicked()
113 {
114     if(!checkoutInput())
115     {
116         return;
117     }
118 
119     m_wathcdog->setTimeoutTime(ui->timeoutEdt->text().toInt());
120     m_wathcdog->setFeedTime(ui->feedEdt->text().toFloat());
121 }
122 
checkoutInput()123 bool WatchDogWidgt::checkoutInput()
124 {
125     QString timeoutSeconds = ui->timeoutEdt->text();
126     if(timeoutSeconds.isEmpty() || timeoutSeconds.toInt()>10 || timeoutSeconds.toInt()<1)
127     {
128         MessageBox::showMessage(this, "Input Timeount is Invalid!");
129         return false;;
130     }
131 
132     QString feedSeconds = ui->feedEdt->text();
133     if(feedSeconds.isEmpty() || timeoutSeconds.toFloat()<0.1 || timeoutSeconds.toFloat()> 10)
134     {
135         MessageBox::showMessage(this, "Input Feed is Invalid!");
136         return false;
137     }
138 
139     return true;
140 }
141