#include "DeviceConnection.h" #include "BoostLog.h" #include #include #include #include #include #include DeviceConnection::DeviceConnection(QObject *parent) : QObject{parent} { } void DeviceConnection::connect() { m_commandSocket = new QTcpSocket(this); m_h264Socket = new QTcpSocket(this); QObject::connect(m_commandSocket, &QTcpSocket::connected, this, &DeviceConnection::onConnected); QObject::connect(m_h264Socket, &QTcpSocket::connected, this, &DeviceConnection::onConnected); QObject::connect(m_h264Socket, &QTcpSocket::readyRead, this, &DeviceConnection::onH264ReadyRead); QObject::connect(m_commandSocket, &QTcpSocket::readyRead, this, &DeviceConnection::onCommandReadyRead); m_commandSocket->connectToHost("192.168.10.2", 8000); m_h264Socket->connectToHost("192.168.10.2", 8000); } void DeviceConnection::start() { boost::json::object request; request["func"] = "openlivestream_setdata"; request["deviceid"] = "0"; boost::json::object data; data["value"] = "1"; request["data"] = std::move(data); auto text = boost::json::serialize(request); m_h264Socket->write(text.data(), text.size()); } void DeviceConnection::requestOpenDoorArea() { auto task = [this]() { boost::json::object request; request["func"] = "a03opendoor1_getdata"; request["deviceid"] = "0"; boost::json::object data; request["data"] = std::move(data); auto text = boost::json::serialize(request); m_commandSocket->write(text.data(), text.size()); LOG(info) << "requestOpenDoorArea"; }; if (m_requests.empty()) { task(); } m_requests.push(task); } void DeviceConnection::updateOpenDoorAreaPoints(bool enabled, const QList &points) { auto task = [this, enabled, points]() { boost::json::object request; request["func"] = "a03opendoor1_setdata"; request["deviceid"] = "0"; boost::json::object data; data["value"] = enabled ? "1" : "0"; boost::json::array pointArray; for (auto &p : points) { boost::json::object point; point["x"] = p.x(); point["y"] = p.y(); pointArray.push_back(std::move(point)); } data["points"] = std::move(pointArray); request["data"] = std::move(data); auto text = boost::json::serialize(request); m_commandSocket->write(text.data(), text.size()); LOG(info) << "updateOpenDoorAreaPoints"; }; if (m_requests.empty()) { task(); } m_requests.push(task); } void DeviceConnection::requestShieldedArea() { auto task = [this]() { boost::json::object request; request["func"] = "a03opendoor4_getdata"; request["deviceid"] = "0"; boost::json::object data; request["data"] = std::move(data); auto text = boost::json::serialize(request); m_commandSocket->write(text.data(), text.size()); LOG(info) << "requestShieldedArea"; }; if (m_requests.empty()) { task(); } m_requests.push(task); } void DeviceConnection::updateShieldedAreaPoints(bool enabled, const QList &points) { auto task = [this, enabled, points]() { boost::json::object request; request["func"] = "a03opendoor4_setdata"; request["deviceid"] = "0"; boost::json::object data; data["value"] = enabled ? "1" : "0"; boost::json::array pointArray; for (auto &p : points) { boost::json::object point; point["x"] = p.x(); point["y"] = p.y(); pointArray.push_back(std::move(point)); } data["points"] = std::move(pointArray); request["data"] = std::move(data); auto text = boost::json::serialize(request); m_commandSocket->write(text.data(), text.size()); LOG(info) << "updateShieldedAreaPoints"; }; if (m_requests.empty()) { task(); } m_requests.push(task); } void DeviceConnection::requestAntiClipArea() { auto task = [this]() { boost::json::object request; request["func"] = "a03opendoor5_getdata"; request["deviceid"] = "0"; boost::json::object data; request["data"] = std::move(data); auto text = boost::json::serialize(request); m_commandSocket->write(text.data(), text.size()); LOG(info) << "requestAntiClipArea"; }; if (m_requests.empty()) { task(); } m_requests.push(task); } void DeviceConnection::updateAntiClipAreaPoints(bool enabled, const QList &points) { auto task = [this, enabled, points]() { boost::json::object request; request["func"] = "a03opendoor5_setdata"; request["deviceid"] = "0"; boost::json::object data; data["value"] = enabled ? "1" : "0"; boost::json::array pointArray; for (auto &p : points) { boost::json::object point; point["x"] = p.x(); point["y"] = p.y(); pointArray.push_back(std::move(point)); } data["points"] = std::move(pointArray); request["data"] = std::move(data); auto text = boost::json::serialize(request); m_commandSocket->write(text.data(), text.size()); LOG(info) << "updateAntiClipAreaPoints"; }; if (m_requests.empty()) { task(); } m_requests.push(task); } void DeviceConnection::requestResolution(Resolution resolution) { auto task = [this, resolution]() { boost::json::object request; request["func"] = "quality_setdata"; request["deviceid"] = "0"; boost::json::object data; data["value"] = static_cast(resolution); request["data"] = std::move(data); auto text = boost::json::serialize(request); m_commandSocket->write(text.data(), text.size()); LOG(info) << "requestShieldedArea"; }; if (m_requests.empty()) { task(); } m_requests.push(task); } void DeviceConnection::handleCommand(const std::string_view &replyText) { boost::system::error_code error; auto replyValue = boost::json::parse(replyText, error); if (error) { LOG(error) << "prase [" << replyText << "] failed, message: " << error.message(); return; } auto &reply = replyValue.as_object(); auto &function = reply.at("func").as_string(); if (function == "a03opendoor1_getdata") { auto &data = reply.at("data").as_object(); auto &value = data.at("value").as_string(); auto &pointArray = data.at("points").as_array(); QList points; for (auto &p : pointArray) { QPointF point; auto &obj = p.as_object(); point.setX(obj.at("x").as_double()); point.setY(obj.at("y").as_double()); points.push_back(point); } emit currentOpenDoorAreaChanged(value == "1", points); } else if (function == "a03opendoor4_getdata") { auto &data = reply.at("data").as_object(); auto &value = data.at("value").as_string(); auto &pointArray = data.at("points").as_array(); QList points; for (auto &p : pointArray) { QPointF point; auto &obj = p.as_object(); point.setX(obj.at("x").as_double()); point.setY(obj.at("y").as_double()); points.push_back(point); } emit currentShieldedAreaChanged(value == "1", points); } else if (function == "a03opendoor5_getdata") { auto &data = reply.at("data").as_object(); auto &value = data.at("value").as_string(); auto &pointArray = data.at("points").as_array(); QList points; for (auto &p : pointArray) { QPointF point; auto &obj = p.as_object(); point.setX(obj.at("x").as_double()); point.setY(obj.at("y").as_double()); points.push_back(point); } emit currentAntiClipAreaChanged(value == "1", points); } } void DeviceConnection::onConnected() { LOG(info) << "onConnected"; auto socket = dynamic_cast(sender()); if (socket == m_commandSocket) { requestOpenDoorArea(); requestShieldedArea(); requestAntiClipArea(); } } void DeviceConnection::setH264FrameCallback(H264FrameCallback &&callback) { m_frameCallback = std::move(callback); } void DeviceConnection::onH264ReadyRead() { auto data = m_h264Socket->readAll(); m_h264Buffer.push_back(data); while (!m_h264Buffer.isEmpty()) { auto packageSize = ntohl(*reinterpret_cast(m_h264Buffer.data())); if (m_h264Buffer.size() < (packageSize + sizeof(uint32_t))) break; // LOG(info) << "onH264ReadyRead " << data.size() << " " << packageSize; if (m_receivedFirstJsonReply) { if (m_frameCallback) { m_frameCallback(m_h264Buffer.data() + sizeof(uint32_t), packageSize); } } else { LOG(info) << "h264 reply: " << m_h264Buffer.data() + sizeof(uint32_t); m_receivedFirstJsonReply = true; } m_h264Buffer.remove(0, packageSize + sizeof(uint32_t)); } } void DeviceConnection::onCommandReadyRead() { auto data = m_commandSocket->readAll(); m_commandBuffer.push_back(data); while (!m_commandBuffer.isEmpty()) { auto packageSize = ntohl(*reinterpret_cast(m_commandBuffer.data())); if (m_commandBuffer.size() < (packageSize + sizeof(uint32_t))) break; LOG(info) << "h264 reply: " << m_commandBuffer.data() + sizeof(uint32_t); handleCommand(std::string_view(m_commandBuffer.data() + sizeof(uint32_t), packageSize)); m_commandBuffer.remove(0, packageSize + sizeof(uint32_t)); m_requests.pop(); if (!m_requests.empty()) { m_requests.front()(); } } }