2023-11-21 20:28:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef file_h__
|
|
|
|
|
#define file_h__
|
|
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include "QByteArray"
|
|
|
|
|
#include "stdint-gcc.h"
|
|
|
|
|
#include "QList"
|
|
|
|
|
#include "base.h"
|
|
|
|
|
#include <QDebug>
|
2023-11-27 14:31:00 +08:00
|
|
|
|
#include <QDir>
|
2023-11-21 20:28:23 +08:00
|
|
|
|
#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
|
|
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
|
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 */
|
2023-11-21 20:28:23 +08:00
|
|
|
|
} Elf32_Ehdr;
|
|
|
|
|
|
2023-11-27 14:31:00 +08:00
|
|
|
|
class app_file : public QObject
|
2023-11-21 20:28:23 +08:00
|
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
|
Q_OBJECT
|
2023-11-21 20:28:23 +08:00
|
|
|
|
public:
|
2023-11-27 14:31:00 +08:00
|
|
|
|
app_file(QString name)
|
|
|
|
|
{
|
|
|
|
|
QFile file;
|
|
|
|
|
file.setFileName(name);
|
|
|
|
|
if (file.exists())
|
2023-11-21 20:28:23 +08:00
|
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
|
|
|
data = file.readAll();
|
|
|
|
|
file.close();
|
2023-11-21 20:28:23 +08:00
|
|
|
|
}
|
2023-11-27 14:31:00 +08:00
|
|
|
|
}
|
|
|
|
|
~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())
|
2023-11-21 20:28:23 +08:00
|
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
|
// qDebug()<<"addr="<<addr<<"data.size()="<<data.size()<<endl;
|
|
|
|
|
return QByteArray();
|
2023-11-21 20:28:23 +08:00
|
|
|
|
}
|
2023-11-27 14:31:00 +08:00
|
|
|
|
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;
|
2023-11-21 20:28:23 +08:00
|
|
|
|
}
|
2023-11-27 14:31:00 +08:00
|
|
|
|
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;
|
2023-11-21 20:28:23 +08:00
|
|
|
|
}
|
2023-11-27 14:31:00 +08:00
|
|
|
|
return QByteArray();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 20:28:23 +08:00
|
|
|
|
private:
|
2023-11-27 14:31:00 +08:00
|
|
|
|
QByteArray data;
|
|
|
|
|
int addr;
|
2023-11-21 20:28:23 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define COMM_FILE_PATH QString("/home/root/comm_log/")
|
|
|
|
|
#define COMM_FILE_NAME "comm_log.csv"
|
|
|
|
|
|
2023-11-27 14:31:00 +08:00
|
|
|
|
class comm_file : public QObject
|
2023-11-21 20:28:23 +08:00
|
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
|
Q_OBJECT
|
2023-11-21 20:28:23 +08:00
|
|
|
|
public:
|
2023-11-27 14:31:00 +08:00
|
|
|
|
comm_file(mycfg *cfg_)
|
|
|
|
|
{
|
|
|
|
|
QDir tempDir;
|
|
|
|
|
if (!tempDir.exists(COMM_FILE_PATH))
|
|
|
|
|
{
|
|
|
|
|
tempDir.mkpath(COMM_FILE_PATH);
|
|
|
|
|
}
|
|
|
|
|
file_ = nullptr;
|
|
|
|
|
this->cfg_ = cfg_;
|
2023-11-21 20:28:23 +08:00
|
|
|
|
|
2023-11-27 14:31:00 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2023-11-21 20:28:23 +08:00
|
|
|
|
|
2023-11-27 14:31:00 +08:00
|
|
|
|
if (!file_->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
|
|
|
|
|
{
|
|
|
|
|
return;
|
2023-11-21 20:28:23 +08:00
|
|
|
|
}
|
2023-11-27 14:31:00 +08:00
|
|
|
|
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";
|
2023-11-21 20:28:23 +08:00
|
|
|
|
}
|
2023-11-27 14:31:00 +08:00
|
|
|
|
}
|
|
|
|
|
~comm_file()
|
|
|
|
|
{
|
|
|
|
|
if (file_ != nullptr)
|
|
|
|
|
{
|
|
|
|
|
file_->flush();
|
|
|
|
|
file_->close();
|
|
|
|
|
delete file_;
|
|
|
|
|
file_ = nullptr;
|
2023-11-21 20:28:23 +08:00
|
|
|
|
}
|
2023-11-27 14:31:00 +08:00
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
2023-11-21 20:28:23 +08:00
|
|
|
|
}
|
2023-11-27 14:31:00 +08:00
|
|
|
|
}
|
2023-11-21 20:28:23 +08:00
|
|
|
|
}
|
2023-11-27 14:31:00 +08:00
|
|
|
|
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()))
|
2023-11-21 20:28:23 +08:00
|
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
|
return true;
|
2023-11-21 20:28:23 +08:00
|
|
|
|
}
|
2023-11-27 14:31:00 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 20:28:23 +08:00
|
|
|
|
private:
|
2023-11-27 14:31:00 +08:00
|
|
|
|
QFile *file_;
|
|
|
|
|
mycfg *cfg_;
|
2023-11-21 20:28:23 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|