AntiClipSettings/Application.cpp

414 lines
16 KiB
C++
Raw Permalink Normal View History

2024-08-26 16:46:41 +08:00
#include "Application.h"
2024-08-13 20:06:10 +08:00
#include "BoostLog.h"
#include "Configuration.h"
#include "H264Palyer.h"
2024-09-11 14:18:56 +08:00
#include "Settings.h"
2024-08-13 20:06:10 +08:00
#include "VideoFrameProvider.h"
2024-08-22 15:32:58 +08:00
#include <QFileInfo>
2024-08-14 20:01:38 +08:00
#include <QFont>
2024-08-13 20:06:10 +08:00
#include <QGuiApplication>
#include <QQmlApplicationEngine>
2024-08-24 22:35:35 +08:00
#include <QQmlContext>
2024-08-13 20:06:10 +08:00
2024-09-11 14:18:56 +08:00
std::ostream &operator<<(std::ostream &os, const ImageFormat &format) {
switch (format) {
case ImageFormat::Jpeg:
os << "Jpeg";
break;
case ImageFormat::YUV:
os << "YUV";
break;
default:
os << "Unknown";
break;
}
return os;
}
2024-08-13 20:06:10 +08:00
Application::Application(int &argc, char **argv)
: m_app(std::make_shared<QGuiApplication>(argc, argv)), m_videoFrameProvider(new VideoFrameProvider()),
2024-08-21 09:26:06 +08:00
m_player(std::make_shared<H264Palyer>()), m_devices(new DeviceListModel(this)),
m_collector(new DataCollection(this)) {
2024-09-11 14:18:56 +08:00
using namespace Amass;
m_settings = Singleton<Settings>::instance<Construct>();
m_settings->load();
m_collector->setImageFormat(m_settings->imageFormat(), m_settings->imageQuality());
2024-08-14 20:01:38 +08:00
QFont font;
font.setPointSize(16);
2024-08-27 11:14:36 +08:00
font.setFamily("微软雅黑");
2024-08-14 20:01:38 +08:00
m_app->setFont(font);
2024-08-13 20:06:10 +08:00
m_app->setApplicationName(APPLICATION_NAME);
2024-11-14 12:11:59 +08:00
m_app->setApplicationVersion(QString("v%1").arg(APP_VERSION));
2024-08-13 20:06:10 +08:00
m_player->open();
}
2024-08-16 16:24:15 +08:00
DeviceConnection::AreaWay Application::currentOpenDoorAreaWay() const {
return m_currentOpenDoorAreaWay;
2024-08-14 20:01:38 +08:00
}
2024-08-16 16:24:15 +08:00
void Application::setCurrentOpenDoorAreaWay(DeviceConnection::AreaWay way) {
if (m_currentOpenDoorAreaWay == way) return;
m_currentOpenDoorAreaWay = way;
if (m_currentOpenDoorAreaWay == DeviceConnection::Quadrangle) {
if ((m_currentOpenDoorAreaPoints.size() < 4) || (m_currentOpenDoorAreaPoints == FullArea)) {
m_currentOpenDoorAreaPoints.clear();
m_currentOpenDoorAreaPoints << QPointF(68, 6) << QPointF(570, 6) << QPointF(570, 354) << QPointF(68, 354);
emit currentOpenDoorAreaPointsChanged();
2024-08-14 20:01:38 +08:00
}
2024-08-16 16:24:15 +08:00
} else if (m_currentOpenDoorAreaWay == DeviceConnection::FullArea) {
m_currentOpenDoorAreaPoints.clear();
m_currentOpenDoorAreaPoints = FullArea;
emit currentOpenDoorAreaPointsChanged();
}
emit currentOpenDoorAreaWayChanged();
2024-08-20 09:29:49 +08:00
if (!m_device.expired()) {
auto device = m_device.lock();
device->updateOpenDoorAreaPoints(m_currentOpenDoorAreaWay, m_currentOpenDoorAreaPoints);
2024-08-14 20:01:38 +08:00
}
}
2024-08-13 20:06:10 +08:00
QList<QPointF> Application::currentOpenDoorAreaPoints() const {
return m_currentOpenDoorAreaPoints;
}
void Application::setCurrentOpenDoorAreaPoints(const QList<QPointF> &points) {
if (m_currentOpenDoorAreaPoints != points) {
m_currentOpenDoorAreaPoints = points;
emit currentOpenDoorAreaPointsChanged();
}
}
2024-08-16 16:24:15 +08:00
NetworkInfomation Application::currentNetworkInfomation() const {
return m_currentNetworkInfomation;
}
2024-08-14 20:01:38 +08:00
bool Application::currentShieldedAreaEnabled() const {
return m_currentShieldedAreaEnabled;
}
void Application::setCurrentShieldedAreaEnabled(bool enabled) {
if (m_currentShieldedAreaEnabled != enabled) {
m_currentShieldedAreaEnabled = enabled;
emit currentShieldedAreaEnabledChanged();
2024-08-16 16:24:15 +08:00
if (m_currentShieldedAreaPoints.size() < 4) {
m_currentShieldedAreaPoints.clear();
m_currentShieldedAreaPoints << QPointF(6, 6) << QPointF(40, 60) << QPointF(590, 6) << QPointF(630, 60);
emit currentShieldedAreaPointsChanged();
}
2024-08-20 09:29:49 +08:00
if (!m_device.expired()) {
auto device = m_device.lock();
device->updateShieldedAreaPoints(m_currentShieldedAreaEnabled, m_currentShieldedAreaPoints);
2024-08-14 20:01:38 +08:00
}
}
}
QList<QPointF> Application::currentShieldedAreaPoints() const {
return m_currentShieldedAreaPoints;
}
void Application::setCurrentShieldedAreaPoints(const QList<QPointF> &points) {
if (m_currentShieldedAreaPoints != points) {
m_currentShieldedAreaPoints = points;
emit currentShieldedAreaPointsChanged();
}
}
bool Application::currentAntiClipAreaEnabled() const {
return m_currentAntiClipAreaEnabled;
}
void Application::setCurrentAntiClipAreaEnabled(bool enabled) {
if (m_currentAntiClipAreaEnabled != enabled) {
m_currentAntiClipAreaEnabled = enabled;
emit currentAntiClipAreaEnabledChanged();
2024-08-20 09:29:49 +08:00
if (!m_device.expired()) {
auto device = m_device.lock();
device->updateAntiClipAreaPoints(m_currentAntiClipAreaEnabled, m_currentAntiClipAreaPoints,
m_currentAntiClipSensitivity);
}
}
}
int Application::currentAntiClipSensitivity() const {
return m_currentAntiClipSensitivity;
}
void Application::setCurrentAntiClipSensitivity(int sensitivity) {
if (m_currentAntiClipSensitivity != sensitivity) {
m_currentAntiClipSensitivity = sensitivity;
emit currentAntiClipSensitivityChanged();
if (!m_device.expired()) {
auto device = m_device.lock();
device->updateAntiClipAreaPoints(m_currentAntiClipAreaEnabled, m_currentAntiClipAreaPoints,
m_currentAntiClipSensitivity);
2024-08-14 20:01:38 +08:00
}
}
}
QList<QPointF> Application::currentAntiClipAreaPoints() const {
return m_currentAntiClipAreaPoints;
}
void Application::setCurrentAntiClipAreaPoints(const QList<QPointF> &points) {
if (m_currentAntiClipAreaPoints != points) {
m_currentAntiClipAreaPoints = points;
emit currentAntiClipAreaPointsChanged();
}
}
void Application::updateOpenDoorAreaPoints(const QList<QPointF> &points) {
2024-08-20 09:29:49 +08:00
if (!m_device.expired()) {
auto device = m_device.lock();
device->updateOpenDoorAreaPoints(m_currentOpenDoorAreaWay, points);
2024-08-16 16:24:15 +08:00
}
2024-08-14 20:01:38 +08:00
}
void Application::updateAntiClipAreaPoints(const QList<QPointF> &points) {
2024-08-20 09:29:49 +08:00
if (!m_device.expired()) {
auto device = m_device.lock();
device->updateAntiClipAreaPoints(m_currentAntiClipAreaEnabled, points, m_currentAntiClipSensitivity);
2024-08-20 09:29:49 +08:00
}
2024-08-14 20:01:38 +08:00
}
void Application::updateShieldedAreaPoints(const QList<QPointF> &points) {
2024-08-20 09:29:49 +08:00
if (!m_device.expired()) {
auto device = m_device.lock();
device->updateShieldedAreaPoints(m_currentShieldedAreaEnabled, points);
}
2024-08-14 20:01:38 +08:00
}
bool Application::currentDeviceFlip() const {
return m_currentDeviceFlip;
}
void Application::setCurrentDeviceFlip(bool flip) {
if (m_currentDeviceFlip != flip) {
m_currentDeviceFlip = flip;
emit currentDeviceFlipChanged();
if (!m_device.expired()) {
auto device = m_device.lock();
device->updateFlip(flip);
}
}
}
int Application::currentDeviceRotation() const {
return m_currentDeviceRotation;
}
void Application::setCurrentDeviceRotation(int rotation) {
if (m_currentDeviceRotation != rotation) {
m_currentDeviceRotation = rotation;
emit currentDeviceRotationChanged();
if (!m_device.expired()) {
auto device = m_device.lock();
device->updateRotation(rotation);
}
}
}
2024-08-22 10:48:28 +08:00
void Application::updateNetworkInfomation(bool dhcp, const QString &ip, const QString &netmask, const QString &gateway,
const QString &dns) {
2024-08-20 09:29:49 +08:00
if (!m_device.expired()) {
auto device = m_device.lock();
2024-08-26 14:55:15 +08:00
if (device->isConnected()) {
device->updateNetworkInfomation(dhcp, ip, netmask, gateway, dns);
emit newMessage(1, "网络设置", "设置成功,请等待设备重新上线!");
} else {
emit newMessage(2, "网络设置", "设备已离线!");
}
2024-08-16 16:24:15 +08:00
}
}
2024-08-20 09:29:49 +08:00
void Application::connectToDevice(int index) {
if (!m_device.expired()) {
auto device = m_device.lock();
disconnect(device.get(), &DeviceConnection::rotationChanged, this, &Application::onDeviceRotationChanged);
disconnect(device.get(), &DeviceConnection::flipChanged, this, &Application::onDeviceFlipChanged);
2024-08-21 09:26:06 +08:00
disconnect(device.get(), &DeviceConnection::openDoorAreaChanged, this, &Application::onDeviceOpenDoorArea);
disconnect(device.get(), &DeviceConnection::shieldedAreaChanged, this, &Application::onDeviceShieldedArea);
disconnect(device.get(), &DeviceConnection::antiClipAreaChanged, this, &Application::onDeviceAntiClipArea);
disconnect(device.get(), &DeviceConnection::networkInfomationChanged, this,
2024-08-20 09:29:49 +08:00
&Application::onDeviceNetworkInfomation);
2024-08-21 16:03:49 +08:00
disconnect(device.get(), &DeviceConnection::firmwareChanged, this, &Application::onDeviceFirmware);
2024-08-21 09:26:06 +08:00
disconnect(device.get(), &DeviceConnection::otaProgressChanged, this,
&Application::currentDeviceOtaProgressChanged);
2024-08-22 10:48:28 +08:00
disconnect(device.get(), &DeviceConnection::connected, this, &Application::onDeviceConnected);
disconnect(device.get(), &DeviceConnection::disconnected, this, &Application::onDeviceDisconnected);
2024-08-20 09:29:49 +08:00
device->setH264FrameCallback(DeviceConnection::H264FrameCallback());
device->setLiveStreamEnabled(false);
2024-08-21 16:03:49 +08:00
if (!m_currentFirmware.isEmpty()) {
m_currentFirmware.clear();
}
2024-08-22 10:48:28 +08:00
m_currentDeviceConnected = false;
2024-08-16 16:24:15 +08:00
}
2024-08-21 09:26:06 +08:00
if (index >= 0) {
auto device = m_devices->device(index);
m_device = device;
connect(device.get(), &DeviceConnection::rotationChanged, this, &Application::onDeviceRotationChanged);
connect(device.get(), &DeviceConnection::flipChanged, this, &Application::onDeviceFlipChanged);
2024-08-21 09:26:06 +08:00
connect(device.get(), &DeviceConnection::openDoorAreaChanged, this, &Application::onDeviceOpenDoorArea);
connect(device.get(), &DeviceConnection::shieldedAreaChanged, this, &Application::onDeviceShieldedArea);
connect(device.get(), &DeviceConnection::antiClipAreaChanged, this, &Application::onDeviceAntiClipArea);
connect(device.get(), &DeviceConnection::networkInfomationChanged, this,
&Application::onDeviceNetworkInfomation);
2024-08-21 16:03:49 +08:00
connect(device.get(), &DeviceConnection::firmwareChanged, this, &Application::onDeviceFirmware);
2024-08-21 09:26:06 +08:00
connect(device.get(), &DeviceConnection::otaProgressChanged, this,
&Application::currentDeviceOtaProgressChanged);
2024-08-22 10:48:28 +08:00
connect(device.get(), &DeviceConnection::connected, this, &Application::onDeviceConnected);
connect(device.get(), &DeviceConnection::disconnected, this, &Application::onDeviceDisconnected);
2024-08-21 09:26:06 +08:00
device->setH264FrameCallback([this](const char *data, uint32_t size) {
auto image = m_player->decode((const uint8_t *)data, size);
if (image) {
m_videoFrameProvider->setImage(*image);
emit newVideoFrame();
}
});
device->setLiveStreamEnabled(true);
auto info = device->infomation();
m_currentOpenDoorAreaWay = info.openDoorAreaWay;
m_currentOpenDoorAreaPoints = info.openDoorArea;
m_currentShieldedAreaEnabled = info.shieldedAreaEnabled;
m_currentShieldedAreaPoints = info.shieldedArea;
m_currentAntiClipAreaEnabled = info.antiClipAreaEnabled;
m_currentAntiClipAreaPoints = info.antiClipArea;
m_currentAntiClipSensitivity = info.antiClipSensitivity;
2024-08-21 09:26:06 +08:00
m_currentNetworkInfomation = device->networkInfomation();
2024-08-21 16:03:49 +08:00
m_currentFirmware = device->infomation().firmwareVersion;
2024-08-22 10:48:28 +08:00
m_currentDeviceConnected = device->isConnected();
m_currentDeviceFlip = info.flip;
m_currentDeviceRotation = info.rotation;
emit currentDeviceRotationChanged();
emit currentDeviceFlipChanged();
2024-08-21 09:26:06 +08:00
emit currentOpenDoorAreaPointsChanged();
emit currentShieldedAreaPointsChanged();
emit currentAntiClipAreaPointsChanged();
emit currentOpenDoorAreaWayChanged();
emit currentShieldedAreaEnabledChanged();
emit currentAntiClipAreaEnabledChanged();
emit currentAntiClipSensitivityChanged();
2024-08-21 09:26:06 +08:00
emit currentNetworkInfomationChanged();
}
2024-08-21 16:03:49 +08:00
emit currentFirmwareChanged();
2024-08-22 10:48:28 +08:00
emit currentDeviceConnectedChanged();
2024-08-21 09:26:06 +08:00
}
void Application::startSearchDevice() {
connectToDevice(-1);
m_devices->startSearchDevice();
2024-08-16 16:24:15 +08:00
}
2024-08-19 09:33:04 +08:00
void Application::upgradeDevice(const QString &file) {
if (m_device.expired()) return;
auto device = m_device.lock();
auto infomation = device->infomation();
auto versionPrefix = infomation.softwareVersion.left(7);
2024-08-22 15:32:58 +08:00
constexpr auto version = "RD_T009_V21R003B013";
QFileInfo fileInfo(file);
QString baseName = fileInfo.baseName();
int position = baseName.indexOf(versionPrefix);
if (position < 0 || ((baseName.length() - position) < std::strlen(version))) {
emit newMessage(2, "OTA升级", "文件名格式不合法!");
2024-10-21 19:35:49 +08:00
LOG(error) << "baseName: " << baseName.toStdString() << ", position: " << position;
2024-08-22 15:32:58 +08:00
return;
}
2024-10-21 19:35:49 +08:00
QString firmware = baseName.mid(position);
if (device->isConnected()) {
device->requestOta(firmware, file);
} else {
emit newMessage(2, "OTA升级", "设备已离线,请重新连接设备!");
2024-08-19 09:33:04 +08:00
}
}
void Application::resetDevice() {
if (m_device.expired()) return;
auto device = m_device.lock();
if (device->isConnected()) {
device->requestReset();
} else {
emit newMessage(2, "恢复出厂设置", "设备已离线,请重新连接设备!");
}
}
2024-08-16 16:24:15 +08:00
void Application::onDeviceOpenDoorArea(DeviceConnection::AreaWay way, const QList<QPointF> &points) {
setCurrentOpenDoorAreaWay(way);
2024-08-13 20:06:10 +08:00
setCurrentOpenDoorAreaPoints(points);
}
void Application::onDeviceRotationChanged(int rotation) {
setCurrentDeviceRotation(rotation);
}
void Application::onDeviceFlipChanged(bool flip) {
setCurrentDeviceFlip(flip);
}
2024-08-14 20:01:38 +08:00
void Application::onDeviceShieldedArea(bool enabled, const QList<QPointF> &points) {
setCurrentShieldedAreaEnabled(enabled);
setCurrentShieldedAreaPoints(points);
}
void Application::onDeviceAntiClipArea(bool enabled, const QList<QPointF> &points, int sensitivity) {
2024-08-14 20:01:38 +08:00
setCurrentAntiClipAreaEnabled(enabled);
setCurrentAntiClipAreaPoints(points);
setCurrentAntiClipSensitivity(sensitivity);
2024-08-14 20:01:38 +08:00
}
2024-08-16 16:24:15 +08:00
void Application::onDeviceNetworkInfomation(const NetworkInfomation &info) {
m_currentNetworkInfomation = info;
emit currentNetworkInfomationChanged();
}
2024-08-21 16:03:49 +08:00
void Application::onDeviceFirmware(const QString &firmware) {
if (m_currentFirmware != firmware) {
m_currentFirmware = firmware;
emit currentFirmwareChanged();
}
}
2024-08-22 10:48:28 +08:00
void Application::onDeviceConnected() {
m_currentDeviceConnected = true;
emit currentDeviceConnectedChanged();
}
void Application::onDeviceDisconnected() {
m_currentDeviceConnected = false;
emit currentDeviceConnectedChanged();
m_collector->stop();
}
2024-08-13 20:06:10 +08:00
int Application::exec() {
QQmlApplicationEngine engine;
engine.addImageProvider("videoframe", m_videoFrameProvider);
2024-08-24 22:35:35 +08:00
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
2024-08-13 20:06:10 +08:00
QObject::connect(
&engine, &QQmlApplicationEngine::objectCreationFailed, this, []() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
engine.loadFromModule("AntiClipSettings", "Main");
2024-08-24 22:35:35 +08:00
#else
qmlRegisterSingletonInstance("AntiClipSettings", 1, 0, "App", this);
qmlRegisterUncreatableType<DeviceConnection>("AntiClipSettings", 1, 0, "DeviceConnection",
"Only created in C++...");
engine.load("qrc:/qt/qml/AntiClipSettings/qml/Main.qml");
#endif
bool isQt5 = (QT_VERSION < QT_VERSION_CHECK(6, 0, 0));
engine.rootContext()->setContextProperty("isQt5", isQt5);
2024-08-13 20:06:10 +08:00
return m_app->exec();
}
Application *Application::create(QQmlEngine *qmlEngine, QJSEngine *jsEngine) {
Application *ret = nullptr;
auto app = Amass::Singleton<Application>::instance();
if (app) {
ret = app.get();
2024-08-24 22:35:35 +08:00
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
2024-08-13 20:06:10 +08:00
QJSEngine::setObjectOwnership(ret, QJSEngine::CppOwnership);
2024-08-24 22:35:35 +08:00
#endif
2024-08-13 20:06:10 +08:00
}
return ret;
}