From ccbda3e4f7ed430843fbc0190e8ee0d0f0e3a721 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期二, 21 一月 2025 11:06:16 +0800
Subject: [PATCH] 1.SECS实现Off-Line和On-Line消息处理回调应用层,如何切换状态需待和机器端确定。

---
 SourceCode/Bond/Servo/Model.cpp |  278 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 278 insertions(+), 0 deletions(-)

diff --git a/SourceCode/Bond/Servo/Model.cpp b/SourceCode/Bond/Servo/Model.cpp
index d80aee1..d9cf032 100644
--- a/SourceCode/Bond/Servo/Model.cpp
+++ b/SourceCode/Bond/Servo/Model.cpp
@@ -1,21 +1,299 @@
 #include "stdafx.h"
 #include "Model.h"
+#include "Log.h"
+#include "Common.h"
 
 
 CModel::CModel()
 {
+	m_pObservableEmitter = nullptr;
+	m_pObservable = nullptr;
 }
 
 CModel::~CModel()
 {
 }
 
+IObservable* CModel::getObservable()
+{
+	return m_pObservable;
+}
+
+void CModel::setWorkDir(const char* pszWorkDir)
+{
+	m_strWorkDir = pszWorkDir;
+}
+
 int CModel::init()
 {
+	CString strIniFile;
+	CString strUnitId;
+	strIniFile.Format(_T("%s\\ServoConfiguration.ini"), (LPTSTR)(LPCTSTR)m_strWorkDir);
+	m_configuration.setFilepath((LPTSTR)(LPCTSTR)strIniFile);
+	m_configuration.getUnitId(strUnitId);
+
+	// 机器型号和软件版本号应从配置中读取,当前先固定值
+	CString strModeType = _T("Bond2860");
+	CString strSoftRev = _T("1.0.2");
+
+
+
+	// Log
+	CString strLogDir;
+	strLogDir.Format(_T("%s\\Log"), (LPTSTR)(LPCTSTR)m_strWorkDir);
+	::CreateDirectory(strLogDir, NULL);
+	CLog::GetLog()->SetOnLogCallback([&](int level, const char* pszMessage) -> void {
+		notifyTextAndInt(RX_CODE_LOG, pszMessage, level);
+	});
+	CLog::GetLog()->SetAutoAppendTimeString(TRUE);
+	CLog::GetLog()->SetOutputTarget(OT_FILE);
+	CLog::GetLog()->SetLogsDir(strLogDir);
+	CLog::GetLog()->SetEquipmentId((LPTSTR)(LPCTSTR)strUnitId);
+	LOGI("\r\n\r\n~~~ Prog Start! ~~~");
+
+
+	m_pObservable = RX_AllocaObservable([&](IObservableEmitter* e) -> void {
+		m_pObservableEmitter = e;			// 保存发射器
+	});
+
+
+	SECSListener listener;
+	listener.onEQOffLine = [&](void* pFrom) -> void {
+		LOGI("远程请求OffLine");
+	};
+	listener.onEQOnLine = [&](void* pFrom) -> void {
+		LOGI("远程请求OnLine");
+	};
+	listener.onEQConstantRequest = [&](void* pFrom, std::vector<EQConstant>& eqcs) -> void {
+		// 在此填充常量值,目前仅是加1后返回
+		for (auto& item : eqcs) {
+			sprintf_s(item.szValue, 256, "Test%d", item.id+1);
+		}
+	};
+	listener.onEQConstantSend = [&](void* pFrom, std::vector<EQConstant>& eqcs) -> void {
+		// 在此保存和设置机器常量值
+		for (auto& item : eqcs) {
+			LOGI("onEQConstantRequest: %d, %s", item.id, item.szValue);
+		}
+	};
+
+	m_hsmsPassive.setListener(listener);
+	m_hsmsPassive.setEquipmentModelType((LPTSTR)(LPCTSTR)strModeType);
+	m_hsmsPassive.setSoftRev((LPTSTR)(LPCTSTR)strSoftRev);
+	m_hsmsPassive.init(this, "APP", 7000);
+
+
 	return 0;
 }
 
 int CModel::term()
 {
+	m_hsmsPassive.term();
+	CLog::GetLog()->SetOnLogCallback(nullptr);
+	return 0;
+}
+
+int CModel::notify(int code)
+{
+	/* code */
+	if (m_pObservableEmitter != NULL) {
+		IAny* pAny = RX_AllocaAny();
+		if (pAny != NULL) {
+			pAny->addRef();
+			pAny->setCode(code);
+			m_pObservableEmitter->onNext(pAny);
+			pAny->release();
+		}
+	}
+
+	return 1;
+}
+
+int CModel::notifyPtr(int code, void* ptr/* = NULL*/)
+{
+	/* code */
+	if (m_pObservableEmitter != NULL) {
+		IAny* pAny = RX_AllocaAny();
+		if (pAny != NULL) {
+			pAny->addRef();
+			pAny->setCode(code);
+			pAny->setPtrValue("ptr", ptr);
+			m_pObservableEmitter->onNext(pAny);
+			pAny->release();
+		}
+	}
+
+	return 1;
+}
+
+int CModel::notifyObj(int code, IRxObject* pObj)
+{
+	/* code */
+	if (m_pObservableEmitter != NULL) {
+		IAny* pAny = RX_AllocaAny();
+		if (pAny != NULL) {
+			pAny->addRef();
+			pAny->setCode(code);
+			pAny->setObject("obj", pObj);
+			m_pObservableEmitter->onNext(pAny);
+			pAny->release();
+		}
+	}
+
+	return 1;
+}
+
+int CModel::notifyObjAndPtr(int code, IRxObject* pObj, void* ptr)
+{
+	/* code */
+	if (m_pObservableEmitter != NULL) {
+		IAny* pAny = RX_AllocaAny();
+		if (pAny != NULL) {
+			pAny->addRef();
+			pAny->setCode(code);
+			pAny->setObject("obj", pObj);
+			pAny->setPtrValue("ptr", ptr);
+			m_pObservableEmitter->onNext(pAny);
+			pAny->release();
+		}
+	}
+
+	return 1;
+}
+
+int CModel::notifyInt(int code, int exCode)
+{
+	if (m_pObservableEmitter != NULL) {
+		IAny* pAny = RX_AllocaAny();
+		if (pAny != NULL) {
+			pAny->addRef();
+			pAny->setCode(code);
+			pAny->setIntValue("exCode", exCode);
+			m_pObservableEmitter->onNext(pAny);
+			pAny->release();
+		}
+	}
+
+	return 0;
+}
+
+int CModel::notifyInt2(int code, int exCode, int exCode2)
+{
+	if (m_pObservableEmitter != NULL) {
+		IAny* pAny = RX_AllocaAny();
+		if (pAny != NULL) {
+			pAny->addRef();
+			pAny->setCode(code);
+			pAny->setIntValue("exCode", exCode);
+			pAny->setIntValue("exCode2", exCode2);
+			m_pObservableEmitter->onNext(pAny);
+			pAny->release();
+		}
+	}
+
+	return 0;
+}
+
+int CModel::notifyDouble(int code, double dValue)
+{
+	if (m_pObservableEmitter != NULL) {
+		IAny* pAny = RX_AllocaAny();
+		if (pAny != NULL) {
+			pAny->addRef();
+			pAny->setCode(code);
+			pAny->setDoubleValue("value", dValue);
+			m_pObservableEmitter->onNext(pAny);
+			pAny->release();
+		}
+	}
+
+	return 0;
+}
+
+int CModel::notifyObjAndInt(int code, IRxObject* pObj1, IRxObject* pObj2, int exCode)
+{
+	if (m_pObservableEmitter != NULL) {
+		IAny* pAny = RX_AllocaAny();
+		if (pAny != NULL) {
+			pAny->addRef();
+			pAny->setCode(code);
+			if (pObj1 != nullptr) pAny->setObject("obj", pObj1);
+			if (pObj2 != nullptr) pAny->setObject("obj2", pObj2);
+			pAny->setIntValue("exCode", exCode);
+			m_pObservableEmitter->onNext(pAny);
+			pAny->release();
+		}
+	}
+
+	return 0;
+}
+
+int CModel::notifyText(int code, const char* pszText)
+{
+	if (m_pObservableEmitter != NULL) {
+		IAny* pAny = RX_AllocaAny();
+		if (pAny != NULL) {
+			pAny->addRef();
+			pAny->setCode(code);
+			pAny->setStringValue("text", pszText);
+			m_pObservableEmitter->onNext(pAny);
+			pAny->release();
+		}
+	}
+
+	return 0;
+}
+
+int CModel::notifyTextAndInt(int code, const char* pszText, int exCode)
+{
+	if (m_pObservableEmitter != NULL) {
+		IAny* pAny = RX_AllocaAny();
+		if (pAny != NULL) {
+			pAny->addRef();
+			pAny->setCode(code);
+			pAny->setStringValue("text", pszText);
+			pAny->setIntValue("exCode", exCode);
+			m_pObservableEmitter->onNext(pAny);
+			pAny->release();
+		}
+	}
+
+	return 0;
+}
+
+int CModel::notifyPtrAndInt(int code, void* ptr1, void* ptr2, int exCode)
+{
+	if (m_pObservableEmitter != NULL) {
+		IAny* pAny = RX_AllocaAny();
+		if (pAny != NULL) {
+			pAny->addRef();
+			pAny->setCode(code);
+			pAny->setPtrValue("ptr", ptr1);
+			pAny->setPtrValue("ptr1", ptr1);
+			pAny->setPtrValue("ptr2", ptr2);
+			pAny->setIntValue("exCode", exCode);
+			m_pObservableEmitter->onNext(pAny);
+			pAny->release();
+		}
+	}
+
+	return 0;
+}
+
+int CModel::notifyMesMsg(int code, int stream, int function, const char* pszText)
+{
+	if (m_pObservableEmitter != NULL) {
+		IAny* pAny = RX_AllocaAny();
+		if (pAny != NULL) {
+			pAny->addRef();
+			pAny->setCode(code);
+			pAny->setIntValue("stream", stream);
+			pAny->setIntValue("function", function);
+			pAny->setStringValue("text", pszText);
+			m_pObservableEmitter->onNext(pAny);
+			pAny->release();
+		}
+	}
+
 	return 0;
 }

--
Gitblit v1.9.3