#ifndef GLOBALSETTING_H
#define GLOBALSETTING_H
#include <QObject>
#include <QSettings>
#include <QSharedPointer>
#include <QTimer>
class GlobalSetting : public QObject
{
Q_OBJECT
Q_PROPERTY(bool unit READ unit WRITE setUnit NOTIFY unitChanged)
Q_PROPERTY(int appVer READ appVer WRITE setAppVer NOTIFY appVerChanged)
Q_PROPERTY(int mcuVer READ mcuVer WRITE setMcuVer NOTIFY mcuVerChanged)
Q_PROPERTY(int version READ version WRITE setVersion NOTIFY versionChanged)
Q_PROPERTY(qreal lum READ lum WRITE setLum NOTIFY lumChanged)
public:
explicit GlobalSetting(QObject *parent = nullptr);
static GlobalSetting *getInstance();
bool unit() const;
void setUnit(bool);
int appVer() const;
void setAppVer(int);
int mcuVer() const;
void setMcuVer(int);
int version() const;
void setVersion(int);
qreal lum() const;
void setLum(qreal);
void writeLightFile(QString valStr);
Q_INVOKABLE QString getIp();
Q_INVOKABLE QString getMac();
virtual bool eventFilter(QObject *obj, QEvent *event);
signals:
void unitChanged(bool);
void appVerChanged(int);
void mcuVerChanged(int);
void versionChanged(int);
void lumChanged(qreal);
public slots:
void setScreenLight(QVariantMap &vMap);
void reportMcuVer(QVariantMap &vMap);
void reportScreen(QVariantMap &vMap);
private:
private:
bool m_isUnitF{false}; // 是否以华氏度作为系统温度单位
QSharedPointer<QSettings> m_settings{nullptr}; // 全局应用配置
int m_appVer{65536}; // 主控软件版本 65536->0x010000->V1.0.0
int m_mcuVer{65536}; // mcu软件版本
int m_version{65536}; // 整个应用软件版本
qreal m_lum{1.0}; // 环境光百分比
QTimer m_timer;
bool m_sleep{false};
};
#endif // GLOBALSETTING_H
#include "globalsetting.h"
#include "serialportdataparse.h"
#include <QHostAddress>
#include <QNetworkInterface>
#include <QFile>
#include <QGuiApplication>
#define SYSTEM_CONFIG_FILE "./config/system.ini"
#define TEMP_UNIT "common/tempUnit"
#define APP_VER "ota/appVer"
#define MCU_VER "ota/mcuVer"
#define VERSION "ota/version"
#define MIN_LUM 0.3
GlobalSetting::GlobalSetting(QObject *parent)
: QObject{parent}
{
m_settings = QSharedPointer<QSettings>(new QSettings(SYSTEM_CONFIG_FILE, QSettings::IniFormat));
if (m_settings.isNull()) {
return;
}
// 温度单位
if (m_settings->contains(TEMP_UNIT)) {
m_isUnitF = m_settings->value(TEMP_UNIT, m_isUnitF).toBool();
} else {
m_settings->setValue(TEMP_UNIT, m_isUnitF);
}
// 整个应用软件版本
if (m_settings->contains(VERSION)) {
m_version = m_settings->value(VERSION, m_version).toInt();
} else {
m_settings->setValue(VERSION, m_version);
}
// APP版本
if (m_settings->contains(APP_VER)) {
m_appVer = m_settings->value(APP_VER, m_appVer).toInt();
} else {
m_settings->setValue(APP_VER, m_appVer);
}
// MCU版本
if (m_settings->contains(MCU_VER)) {
m_mcuVer = m_settings->value(MCU_VER, m_mcuVer).toInt();
} else {
m_settings->setValue(MCU_VER, m_mcuVer);
}
QGuiApplication::instance()->installEventFilter(this);
connect(&m_timer, &QTimer::timeout, this, [this](){
m_sleep = true;
writeLightFile(QString::number(0));
});
m_timer.setInterval(5*60*1000); // 5分钟未操作,自动休眠
m_timer.start();
connect(SerialPortDataParse::getInstance(), &SerialPortDataParse::sig_parseCmd0x01Frame, this, &GlobalSetting::setScreenLight);
connect(SerialPortDataParse::getInstance(), &SerialPortDataParse::sig_parseCmd0x02Frame, this, &GlobalSetting::reportMcuVer);
connect(SerialPortDataParse::getInstance(), &SerialPortDataParse::sig_parseCmd0x03Frame, this, &GlobalSetting::reportScreen);
}
GlobalSetting *GlobalSetting::getInstance()
{
static GlobalSetting instance;
return &instance;
}
bool GlobalSetting::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::TouchBegin) {
m_timer.start();
if (m_sleep) {
if (m_lum < MIN_LUM) {
setLum(MIN_LUM);
}
writeLightFile(QString::number(qRound(255*m_lum)));
m_sleep = false;
return true;
}
}
return QObject::eventFilter(obj, event);
}
bool GlobalSetting::unit() const
{
return m_isUnitF;
}
void GlobalSetting::setUnit(bool unit)
{
if (m_isUnitF != unit) {
m_isUnitF = unit;
m_settings->setValue(TEMP_UNIT, unit);
emit unitChanged(unit);
}
}
int GlobalSetting::appVer() const
{
return m_appVer;
}
void GlobalSetting::setAppVer(int appVer)
{
if (m_appVer != appVer) {
m_appVer = appVer;
m_settings->setValue(APP_VER, appVer);
emit appVerChanged(appVer);
}
}
int GlobalSetting::mcuVer() const
{
return m_mcuVer;
}
void GlobalSetting::setMcuVer(int mcuVer)
{
if (m_mcuVer != mcuVer) {
m_mcuVer = mcuVer;
m_settings->setValue(MCU_VER, mcuVer);
emit mcuVerChanged(mcuVer);
}
}
int GlobalSetting::version() const
{
return m_version;
}
void GlobalSetting::setVersion(int version)
{
if (m_version != version) {
m_version = version;
m_settings->setValue(VERSION, version);
emit versionChanged(m_version);
}
}
qreal GlobalSetting::lum() const
{
return m_lum;
}
void GlobalSetting::setLum(qreal lum)
{
m_lum = lum;
emit lumChanged(lum);
}
void GlobalSetting::writeLightFile(QString valStr)
{
QString lightFile = "/sys/devices/platform/backlight/backlight/backlight/brightness";
QFile file(lightFile);
if (file.open(QIODevice::WriteOnly)) {
int ret = file.write(valStr.toUtf8().data());
if (ret < 0) {
qDebug()<<__FILE__<<__FUNCTION__<<__LINE__<<"write light file failed!";
}
} else {
qDebug()<<__FILE__<<__FUNCTION__<<__LINE__<<"open light file failed!";
}
file.close();
}
QString GlobalSetting::getIp()
{
QString localIPAddress = "";
QList<QHostAddress> listAddress = QNetworkInterface::allAddresses();
for(int j = 0; j < listAddress.size(); j++){
if(!listAddress.at(j).isNull()
&& listAddress.at(j).protocol() == QAbstractSocket::IPv4Protocol
&& listAddress.at(j) != QHostAddress::LocalHost){
localIPAddress = listAddress.at(j).toString();
return localIPAddress;
}
}
return localIPAddress;
}
QString GlobalSetting::getMac()
{
QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();// 获取所有网络接口列表
int nCnt = nets.count();
QString strMacAddr = "";
for(int i = 0; i < nCnt; i ++)
{
// 如果此网络接口被激活并且正在运行并且不是回环地址,则就是我们需要找的Mac地址
if(nets[i].flags().testFlag(QNetworkInterface::IsUp) &&
nets[i].flags().testFlag(QNetworkInterface::IsRunning) &&
!nets[i].flags().testFlag(QNetworkInterface::IsLoopBack)) {
strMacAddr = nets[i].hardwareAddress();
break;
}
}
return strMacAddr;
}
void GlobalSetting::setScreenLight(QVariantMap &vMap)
{
if (!m_sleep) { // 未休眠,则调整背光亮度
uint16_t u16STK33_1 = vMap.value("u16STK33_1").toUInt();
if (qAbs(u16STK33_1-m_lum*65535) >= 655) {
setLum(u16STK33_1*1.0/65535);
if (m_lum < MIN_LUM) {
setLum(MIN_LUM);
}
writeLightFile(QString::number(qRound(255*m_lum)));
}
}
}
void GlobalSetting::reportMcuVer(QVariantMap &vMap)
{
int version = vMap.value("version").toInt();
setMcuVer(version);
}
void GlobalSetting::reportScreen(QVariantMap &vMap)
{
int screen = vMap.value("screen").toInt();
QString cmd;
if (screen == 0x0001) { // 息屏
cmd = QString("echo \"compositor:state:sleep\" > /tmp/.weston_drm.conf");
system(cmd.toUtf8().data());
} else if (screen == 0x0002) { // 唤醒屏幕
cmd = QString("echo \"compositor:state:on\" > /tmp/.weston_drm.conf");
system(cmd.toUtf8().data());
}
}