241 lines
6.8 KiB
C++
241 lines
6.8 KiB
C++
|
||
|
||
|
||
#ifndef file_h__
|
||
#define file_h__
|
||
|
||
|
||
|
||
#include <QFile>
|
||
#include "QByteArray"
|
||
#include "stdint-gcc.h"
|
||
#include "QList"
|
||
#include "base.h"
|
||
#include <QDebug>
|
||
#include<QDir>
|
||
#include "base/crc.h"
|
||
#include "QDateTime"
|
||
#include "base/mycfg.h"
|
||
|
||
|
||
/* Type for a 16-bit quantity. */
|
||
typedef uint16_t Elf32_Half;
|
||
typedef uint32_t Elf32_Word;
|
||
/* Type of addresses. */
|
||
typedef uint32_t Elf32_Addr;
|
||
|
||
/* Type of file offsets. */
|
||
typedef uint32_t Elf32_Off;
|
||
|
||
#define EI_NIDENT (16)
|
||
|
||
typedef struct
|
||
{
|
||
unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
|
||
Elf32_Half e_type; /* Object file type */
|
||
Elf32_Half e_machine; /* Architecture */
|
||
Elf32_Word e_version; /* Object file version */
|
||
Elf32_Addr e_entry; /* Entry point virtual address */
|
||
Elf32_Off e_phoff; /* Program header table file offset */
|
||
Elf32_Off e_shoff; /* Section header table file offset */
|
||
Elf32_Word e_flags; /* Processor-specific flags */
|
||
Elf32_Half e_ehsize; /* ELF header size in bytes */
|
||
Elf32_Half e_phentsize; /* Program header table entry size */
|
||
Elf32_Half e_phnum; /* Program header table entry count */
|
||
Elf32_Half e_shentsize; /* Section header table entry size */
|
||
Elf32_Half e_shnum; /* Section header table entry count */
|
||
Elf32_Half e_shstrndx; /* Section header string table index */
|
||
} Elf32_Ehdr;
|
||
|
||
|
||
|
||
|
||
class app_file:public QObject
|
||
{
|
||
Q_OBJECT
|
||
public:
|
||
app_file(QString name){
|
||
QFile file;
|
||
file.setFileName(name);
|
||
if(file.exists())
|
||
{
|
||
file.open(QIODevice::ReadOnly);
|
||
data = file.readAll();
|
||
file.close();
|
||
}
|
||
}
|
||
~app_file(){}
|
||
void set_file_point()
|
||
{
|
||
Elf32_Ehdr h;
|
||
memcpy((void *)&h,data.data(),sizeof(h));
|
||
addr= h.e_shoff+h.e_shnum*h.e_shentsize;
|
||
//qDebug()<<"ext file addr="<<addr<<endl;
|
||
}
|
||
// 找到下一个文件
|
||
QByteArray get_next_file(QString &name)
|
||
{
|
||
if(addr>=data.size())
|
||
{
|
||
//qDebug()<<"addr="<<addr<<"data.size()="<<data.size()<<endl;
|
||
return QByteArray();
|
||
}
|
||
int size;
|
||
size=data[addr]|(data[addr+1]<<8)|(data[addr+2]<<16)|(data[addr+3]<<24);
|
||
name=data.mid(addr+4,256-4);
|
||
//qDebug()<<"file name="<<name<<endl;
|
||
QByteArray da=data.mid(addr+256,size-256);
|
||
addr+=size;
|
||
return da;
|
||
}
|
||
// 输出文件列表
|
||
QList<mystring> get_file_list(){
|
||
QList<mystring> l;
|
||
set_file_point();
|
||
for(int i=0;addr<data.size();i++){
|
||
int size;
|
||
size=data[addr]|(data[addr+1]<<8)|(data[addr+2]<<16)|(data[addr+3]<<24);
|
||
myarray name_t(data.mid(addr+4,256-4));
|
||
mystring a=name_t;
|
||
l.append(a);
|
||
addr+=size;
|
||
}
|
||
return l;
|
||
}
|
||
// 根据文件名返回数据
|
||
QByteArray get_file(QString name){
|
||
set_file_point();
|
||
for(int i=0;addr<data.size();i++){
|
||
int size;
|
||
size=data[addr]|(data[addr+1]<<8)|(data[addr+2]<<16)|(data[addr+3]<<24);
|
||
QByteArray n=data.mid(addr+4,256-4);
|
||
if(name==QString(n)){
|
||
return data.mid(addr+256,size-256);
|
||
}
|
||
addr+=size;
|
||
}
|
||
return QByteArray();
|
||
}
|
||
private:
|
||
QByteArray data;
|
||
int addr;
|
||
};
|
||
|
||
|
||
|
||
#define COMM_FILE_PATH QString("/home/root/comm_log/")
|
||
#define COMM_FILE_NAME "comm_log.csv"
|
||
|
||
|
||
class comm_file:public QObject
|
||
{
|
||
Q_OBJECT
|
||
public:
|
||
comm_file(mycfg *cfg_){
|
||
QDir tempDir;
|
||
if(!tempDir.exists(COMM_FILE_PATH))
|
||
{
|
||
tempDir.mkpath(COMM_FILE_PATH);
|
||
}
|
||
file_=nullptr;
|
||
this->cfg_=cfg_;
|
||
|
||
bool new_file=true;
|
||
file_ = new QFile(COMM_FILE_PATH+find_file());
|
||
qInfo()<<"save comm_log at "<<file_->fileName()<<endl;
|
||
if(file_->exists())
|
||
{
|
||
new_file=false;
|
||
}
|
||
|
||
if (!file_->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
|
||
return;
|
||
}
|
||
if(new_file==true){
|
||
QTextStream tWrite(file_);
|
||
// QStringList ret_infos=cfg_->get_return_info();
|
||
QStringList ret_infos;
|
||
qDebug()<<"ret_info:"<<ret_infos<<"size="<<ret_infos.size()<<endl;
|
||
tWrite<<QString("时间,序号,流程结果,");
|
||
for(int i=0;i<ret_infos.size();i++){
|
||
tWrite<<ret_infos[i];
|
||
tWrite<<",";
|
||
}
|
||
tWrite<<"\n";
|
||
}
|
||
|
||
}
|
||
~comm_file(){
|
||
if(file_!=nullptr) {
|
||
file_->flush();
|
||
file_->close();
|
||
delete file_;
|
||
file_=nullptr;
|
||
}
|
||
}
|
||
QString find_file(){
|
||
// 找到检测日志文件,超过7天则删除
|
||
QDir dir=QDir(COMM_FILE_PATH);
|
||
QStringList list;
|
||
list.append("*.csv");
|
||
QStringList files=dir.entryList(list,QDir::NoFilter,QDir::NoSort);
|
||
qDebug()<<"files="<<files<<endl;
|
||
if(files.size()>0){
|
||
// 删除超时的记录
|
||
foreach (QString name, files) {
|
||
if(dateout(name)==true){
|
||
QFile fi;
|
||
fi.setFileName(COMM_FILE_PATH+name);
|
||
qDebug()<<"will remove "<<fi.fileName()<<endl;
|
||
if(fi.remove()!=true)
|
||
{
|
||
qWarning()<<"remove file "<<fi.fileName()<<"err"<<endl;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
QDate t=QDate::currentDate();
|
||
// return t.toString("yyyy_MM_dd")+"-"+QString::number(cfg_->get_plan_id())+".csv";
|
||
return t.toString("yyyy_MM_dd")+"-"+QString::number(0)+".csv";
|
||
}
|
||
// 是否超时,true超时
|
||
bool dateout(QString file){
|
||
QStringList name=file.split(".");
|
||
qDebug()<<"name="<<name<<endl;
|
||
QDate t=QDate::fromString(name[0].split("-")[0],"yyyy_MM_dd");
|
||
QDate t2=QDate::currentDate();
|
||
qDebug()<<"t="<<t.toString("yyyy_MM_dd")<<" t2="<<t2.toString("yyyy_MM_dd")<<endl;
|
||
if((t.daysTo(t2)>30)||(!t.isValid())){
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
void save(uint8_t src,QByteArray data)
|
||
{
|
||
if(file_){
|
||
QTextStream tWrite(file_);
|
||
QString str=QString("%1,").arg(src);
|
||
str.append(QString("%1,").arg(crc::byte_array_to_string(data.mid(0,8))));
|
||
str.append(QString("%1\n").arg(crc::byte_array_to_int_string(data.mid(16,data.size()-16))));
|
||
QDateTime t=QDateTime::currentDateTime();
|
||
tWrite<<t.toString("yyyy.MM.dd hh:mm:ss.zzz ddd");
|
||
tWrite<<",";
|
||
tWrite<<str;
|
||
}
|
||
}
|
||
private:
|
||
QFile *file_;
|
||
mycfg *cfg_;
|
||
};
|
||
|
||
|
||
|
||
|
||
|
||
|
||
#endif
|
||
|
||
|
||
|
||
|