| | |
| | | { |
| | | m_pObservableEmitter = nullptr; |
| | | m_pObservable = nullptr; |
| | | m_pServo = nullptr; |
| | | m_pEqBond1 = nullptr; |
| | | m_pEquipment = nullptr; |
| | | m_nTimerID = 0; |
| | | } |
| | | |
| | |
| | | |
| | | |
| | | // 创建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); |
| | | BEQ_CreateEquipment(m_pEquipment, "BLBonder"); |
| | | ASSERT(m_pEquipment); |
| | | BEQ::EquipmentListener listener; |
| | | listener.onConnected = [&](void* pEiuipment, const char* pszAddr, int port) -> void { |
| | | LOGI("<EquipmentListener>连接进入(%s:%d).", pszAddr, port); |
| | | notifyPtr(RX_CODE_EQ_STATE_CHANGED, pEiuipment); |
| | | }; |
| | | 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.onDisconnected = [&](void* pEiuipment, const char* pszAddr, int port) -> void { |
| | | LOGI("<EquipmentListener>连接断开(%s:%d).", pszAddr, port); |
| | | notifyPtr(RX_CODE_EQ_STATE_CHANGED, pEiuipment); |
| | | }; |
| | | 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_pEquipment->setEquipmentListener(listener); |
| | | |
| | | |
| | | m_pObservable = RX_AllocaObservable([&](IObservableEmitter* e) -> void { |
| | |
| | | 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; |
| | | } |
| | |
| | | return m_bonder; |
| | | } |
| | | |
| | | BEQ::IRemoteEquipment* CModel::getBond1() |
| | | BEQ::IEquipment* CModel::getEquipment() |
| | | { |
| | | return m_pEqBond1; |
| | | return m_pEquipment; |
| | | } |
| | | |
| | | int CModel::notify(int code) |
| | |
| | | |
| | | 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(); |
| | | } |
| | | } |
| | | } |
| | | } |