2024-08-16 16:24:15 +08:00
|
|
|
#include "DeviceListModel.h"
|
|
|
|
#include "BoostLog.h"
|
2024-08-20 09:29:49 +08:00
|
|
|
#include "DeviceConnection.h"
|
2024-08-16 16:24:15 +08:00
|
|
|
#include <QNetworkDatagram>
|
|
|
|
#include <QNetworkInterface>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QUdpSocket>
|
|
|
|
#include <boost/json/parse.hpp>
|
|
|
|
|
|
|
|
DeviceListModel::DeviceListModel(QObject *parent) : QAbstractListModel{parent} {
|
|
|
|
m_broadcastSocket = new QUdpSocket(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DeviceListModel::rowCount(const QModelIndex &parent) const {
|
|
|
|
return m_devices.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DeviceListModel::data(const QModelIndex &index, int role) const {
|
|
|
|
QVariant ret;
|
|
|
|
auto row = index.row();
|
2024-08-20 09:29:49 +08:00
|
|
|
if (row >= m_devices.size()) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
auto info = m_devices.at(row)->infomation();
|
2024-08-16 16:24:15 +08:00
|
|
|
if (role == DeviceIdRole) {
|
2024-08-20 09:29:49 +08:00
|
|
|
ret = info.deviceId;
|
2024-08-16 16:24:15 +08:00
|
|
|
} else if (role == FirmwareVersionRole) {
|
2024-08-20 09:29:49 +08:00
|
|
|
ret = info.firmwareVersion;
|
2024-08-16 16:24:15 +08:00
|
|
|
} else if (role == SoftwareVersionRole) {
|
2024-08-20 09:29:49 +08:00
|
|
|
ret = info.softwareVersion;
|
2024-08-16 16:24:15 +08:00
|
|
|
} else if (role == IpRole) {
|
2024-08-20 09:29:49 +08:00
|
|
|
ret = info.ip;
|
|
|
|
} else if (role == OnlineStatusRole) {
|
|
|
|
ret = m_devices.at(row)->isConnected();
|
2024-08-16 16:24:15 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> DeviceListModel::roleNames() const {
|
|
|
|
QHash<int, QByteArray> roleNames;
|
|
|
|
roleNames.insert(DeviceIdRole, "deviceId");
|
|
|
|
roleNames.insert(FirmwareVersionRole, "firmwareVersion");
|
|
|
|
roleNames.insert(SoftwareVersionRole, "softwareVersion");
|
|
|
|
roleNames.insert(IpRole, "ip");
|
2024-08-20 09:29:49 +08:00
|
|
|
roleNames.insert(OnlineStatusRole, "onlineStatus");
|
2024-08-16 16:24:15 +08:00
|
|
|
return roleNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap DeviceListModel::get(int index) const {
|
|
|
|
QVariantMap map;
|
|
|
|
if (index >= 0 && index < m_devices.size()) {
|
2024-08-20 09:29:49 +08:00
|
|
|
auto info = m_devices.at(index)->infomation();
|
|
|
|
map["firmwareVersion"] = info.firmwareVersion;
|
|
|
|
map["softwareVersion"] = info.softwareVersion;
|
|
|
|
map["deviceId"] = info.deviceId;
|
|
|
|
map["ip"] = info.ip;
|
|
|
|
map["onlineStatus"] = m_devices.at(index)->isConnected();
|
2024-08-16 16:24:15 +08:00
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2024-08-20 09:29:49 +08:00
|
|
|
std::shared_ptr<DeviceConnection> DeviceListModel::device(int index) {
|
|
|
|
std::shared_ptr<DeviceConnection> ret;
|
|
|
|
if (index < m_devices.size()) {
|
|
|
|
ret = m_devices.at(index);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-16 16:24:15 +08:00
|
|
|
void DeviceListModel::startSearchDevice() {
|
|
|
|
if (m_timerId >= 0) {
|
|
|
|
LOG(error) << "app is searching device.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
beginResetModel();
|
|
|
|
m_devices.clear();
|
|
|
|
endResetModel();
|
|
|
|
auto interfaces = QNetworkInterface::allInterfaces();
|
|
|
|
for (auto &interface : interfaces) {
|
|
|
|
if (interface.flags() & QNetworkInterface::IsLoopBack) continue;
|
|
|
|
if (interface.flags() & QNetworkInterface::IsUp && interface.flags() & QNetworkInterface::IsRunning) {
|
|
|
|
const QList<QNetworkAddressEntry> entries = interface.addressEntries();
|
|
|
|
for (const QNetworkAddressEntry &entry : entries) {
|
|
|
|
if (entry.ip().protocol() != QAbstractSocket::IPv4Protocol) continue;
|
|
|
|
QUdpSocket *socket = new QUdpSocket(this);
|
|
|
|
if (socket->bind(entry.ip(), ListenPort, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint)) {
|
|
|
|
connect(socket, &QUdpSocket::readyRead, this, &DeviceListModel::onDeviceReplyReadyRead);
|
|
|
|
m_udpSockets.push_back(socket);
|
|
|
|
LOG(info) << "Listening on " << entry.ip().toString().toStdString() << ":" << ListenPort;
|
|
|
|
} else {
|
|
|
|
LOG(error) << "Failed to bind UDP socket on" << entry.ip().toString().toStdString() << ":"
|
|
|
|
<< ListenPort;
|
|
|
|
delete socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!m_udpSockets.empty()) {
|
|
|
|
m_retries = 0;
|
|
|
|
emit searchProgressChanged();
|
|
|
|
m_timerId = startTimer(1000);
|
|
|
|
emit isSearchingChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeviceListModel::isSearching() const {
|
|
|
|
return m_timerId >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float DeviceListModel::searchProgress() const {
|
|
|
|
return static_cast<float>(m_retries) * 100 / RetryCount;
|
|
|
|
}
|
|
|
|
|
2024-08-20 09:29:49 +08:00
|
|
|
void DeviceListModel::onDeviceConnected() {
|
|
|
|
auto device = dynamic_cast<DeviceConnection *>(sender());
|
|
|
|
auto iterator =
|
|
|
|
std::find_if(m_devices.cbegin(), m_devices.cend(),
|
|
|
|
[device](const std::shared_ptr<DeviceConnection> &item) { return item.get() == device; });
|
|
|
|
if (iterator != m_devices.cend()) {
|
|
|
|
QList<int> roles;
|
|
|
|
roles << OnlineStatusRole;
|
|
|
|
int row = std::distance(m_devices.cbegin(), iterator);
|
|
|
|
emit dataChanged(index(row), index(row), roles);
|
|
|
|
LOG(info) << "device " << row << " connected.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceListModel::onDeviceDisconnected() {
|
|
|
|
auto device = dynamic_cast<DeviceConnection *>(sender());
|
|
|
|
auto iterator =
|
|
|
|
std::find_if(m_devices.cbegin(), m_devices.cend(),
|
|
|
|
[device](const std::shared_ptr<DeviceConnection> &item) { return item.get() == device; });
|
|
|
|
if (iterator != m_devices.cend()) {
|
|
|
|
int row = std::distance(m_devices.cbegin(), iterator);
|
|
|
|
QList<int> roles;
|
|
|
|
roles << OnlineStatusRole;
|
|
|
|
emit dataChanged(index(row), index(row), roles);
|
|
|
|
LOG(info) << "device " << row << " disconnected.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 16:24:15 +08:00
|
|
|
void DeviceListModel::onDeviceReplyReadyRead() {
|
|
|
|
auto udp = dynamic_cast<QUdpSocket *>(sender());
|
|
|
|
while (udp->hasPendingDatagrams()) {
|
|
|
|
QNetworkDatagram datagram = udp->receiveDatagram();
|
|
|
|
auto replyVale = boost::json::parse(datagram.data().toStdString());
|
|
|
|
auto &reply = replyVale.as_object();
|
2024-08-20 09:29:49 +08:00
|
|
|
DeviceConnection::Infomation device;
|
2024-08-21 16:03:49 +08:00
|
|
|
if (reply.contains("devid")) {
|
|
|
|
device.deviceId = QString::fromStdString(std::string(reply.at("devid").as_string()));
|
|
|
|
}
|
|
|
|
if (reply.contains("fw_ver")) {
|
|
|
|
device.firmwareVersion = QString::fromStdString(std::string(reply.at("fw_ver").as_string()));
|
|
|
|
}
|
|
|
|
if (reply.contains("sw_ver")) {
|
|
|
|
device.softwareVersion = QString::fromStdString(std::string(reply.at("sw_ver").as_string()));
|
|
|
|
}
|
2024-08-16 16:24:15 +08:00
|
|
|
device.ip = datagram.senderAddress().toString();
|
|
|
|
|
2024-08-20 09:29:49 +08:00
|
|
|
auto iterator = std::find_if(m_devices.cbegin(), m_devices.cend(),
|
|
|
|
[&device](const std::shared_ptr<DeviceConnection> &item) {
|
|
|
|
return item->infomation().deviceId == device.deviceId;
|
|
|
|
});
|
2024-08-16 16:24:15 +08:00
|
|
|
if (iterator == m_devices.cend()) {
|
2024-08-20 09:29:49 +08:00
|
|
|
auto connection = std::shared_ptr<DeviceConnection>(new DeviceConnection(),
|
|
|
|
[](DeviceConnection *self) { self->deleteLater(); });
|
|
|
|
connect(connection.get(), &DeviceConnection::connected, this, &DeviceListModel::onDeviceConnected);
|
|
|
|
connect(connection.get(), &DeviceConnection::disconnected, this, &DeviceListModel::onDeviceDisconnected);
|
|
|
|
connection->connect(device);
|
2024-08-16 16:24:15 +08:00
|
|
|
beginInsertRows(QModelIndex(), m_devices.size(), m_devices.size());
|
2024-08-20 09:29:49 +08:00
|
|
|
m_devices.push_back(connection);
|
2024-08-16 16:24:15 +08:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
LOG(info) << "Received datagram from " << datagram.senderAddress().toString().toStdString() << ":"
|
|
|
|
<< datagram.senderPort() << ", Data: " << datagram.data().toStdString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceListModel::broadcast() {
|
|
|
|
if (m_udpSockets.empty()) return;
|
|
|
|
QByteArray datagram = "FACEPASS_V2";
|
|
|
|
auto interfaces = QNetworkInterface::allInterfaces();
|
|
|
|
for (auto &interface : interfaces) {
|
|
|
|
if (interface.flags() & QNetworkInterface::IsLoopBack) continue;
|
|
|
|
if (interface.flags() & QNetworkInterface::IsUp && interface.flags() & QNetworkInterface::IsRunning) {
|
|
|
|
const QList<QNetworkAddressEntry> entries = interface.addressEntries();
|
|
|
|
for (const QNetworkAddressEntry &entry : entries) {
|
|
|
|
if (entry.broadcast().toIPv4Address()) {
|
|
|
|
m_broadcastSocket->writeDatagram(datagram, entry.broadcast(), BroadcastPort);
|
2024-08-20 09:29:49 +08:00
|
|
|
// LOG(info) << "Broadcasted datagram: " << datagram.toStdString() << " to "
|
|
|
|
// << entry.broadcast().toString().toStdString() << ":" << BroadcastPort;
|
2024-08-16 16:24:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceListModel::stopSearchDevice() {
|
|
|
|
if (m_timerId >= 0) {
|
|
|
|
killTimer(m_timerId);
|
|
|
|
m_timerId = -1;
|
|
|
|
}
|
|
|
|
if (!m_udpSockets.empty()) {
|
|
|
|
for (auto &socket : m_udpSockets) {
|
|
|
|
socket->deleteLater();
|
|
|
|
}
|
|
|
|
m_udpSockets.clear();
|
|
|
|
}
|
|
|
|
emit isSearchingChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceListModel::timerEvent(QTimerEvent *event) {
|
|
|
|
broadcast();
|
|
|
|
m_retries++;
|
|
|
|
emit searchProgressChanged();
|
|
|
|
if (m_retries >= RetryCount) {
|
|
|
|
QTimer::singleShot(0, this, &DeviceListModel::stopSearchDevice);
|
|
|
|
}
|
|
|
|
}
|