#include "stdafx.h"
|
#include "Model.h"
|
#include "Log.h"
|
#include "Common.h"
|
#include "ToolUnits.h"
|
#include "CEqAlarmStep.h"
|
#include "AlarmManager.h"
|
#include "CGlassPool.h"
|
#include "TransferManager.h"
|
|
|
CModel::CModel()
|
{
|
m_pObservableEmitter = nullptr;
|
m_pObservable = nullptr;
|
}
|
|
CModel::~CModel()
|
{
|
}
|
|
IObservable* CModel::getObservable()
|
{
|
if (m_pObservable == nullptr) {
|
m_pObservable = RX_AllocaObservable([&](IObservableEmitter* e) -> void {
|
m_pObservableEmitter = e; // ±£´æ·¢ÉäÆ÷
|
});
|
}
|
|
return m_pObservable;
|
}
|
|
SERVO::CMaster& CModel::getMaster()
|
{
|
return m_master;
|
}
|
|
void CModel::setWorkDir(const char* pszWorkDir)
|
{
|
m_strWorkDir = pszWorkDir;
|
}
|
|
void CModel::loadPortParams()
|
{
|
BOOL portEnable, autoChangeEnable;
|
int portType, portMode, cassetteType, transferMode;
|
for (int i = 0; i < 4; i++) {
|
m_configuration.getPortParms(i, portEnable, portType, portMode,
|
cassetteType, transferMode, autoChangeEnable);
|
m_master.setPortType(i, portEnable, portType, portMode, cassetteType,
|
transferMode, autoChangeEnable);
|
}
|
}
|
|
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("Master");
|
CString strSoftRev = _T("1.0.2");
|
|
|
// CGlassPool
|
m_glassPool.initPool();
|
|
|
// 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! ~~~");
|
|
|
SECSListener listener;
|
listener.onEQOffLine = [&](void* pFrom) -> void {
|
LOGI("Ô¶³ÌÇëÇóOffLine");
|
};
|
listener.onEQOnLine = [&](void* pFrom) -> void {
|
LOGI("Ô¶³ÌÇëÇóOnLine");
|
};
|
listener.onCommand = [&](void* pFrom, const char* pszName, std::vector<CommandParameter>& params) -> void {
|
LOGI("onCommand:%s", pszName);
|
for (auto& item : params) {
|
LOGI("param:%s,%s", item.szName, item.szValue);
|
}
|
};
|
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);
|
|
|
SERVO::MasterListener masterListener;
|
masterListener.onMasterStateChanged = [&](void* pMaster, SERVO::MASTERSTATE state) -> void {
|
LOGI("<CModel>Master state changed(%d)", (int)state);
|
notify(RX_CODE_MASTER_STATE_CHANGED);
|
};
|
masterListener.onEqAlive = [&](void* pMaster, SERVO::CEquipment* pEquipment, BOOL bAlive) -> void {
|
LOGI("<CModel>Equipment onAlive:%s(%s).\n", pEquipment->getName().c_str(),
|
bAlive ? _T("ON") : _T("OFF"));
|
notifyPtr(RX_CODE_EQ_ALIVE, pEquipment);
|
|
};
|
masterListener.onEqCimStateChanged = [&](void* pMaster, SERVO::CEquipment* pEquipment, BOOL bOn) -> void {
|
LOGI("<CModel>Equipment Cim State:%s(%s).\n", pEquipment->getName().c_str(),
|
bOn ? _T("ON") : _T("OFF"));
|
notifyPtr(RX_CODE_EQ_ALIVE, pEquipment);
|
|
};
|
masterListener.onEqAlarm = [&](void* pMaster, SERVO::CEquipment* pEquipment, int state, int alarmId, int unitId, int level) -> void {
|
AlarmManager& alarmManager = AlarmManager::getInstance();
|
std::string strAlarmText;
|
const AlarmInfo* pAlarmInfo = alarmManager.getAlarmInfoByID(alarmId);
|
if (pAlarmInfo != nullptr) {
|
strAlarmText = pAlarmInfo->strAlarmText;
|
}
|
if (state == 1) {
|
AlarmData alarmData;
|
alarmData.nId = alarmId;
|
alarmData.nSeverityLevel = level;
|
alarmData.nDeviceId = pEquipment->getID();
|
alarmData.nUnitId = unitId;
|
alarmData.strDeviceName = alarmManager.getDeviceNameById(alarmData.nDeviceId);
|
alarmData.strUnitName = alarmManager.getUnitNameById(alarmData.nDeviceId, alarmData.nUnitId);
|
alarmData.strStartTime = CToolUnits::timeToString2(CToolUnits::getTimestamp());
|
alarmData.strEndTime = "";
|
|
const AlarmInfo* alarmInfo = alarmManager.getAlarmInfoByID(alarmData.nId);
|
alarmData.strDescription = alarmInfo != nullptr ? alarmInfo->strAlarmText : "";
|
|
int nAlarmEventId = 0;
|
bool result = alarmManager.addAlarm(alarmData, nAlarmEventId);
|
if (result) {
|
LOGI("<CModel> Alarm added successfully!");
|
}
|
else {
|
LOGE("<CModel> Failed to add alarm.");
|
}
|
|
notify(RX_CODE_ALARM_SET);
|
}
|
else {
|
alarmManager.clearAlarmByAttributes(alarmId,
|
pEquipment->getID(),
|
unitId,
|
CToolUnits::getCurrentTimeString());
|
notify(RX_CODE_ALARM_CLEAR);
|
}
|
|
m_hsmsPassive.requestAlarmReport(state,
|
pEquipment->getBaseAlarmId() + alarmId,
|
strAlarmText.c_str());
|
};
|
masterListener.onEqVcrEventReport = [&](void* pMaster, SERVO::CEquipment* pEquipment, SERVO::CVcrEventReport* pReport) {
|
LOGE("<CModel>onEqVcrEventReport.");
|
};
|
masterListener.onEqDataChanged = [&](void* pMaster, SERVO::CEquipment* pEquipment, int code) {
|
LOGE("<CModel>onEqDataChanged.");
|
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());
|
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", "Picking", "Placing", "Restoring", "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);
|
|
notifyPtrAndInt(RX_CODE_EQ_ROBOT_TASK, pTask, nullptr, code);
|
LOGI("<CModel>onRobotTaskEvent: ÈÎÎñ¼Ç¼Òѱ£´æ£¬RecordID=%d", nRecordId);
|
};
|
m_master.setListener(masterListener);
|
|
|
// master ÉèÖûº´æÎļþ
|
CString strMasterDataFile;
|
strMasterDataFile.Format(_T("%s\\Master.dat"), (LPTSTR)(LPCTSTR)m_strWorkDir);
|
m_master.setCacheFilepath((LPTSTR)(LPCTSTR)strMasterDataFile);
|
|
|
// ¼ÓÔØ¾¯¸æÐÅÏ¢
|
AlarmManager& alarmManager = AlarmManager::getInstance();
|
char szBuffer[MAX_PATH];
|
sprintf_s(szBuffer, MAX_PATH, "%s\\AlarmList.csv", (LPTSTR)(LPCTSTR)m_strWorkDir);
|
alarmManager.readAlarmFile(szBuffer);
|
|
|
|
return 0;
|
}
|
|
int CModel::term()
|
{
|
m_hsmsPassive.term();
|
CLog::GetLog()->SetOnLogCallback(nullptr);
|
m_master.term();
|
m_glassPool.term();
|
|
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;
|
}
|