From 95ab46e7acc80e11494f3c3796c571f77c83ed41 Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期三, 04 六月 2025 11:53:22 +0800
Subject: [PATCH] 1. 添加搬运数据到数据库 2. 添加秒级的时间戳(毫秒级使用time_t可能是负数,因为有符号),防止使用strftime闪退 3. 实时更新搬运记录界面

---
 SourceCode/Bond/Servo/ToolUnits.cpp       |    8 ++++
 SourceCode/Bond/Servo/CRobotTask.cpp      |    6 +-
 SourceCode/Bond/Servo/CMaster.cpp         |    6 +-
 SourceCode/Bond/Servo/PageTransferLog.cpp |    9 +---
 SourceCode/Bond/Servo/Model.cpp           |   78 +++++++++++++++++++++++++++++++++++++++
 SourceCode/Bond/Servo/ToolUnits.h         |    1 
 SourceCode/Bond/Servo/Common.h            |    5 ++
 7 files changed, 101 insertions(+), 12 deletions(-)

diff --git a/SourceCode/Bond/Servo/CMaster.cpp b/SourceCode/Bond/Servo/CMaster.cpp
index 7a30bfa..e1da7e9 100644
--- a/SourceCode/Bond/Servo/CMaster.cpp
+++ b/SourceCode/Bond/Servo/CMaster.cpp
@@ -589,12 +589,12 @@
 
 
 					lock();
-					delete m_pActiveRobotTask;
-					m_pActiveRobotTask = nullptr;
 
 					if (m_listener.onRobotTaskEvent != nullptr) {
-						m_listener.onRobotTaskEvent(this, m_pActiveRobotTask, 0);
+						m_listener.onRobotTaskEvent(this, m_pActiveRobotTask, 1);
 					}
+					delete m_pActiveRobotTask;
+					m_pActiveRobotTask = nullptr;
 				}
 				unlock();
 			}
diff --git a/SourceCode/Bond/Servo/CRobotTask.cpp b/SourceCode/Bond/Servo/CRobotTask.cpp
index 6a223de..a7e7760 100644
--- a/SourceCode/Bond/Servo/CRobotTask.cpp
+++ b/SourceCode/Bond/Servo/CRobotTask.cpp
@@ -8,7 +8,7 @@
 	{
 		generateId(m_strId);
 		m_state = ROBOT_TASK_STATE::Ready;
-		m_timeCreate = CToolUnits::getTimestamp();
+		m_timeCreate = CToolUnits::getUnixTimestamp();
 		m_timeFetchOut = 0;
 		m_timeStored = 0;
 		m_timeFinish = 0;
@@ -176,11 +176,11 @@
 
 	void CRobotTask::fetchOut()
 	{
-		m_timeFetchOut = CToolUnits::getTimestamp();;
+		m_timeFetchOut = CToolUnits::getUnixTimestamp();;
 	}
 
 	void CRobotTask::stored()
 	{
-		m_timeStored = CToolUnits::getTimestamp();;
+		m_timeStored = CToolUnits::getUnixTimestamp();;
 	}
 }
diff --git a/SourceCode/Bond/Servo/Common.h b/SourceCode/Bond/Servo/Common.h
index 590226c..fc13f87 100644
--- a/SourceCode/Bond/Servo/Common.h
+++ b/SourceCode/Bond/Servo/Common.h
@@ -483,3 +483,8 @@
 #define RT_REQUEST_FROM_EAS		4
 
 
+ /* Robot Task Status */
+#define ROBOT_EVENT_CREATE		0   // 新任务创建
+#define ROBOT_EVENT_FINISH		1   // 正常完成
+#define ROBOT_EVENT_ERROR		2   // 出现错误
+#define ROBOT_EVENT_ABORT		3   // 人为中止
\ No newline at end of file
diff --git a/SourceCode/Bond/Servo/Model.cpp b/SourceCode/Bond/Servo/Model.cpp
index 8e32798..30793da 100644
--- a/SourceCode/Bond/Servo/Model.cpp
+++ b/SourceCode/Bond/Servo/Model.cpp
@@ -6,6 +6,7 @@
 #include "CEqAlarmStep.h"
 #include "AlarmManager.h"
 #include "CGlassPool.h"
+#include "TransferManager.h"
 
 
 CModel::CModel()
@@ -171,7 +172,84 @@
 		notifyPtr(RX_CODE_EQ_DATA_CHANGED, pEquipment);
 	};
 	masterListener.onRobotTaskEvent = [&](void* pMaster, SERVO::CRobotTask* pTask, int code) {
+		if (pTask == nullptr) {
+			LOGE("<CModel>onRobotTaskEvent: 空任务指针,忽略事件 code=%d", code);
+			return;
+		}
+
+		// 任务描述与 ID 用于日志
+		const std::string& strDesc = pTask->getDescription();
+		const std::string& strClassID = pTask->getId();
+
+		// 日志输出与状态处理
+		switch (code) {
+		case ROBOT_EVENT_CREATE:
+			LOGI("<CModel>onRobotTaskEvent: 新任务创建(%s, ClassID=%s).", strDesc.c_str(), strClassID.c_str());
+			break;
+		case ROBOT_EVENT_FINISH:
+			LOGI("<CModel>onRobotTaskEvent: 任务完成(%s, ClassID=%s).", strDesc.c_str(), strClassID.c_str());
+			pTask->completed();
+			break;
+		case ROBOT_EVENT_ERROR:
+			LOGE("<CModel>onRobotTaskEvent: 任务错误(%s, ClassID=%s).", strDesc.c_str(), strClassID.c_str());
+			pTask->error();
+			break;
+		case ROBOT_EVENT_ABORT:
+			LOGE("<CModel>onRobotTaskEvent: 任务停止(%s, ClassID=%s).", strDesc.c_str(), strClassID.c_str());
+			pTask->abort();
+			break;
+		default:
+			LOGE("<CModel>onRobotTaskEvent: 未知事件 code=%d, 任务=%s", code, strDesc.c_str());
+			break;
+		}
+
+		// 安全格式化时间
+		auto format_time = [](time_t t) -> std::string {
+			if (t < 0 || t == _I64_MIN || t == _I64_MAX) { 
+				return "";
+			}
+
+			// 使用 localtime_s 确保线程安全
+			tm tmBuf{};
+			errno_t err = localtime_s(&tmBuf, &t);
+			if (err != 0 || tmBuf.tm_mon < 0 || tmBuf.tm_mon > 11) {
+				return "";
+			}
+
+			// 格式化时间字符串
+			char buf[64] = {};
+			strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tmBuf);
+			return std::string(buf);
+		};
+
+		// 构造 TransferData 数据结构
+		TransferData data;
+		data.strClassID = strClassID;
+		data.strCreateTime = format_time(pTask->getCreateTime());
+		data.strPickTime = format_time(pTask->getFetchoutTime());
+		data.strPlaceTime = format_time(pTask->getStoredTime());
+		data.strEndTime = format_time(pTask->getFinishTime());
+		data.strDescription = pTask->getSimpleDescription();
+
+		// 状态映射
+		static const char* STATUS_STR[] = {
+			"Unknown", "Ready", "Running", "Error", "Abort", "Completed"
+		};
+		auto state = pTask->getState();
+		int index = static_cast<int>(state);
+		if (index > 0 && index < static_cast<int>(std::size(STATUS_STR))) {
+			data.strStatus = STATUS_STR[index];
+		}
+		else {
+			data.strStatus = STATUS_STR[0];
+		}
+
+		// 写入数据库
+		int nRecordId = 0;
+		TransferManager::getInstance().addTransferRecord(data, nRecordId);
+
 		notifyPtr(RX_CODE_EQ_ROBOT_TASK, pTask);
+		LOGI("<CModel>onRobotTaskEvent: 任务记录已保存,RecordID=%d", nRecordId);
 	};
 	m_master.setListener(masterListener);
 
diff --git a/SourceCode/Bond/Servo/PageTransferLog.cpp b/SourceCode/Bond/Servo/PageTransferLog.cpp
index 8000c23..9896a4e 100644
--- a/SourceCode/Bond/Servo/PageTransferLog.cpp
+++ b/SourceCode/Bond/Servo/PageTransferLog.cpp
@@ -63,12 +63,9 @@
 			pAny->addRef();
 			int code = pAny->getCode();
 
-			//if (RX_CODE_ALARM_SET == code) {
-			//	UpdatePageData();
-			//}
-			//else if (RX_CODE_ALARM_CLEAR == code) {
-			//	UpdatePageData();
-			//}
+			if (RX_CODE_EQ_ROBOT_TASK == code) {
+				UpdatePageData();
+			}
 
 			pAny->release();
 			}, [&]() -> void {
diff --git a/SourceCode/Bond/Servo/ToolUnits.cpp b/SourceCode/Bond/Servo/ToolUnits.cpp
index 2964355..15cb278 100644
--- a/SourceCode/Bond/Servo/ToolUnits.cpp
+++ b/SourceCode/Bond/Servo/ToolUnits.cpp
@@ -74,11 +74,19 @@
 
 ULONGLONG CToolUnits::getTimestamp()
 {
+	// 返回毫秒数的版本
 	auto now = std::chrono::system_clock::now();
 	auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
 	return static_cast<ULONGLONG>(ms.time_since_epoch().count());
 }
 
+time_t CToolUnits::getUnixTimestamp()
+{
+	// 返回秒数的版本
+	auto now = std::chrono::system_clock::now();
+	return std::chrono::system_clock::to_time_t(now);
+}
+
 void CToolUnits::createDir(const char* pszDir)
 {
 	if (isDirectory(std::string(pszDir))) {
diff --git a/SourceCode/Bond/Servo/ToolUnits.h b/SourceCode/Bond/Servo/ToolUnits.h
index feeae08..b91ca36 100644
--- a/SourceCode/Bond/Servo/ToolUnits.h
+++ b/SourceCode/Bond/Servo/ToolUnits.h
@@ -15,6 +15,7 @@
 	static CString& floatToString1(float value, CString& strOut);
 	static CString& floatToString3(float value, CString& strOut);
 	static ULONGLONG getTimestamp();
+	static time_t getUnixTimestamp();
 	static void createDir(const char* pszDir);
 	static BOOL copyTextToClipboard(CWnd* pWnd, const CString& strText);
 	static std::string getCurrentExePath();

--
Gitblit v1.9.3