#include "stdafx.h" #include "DIOS.Dev.Machinery.Driver.ECOM.h" #include "ConfigurerDiosBoard.h" #include "ICommunicateEntity.h" #include "ConfigurationManager.h" #include "DriverLogger.h" #include "MotionLogger.h" #include "CommunicationLogger.h" #include "BusinessLogger.h" #include "DigitalTwinLogger.h" #include "ConfigurerMotion.h" #include "IOInterfaceMapper.h" #include "LogicDeviceManager.h" #include "SCFLoader.h" #include "PacketDispatcher.h" #include "TomoDPCHandler.h" #include "CArmDPCHandler.h" #define MECHMODEL_STDS "3DDR" #define MECHMODEL_CARM "CARM" using namespace DIOS::Dev::Detail; using namespace DIOS::Dev::Detail::MachineryECOM; #ifdef _WIN64 #ifdef _DEBUG static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64D.dll"; #else static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64.dll"; #endif #endif #ifdef _WIN64 #ifdef _DEBUG static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64D.dll"; #else static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64.dll"; #endif #endif //----------------------------------------------------------------------------- // MachineryECOMDevice //----------------------------------------------------------------------------- MachineryECOMDevice::MachineryECOMDevice(std::shared_ptr center, nsSCF::SCF SCF) :super(center, SCF) { } MachineryECOMDevice::~MachineryECOMDevice() { } std::string MachineryECOMDevice::GetGUID() const { return MachineryUnitGUID; } void MachineryECOMDevice::Register() { auto Disp = Dispatch.Lock().As(); superMach::Register(Disp); } //----------------------------------------------------------------------------- // MachineryECOMDriver //----------------------------------------------------------------------------- MachineryECOMDriver::MachineryECOMDriver() :m_dpcHandler(nullptr), m_handleComm(nullptr), m_handleDispatcher(nullptr) { //CreateLogger(EventCenter); } MachineryECOMDriver::~MachineryECOMDriver() { } string MachineryECOMDriver::GetWorkPath(std::string path) { string temppath = path; size_t pos = temppath.find_last_of('\\'); return temppath.substr(0, pos); } void MachineryECOMDriver::Prepare() { printf("MachineryECOMDriver::Prepare\n"); ResDataObject r_config; if (r_config.loadFile(m_ConfigFileName.c_str())) { ResDataObject Connection = r_config["CONFIGURATION"]["connections"][0]; if ((string)Connection["type"] == "COM") m_SCFDllName = COM_SCFDllName; else m_SCFDllName = TCP_SCFDllName; } if (r_config["CONFIGURATION"]["SystemType"] == MECHMODEL_STDS) { m_SystemType = MECHMODEL_STDS; } else if (r_config["CONFIGURATION"]["SystemType"] == MECHMODEL_CARM) { m_SystemType = MECHMODEL_CARM; } //m_ConfigFileName = "C:\\Release\\DriverConfig\\MachineryECOM.xml" MachDriverPrepare(GetWorkPath(m_ConfigFileName)); super::Prepare(); } void MachineryECOMDriver::MachDriverPrepare(std::string driverconfigpath) { //driverconfigpath = "C:\\Release\\DriverConfig" //strrootpath = "C:\\Release" string strrootpath = GetWorkPath(driverconfigpath); InstallLoggers(strrootpath); //strConfigPath = "C:\\Release\\MachineryECOM\\Config\\" string strConfigPath = strrootpath + "\\MachineryECOM\\Config\\"; ConfigurationManager::Instance()->Initialize(strConfigPath); IntallPinDefineFile(strConfigPath); IDPCHandler* dpcHandler = nullptr; if (m_SystemType == MECHMODEL_STDS) { printf_s("\nDiosCtrlBoxDPC MechModelType %s\n", MECHMODEL_STDS); { if (gbusinessLog) { gbusinessLog->Info("[DiosCtrlBoxDPC MechModelType]->[{$}]", MECHMODEL_STDS); } } dpcHandler = TomoDPCHandler::Instance(); } else if (m_SystemType == MECHMODEL_CARM) { printf_s("\nDiosCtrlBoxDPC MechModelType %s\n", MECHMODEL_CARM); { if (gbusinessLog) { gbusinessLog->Info("[DiosCtrlBoxDPC MechModelType]->[{$}]", MECHMODEL_CARM); } } dpcHandler = CArmDPCHandler::Instance(); } m_dpcHandler = dpcHandler; ICommunicateEntity* commu = nullptr; IPacketDispatcher* dispatcher = nullptr; bool status = true; dpcHandler->OnDriverEntry(this, commu, dispatcher, status); m_handleComm = commu; m_handleDispatcher = dispatcher; if (m_dpcHandler) { ((IDPCHandler*)m_dpcHandler)->OnSetDriverWorkPath(driverconfigpath.c_str()); } } int MachineryECOMDriver::Connect() { printf("MachineryECOMDriver::Connect\n"); ResDataObject r_config; if (!r_config.loadFile(m_ConfigFileName.c_str())) return SCF_ERR::SCF_OPEN_FAILED; ResDataObject Connection = r_config["CONFIGURATION"]["connections"][0]; printf("connections:%s \n", Connection.encode()); std::string ioboardName = ConfigurerDiosBoard::GetCommunicateBoardName(); { if (gcommLog) { gcommLog->Info("[DiosCtrlBoxDPC][Connect]->[{$}]", Connection.encode()); } } if (m_handleComm == nullptr) { if(gcommLog) gcommLog->Info("[DiosCtrlBoxDPC][Connect]->[communicate is null]"); } if (m_handleComm) { bool bConnect = ((ICommunicateEntity*)m_handleComm)->Connect(&m_SCF, Connection, nullptr, ioboardName); if (bConnect) { auto rc = super::Connect(); if (!rc) return 0; std::string code = "1"; int level = 1; std::string info = "SCF DisConnected,please Stop Procedure!"; } else { return SCF_ERR::SCF_FAILED; } } return SCF_ERR::SCF_SUCCEED; } void MachineryECOMDriver::Disconnect() { super::Disconnect(); if (m_handleComm) { ((ICommunicateEntity*)m_handleComm)->Disonnect(); if(gcommLog) gcommLog->Warn("[DiosCtrlBoxDPC][DisConnect]->[Communication Disonnected]"); } } bool MachineryECOMDriver::isConnected() const { return super::isConnected(); } void MachineryECOMDriver::LogInfo(char* Packet, int len, char* others) { std::thread::id this_id = std::this_thread::get_id(); unsigned int t = *(unsigned int*)&this_id;// threadid 转成 unsigned int } void MachineryECOMDriver::Dequeue(const char* Packet, DWORD Length) { //DecodeFrame(Packet, Length); auto packet = SCFLoader::Instance()->CreateSingletonSCFPacket("NotifyPacket"); packet->SetPacket(Packet, Length); ((PacketDispatcher*)m_handleDispatcher)->DispatchPacket(packet); memset((void*)Packet, 0, MAX_PATH); } void MachineryECOMDriver::FireNotify(std::string key, std::string content) { FireNotify(1, key, content); } void MachineryECOMDriver::FireNotify(int code, std::string key, std::string content) { EventCenter->OnNotify(code, key, content); } auto MachineryECOMDriver::CreateDevice(int index) -> std::unique_ptr { printf("MachineryECOMDriver::CreateDevice\n"); ((IDPCHandler*)m_dpcHandler)->OnLoadLogicDevices(this); auto machdevice = new MachineryECOMDevice(EventCenter, m_SCF); machdevice->SetCollimatorUnit((CollimatorUnit*)(LogicDeviceManager::Instance()->Resove(LOGICDEVICE_MECH_COLLIMATOR))); machdevice->SetExposureSwitchUnit((ExposureSwitchUnit*)(LogicDeviceManager::Instance()->Resove(LOGICDEVICE_MECH_HSW))); if (m_SystemType == MECHMODEL_STDS) { machdevice->SetMotionControlUnit((MotionControlUnit*)(LogicDeviceManager::Instance()->Resove(LOGICDEVICE_MECH_TOMO))); } else if (m_SystemType == MECHMODEL_CARM) { machdevice->SetMotionControlUnit((MotionControlUnit*)(LogicDeviceManager::Instance()->Resove(LOGICDEVICE_MECH_CARM))); } machdevice->Register(); auto device = std::unique_ptr(new IODevice(machdevice)); if (m_dpcHandler) { ((IDPCHandler*)m_dpcHandler)->OnConnected((ICommunicateEntity*)m_handleComm); } std::thread::id this_id = std::this_thread::get_id(); unsigned int t = *(unsigned int*)&this_id;// threadid 转成 unsigned int printf("MachineryECOMDriver CreateDevice Thread ID: %d\n", t); printf_s("Communication Established.\n"); return device; } std::string MachineryECOMDriver::DriverProbe() { printf("MachineryECOMDriver::DriverProbe\n"); 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", "MachineryECOM"); HardwareInfo.add("MinorID", "DIOS"); HardwareInfo.add("VendorID", "E-COM"); HardwareInfo.add("ProductID", "Mech"); HardwareInfo.add("SerialID", "DRVOBJ"); } string ret = HardwareInfo.encode(); return ret; } std::string MachineryECOMDriver::GetResource() { printf("MachineryECOMDriver::GetResource\n"); if (m_dpcHandler) { ((IDPCHandler*)m_dpcHandler)->OnNitifyReadyStatus(this); } return std::string(); } std::string MachineryECOMDriver::DeviceProbe() { printf("MachineryECOMDriver::DeviceProbe\n"); 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", "MachineryECOM"); HardwareInfo.add("MinorID", "DIOS"); HardwareInfo.add("VendorID", "E-COM"); HardwareInfo.add("ProductID", "Mech"); HardwareInfo.add("SerialID", "DRVOBJ"); } string ret = HardwareInfo.encode(); return ret; } void MachineryECOMDriver::InstallLoggers(const std::string& workpath) { std::string logpath = workpath; if (logpath.empty()) { logpath = workpath; } if (logpath[logpath.size() - 1] != '\\') { logpath = logpath + "\\"; } std::string logMotion = logpath + "Motion"; std::string logCommun = logpath + "Communication"; std::string logLogic = logpath + "Logic"; std::string logDwt = logpath + "DigitalTwin"; MotionLogger::Instance()->Initialize("Motion"); CommunicationLogger::Instance()->Initialize("Communication"); BusinessLogger::Instance()->Initialize("Logic"); DriverLogger::Instance()->Initialize("Driver"); DigitalTwinLogger::Instance()->Initialize("DigitalTwin"); gmotionLog = MotionLogger::Instance()->Get(); gcommLog = CommunicationLogger::Instance()->Get(); gdriverLog = DriverLogger::Instance()->Get(); gbusinessLog = BusinessLogger::Instance()->Get(); gdigitalTwinLog = DigitalTwinLogger::Instance()->Get(); if(gdriverLog) gdriverLog->Info("=======================Log Begin========================="); if (gmotionLog) gmotionLog->Info("=======================Log Begin========================="); if(gcommLog) gcommLog->Info("=======================Log Begin========================="); if(gbusinessLog) gbusinessLog->Info("=======================Log Begin========================="); if (gdigitalTwinLog) gdigitalTwinLog->Info("=======================Log Begin========================="); } void MachineryECOMDriver::IntallPinDefineFile(const std::string& workpath) { auto pindefineFile = ConfigurerMotion::GetPinDefineFileName(); if (pindefineFile != "") { auto path = workpath; if (path[path.size() - 1] != '\\') { path = path + "\\"; } auto pindefineFullPath = path + pindefineFile; IOInterfaceMapper::Load(pindefineFullPath); } } bool MachineryECOMDriver::GetDeviceConfig(std::string& Cfg) { return true; } bool MachineryECOMDriver::SetDeviceConfig(std::string Cfg) { return true; } //----------------------------------------------------------------------------- // GetIODriver & CreateIODriver //----------------------------------------------------------------------------- static MachineryECOMDriver gIODriver; extern "C" DIOS::Dev::IODriver * __cdecl GetIODriver() // 返回静态对象的引用, 调用者不能删除 ! { return &gIODriver; } extern "C" DIOS::Dev::IODriver * __cdecl CreateIODriver() // 返回新对象, 调用者必须自行删除此对象 ! { return new MachineryECOMDriver(); }