123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- #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 <IOEventCenter> 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<IODevice>
- {
- 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<IODevice>(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();
- }
|