更新一些mqtt文件

main
Jinhuan 7 months ago
parent c10ad02d1a
commit a8d4a359c4

@ -1,5 +1,68 @@
{
"files.associations": {
"string": "cpp"
"string": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"ratio": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp"
}
}

@ -4,12 +4,18 @@
#include <stdio.h>
#include <qtcpsocket.h>
// 相关头文件
#include <QFile> // 文件操作
#include <QJsonObject> // JSON对象
#include <QJsonArray> // JSON数组
#include <QJsonDocument> // JSON文档
#include <QJsonParseError> // JSON异常捕捉
void threadSendMqtt::init(SyncQueue<_MqttSendInfo>* p_TCP_Info_queue, std::string ip_, int port_) {
void threadSendMqtt::init(SyncQueue<_MqttSendInfo>* p_MQTT_Info_queue, std::string ip_, int port_) {
ip = QString::fromStdString(ip_);
port = port_;
qDebug() << "Mqtt ip:" << ip << "| Mqtt port:" << port;
Local_TCP_Info_queue = p_TCP_Info_queue;
Local_MQTT_Info_queue = p_MQTT_Info_queue;
}
void threadSendMqtt::start_work()
{
@ -29,7 +35,9 @@ bool threadSendMqtt::connectTCP() {
m_client = new QMqttClient(this);
m_client->setHostname(ip);
m_client->setPort(port);
//m_client->setUsername(userName);
//m_client->setPassword(userPassword);
m_client->connectToHost();
//connect(m_client, &QMqttClient::stateChanged, this, &MainWindow::updateLogStateChange);
connect(m_client, &QMqttClient::connected, this, [this](void) {qDebug() << "Mqtt connected";});
@ -67,15 +75,23 @@ void threadSendMqtt::run()
while (isLoop) {
_MqttSendInfo TCPSendInfo;
Local_TCP_Info_queue->take(TCPSendInfo);
Local_MQTT_Info_queue->take(TCPSendInfo);
num++;
sendData(&TCPSendInfo, num);
}
}
void threadSendMqtt::sendData(_MqttSendInfo* TCPSendInfo, int Num) {
std::string fileName = TCPSendInfo->pics_name + ", " + QString::number(Num).toStdString();
QJsonObject addressObject;
// 方式二:赋值
addressObject["street"] = "123 Main St.";
addressObject["city"] = "Anytown";
addressObject["country"] = "USA";
if (m_client->state() == QMqttClient::Connected) {
auto result = m_client->publish(QMqttTopicName("topic"), QString("message").toUtf8(), 0, true);
QJsonDocument jsonDoc(addressObject);
QByteArray jsonBytes = jsonDoc.toJson();
auto result = m_client->publish(QMqttTopicName("topic"), jsonBytes, 0, true);
}
}

@ -10,11 +10,12 @@
class _MqttSendInfo
{
public:
std::string pics_name;
int MsgID;
QString timecost;
_MqttSendInfo()
{
pics_name = "";
}
};
@ -33,7 +34,7 @@ public:
{
stop();
_MqttSendInfo TCPSendInfo;
Local_TCP_Info_queue->put(TCPSendInfo);
Local_MQTT_Info_queue->put(TCPSendInfo);
quit();
wait();
}
@ -44,12 +45,12 @@ protected:
void run();
public:
void init(SyncQueue<_MqttSendInfo>* p_TCP_Info_queue, std::string ip_, int port_);
void init(SyncQueue<_MqttSendInfo>* p_MQTT_Info_queue, std::string ip_, int port_);
void start_work();
void sendData(_MqttSendInfo* TCPSendInfo, int Num);
public:
SyncQueue<_MqttSendInfo>* Local_TCP_Info_queue;
SyncQueue<_MqttSendInfo>* Local_MQTT_Info_queue;
std::atomic_bool isLoop = { 0 };
QMqttClient* m_client = NULL;
};

@ -68,7 +68,8 @@ SyncQueue<_UDPSendInfo>* UDP_Info_queue;
#ifdef __TCPSend
SyncQueue<_TCPSendInfo>* TCP_Info_queue;
#endif
#ifdef __MqttSend
#ifdef __MQTTSend
SyncQueue<_MqttSendInfo>* MQTT_Info_queue;
#endif
bool flag = false;
SyncQueue<_XMLExportDataInfo>* export_XMLData_Info_queue;
@ -132,7 +133,7 @@ Cigarette::Cigarette(QWidget* parent)
if (!read_rotate_message()) {
update_rotate = true;
}
#ifdef LOG_RECORD
QThread* hThread = new QThread();
CLog* clogWorkThread = new CLog();
clogWorkThread->moveToThread(hThread);
@ -140,9 +141,8 @@ Cigarette::Cigarette(QWidget* parent)
connect(hThread, &QThread::finished, clogWorkThread, &CLog::deleteLater);
connect(this, &Cigarette::sengMsgToClog, clogWorkThread, &CLog::recMegFromCigarette);
hThread->start();
emit sengMsgToClog("Application Start.");
#endif
g_save_queue = new SyncQueue<std::pair<std::string, cv::Mat>>(Queue_Size);
g_save_queue->name = "save queue";
#ifdef __UDPSend
@ -153,6 +153,10 @@ Cigarette::Cigarette(QWidget* parent)
TCP_Info_queue = new SyncQueue<_TCPSendInfo>(Queue_Size);
TCP_Info_queue->name = "TCP Info queue";
#endif
#ifdef __MQTTSend
MQTT_Info_queue = new SyncQueue<_MqttSendInfo>(Queue_Size);
MQTT_Info_queue->name = "MQTT Info queue";
#endif
#ifdef __ExportData
export_XMLData_Info_queue = new SyncQueue<_XMLExportDataInfo>(Queue_Size);
export_XMLData_Info_queue->name = "Export Data Info queue";
@ -270,17 +274,23 @@ Cigarette::Cigarette(QWidget* parent)
sThread.init(UDP_Info_queue, g_sys_conf.MonitorIP, g_sys_conf.MonitorPort);
sThread.start_work();
rThread = new threadReceive();
rThread->init(g_sys_conf.MonitorIP, g_sys_conf.MonitorPort);
connect(rThread, &threadReceive::sendMsgToCigratte, this, &Cigarette::recMsgFromUdp);
rThread->start_work();
rThread.init(g_sys_conf.MonitorIP, g_sys_conf.MonitorPort);
connect(&rThread, &threadReceive::sendMsgToCigratte, this, &Cigarette::recMsgFromUdp);
rThread.start_work();
#endif
#ifdef __TCPSend
std::string serverIp = g_sys_conf.TcpIP;
int serverPort = g_sys_conf.TcpPort;
tcpSendThread.init(TCP_Info_queue, serverIp, serverPort);
tcpSendThread.start_work();
std::string ServerIp = g_sys_conf.TcpIP;
int ServerPort = g_sys_conf.TcpPort;
TcpSendThread.init(TCP_Info_queue, ServerIp, ServerPort);
TcpSendThread.start_work();
#endif
#ifdef __MQTTSend
std::string MqttServerIp = g_sys_conf.TcpIP;
int MqttServerPort = g_sys_conf.TcpPort;
MqttSendThread.init(MQTT_Info_queue, MqttServerIp, MqttServerPort);
MqttSendThread.start_work();
#endif
#ifdef __ExportData
@ -437,21 +447,25 @@ Cigarette::Cigarette(QWidget* parent)
m_delay = new QTimer(this);//换班防连击
connect(m_delay, SIGNAL(timeout()), this, SLOT(enable_shift()));
#ifdef __UDPSend
m_sendMsg_delay = new QTimer(this);
connect(m_sendMsg_delay, SIGNAL(timeout()), this, SLOT(sendLatestData()));
#endif
m_op_delay = new QTimer(this);
connect(m_op_delay, SIGNAL(timeout()), this, SLOT(op_timeout()));
m_admin_delay = new QTimer(this);
connect(m_admin_delay, SIGNAL(timeout()), this, SLOT(admin_timeout()));
#ifdef CLEAR_THREAD
clean_pTimer = new QTimer(this);
connect(clean_pTimer, SIGNAL(timeout()), this, SLOT(CleanThreadStartAuto()));
clean_pTimer->start(3600000);
clean_pTimer->start(5000);
#endif
#ifdef __MQTTSend
mqtt_pTimer = new QTimer(this);
connect(mqtt_pTimer, SIGNAL(timeout()), this, SLOT(MqttMsgSend()));
mqtt_pTimer->start(3600000);
#endif
connect(ui.label_alarm, SIGNAL(QlabelDoubleClick()), this, SLOT(OnCancelAlarm()));//报警标签双击消警
@ -556,7 +570,7 @@ Cigarette::~Cigarette()
{
emit sengMsgToClog("Application exited.");
Exit();
exit(-1);
//exit(-1);
}
void Cigarette::start_work()
@ -705,12 +719,15 @@ void Cigarette::Exit()
delete g_save_queue;
#ifdef __UDPSend
sThread.stop();
rThread->stop();
delete rThread;
rThread.stop();
#endif
#ifdef __TCPSend
tcpSendThread.stop();
TcpSendThread.stop();
#endif
#ifdef __MQTTSend
MqttSendThread.stop();
#endif
#ifdef __ExportData
@ -1860,7 +1877,6 @@ void Cigarette::handleTimeout()
ui.label_alarm->setStyleSheet(tr("background-color: rgb(0, 170, 0);"));
ui.label_alarm->setText(QStringLiteral("无报警"));
}
#ifdef __UDPSend
QString str;
str = QString("alarm") + '_' + QString::number(alarm_info.alarm_code);
@ -3597,6 +3613,7 @@ void Cigarette::DisableDebugMode()
g_debug_mode = false;
}
#ifdef CLEAR_THREAD
void Cigarette::CleanThreadStart()
{
QThread* handleThread = new QThread();
@ -3625,6 +3642,7 @@ void Cigarette::CleanThreadStartAuto()
connect(handleThread, &QThread::finished, handleThread, &QThread::deleteLater);
handleThread->start();
}
#endif
void Cigarette::record_output_statistic(qint64 quantity, int Kick[NumberOfSupportedCameras], int shift)
{
@ -3669,6 +3687,12 @@ void Cigarette::record_output_statistic(qint64 quantity, int Kick[NumberOfSuppor
last_shift = shift;
}
void Cigarette::MqttMsgSend()
{
_MqttSendInfo MqttSendInfo;
MQTT_Info_queue->put(MqttSendInfo);
}
#ifdef __UDPSend
void Cigarette::recMsgFromUdp(QString data)
{
@ -3770,7 +3794,9 @@ void Cigarette::recMsgFromUdp(QString data)
else if (data == "CLEARNIMAGE")
{
emit sengMsgToClog("Clean images.");
#ifdef CLEAR_THREAD
CleanThreadStart();
#endif
}
else if (data == "DEBUGEN")
{

@ -35,6 +35,7 @@
#include "threadReceive.h"
#include "threadSendTCP.h"
#include "Cleanthread.h"
#include "threadSendMqtt.h"
#include <QtWidgets/QMainWindow>
#include <QTimer>
@ -117,12 +118,12 @@ private slots:
void OnMouse(QMouseEvent* event);
void OnKey(QKeyEvent* event);
void handleTimeout(); //定时任务,每秒运行一次
void op_timeout(); //操作员剩余时间
void admin_timeout(); //管理员权限剩余时间
void op_timeout(); //操作员剩余时间
void admin_timeout(); //管理员权限剩余时间
void EnableDebugMode();
void DisableDebugMode();
void OnCancelAlarm(); //双击消警
void OnCancelAlarm(); //双击消警
void on_pushButton_reset_released();//复位
void OnOp();
@ -141,7 +142,7 @@ private:
PlcSetup* dialog_plc_setup;
std::vector<PlcItem> m_plc_items;
public:
QTimer* m_pTimer_Cam_mat[NumberOfSupportedCameras]; //相机重启定时器
QTimer* m_pTimer_Cam_mat[NumberOfSupportedCameras]; //相机重启定时器
QLabel* label_cap_speed_mat[NumberOfSupportedCameras];
QLabel* cam_status_mat[NumberOfSupportedCameras];
QLabel* cam_work_mat[NumberOfSupportedCameras];
@ -166,14 +167,6 @@ public:
};
widget_info display_lable_info[NumberOfSupportedCameras][2];
widget_info rotate_info[NumberOfSupportedCameras];
#ifdef SYNC_CAMERA
SyncWorkThread sync_work_thread;
#else
WorkThread work_thread[NumberOfSupportedCameras];
#endif
DebugThread debug_thread[NumberOfSupportedCameras];
struct export_info {
int cameraId = -1;
int totalCheckNum = 0;
@ -188,16 +181,19 @@ public:
};
export_info exportData[NumberOfSupportedCameras];
SaveThread saveThread;
void record_output_statistic(qint64 cur_quantity, int Kick[NumberOfSupportedCameras], int shift);
QTimer* m_pTimer;
QTimer* m_delay; //换班防止连击
QTimer* m_sendMsg_delay; // 停止工作后发送最新数据给远程端
QTimer* m_op_delay; //操作员权限剩余时间
QTimer* m_admin_delay; //操作员权限剩余时间
QTimer* clean_pTimer; //定时清理任务
QTimer* m_delay; //换班防止连击
QTimer* m_sendMsg_delay; //停止工作后发送最新数据给远程端
QTimer* m_op_delay; //操作员权限剩余时间
QTimer* m_admin_delay; //管理员权限剩余时间
#ifdef CLEAR_THREAD
QTimer* clean_pTimer; //定时清理任务
#endif
#ifdef __MQTTSend
QTimer* mqtt_pTimer; //定时MQTT发送
#endif
QSignalMapper* image_lable_DBsignalMapper0;
QSignalMapper* image_lable_DBsignalMapper1;
QSignalMapper* image_lable_TPsignalMapper0;
@ -210,24 +206,38 @@ public:
int production_number[NumberOfSupportedCameras];
int OpenWithUserID[NumberOfSupportedCameras];
int ok[NumberOfSupportedCameras], ng[NumberOfSupportedCameras];
#ifdef __UDPSend
private:
#ifdef SYNC_CAMERA
SyncWorkThread sync_work_thread;
#else
WorkThread work_thread[NumberOfSupportedCameras];
#endif
DebugThread debug_thread[NumberOfSupportedCameras];
SaveThread saveThread;
#ifdef __UDPSend
threadSend sThread;
threadReceive* rThread;
threadReceive rThread;
#endif
#ifdef __TCPSend
private:
threadSendTCP tcpSendThread;
threadSendTCP TcpSendThread;
#endif
#ifdef __MQTTSend
threadSendMqtt MqttSendThread;
#endif
public slots:
void CleanThreadStartAuto();
#ifdef __UDPSend
void recMsgFromUdp(QString data);
#endif
//void ClogThreadStart();
#ifdef __MQTTSend
void MqttMsgSend();
#endif
signals:
#ifdef LOG_RECORD
void sengMsgToClog(QString);
#endif
void sendMsgToExportData();
};

@ -5,15 +5,17 @@
#include "basecamera.h"
#include "QtCore\qdatetime.h"
//#define __DEBUG //debug信息输出功能
//#define __DEBUG //debug信息输出功能
#define LOG_RECORD //log日志功能
#define CLEAR_THREAD //定时清理线程
//#define __UDPSend //网络发送功能
#define __TCPSend // TCP发送
#define __MqttSend // TCP发送
#define USB_BASLER_NEW_FW //使用basler定制固件
//#define __TCPSend // TCP发送
#define __MQTTSend // MQTT发送
#define USB_BASLER_NEW_FW //使用basler定制固件
//#define IMM_PROCESS //拍照后立马处理,不等校验信号
//#define IMM_FEED_BACK //处理完后立马反馈,不等校验信号
#define ONE_TIME_SHIFT //错开一拍发送反馈(默认错开两次)
#define AI_WARM_UP //AI识别开始前的热身动作
#define ONE_TIME_SHIFT //错开一拍发送反馈(默认错开两次)
#define AI_WARM_UP //AI识别开始前的热身动作
//#define LICENSE_VERIFY //开启license文件校验
//CAP_FEED_BACK和DOUBLE_FEED_BACK不要一起开
#if defined (ONE_TIME_SHIFT)

Loading…
Cancel
Save