148 lines
5.0 KiB
C++
148 lines
5.0 KiB
C++
#include "appcontext.h"
|
|
#include "udpcommunication.h"
|
|
#include "mockvehicle.h"
|
|
#include <QApplication>
|
|
#include <QDateTime>
|
|
|
|
AppContext* AppContext::instance()
|
|
{
|
|
static AppContext* s = nullptr;
|
|
if (!s) s = new AppContext();
|
|
return s;
|
|
}
|
|
|
|
AppContext::AppContext(QObject* parent)
|
|
: QObject(parent)
|
|
{
|
|
// 参数与密码持久化:QSettings 写入程序目录 config.ini
|
|
m_settings = new QSettings(QApplication::applicationDirPath() + "/config.ini",
|
|
QSettings::IniFormat, this);
|
|
|
|
// 首次运行写入默认值
|
|
if (!m_settings->contains(keySpeed())) m_settings->setValue(keySpeed(), 4.5);
|
|
if (!m_settings->contains(keyPumpFlow())) m_settings->setValue(keyPumpFlow(), 20);
|
|
if (!m_settings->contains(keyFoamRatio())) m_settings->setValue(keyFoamRatio(), 6);
|
|
if (!m_settings->contains(keyFoamPercent()))m_settings->setValue(keyFoamPercent(), 3);
|
|
if (!m_settings->contains(keyAirFlow())) m_settings->setValue(keyAirFlow(), 100);
|
|
if (!m_settings->contains("param/pwd")) m_settings->setValue("param/pwd", "6789");
|
|
if (!m_settings->contains("param/admin_pwd"))m_settings->setValue("param/admin_pwd", "6789");
|
|
|
|
// 通信层(UDP 收包发包)
|
|
m_comm = new UdpCommunication(&telemetry, this);
|
|
|
|
// 内置模拟车载端(离线演示:回环 UDP 收发)
|
|
m_vehicle = new MockVehicle(this);
|
|
|
|
// 通信层故障信号 → 写入故障历史
|
|
connect(m_comm, &UdpCommunication::faultAppended,
|
|
this, &AppContext::appendFault);
|
|
|
|
seedFaultHistory();
|
|
}
|
|
|
|
void AppContext::seedFaultHistory()
|
|
{
|
|
// 演示用历史故障汇总(协议示例数据 + 补充)
|
|
struct Seed { const char* t; const char* sys; const char* code; const char* d; };
|
|
const Seed seeds[] = {
|
|
{"2026-05-16 10:08", "机器人底盘", "C01", "底盘电量低"},
|
|
{"2026-05-16 09:58", "机器人底盘", "C02", "底盘电机温度高"},
|
|
{"2026-05-16 09:41", "机器人底盘", "C03", "总电压低"},
|
|
{"2026-05-15 09:08", "机械臂", "A01", "机械臂通信故障"},
|
|
{"2026-05-15 08:55", "机械臂", "A02", "机械臂自检故障"},
|
|
{"2026-05-15 08:30", "机械臂", "A03", "关节J2过载"},
|
|
{"2026-05-14 09:58", "洗消系统", "W01", "柱塞泵故障"},
|
|
{"2026-05-14 09:20", "洗消系统", "W02", "空压机压力异常"},
|
|
{"2026-05-14 08:45", "洗消系统", "W03", "电动球阀D1开启故障"},
|
|
{"2026-05-13 09:08", "洗消系统", "W04", "清水液位低"},
|
|
{"2026-05-13 08:18", "洗消系统", "W05", "药剂液位低"},
|
|
{"2026-05-13 08:02", "洗消系统", "W06", "叶片泵故障"},
|
|
{"2026-05-12 11:20", "气体检测仪", "G01", "检测仪通信故障"},
|
|
{"2026-05-12 10:50", "气体检测仪", "G02", "有毒物浓度超阈值"},
|
|
{"2026-05-11 09:30", "气体检测仪", "G03", "主机标定异常"},
|
|
{"2026-05-11 09:10", "机器人底盘", "C04", "手刹失效"},
|
|
{"2026-05-10 08:40", "机械臂", "A04", "末端速度超限"},
|
|
{"2026-05-10 08:15", "气体检测仪", "G04", "传感器信号异常"},
|
|
};
|
|
for (const Seed& s : seeds) {
|
|
FaultRecord r;
|
|
r.time = QString::fromUtf8(s.t);
|
|
r.system = QString::fromUtf8(s.sys);
|
|
r.code = QString::fromUtf8(s.code);
|
|
r.desc = QString::fromUtf8(s.d);
|
|
faultHistory.append(r); // 种子数据按“最新在前”顺序直接追加
|
|
}
|
|
}
|
|
|
|
void AppContext::appendFault(const QString& system, const QString& code, const QString& desc)
|
|
{
|
|
FaultRecord r;
|
|
r.time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm");
|
|
r.system = system;
|
|
r.code = code;
|
|
r.desc = desc;
|
|
faultHistory.prepend(r); // 最新在前
|
|
}
|
|
|
|
double AppContext::getDouble(const QString& key, double def) const
|
|
{
|
|
return m_settings->value(key, def).toDouble();
|
|
}
|
|
|
|
void AppContext::setDouble(const QString& key, double val)
|
|
{
|
|
m_settings->setValue(key, val);
|
|
m_settings->sync();
|
|
}
|
|
|
|
QString AppContext::getString(const QString& key, const QString& def) const
|
|
{
|
|
return m_settings->value(key, def).toString();
|
|
}
|
|
|
|
void AppContext::setString(const QString& key, const QString& val)
|
|
{
|
|
m_settings->setValue(key, val);
|
|
m_settings->sync();
|
|
}
|
|
|
|
QString AppContext::password() const
|
|
{
|
|
return m_settings->value("param/pwd", "6789").toString();
|
|
}
|
|
|
|
void AppContext::setPassword(const QString& pwd)
|
|
{
|
|
m_settings->setValue("param/pwd", pwd);
|
|
m_settings->sync();
|
|
}
|
|
|
|
QString AppContext::adminPassword() const
|
|
{
|
|
return m_settings->value("param/admin_pwd", "6789").toString();
|
|
}
|
|
|
|
void AppContext::resetEstop()
|
|
{
|
|
estopTriggered = false;
|
|
telemetry.estop = false;
|
|
emit estopChanged(false);
|
|
// 通知车载端复位
|
|
if (m_comm) m_comm->sendEstopReset();
|
|
}
|
|
|
|
void AppContext::triggerEstop()
|
|
{
|
|
estopTriggered = true;
|
|
telemetry.estop = true;
|
|
emit estopChanged(true);
|
|
if (m_comm) m_comm->sendEstop();
|
|
}
|
|
|
|
void AppContext::setMode(Mode m)
|
|
{
|
|
if (mode == m) return;
|
|
mode = m;
|
|
emit modeChanged(m);
|
|
}
|