Files
checker_host/prot_cmd/keep_live.cpp
ranchuan f00a2b79f0 自动检测指令0x44不读取流水号
解决新赋码指令回复指令不对的问题
  解决tcp不上报心跳的问题
2024-01-13 11:48:21 +08:00

126 lines
2.7 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){
qDebug("keeplive not init.");
return;
}else{
qDebug("keeplive init.");
}
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()
{
// qDebug("keeplive connected.");
if(timer_!=nullptr){
timer_->setInterval(1*1000);
timer_->start();
}
}
void keep_live::recv_slot()
{
// qDebug("keeplive recv.");
if(timer_!=nullptr){
timer_->setInterval(30*1000);
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);
// qDebug("coder keeplive.");
}
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();
}else{
qWarning("device type err,not creat keeplive.");
}
if(kp!=nullptr){
QTimer::singleShot(0, kp, &keep_live::init);
}
}
return kp;
}