#include "stdafx.h"
|
#include "Common.h"
|
#include "Log.h"
|
#include "Model.h"
|
#include "Log.h"
|
#include "ToolUnits.h"
|
|
|
CModel* g_pModel = NULL;
|
void CALLBACK ModerTimerProc(HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime)
|
{
|
if (g_pModel != NULL) {
|
g_pModel->onTimer(nTimerid);
|
}
|
}
|
|
|
CModel::CModel()
|
{
|
m_pObservableEmitter = nullptr;
|
m_pObservable = nullptr;
|
m_pServo = nullptr;
|
m_pEqBond1 = nullptr;
|
m_nTimerID = 0;
|
}
|
|
|
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\\Configuration.ini"), (LPTSTR)(LPCTSTR)m_strWorkDir);
|
m_configuration.setFilepath((LPTSTR)(LPCTSTR)strIniFile);
|
m_configuration.getUnitId(strUnitId);
|
|
|
/*
|
char szUnitId[256], szDataDir[MAX_PATH];
|
char szModeType[256];
|
CString strIniFile;
|
strIniFile.Format(_T("%s\\configuration.ini"), (LPTSTR)(LPCTSTR)m_strWorkDir);
|
GetPrivateProfileString("Equipment", "UNITID", _T(""), szUnitId, 256, strIniFile);
|
GetPrivateProfileString("Equipment", "ModelType", _T("GB2860"), szModeType, 256, strIniFile);
|
GetPrivateProfileString("APP", "DataDir", _T(""), szDataDir, MAX_PATH, strIniFile);
|
m_strDataDir = szDataDir;
|
CToolUnits::createDir(szDataDir);
|
*/
|
|
|
// 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! ~~~");
|
|
|
// ´´½¨Servo
|
BEQ_CreateServo(m_pServo, "BLServo");
|
ASSERT(m_pServo);
|
BEQ::ServoListener listener;
|
listener.onRemoteEqConnecting = [&](void* pServo, void* pRemoteEiuipment) -> void {
|
BEQ::IRemoteEquipment* p = (BEQ::IRemoteEquipment*)pRemoteEiuipment;
|
char szBuffer[256];
|
p->getName(szBuffer, 256);
|
LOGI("<ServoListener>ÕýÔÚÁ¬½ÓÔ¶³ÌÉ豸(%s)...", szBuffer);
|
};
|
listener.onRemoteEqConnected = [&](void* pServo, void* pRemoteEiuipment) -> void {
|
BEQ::IRemoteEquipment* p = (BEQ::IRemoteEquipment*)pRemoteEiuipment;
|
char szBuffer[256];
|
p->getName(szBuffer, 256);
|
LOGI("<ServoListener>Á¬½ÓÔ¶³ÌÉ豸(%s)³É¹¦.", szBuffer);
|
notifyPtr(RX_CODE_REMOTEEQ_STATE_CHANGED, pRemoteEiuipment);
|
};
|
listener.onRemoteEqConnectFailed = [&](void* pServo, void* pRemoteEiuipment, int errorCode) -> void {
|
BEQ::IRemoteEquipment* p = (BEQ::IRemoteEquipment*)pRemoteEiuipment;
|
char szBuffer[256];
|
p->getName(szBuffer, 256);
|
LOGW("<ServoListener>Á¬½ÓÔ¶³ÌÉ豸(%s)ʧ°Ü.", szBuffer);
|
};
|
listener.onRemoteEqDisconnecting = [&](void* pServo, void* pRemoteEiuipment) -> void {
|
BEQ::IRemoteEquipment* p = (BEQ::IRemoteEquipment*)pRemoteEiuipment;
|
char szBuffer[256];
|
p->getName(szBuffer, 256);
|
LOGI("<ServoListener>ÕýÔÚ¶Ï¿ªÔ¶³ÌÉ豸(%s).", szBuffer);
|
};
|
listener.onRemoteEqDisconnected = [&](void* pServo, void* pRemoteEiuipment) -> void {
|
BEQ::IRemoteEquipment* p = (BEQ::IRemoteEquipment*)pRemoteEiuipment;
|
char szBuffer[256];
|
p->getName(szBuffer, 256);
|
LOGW("<ServoListener>Ô¶³ÌÉ豸(%s)Á¬½Ó¶Ï¿ª.", szBuffer);
|
notifyPtr(RX_CODE_REMOTEEQ_STATE_CHANGED, pRemoteEiuipment);
|
};
|
listener.onRemoteEqReadRawdata = [&](void* pServo, void* pRemoteEiuipment, const char* pszData, int len) -> void {
|
BEQ::IRemoteEquipment* p = (BEQ::IRemoteEquipment*)pRemoteEiuipment;
|
char szBuffer[256];
|
p->getName(szBuffer, 256);
|
LOGD("<ServoListener>ÊÕµ½Ô¶³ÌÉ豸(%s)ÔʼÊý¾Ý(%s).", szBuffer, std::string(pszData, len).c_str());
|
};
|
listener.onRemoteEqEventUpdate = [&](void* pServo, void* pRemoteEiuipment, void* pUnit, BEQ::REMOTE_EQ_EVENT eventCode) -> void {
|
BEQ::IRemoteEquipment* p = (BEQ::IRemoteEquipment*)pRemoteEiuipment;
|
char szBuffer[256];
|
p->getName(szBuffer, 256);
|
LOGD("<ServoListener>Ô¶³ÌÉ豸(%s)onRemoteEqEventUpdate.", szBuffer);
|
};
|
m_pServo->setListener(listener);
|
|
|
m_pObservable = RX_AllocaObservable([&](IObservableEmitter* e) -> void {
|
m_pObservableEmitter = e; // ±£´æ·¢ÉäÆ÷
|
});
|
|
|
m_nTimerID = (int)SetTimer(NULL, 1, 1000, (TIMERPROC)ModerTimerProc);
|
g_pModel = this;
|
|
|
return 0;
|
}
|
|
int CModel::createRemoteEqs()
|
{
|
// ´ÓÅäÖÃÖжÁȡԶ³Ì»úÆ÷ÅäÖÃ
|
CString strIp, strIniFile;
|
UINT nPort, nDoorCount;
|
strIniFile.Format(_T("%s\\Configuration.ini"), (LPTSTR)(LPCTSTR)m_strWorkDir);
|
CConfiguration configuration((LPTSTR)(LPCTSTR)strIniFile);
|
configuration.getBond1(strIp, nPort, nDoorCount);
|
m_pServo->createRemoteEquipment(m_pEqBond1, (LPTSTR)(LPCTSTR)strIp, nPort);
|
ASSERT(m_pEqBond1);
|
m_pEqBond1->addUnit("UNITA", nDoorCount);
|
m_pServo->connectRemoteEquipment(m_pEqBond1);
|
|
return 0;
|
}
|
|
int CModel::term()
|
{
|
CLog::GetLog()->SetOnLogCallback(nullptr);
|
return 0;
|
}
|
|
CBonder& CModel::getBonder()
|
{
|
return m_bonder;
|
}
|
|
BEQ::IRemoteEquipment* CModel::getBond1()
|
{
|
return m_pEqBond1;
|
}
|
|
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::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;
|
}
|
|
void CModel::onTimer(UINT nTimerid)
|
{
|
ULONGLONG tick = GetTickCount64();
|
int interval = m_configuration.getP2RemoteEqReconnectInterval() * 1000;
|
|
if (m_pEqBond1 != nullptr) {
|
if (!m_pEqBond1->isConnected()) {
|
if (tick - m_pEqBond1->getConnectTick() > interval) {
|
m_pEqBond1->connect();
|
}
|
}
|
}
|
}
|