Files
checker_host/prot_cmd/keep_live.cpp
ranchuan 92f3ebbe33 添加tcp心跳
添加升级小板程序、方案、jwt文件的进度条命令
2023-12-29 09:41:37 +08:00

114 lines
2.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "keep_live.h"
#include "interface/if_tcp.h"
#include "base/mycfg.h"
#include "interface/if_tcp.h"
#include "prot/prot_pc.h"
#include "QDateTime"
int keep_live_checker::dolater(int cmd,myarray data)
{
data.remove(0,1);
QDateTime t=QDateTime::currentDateTime();
if(t.toString("yyyy-MM-dd hh:mm:ss")!=QString(data))
{
// 时间不相等则设置
//qInfo()<<"set time="<<QString(data)<<endl;
QString m="date -s \"%1\"";
m=m.arg(QString(data));
//qInfo()<<m<<endl;
system(m.toLocal8Bit().data()); // 0312 表示3月12日1025表示当前时分
system("hwclock -w"); // 同步系统时间
system("sync"); // 保存配置
}
return 0;
}
static HandlePc *get_keep_live_checker(){
return new keep_live_checker();}
protpc_export(0x23, get_keep_live_checker);
void keep_live::init()
{
mycfg *cfg_=syscfg();
if_tcp *tcp=static_cast<if_tcp *>(if_tcp_get());
if(cfg_->tcp_enable!=true){
return;
}
if(timer_==nullptr){
timer_=new QTimer();
connect(timer_,&QTimer::timeout,this,&keep_live::timeout_slot);
timer_->setInterval(1000);
timer_->start();
connect(tcp,&if_tcp::tcp_connected_signal,this,&keep_live::connected_slot);
connect(tcp,&if_tcp::tcp_recv_signal,this,&keep_live::recv_slot);
}
}
void keep_live::connected_slot()
{
if(timer_!=nullptr){
timer_->setInterval(30*1000);
timer_->start();
}
}
void keep_live::recv_slot()
{
if(timer_!=nullptr){
timer_->start();
}
}
// state 0idle,1,busy,2,failt
void keep_checker::timeout_slot(){
myarray data;
mycfg *cfg_=syscfg();
ProtPc *pc_=protPc();
data.append(uint8_t(cfg_->local_id));
data.append(uint8_t(pc_->busy()));
data.append(myarray("正常"));
pc_->send_data_slot(uint8_t(0x23),data);
}
void keep_coder::timeout_slot(){
myarray data;
mycfg *cfg_=syscfg();
ProtPc *pc_=protPc();
data.append(uint8_t(0));
data.append(uint8_t(0xff));
data.append(uint8_t(0x03));
data.append(uint8_t(cfg_->local_id));
data.append(uint8_t(0xff));
data.append(uint8_t(0x03));
pc_->send_data_slot(uint8_t(0x8a),data);
}
keep_live *KeepLive()
{
static keep_live *kp=nullptr;
mycfg *cfg_=syscfg();
if(kp!=nullptr){
return kp;
}
if(cfg_->tcp_enable){
if(cfg_->device_type=="checker"){
kp=new keep_checker();
}else if(cfg_->device_type.contains("coder",Qt::CaseInsensitive)==true){
kp=new keep_coder();
}
}
return kp;
}