#include "stdafx.h"
|
#include "Model.h"
|
#include "Log.h"
|
#include "Common.h"
|
#include "ToolUnits.h"
|
#include "CEqAlarmStep.h"
|
#include "AlarmManager.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.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.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.onEqStepEvent = [&](SERVO::CStep* pStep, int code, void* pData) -> void {
|
if (code == STEP_EVENT_READDATA) {
|
LOGI("<CModel>onEqStepEvent,Êý¾Ý±ä»¯:%s(%s, 0x%x).\n", pStep->getEquipment()->getName().c_str(),
|
pStep->getName().c_str(), pData);
|
notifyPtr(RX_CODE_STEP_EVENT_READDATA, pStep);
|
|
// ´¦Àí¾¯¸æÐÅÏ¢
|
if (isAlarmStep(pStep)) {
|
// ±£´æµ½Êý¾Ý¿â
|
AlarmManager& alarmManager = AlarmManager::getInstance();
|
std::string strAlarmText;
|
SERVO::CEquipment* pEquipment = pStep->getEquipment();
|
SERVO::CEqAlarmStep* pEqAlarmStep = (SERVO::CEqAlarmStep*)pStep;
|
const AlarmInfo* pAlarmInfo = alarmManager.getAlarmInfoByID(pEqAlarmStep->getAlarmId());
|
if (pAlarmInfo != nullptr) {
|
strAlarmText = pAlarmInfo->strAlarmText;
|
}
|
int state = pEqAlarmStep->getAlarmState();
|
if (state == 1) {
|
LOGE("<CAlarmDlg> ·¢Éú¾¯¸æ");
|
std::string startTime = CToolUnits::timeToString2(CToolUnits::getTimestamp());
|
std::string endTime = "";
|
bool result = alarmManager.addAlarm(std::to_string(pEqAlarmStep->getAlarmId()),
|
pEquipment->getName(), strAlarmText, startTime, endTime);
|
if (result) {
|
LOGI("<CAlarmDlg> Alarm added successfully!");
|
}
|
else {
|
LOGE("<CAlarmDlg> Failed to add alarm.");
|
}
|
}
|
else {
|
LOGE("<CAlarmDlg> iÏû³ý¾¯¸æ");
|
//alarmManager.updateAlarmEndTime(std::to_string(pEqAlarmStep->getAlarmId()),
|
// pEquipment->getName());
|
}
|
|
m_hsmsPassive.requestAlarmReport(pEqAlarmStep->getAlarmState(),
|
pEquipment->getBaseAlarmId() + pEqAlarmStep->getAlarmId(),
|
strAlarmText.c_str());
|
}
|
}
|
};
|
|
m_master.setListener(masterListener);
|
|
|
// ¼ÓÔØ¾¯¸æÐÅÏ¢
|
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();
|
|
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;
|
}
|
|
bool CModel::isAlarmStep(SERVO::CStep* pStep)
|
{
|
return CToolUnits::startsWith(pStep->getName(), STEP_ALARM_START);
|
}
|