#include "stdafx.h" #include #include "Helper.JSON.hpp" #include "DIOS.Dev.Mech.Demo.hpp" using namespace DIOS::Dev; using namespace DIOS::Dev::Detail::MECH; namespace nsMech = DIOS::Dev::Detail::MECH; #pragma warning (disable:4305) #define MY_PI 3.14159265358979323846 Log4CPP::Logger* mLog::gLogger = nullptr; //----------------------------------------------------------------------------- // DemoDevice //----------------------------------------------------------------------------- nsMech::DemoDevice::DemoDevice(std::shared_ptr center, string configfile /*= ""*/):super(center) { string version; if (GetVersion(version, hMyModule)) mLog::Info("\n===============log begin : version:{$} ===================\n", version.c_str()); else mLog::Info("\n===============log begin : version:0.0.0.0 ===================\n"); ResDataObject Conftemp; if (!configfile.empty()) { Conftemp.loadFile(configfile.c_str()); m_MechConfig = Conftemp["CONFIGURATION"]; TransJsonText(m_MechConfig); } //normal parameter m_MECHUnit.m_MechState.reset(new MechStateMould(AttrKey::MACHINE_STATUS_STANDBY, AttrKey::MACHINE_STATUS_SHUTDOWN, AttrKey::MACHINE_STATUS_SERVICE, 1)); m_MECHUnit.m_FID.reset(new FIDMould(0, 0, 10000, 1)); m_MECHUnit.m_TubeAngle.reset(new TubeAngleMould(0, -180, 180, 1)); m_MECHUnit.m_FPDPosition.reset(new FPDPositionMould(0, 0, 100, 1)); m_MECHUnit.m_PatientSize.reset(new PATIENTSIZEMould(0, 0, 10, 1)); m_MECHUnit.m_PostionNumber.reset(new POSITIONNUMBERMould(0, 0, 10000, 1)); m_MECHUnit.m_SID.reset(new SIDMould(100, 0, 1000, 1)); if (m_MechConfig.GetKeyCount("MechanicalAngle") > 0) { if (m_MechConfig["MechanicalAngle"].GetKeyCount("min") > 0) m_nMinAngle = m_MechConfig["MechanicalAngle"]["min"]; if (m_MechConfig["MechanicalAngle"].GetKeyCount("max") > 0) m_nMaxAngle = m_MechConfig["MechanicalAngle"]["max"]; mLog::Debug("config:MechanicalAngle[min:{$},max:{$}]", m_nMinAngle, m_nMaxAngle); } m_MECHUnit.m_TubeAngle.reset(new TubeAngleMould(90, m_nMinAngle, m_nMaxAngle, 1)); if (m_MechConfig.GetKeyCount("MechanicalHeight") > 0) { if (m_MechConfig["MechanicalHeight"].GetKeyCount("min") > 0) m_nMinHeight = m_MechConfig["MechanicalHeight"]["min"]; if (m_MechConfig["MechanicalHeight"].GetKeyCount("max") > 0) m_nMaxHeight = m_MechConfig["MechanicalHeight"]["max"]; mLog::Debug("config:MechanicalHeight[min:{$},max:{$}]", m_nMinHeight, m_nMaxHeight); } m_MECHUnit.m_TubeHeight.reset(new TubeHeightMould(150, m_nMinHeight, m_nMaxHeight, 1)); m_MECHUnit.m_FID.reset(new FIDMould(150, m_nMinHeight, m_nMaxHeight, 1)); //拼接 m_MECHUnit.m_StitchingState.reset(new StitchingStateMould(AttrKey::STITCHSTATE_INIT, AttrKey::STITCHSTATE_ERR, AttrKey::STITCHSTATE_MAX, 1)); m_MECHUnit.m_StitchHeight.reset(new StitchHeightMould(0, m_nMinHeight, m_nMaxHeight, 1)); m_MECHUnit.m_StitchLength.reset(new StitchLengthMould(0, 0, 1000, 1)); m_MECHUnit.m_StitchOverLap.reset(new StitchOverLapMould(0, 0, 1000, 1)); m_MECHUnit.m_StitchPID.reset(new StitchPIDMould(0, 0, 1000, 1)); m_MECHUnit.m_StitchDirection.reset(new StitchDirectionMould(AttrKey::STITCHDIR_NULL, AttrKey::STITCHDIR_NULL, AttrKey::STITCHDIR_MAX, 1)); m_MECHUnit.m_StitchType.reset(new StitchTypeMould(AttrKey::STITCHTYPE_NULL, AttrKey::STITCHTYPE_NULL, AttrKey::STITCHTYPE_MAX, 1)); m_MECHUnit.m_StitchStepLength.reset(new StitchStepLengthMould(10, 0, 1000, 1)); m_MECHUnit.m_FPDPosition.reset(new FPDPositionMould(0, 0, 100, 1)); m_MECHUnit.m_TotalImageCount.reset(new TotalImageCountMould(0, 0, 100, 1)); m_MECHUnit.m_CurrentImageNumber.reset(new CurrentImageNumberMould(100, 0, 100, 1)); //遮光器 m_Collimator.fHeight = 0; m_Collimator.fWidth = 0; m_MSGUnit.reset(new nsDetail::MSGUnit(center, MechUnitType)); //配置响应操作对照表 //OnCallBack(); //将机架可以对外提供的指令注册集进行补充 Register(); } nsMech::DemoDevice::~DemoDevice() { mLog::Info("\n===============log end ===================\n"); } std::string nsMech::DemoDevice::GetGUID() const { mLog::Info("\n===============GetGUID : {$} ===================\n", MechUnitType); return StitchMechUnitType; } void nsMech::DemoDevice::Register() { auto Disp = &Dispatch; superMech::RegisterNormalControl(Disp); superMech::RegisterStitchingControl(Disp); Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; }); Disp->Get.Push("StitchPlanMinAngle", [this](std::string& out) { out = to_string(m_nMinAngle); return RET_STATUS::RET_SUCCEED; }); Disp->Get.Push("StitchPlanMaxAngle", [this](std::string& out) { out = to_string(m_nMaxAngle); return RET_STATUS::RET_SUCCEED; }); Disp->Get.Push("StitchPlanMinHeight", [this](std::string& out) { out = to_string(m_nMinHeight); return RET_STATUS::RET_SUCCEED; }); Disp->Get.Push("StitchPlanMaxHeight", [this](std::string& out) { out = to_string(m_nMaxHeight); return RET_STATUS::RET_SUCCEED; }); auto fun_SendStitchState = [this](auto in, auto& out) { FireNotify(AttrKey::STITCHINGSTATE, m_MECHUnit.m_StitchingState->Get()); return RET_STATUS::RET_SUCCEED; }; Disp->Action.Push("SendStitchState", fun_SendStitchState); } void nsMech::DemoDevice::FireNotify(std::string key, std::string content) { EventCenter->OnNotify(1, key, content); } void nsMech::DemoDevice::FireNotify(std::string key, int context) { char szInfo[64] = { 0 }; sprintf_s(szInfo, "%d", context); EventCenter->OnNotify(1, key, szInfo); } //检查信息接口 RET_STATUS nsMech::DemoDevice::SetStudyInfo(ResDataObject& pParam) { auto& P0 = pParam[0]; SetPatientInfo(P0); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetViewInfo(ResDataObject& pParam) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetPatientInfo(ResDataObject& pParam) { ECOM_PATIENT Info; Info.strPatientName = (string)pParam["PatientName"]; Info.strSex = (string)pParam["Sex"]; Info.strAge = (string)pParam["Age"]; Info.strPatientID = (string)pParam["PatientID"]; Info.strPatientSize = (string)pParam["PatientSize"]; Info.strStudyDescription = (string)pParam["StudyDescription"]; Info.strAccessionNumber = (string)pParam["AccessionNumber"]; mLog::Debug("Enter SetPatientInfo:[Name:{$}],[Sex:{$}],[Age:{$}],[ID:{$}],[Size:{$}],[Description:{$}],[AccessionNumber:{$}]", Info.strPatientName.c_str(), Info.strSex.c_str(), Info.strAge.c_str(), Info.strPatientID.c_str(), Info.strPatientSize.c_str(), Info.strStudyDescription.c_str(), Info.strAccessionNumber.c_str()); return RET_STATUS::RET_SUCCEED; } //基本 RET_STATUS nsMech::DemoDevice::Reset() { mLog::Debug("Enter Reset"); mLog::Debug("End Reset"); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetTechParamsInfo(ResDataObject& pParam) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetPositionNumber(int pn) { mLog::Debug("Enter SetPositionNumber:[{$}]", pn); m_MECHUnit.m_PostionNumber->Update(pn); FireNotify(AttrKey::POSITIONNUMBER, m_MECHUnit.m_PostionNumber->JSGet()); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetSID(float value) { mLog::Debug("Enter SetSID:[{$}]->[{$}]", m_MECHUnit.m_SID->Get(), value); if (m_MECHUnit.m_SID->Update(value)) FireNotify(AttrKey::SID, m_MECHUnit.m_SID->JSGet()); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetSOD(float value) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::MoveToHome(string& value) { int PosNum = atoi(value.c_str()); mLog::Debug("Enter MoveToHome:[{$}]", PosNum); return RET_STATUS::RET_FAILED; } RET_STATUS nsMech::DemoDevice::MoveMech(string& value) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::StopMech(string& value) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetGrid(AttrKey::MECH_GRIDSTATE GridState) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetAutoTracking(int nAutoTracking) { mLog::Debug("Enter MoveToHome:[{$}]", nAutoTracking); return RET_STATUS::RET_SUCCEED; } //拼接接口 RET_STATUS nsMech::DemoDevice::BeginStitching() { if(m_MECHUnit.m_MechState->Update(AttrKey::MACHINE_STATUS_STITCH)) FireNotify(AttrKey::MACHSTATE, m_MECHUnit.m_MechState->Get()); mLog::Debug("Enter BeginStitching"); if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_START)) { FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_START); m_MECHUnit.m_strInfo = "Stitch Start"; mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str()); FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo); } InitStitching(); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::InitStitching() { mLog::Debug("Enter InitStitching"); if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_READY)) { FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_READY); m_MECHUnit.m_strInfo = "Stitch Ready"; mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str()); FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo); } return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::EndStitching() { mLog::Debug("Enter EndStitching"); if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_END)) { FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_END); m_MECHUnit.m_strInfo = "Stitch Finish"; mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str()); FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo); } return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetupStitching(string& value) { ResDataObject json, P0; json.decode(value.c_str()); if (json.GetKeyCount("P0") > 0) { P0 = json[0]; } else P0 = json; int nInitHeight = atoi(((string)P0[AttrKey::STITCHHEIGHT]).c_str()); int nTargetLength = atoi(((string)P0[AttrKey::STITCHLENGTH]).c_str()); int nOverlap = atoi(((string)P0[AttrKey::STITCHOVERLAP]).c_str()); int nSID = atoi(((string)P0[AttrKey::STITCHSID]).c_str()); int nTID = atoi(((string)P0[AttrKey::STITCHTID]).c_str()); int nPID = atoi(((string)P0[AttrKey::STITCHPID]).c_str()); int nDirection = atoi(((string)P0[AttrKey::STITCHDIRECTION]).c_str()); int nStitchType = atoi(((string)P0[AttrKey::STITCHTYPE]).c_str()); mLog::Debug("Enter SetupStitching:[InitHeight:{$},TotalLength:{$},Overlap:{$},SID:{$},TID:{$},PID:{$},Direction:{$},StitchType:{$}]", nInitHeight, nTargetLength, nOverlap, nSID, nTID, nPID, nDirection, nStitchType); m_MECHUnit.m_StitchHeight->Update(nInitHeight); m_MECHUnit.m_StitchOverLap->Update(nOverlap); m_MECHUnit.m_StitchDirection->Update(nDirection); m_MECHUnit.m_StitchType->Update(nStitchType); if (m_MECHUnit.m_CurrentImageNumber->Update(0)) FireNotify(AttrKey::CURRENTIMAGENUMBER, 0); switch (m_MECHUnit.m_StitchType->Get()) { case AttrKey::STITCHTYPE_AutomaticTranslation: case AttrKey::STITCHTYPE_ManualTranslation: { //总长度校验 int maxLength = m_MECHUnit.m_TubeHeight->GetLimitMax() - m_MECHUnit.m_TubeHeight->GetLimitMin(); if (nTargetLength > (maxLength)) { mLog::Warn("StitchingLengh[{$}] too big [height min:{$},height max:{$}],need change to[{$}]", nTargetLength, m_MECHUnit.m_TubeHeight->GetLimitMax(), m_MECHUnit.m_TubeHeight->GetLimitMin(), maxLength); nTargetLength = maxLength; } m_MECHUnit.m_StitchLength->Update(nTargetLength); //总张数计算 int totalImage = (float)(nTargetLength - nOverlap) / (float)(m_nFPDLengh - nOverlap); if (m_MECHUnit.m_TotalImageCount->Update(totalImage)) FireNotify(AttrKey::TOTALIMAGECOUNT, totalImage); //计算步长 float tempStepLength = (float)nTargetLength / (float)totalImage; if (m_MECHUnit.m_StitchStepLength->Update(tempStepLength)) FireNotify(AttrKey::STITCHSTEPLENGTH, tempStepLength); mLog::Debug("compute TransStitching [total ImageCount:{$},StepLength:{$}]", totalImage, tempStepLength); }break; case AttrKey::STITCHTYPE_AutomaticAngle: case AttrKey::STITCHTYPE_ManualAngle: { //总长度校验 int maxLength = m_MECHUnit.m_TubeHeight->GetLimitMax() - m_MECHUnit.m_TubeHeight->GetLimitMin(); //角度范围 float maxStitchAngle = 2 * atan((float)maxLength / 2.0f / m_MECHUnit.m_SID->Get()) * 180.0f / MY_PI; mLog::Debug("compute AngleStitching limit[FPDLengh:{$},OverLap:{$}, SID:{$}]->angle[min:0,max:{$}]", m_nFPDLengh, nOverlap, m_MECHUnit.m_SID->Get(), maxStitchAngle); //设置参数的总角度 if (nTargetLength > maxStitchAngle) { nTargetLength = maxStitchAngle; } m_MECHUnit.m_StitchLength->Update(nTargetLength); //总张数计算 int moveLengh = 2 * tan((float)nTargetLength / 2.0f / 180.0f * MY_PI) * m_MECHUnit.m_SID->Get(); int totalImage = std::ceil((float)(moveLengh - nOverlap) / (float)(m_nFPDLengh - nOverlap)); m_MECHUnit.m_TotalImageCount->Update(totalImage); FireNotify(AttrKey::TOTALIMAGECOUNT, totalImage); //计算步长 float tempStepLength = (float)nTargetLength / (float)totalImage; if (m_MECHUnit.m_StitchStepLength->Update(tempStepLength)) FireNotify(AttrKey::STITCHSTEPLENGTH, tempStepLength); mLog::Debug("compute AngleStitching [total ImageCount:{$},StepLength:{$}]", totalImage, tempStepLength); }break; default: return RET_STATUS::RET_FAILED; } if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_INIT)) { FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_INIT); m_MECHUnit.m_strInfo = "Stitch Init"; mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str()); FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo); } if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_REACH_INIT)) { FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_REACH_INIT); m_MECHUnit.m_strInfo = "Stitch Reach to the initial position"; mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str()); FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo); } return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::AcceptStitchingImage() { mLog::Debug("Enter AcceptStitchingImage"); int Currentnum = m_MECHUnit.m_CurrentImageNumber->Get() + 1; if (m_MECHUnit.m_CurrentImageNumber->Update(Currentnum)) { mLog::Debug("stitching current image[{$}:{$}]", m_MECHUnit.m_CurrentImageNumber->Get(), m_MECHUnit.m_TotalImageCount->Get()); FireNotify(AttrKey::CURRENTIMAGENUMBER, Currentnum); } if (m_MECHUnit.m_CurrentImageNumber->Get() >= m_MECHUnit.m_TotalImageCount->Get()) { EndStitching(); } else { if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_Accept_Image)) { FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_Accept_Image); m_MECHUnit.m_strInfo = "Stitch Accept Current image"; mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str()); FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo); } Sleep(2000); if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_READY)) { FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_READY); m_MECHUnit.m_strInfo = "Stitch Ready"; mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str()); FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo); } } return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::RejectStitchingImage() { mLog::Debug("Enter RejectStitchingImage"); if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_Reject_Image)) { FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_Reject_Image); m_MECHUnit.m_strInfo = "Stitch Reject Current Image"; mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str()); FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo); } return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::CancelStitching() { mLog::Debug("Enter CancelStitching"); if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_CANCEL)) { FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_CANCEL); m_MECHUnit.m_strInfo = "Stitch Cancel"; mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str()); FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo); } return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::CompleteStitching() { mLog::Debug("Enter CompleteStitching"); if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_END)) FireNotify(AttrKey::STITCHINGSTATE, m_MECHUnit.m_StitchingState->JSGet()); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::NewExtraView() { mLog::Debug("Enter NewExtraView"); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::RepeatStitching() { mLog::Debug("Enter RepeatStitching"); if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_RESTART)) FireNotify(AttrKey::STITCHINGSTATE, m_MECHUnit.m_StitchingState->JSGet()); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetAutoPosiitonNo(int nPN) { mLog::Debug("Enter SetAutoPosiitonNo:[{$}]->[{$}]", m_MECHUnit.m_PostionNumber->Get(), nPN); m_MECHUnit.m_PostionNumber->Update(nPN); FireNotify(AttrKey::POSITIONNUMBER, m_MECHUnit.m_PostionNumber->JSGet()); return RET_STATUS::RET_SUCCEED; } //牛头 RET_STATUS nsMech::DemoDevice::SetExpEnable(bool nExpEnabled) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetWS(int nWS) { mLog::Debug("Enter SetWS:[{$}]", nWS); if (m_nWS == nWS)return RET_STATUS::RET_SUCCEED; m_nWS = nWS; return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetKV(int nKV) { mLog::Debug("Enter SetKV:[{$}]", nKV); if (m_nKV == nKV)return RET_STATUS::RET_SUCCEED; m_nKV = nKV; return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetMA(float fMA) { mLog::Debug("Enter SetMA:[{$}]", fMA); if (m_fMA == fMA)return RET_STATUS::RET_SUCCEED; m_fMA = fMA; return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetMS(float fMS) { mLog::Debug("Enter SetMS:[{$}]", fMS); if (m_fMS == fMS)return RET_STATUS::RET_SUCCEED; m_fMS = fMS; return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetMAS(float fMAS) { mLog::Debug("Enter SetMAS:[{$}]", fMAS); if (m_fMAS == fMAS)return RET_STATUS::RET_SUCCEED; m_fMAS = fMAS; return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetFO(int nFO) { mLog::Debug("Enter SetFO:[{$}]", nFO); if (m_nFocus == nFO)return RET_STATUS::RET_SUCCEED; m_nFocus = nFO; return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetTechMode(int nET) { mLog::Debug("Enter SetTechMode:[{$}]", nET); if (m_nET == nET)return RET_STATUS::RET_SUCCEED; m_nET = nET; return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetAECField(int nAECFieldSel) { mLog::Debug("Enter SetAECField:[{$}]", nAECFieldSel); if (m_nAECField == nAECFieldSel)return RET_STATUS::RET_SUCCEED; m_nAECField = nAECFieldSel; return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetDensity(float nAECDensity) { mLog::Debug("Enter SetDensity:[{$}]", nAECDensity); if (m_nAECDensity == nAECDensity)return RET_STATUS::RET_SUCCEED; m_nAECDensity = nAECDensity; return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetBodySize(string strSize) { mLog::Debug("Enter SetBodySize:[{$}]", strSize.c_str()); char temp[50] = { 0 }; int nPS = 2; if (strSize == "Small") { nPS = 1; } else if (strSize == "Middle") { nPS = 2; } else if (strSize == "Large") { nPS = 3; } else if (strSize == "Child") { nPS = 4; } if (m_nBodySize == nPS)return RET_STATUS::RET_SUCCEED; m_nBodySize = nPS; return RET_STATUS::RET_SUCCEED; } //遮光器 RET_STATUS nsMech::DemoDevice::SetCollimator(ECOM_COLLIMATOR_INFO& curCollimator) { mLog::Debug("Enter SetCollimator:Height[{$}]->[{$}],Width[{$}]->[{$}]", m_Collimator.fHeight, curCollimator.fHeight, m_Collimator.fWidth, curCollimator.fWidth); m_Collimator.fHeight = curCollimator.fHeight; m_Collimator.fWidth = curCollimator.fWidth; FireNotify("XSize", m_Collimator.fHeight); FireNotify("YSize", m_Collimator.fWidth); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsMech::DemoDevice::SetFilter(int nFilter) { mLog::Debug("Enter SetFilter[{$}]", nFilter); FireNotify("Filter", nFilter); return RET_STATUS::RET_SUCCEED; } //----------------------------------------------------------------------------- // DemoDriver //----------------------------------------------------------------------------- nsMech::DemoDriver::DemoDriver() { m_pAttribute.reset(new ResDataObject()); m_pDescription.reset(new ResDataObject()); } nsMech::DemoDriver::~DemoDriver() { } auto nsMech::DemoDriver::CreateDevice(int index) -> std::unique_ptr { mLog::Info("Enter CreateDevice, index={$}", index); if (index == 0) { m_pDrMechDev = new DemoDevice(EventCenter, m_ConfigFileName); auto dev = std::unique_ptr(new IODevice(m_pDrMechDev)); return dev; } mLog::Error("unknown index[{$}]", index); unique_ptr dev; return dev; } void nsMech::DemoDriver::FireNotify(int code, std::string key, std::string content) { EventCenter->OnNotify(code, key, content); } void nsMech::DemoDriver::Prepare() { string strLogPath = GetProcessDirectory() + R"(\OEMDrivers\Mechanical\Conf\Log4CPP.Config.MECH.xml)"; //Log4CPP::ThreadContext::Map::Set("LogFileName", "MECH.Demo"); Log4CPP::GlobalContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "Mech.Demo"); auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str()); mLog::gLogger = Log4CPP::LogManager::GetLogger("Mech.Demo"); ResDataObject r_config; if (r_config.loadFile(m_ConfigFileName.c_str())) { ResDataObject Connection = r_config["CONFIGURATION"]["connections"][0]; } } bool nsMech::DemoDriver::Connect() { m_bDemoConnected = true; return true; } void nsMech::DemoDriver::Disconnect() { super::Disconnect(); m_bDemoConnected = false; } bool nsMech::DemoDriver::isConnected() const { return m_bDemoConnected; } std::string nsMech::DemoDriver::DriverProbe() { ResDataObject r_config, HardwareInfo; if (r_config.loadFile(m_ConfigFileName.c_str())) { HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]); HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]); HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]); HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]); HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]); } else { HardwareInfo.add("MajorID", "Machine"); HardwareInfo.add("MinorID", "Dr"); HardwareInfo.add("VendorID", "ECOM"); HardwareInfo.add("ProductID", "HF"); HardwareInfo.add("SerialID", "1234"); } string ret = HardwareInfo.encode(); return ret; } std::string nsMech::DemoDriver::DeviceProbe() { ResDataObject r_config, HardwareInfo; if (r_config.loadFile(m_ConfigFileName.c_str())) { HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]); HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]); HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]); HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]); HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]); } else { HardwareInfo.add("MajorID", "Machine"); HardwareInfo.add("MinorID", "Dr"); HardwareInfo.add("VendorID", "ECOM"); HardwareInfo.add("ProductID", "HF"); HardwareInfo.add("SerialID", "1234"); } string ret = HardwareInfo.encode(); return ret; } bool nsMech::DemoDriver::GetDeviceConfig(std::string& Cfg) { Cfg = m_DeviceConfigSend.encode(); return true; } bool nsMech::DemoDriver::SetDeviceConfig(std::string Cfg) { //mLog::Info("--Func-- SetDeviceConfig {$}\n", Cfg.c_str()); ResDataObject DeviceConfig; DeviceConfig.decode(Cfg.c_str()); ResDataObject DescriptionTempEx; DescriptionTempEx = DeviceConfig["DeviceConfig"]["Attribute"]; //mLog::Debug("Attribute:{$}", DescriptionTempEx.encode()); bool bSaveFile = false; //true:重新保存配置文件 string strAccess = ""; for (int i = 0; i < DescriptionTempEx.size(); i++) { string strKey = DescriptionTempEx.GetKey(i); //mLog::Info("{$}", strKey.c_str()); printf("%s\n", strKey.c_str()); try { if (m_pAttribute->GetFirstOf(strKey.c_str()) >= 0) { strAccess = (string)(*m_pDescription)[strKey.c_str()]["Access"]; if ("RW" == strAccess) { //修改对应配置,在其他单元的配置项要同时调用其修改函数修改真实值 //1. 修改内存中的值,用于给上层发消息 (*m_pAttribute)[strKey.c_str()] = DescriptionTempEx[i]; //2. 拿到Innerkey int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo"); //mLog::Info("nConfigInfoCount {$}", nConfigInfoCount); string strTemp = ""; //存储AttributeKey for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++) { strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"]; if (strTemp == strKey) { strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"]; break; } } //3. 修改配置文件中的值 if (SetDeviceConfigValue(m_Configurations, strTemp.c_str(), 1, DescriptionTempEx[i])) { //mLog::Debug("SetDeviceConfigValue over"); bSaveFile = true; } } else { //mLog::Info("{$} is not a RW configuration item", strKey.c_str()); } } else { //mLog::Info("without this attribute {$}", strKey.c_str()); } } catch (ResDataObjectExption& e) { //mLog::Error("SetDriverConfig crashed: {$}", e.what()); return false; } } if (bSaveFile) { //3. 重新保存配置文件 SaveConfigFile(true); } return true; } bool nsMech::DemoDriver::SaveConfigFile(bool bSendNotify) { m_ConfigAll["CONFIGURATION"] = m_Configurations; bool bRt = m_ConfigAll.SaveFile(m_ConfigFileName.c_str()); ////mLog::Info("SaveConfigFile over {$}", bRt); return true; } bool nsMech::DemoDriver::GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue) { strValue = ""; string strTemp = pInnerKey; if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取 { int pos = 0; ResDataObject resTemp = config; while ((pos = strTemp.find_first_of(',')) != string::npos) { string Key = strTemp.substr(0, pos); string TempValue = resTemp[Key.c_str()].encode(); resTemp.clear(); resTemp.decode(TempValue.c_str()); strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1); } if (strTemp != "") { strValue = (string)resTemp[strTemp.c_str()]; } else { strValue = (string)resTemp; } } return true; } bool nsMech::DemoDriver::SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue) { string strTemp = pInnerKey; //mLog::Debug("Begin to change {$} item value to {$}", pInnerKey, szValue); if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取 { try { int pos = 0; ResDataObject* resTemp = &config; while ((pos = strTemp.find_first_of(',')) != string::npos) { string Key = strTemp.substr(0, pos); resTemp = &(*resTemp)[Key.c_str()]; strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1); } if (strTemp != "") { (*resTemp)[strTemp.c_str()] = szValue; } else { *resTemp = szValue; } } catch (ResDataObjectExption& e) { mLog::Error("SetDriverConfigvalue crashed: {$}", e.what()); return false; } } return true; } std::string nsMech::DemoDriver::GetResource() { ResDataObject r_config, temp; if (!temp.loadFile(m_ConfigFileName.c_str())) { return ""; } m_ConfigAll = temp; r_config = temp["CONFIGURATION"]; m_Configurations = r_config; ResDataObject DescriptionTemp; ResDataObject DescriptionSend; ResDataObject m_DescriptionSend; ResDataObject ListTemp; string strTemp = ""; //用于读取字符串配置信息 string strIndex = ""; //用于读取配置信息中的List项 int nTemp = -1; //用于读取整型配置信息 char sstream[10] = { 0 }; //用于转换值 string strValue = ""; //用于存储配置的值 string strType = ""; //用于存储配置的类型 int/float/string... try { //便利ConfigToolInfo 中 所有的AttributeInfo 属性段 int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo"); m_pAttribute->clear(); m_pDescription->clear(); for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++) { DescriptionTemp.clear(); DescriptionSend.clear(); ListTemp.clear(); //AttributeType strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Type"]; DescriptionTemp.add(ConfKey::DiosType, strTemp.c_str());//DiosMechanicalAttribute DescriptionSend.add(ConfKey::DiosType, strTemp.c_str());//DiosMechanicalAttribute strType = strTemp; //记录配置项的类型 //AttributeKey //1. 根据AttributeType,内部key和配置路径,拿到当前的真实值 strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"]; nTemp = (int)m_Configurations["ConfigToolInfo"][nInfoIndex]["PathID"]; GetDeviceConfigValue(r_config, strTemp.c_str(), nTemp, strValue); //得到strValue的值 //2. 赋值 strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"]; if ("int" == strType) { (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str())); } else if ("float" == strType) { (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str())); } else //其它先按string类型处理 { (*m_pAttribute).add(strTemp.c_str(), strValue.c_str()); } //AttributeAccess strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Access"]; DescriptionTemp.add(ConfKey::DiosAccess, strTemp.c_str()); DescriptionSend.add(ConfKey::DiosAccess, strTemp.c_str()); //AttributeRangeMin //strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMin"]; //if (strTemp != "") //不需要的配置项为空 //{ // DescriptionTemp.add(ConfKey::DiosRangeMin, strTemp.c_str()); //} ////AttributeRangeMax //strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMax"]; //if (strTemp != "") //不需要的配置项为空 //{ // DescriptionTemp.add(ConfKey::DiosRangeMax, strTemp.c_str()); //} //AttributeList nTemp = m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListNum"]; if (nTemp > 0) //ListNum不大于0时说明不需要list配置 { for (int nListIndex = 0; nListIndex < nTemp; nListIndex++) { strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListInfo"][nListIndex]; auto temKey = std::to_string(nListIndex); ListTemp.add(temKey.c_str(), strTemp.c_str()); } DescriptionTemp.add(ConfKey::DiosList, ListTemp); DescriptionSend.add(ConfKey::DiosList, ListTemp.encode()); } //AttributeRequired strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Required"]; DescriptionTemp.add(ConfKey::DiosRequired, strTemp.c_str()); DescriptionSend.add(ConfKey::DiosRequired, strTemp.c_str()); //AttributeDefaultValue strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["DefaultValue"]; if (strTemp != "") //不需要的配置项为空 { DescriptionTemp.add(ConfKey::DiosDefaultValue, strTemp.c_str()); DescriptionSend.add(ConfKey::DiosDefaultValue, strTemp.c_str()); } strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"]; (*m_pDescription).add(strTemp.c_str(), DescriptionTemp); m_DescriptionSend.add(strTemp.c_str(), DescriptionSend.encode()); } } catch (ResDataObjectExption& e) { mLog::Error("Get config error: {$}", e.what()); return ""; } ResDataObject resDeviceResource; resDeviceResource.add(ConfKey::DiosAttribute, (*m_pAttribute)); resDeviceResource.add(ConfKey::DiosDescription, (*m_pDescription)); ResDataObject DescriptionTempEx; DescriptionTempEx.add(ConfKey::DiosConfig, resDeviceResource); m_DeviceConfig.clear(); m_DeviceConfig = DescriptionTempEx; //mLog::Debug("local ************* get resource over {$}", DescriptionTempEx.encode()); resDeviceResource.clear(); resDeviceResource.add(ConfKey::DiosAttribute, (*m_pAttribute)); resDeviceResource.add(ConfKey::DiosDescription, m_DescriptionSend); DescriptionTempEx.clear(); DescriptionTempEx.add(ConfKey::DiosConfig, resDeviceResource); m_DeviceConfigSend.clear(); m_DeviceConfigSend = DescriptionTempEx; string res = m_DeviceConfigSend.encode(); //mLog::Debug("get resource over {$}", DescriptionTempEx.encode()); return res; } void nsMech::DemoDriver::Dequeue(const char* Packet, DWORD Length) { } PACKET_RET nsMech::DemoDriver::callbackPackageProcess(const char* RecData, DWORD nLength, DWORD& PacketLength) { return PACKET_NOPACKET; } static nsMech::DemoDriver gIODriver; extern "C" DIOS::Dev::IODriver * __cdecl GetIODriver() // 返回静态对象的引用, 调用者不能删除 ! { return &gIODriver; } extern "C" DIOS::Dev::IODriver * __cdecl CreateIODriver() // 返回新对象, 调用者必须自行删除此对象 ! { return new nsMech::DemoDriver(); }