#include "stdafx.h" #include "Common.h" #include "Log.h" #include "Model.h" #include "Log.h" #include "ToolUnits.h" #include "Alarm.h" #include "EQState.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_pEquipment = nullptr; m_nTimerID = 0; } CModel::~CModel() { } IObservable* CModel::getObservable() { return m_pObservable; } void CModel::setWorkDir(const char* pszWorkDir) { m_strWorkDir = pszWorkDir; m_bonder.setWorkDir(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_CreateEquipment(m_pEquipment, "BLBonder"); ASSERT(m_pEquipment); BEQ::EquipmentListener listener; listener.onConnected = [&](void* pEiuipment, const char* pszAddr, int port) -> void { LOGI("????(%s:%d).", pszAddr, port); notifyPtr(RX_CODE_EQ_STATE_CHANGED, pEiuipment); }; listener.onDisconnected = [&](void* pEiuipment, const char* pszAddr, int port) -> void { LOGI("????(%s:%d).", pszAddr, port); notifyPtr(RX_CODE_EQ_STATE_CHANGED, pEiuipment); }; // ??????????????? m_pEquipment->setEquipmentListener(listener); m_pObservable = RX_AllocaObservable([&](IObservableEmitter* e) -> void { m_pObservableEmitter = e; // ????? }); m_nTimerID = (int)SetTimer(NULL, 1, 1000, (TIMERPROC)ModerTimerProc); g_pModel = this; BondListener bonderListener; bonderListener.onStateChanged = [&](void* pFrom, int state) -> void { notifyInt(RX_CODE_BONDER_STATE_CHANGED, state); }; bonderListener.onPlcStateChanged = [&](void* pFrom, int state) -> void { PLCSTATE ps = (PLCSTATE)state; if (ps == PLCSTATE::CONNECTED) { notifyPtrAndInt(RX_CODE_PLC1_CONNECTTD, pFrom, 0, state); } else if (ps == PLCSTATE::DISCONNECTED) { notifyPtrAndInt(RX_CODE_PLC1_DISCONNECTTD, pFrom, 0, state); } }; bonderListener.onEfemStateChanged = [&](void* pFrom, const char* pszAddr, int port, int state) -> void { if (m_pObservableEmitter != NULL) { IAny* pAny = RX_AllocaAny(); if (pAny != NULL) { pAny->addRef(); pAny->setCode(RX_CODE_EFEM_STATUS_CHANGED); pAny->setStringValue("addr", pszAddr); pAny->setIntValue("port", port); pAny->setIntValue("state", state); m_pObservableEmitter->onNext(pAny); pAny->release(); } } }; bonderListener.onRecvBroadcast = [&](void* pFrom, void* p) -> void { CComponent* pSender = (CComponent*)pFrom; CIntent* pIntent = (CIntent*)p; int code = pIntent->getCode(); if (code == BC_CODE_DATA1_MATERIAL_RECEIVED) { notifyPtr(RX_CODE_DATA1_MATERIAL_RECEIVED, pSender); } else if (code == BC_CODE_DATA1_BEGIN_SAMPLING) { notifyPtr(RX_CODE_DATA1_BEGIN_SAMPLING, pSender); } else if (code == BC_CODE_DATA1_UPDATE) { notifyPtr(RX_CODE_DATA1_UPDATE, pSender); } else if (code == BC_CODE_DATA1_END_SAMPLING) { notifyPtr(RX_CODE_DATA1_END_SAMPLING, pSender); } else if (code == BC_CODE_DATA1_MATERIAL_REMOVED) { std::string strError; CPanel* pPanel = (CPanel*)pIntent->getContext(); pPanel->addRef(); savePanel(pPanel, pSender); m_sqlite.insertPanel(pPanel, strError); notifyPtr(RX_CODE_DATA1_MATERIAL_REMOVEED, pSender); notifyObj(RX_CODE_PANEL_COMPLATE, pPanel); pPanel->release(); } else if (code == BC_CODE_ALARM_EVENT) { CAlarm* pAlarm = (CAlarm*)pIntent->getContext(); pAlarm->addRef(); notifyObjAndPtr(RX_CODE_ALARM_EVENT, pAlarm, pSender); pAlarm->release(); } else if (code == BC_CODE_EQSTATE_EVENT) { CEQState* pState = (CEQState*)pIntent->getContext(); pState->addRef(); notifyObjAndPtr(RX_CODE_EQSTATE_EVENT, pState, pSender); pState->release(); } }; m_bonder.setListener(bonderListener); m_bonder.init(); return 0; } int CModel::term() { m_bonder.save(); m_bonder.term(); m_sqlite.term(); CLog::GetLog()->SetOnLogCallback(nullptr); return 0; } CBonder& CModel::getBonder() { return m_bonder; } BEQ::IEquipment* CModel::getEquipment() { return m_pEquipment; } 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) { } int CModel::savePanel(CPanel* pPanel, CComponent* pComponent) { int year, month, day; pPanel->getReceivedTime(year, month, day); CString strDir, strFilepath; strDir.Format("%s\\%d\\%d\\%d", (LPTSTR)(LPCTSTR)m_strDataDir, year, month, day); CToolUnits::createDir((LPTSTR)(LPCTSTR)strDir); strFilepath.Format("%s\\%s.csv", (LPTSTR)(LPCTSTR)strDir, pPanel->getQRCode().c_str()); CStdioFile file; if (!file.Open(strFilepath, CFile::modeCreate | CFile::modeWrite)) { return -1; } // ???? // ??, id,??????,?????? CString strLine; file.WriteString("# ??\n"); strLine.Format(_T("??,%s\n"), pComponent->getName().c_str()); file.WriteString(strLine); strLine.Format(_T("id,%s\n"), pPanel->getQRCode().c_str()); file.WriteString(strLine); strLine.Format(_T("????,%s\n"), CToolUnits::timeToString3(pPanel->getReceivedTime()).c_str()); file.WriteString(strLine); strLine.Format(_T("??????,%s\n"), CToolUnits::timeToString3(pPanel->getBeginSamplingTime()).c_str()); file.WriteString(strLine); strLine.Format(_T("??????,%s\n"), CToolUnits::timeToString3(pPanel->getEndSamplingTime()).c_str()); file.WriteString(strLine); strLine.Format(_T("????,%s\n"), CToolUnits::timeToString3(pPanel->getRemovedTime()).c_str()); file.WriteString(strLine); strLine.Format(_T("Recipe name,%s\n"), pPanel->getRecipeName().c_str()); file.WriteString(strLine); strLine.Format(_T("??????,%f\n"), pPanel->getAir1()); file.WriteString(strLine); strLine.Format(_T("???????,%f\n"), pPanel->getAir2()); file.WriteString(strLine); strLine.Format(_T("?????,%f\n"), pPanel->getAir3()); file.WriteString(strLine); strLine.Format(_T("?????,%d\n"), pPanel->getPre1()); file.WriteString(strLine); strLine.Format(_T("?????,%f\n"), pPanel->getTmp1()); file.WriteString(strLine); strLine.Format(_T("?????,%f\n"), pPanel->getTmp2()); file.WriteString(strLine); file.WriteString("\n"); // ??? file.WriteString("# ??\n"); file.WriteString("?,?????(?),?????(?),?????1(?),?????2(?),?????3(?),?????4(?),?????5(?),?????1(?),?????2(?),?????3(?),?????4(?),?????5(?),????(Pa),????1(Kg),????2(Kg),????3(Kg),????4(Kg),????5(Kg)\n"); UNITDATA1 dataMax = pPanel->getMathData(IMAX); strLine.Format(_T("???,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.03f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f\n"), dataMax.temp[0], dataMax.temp[1], dataMax.temp[2], dataMax.temp[3], dataMax.temp[4], dataMax.temp[5], dataMax.temp[6], dataMax.temp[7], dataMax.temp[8], dataMax.temp[9], dataMax.temp[10], dataMax.temp[11], dataMax.vacuum, dataMax.pressure[0], dataMax.pressure[1], dataMax.pressure[2], dataMax.pressure[3], dataMax.pressure[4]); file.WriteString(strLine); UNITDATA1 dataMin = pPanel->getMathData(IMIN); strLine.Format(_T("???,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.03f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f\n"), dataMin.temp[0], dataMin.temp[1], dataMin.temp[2], dataMin.temp[3], dataMin.temp[4], dataMin.temp[5], dataMin.temp[6], dataMin.temp[7], dataMin.temp[8], dataMin.temp[9], dataMin.temp[10], dataMin.temp[11], dataMin.vacuum, dataMin.pressure[0], dataMin.pressure[1], dataMin.pressure[2], dataMin.pressure[3], dataMin.pressure[4]); file.WriteString(strLine); UNITDATA1 dataAve = pPanel->getMathData(IAVE); strLine.Format(_T("???,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.03f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f\n"), dataAve.temp[0], dataAve.temp[1], dataAve.temp[2], dataAve.temp[3], dataAve.temp[4], dataAve.temp[5], dataAve.temp[6], dataAve.temp[7], dataAve.temp[8], dataAve.temp[9], dataAve.temp[10], dataAve.temp[11], dataAve.vacuum, dataAve.pressure[0], dataAve.pressure[1], dataAve.pressure[2], dataAve.pressure[3], dataAve.pressure[4]); file.WriteString(strLine); file.WriteString("\n"); // ??? file.WriteString("# ???\n"); file.WriteString("??,?????(?),?????(?),?????1(?),?????2(?),?????3(?),?????4(?),?????5(?),?????1(?),?????2(?),?????3(?),?????4(?),?????5(?),????(Pa),????1(Kg),????2(Kg),????3(Kg),????4(Kg),????5(Kg)\n"); std::vector datas; pPanel->getDatas(datas); for (auto item : datas) { strLine.Format(_T("%s,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.03f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f,%.01f\n"), CToolUnits::timeToString3(item.time).c_str(), item.temp[0], item.temp[1], item.temp[2], item.temp[3], item.temp[4], item.temp[5], item.temp[6], item.temp[7], item.temp[8], item.temp[9], item.temp[10], item.temp[11], item.vacuum, item.pressure[0], item.pressure[1], item.pressure[2], item.pressure[3], item.pressure[4]); file.WriteString(strLine); } file.Close(); return 0; }