DIOS.Dev.Machinery.Driver.ECOM.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #include "stdafx.h"
  2. #include "DIOS.Dev.Machinery.Driver.ECOM.h"
  3. #include "ConfigurerDiosBoard.h"
  4. #include "ICommunicateEntity.h"
  5. #include "ConfigurationManager.h"
  6. #include "DriverLogger.h"
  7. #include "MotionLogger.h"
  8. #include "CommunicationLogger.h"
  9. #include "BusinessLogger.h"
  10. #include "DigitalTwinLogger.h"
  11. #include "ConfigurerMotion.h"
  12. #include "IOInterfaceMapper.h"
  13. #include "LogicDeviceManager.h"
  14. #include "SCFLoader.h"
  15. #include "PacketDispatcher.h"
  16. #include "TomoDPCHandler.h"
  17. #include "CArmDPCHandler.h"
  18. #define MECHMODEL_STDS "3DDR"
  19. #define MECHMODEL_CARM "CARM"
  20. using namespace DIOS::Dev::Detail;
  21. using namespace DIOS::Dev::Detail::MachineryECOM;
  22. #ifdef _WIN64
  23. #ifdef _DEBUG
  24. static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64D.dll";
  25. #else
  26. static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64.dll";
  27. #endif
  28. #endif
  29. #ifdef _WIN64
  30. #ifdef _DEBUG
  31. static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64D.dll";
  32. #else
  33. static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64.dll";
  34. #endif
  35. #endif
  36. //-----------------------------------------------------------------------------
  37. // MachineryECOMDevice
  38. //-----------------------------------------------------------------------------
  39. MachineryECOMDevice::MachineryECOMDevice(std::shared_ptr <IOEventCenter> center, nsSCF::SCF SCF)
  40. :super(center, SCF)
  41. {
  42. }
  43. MachineryECOMDevice::~MachineryECOMDevice()
  44. {
  45. }
  46. std::string MachineryECOMDevice::GetGUID() const
  47. {
  48. return MachineryUnitGUID;
  49. }
  50. void MachineryECOMDevice::Register()
  51. {
  52. auto Disp = Dispatch.Lock().As();
  53. superMach::Register(Disp);
  54. }
  55. //-----------------------------------------------------------------------------
  56. // MachineryECOMDriver
  57. //-----------------------------------------------------------------------------
  58. MachineryECOMDriver::MachineryECOMDriver()
  59. :m_dpcHandler(nullptr),
  60. m_handleComm(nullptr),
  61. m_handleDispatcher(nullptr)
  62. {
  63. //CreateLogger(EventCenter);
  64. }
  65. MachineryECOMDriver::~MachineryECOMDriver()
  66. {
  67. }
  68. string MachineryECOMDriver::GetWorkPath(std::string path)
  69. {
  70. string temppath = path;
  71. size_t pos = temppath.find_last_of('\\');
  72. return temppath.substr(0, pos);
  73. }
  74. void MachineryECOMDriver::Prepare()
  75. {
  76. printf("MachineryECOMDriver::Prepare\n");
  77. ResDataObject r_config;
  78. if (r_config.loadFile(m_ConfigFileName.c_str()))
  79. {
  80. ResDataObject Connection = r_config["CONFIGURATION"]["connections"][0];
  81. if ((string)Connection["type"] == "COM")
  82. m_SCFDllName = COM_SCFDllName;
  83. else
  84. m_SCFDllName = TCP_SCFDllName;
  85. }
  86. if (r_config["CONFIGURATION"]["SystemType"] == MECHMODEL_STDS)
  87. {
  88. m_SystemType = MECHMODEL_STDS;
  89. }
  90. else if (r_config["CONFIGURATION"]["SystemType"] == MECHMODEL_CARM)
  91. {
  92. m_SystemType = MECHMODEL_CARM;
  93. }
  94. //m_ConfigFileName = "C:\\Release\\DriverConfig\\MachineryECOM.xml"
  95. MachDriverPrepare(GetWorkPath(m_ConfigFileName));
  96. super::Prepare();
  97. }
  98. void MachineryECOMDriver::MachDriverPrepare(std::string driverconfigpath)
  99. {
  100. //driverconfigpath = "C:\\Release\\DriverConfig"
  101. //strrootpath = "C:\\Release"
  102. string strrootpath = GetWorkPath(driverconfigpath);
  103. InstallLoggers(strrootpath);
  104. //strConfigPath = "C:\\Release\\MachineryECOM\\Config\\"
  105. string strConfigPath = strrootpath + "\\MachineryECOM\\Config\\";
  106. ConfigurationManager::Instance()->Initialize(strConfigPath);
  107. IntallPinDefineFile(strConfigPath);
  108. IDPCHandler* dpcHandler = nullptr;
  109. if (m_SystemType == MECHMODEL_STDS)
  110. {
  111. printf_s("\nDiosCtrlBoxDPC MechModelType %s\n", MECHMODEL_STDS);
  112. { if (gbusinessLog) { gbusinessLog->Info("[DiosCtrlBoxDPC MechModelType]->[{$}]", MECHMODEL_STDS); } }
  113. dpcHandler = TomoDPCHandler::Instance();
  114. }
  115. else if (m_SystemType == MECHMODEL_CARM)
  116. {
  117. printf_s("\nDiosCtrlBoxDPC MechModelType %s\n", MECHMODEL_CARM);
  118. { if (gbusinessLog) { gbusinessLog->Info("[DiosCtrlBoxDPC MechModelType]->[{$}]", MECHMODEL_CARM); } }
  119. dpcHandler = CArmDPCHandler::Instance();
  120. }
  121. m_dpcHandler = dpcHandler;
  122. ICommunicateEntity* commu = nullptr;
  123. IPacketDispatcher* dispatcher = nullptr;
  124. bool status = true;
  125. dpcHandler->OnDriverEntry(this, commu, dispatcher, status);
  126. m_handleComm = commu;
  127. m_handleDispatcher = dispatcher;
  128. if (m_dpcHandler)
  129. {
  130. ((IDPCHandler*)m_dpcHandler)->OnSetDriverWorkPath(driverconfigpath.c_str());
  131. }
  132. }
  133. int MachineryECOMDriver::Connect()
  134. {
  135. printf("MachineryECOMDriver::Connect\n");
  136. ResDataObject r_config;
  137. if (!r_config.loadFile(m_ConfigFileName.c_str()))
  138. return SCF_ERR::SCF_OPEN_FAILED;
  139. ResDataObject Connection = r_config["CONFIGURATION"]["connections"][0];
  140. printf("connections:%s \n", Connection.encode());
  141. std::string ioboardName = ConfigurerDiosBoard::GetCommunicateBoardName();
  142. { if (gcommLog) { gcommLog->Info("[DiosCtrlBoxDPC][Connect]->[{$}]", Connection.encode()); } }
  143. if (m_handleComm == nullptr)
  144. {
  145. if(gcommLog) gcommLog->Info("[DiosCtrlBoxDPC][Connect]->[communicate is null]");
  146. }
  147. if (m_handleComm)
  148. {
  149. bool bConnect = ((ICommunicateEntity*)m_handleComm)->Connect(&m_SCF, Connection, nullptr, ioboardName);
  150. if (bConnect)
  151. {
  152. auto rc = super::Connect();
  153. if (!rc)
  154. return 0;
  155. std::string code = "1";
  156. int level = 1;
  157. std::string info = "SCF DisConnected,please Stop Procedure!";
  158. }
  159. else
  160. {
  161. return SCF_ERR::SCF_FAILED;
  162. }
  163. }
  164. return SCF_ERR::SCF_SUCCEED;
  165. }
  166. void MachineryECOMDriver::Disconnect()
  167. {
  168. super::Disconnect();
  169. if (m_handleComm)
  170. {
  171. ((ICommunicateEntity*)m_handleComm)->Disonnect();
  172. if(gcommLog) gcommLog->Warn("[DiosCtrlBoxDPC][DisConnect]->[Communication Disonnected]");
  173. }
  174. }
  175. bool MachineryECOMDriver::isConnected() const
  176. {
  177. return super::isConnected();
  178. }
  179. void MachineryECOMDriver::LogInfo(char* Packet, int len, char* others)
  180. {
  181. std::thread::id this_id = std::this_thread::get_id();
  182. unsigned int t = *(unsigned int*)&this_id;// threadid 转成 unsigned int
  183. }
  184. void MachineryECOMDriver::Dequeue(const char* Packet, DWORD Length)
  185. {
  186. //DecodeFrame(Packet, Length);
  187. auto packet = SCFLoader::Instance()->CreateSingletonSCFPacket("NotifyPacket");
  188. packet->SetPacket(Packet, Length);
  189. ((PacketDispatcher*)m_handleDispatcher)->DispatchPacket(packet);
  190. memset((void*)Packet, 0, MAX_PATH);
  191. }
  192. void MachineryECOMDriver::FireNotify(std::string key, std::string content)
  193. {
  194. FireNotify(1, key, content);
  195. }
  196. void MachineryECOMDriver::FireNotify(int code, std::string key, std::string content)
  197. {
  198. EventCenter->OnNotify(code, key, content);
  199. }
  200. auto MachineryECOMDriver::CreateDevice(int index) -> std::unique_ptr<IODevice>
  201. {
  202. printf("MachineryECOMDriver::CreateDevice\n");
  203. ((IDPCHandler*)m_dpcHandler)->OnLoadLogicDevices(this);
  204. auto machdevice = new MachineryECOMDevice(EventCenter, m_SCF);
  205. machdevice->SetCollimatorUnit((CollimatorUnit*)(LogicDeviceManager::Instance()->Resove(LOGICDEVICE_MECH_COLLIMATOR)));
  206. machdevice->SetExposureSwitchUnit((ExposureSwitchUnit*)(LogicDeviceManager::Instance()->Resove(LOGICDEVICE_MECH_HSW)));
  207. if (m_SystemType == MECHMODEL_STDS)
  208. {
  209. machdevice->SetMotionControlUnit((MotionControlUnit*)(LogicDeviceManager::Instance()->Resove(LOGICDEVICE_MECH_TOMO)));
  210. }
  211. else if (m_SystemType == MECHMODEL_CARM)
  212. {
  213. machdevice->SetMotionControlUnit((MotionControlUnit*)(LogicDeviceManager::Instance()->Resove(LOGICDEVICE_MECH_CARM)));
  214. }
  215. machdevice->Register();
  216. auto device = std::unique_ptr<IODevice>(new IODevice(machdevice));
  217. if (m_dpcHandler)
  218. {
  219. ((IDPCHandler*)m_dpcHandler)->OnConnected((ICommunicateEntity*)m_handleComm);
  220. }
  221. std::thread::id this_id = std::this_thread::get_id();
  222. unsigned int t = *(unsigned int*)&this_id;// threadid 转成 unsigned int
  223. printf("MachineryECOMDriver CreateDevice Thread ID: %d\n", t);
  224. printf_s("Communication Established.\n");
  225. return device;
  226. }
  227. std::string MachineryECOMDriver::DriverProbe()
  228. {
  229. printf("MachineryECOMDriver::DriverProbe\n");
  230. ResDataObject r_config, HardwareInfo;
  231. if (r_config.loadFile(m_ConfigFileName.c_str()))
  232. {
  233. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  234. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  235. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  236. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  237. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  238. }
  239. else
  240. {
  241. HardwareInfo.add("MajorID", "MachineryECOM");
  242. HardwareInfo.add("MinorID", "DIOS");
  243. HardwareInfo.add("VendorID", "E-COM");
  244. HardwareInfo.add("ProductID", "Mech");
  245. HardwareInfo.add("SerialID", "DRVOBJ");
  246. }
  247. string ret = HardwareInfo.encode();
  248. return ret;
  249. }
  250. std::string MachineryECOMDriver::GetResource()
  251. {
  252. printf("MachineryECOMDriver::GetResource\n");
  253. if (m_dpcHandler)
  254. {
  255. ((IDPCHandler*)m_dpcHandler)->OnNitifyReadyStatus(this);
  256. }
  257. return std::string();
  258. }
  259. std::string MachineryECOMDriver::DeviceProbe()
  260. {
  261. printf("MachineryECOMDriver::DeviceProbe\n");
  262. ResDataObject r_config, HardwareInfo;
  263. if (r_config.loadFile(m_ConfigFileName.c_str()))
  264. {
  265. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  266. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  267. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  268. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  269. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  270. }
  271. else
  272. {
  273. HardwareInfo.add("MajorID", "MachineryECOM");
  274. HardwareInfo.add("MinorID", "DIOS");
  275. HardwareInfo.add("VendorID", "E-COM");
  276. HardwareInfo.add("ProductID", "Mech");
  277. HardwareInfo.add("SerialID", "DRVOBJ");
  278. }
  279. string ret = HardwareInfo.encode();
  280. return ret;
  281. }
  282. void MachineryECOMDriver::InstallLoggers(const std::string& workpath)
  283. {
  284. std::string logpath = workpath;
  285. if (logpath.empty())
  286. {
  287. logpath = workpath;
  288. }
  289. if (logpath[logpath.size() - 1] != '\\')
  290. {
  291. logpath = logpath + "\\";
  292. }
  293. std::string logMotion = logpath + "Motion";
  294. std::string logCommun = logpath + "Communication";
  295. std::string logLogic = logpath + "Logic";
  296. std::string logDwt = logpath + "DigitalTwin";
  297. MotionLogger::Instance()->Initialize("Motion");
  298. CommunicationLogger::Instance()->Initialize("Communication");
  299. BusinessLogger::Instance()->Initialize("Logic");
  300. DriverLogger::Instance()->Initialize("Driver");
  301. DigitalTwinLogger::Instance()->Initialize("DigitalTwin");
  302. gmotionLog = MotionLogger::Instance()->Get();
  303. gcommLog = CommunicationLogger::Instance()->Get();
  304. gdriverLog = DriverLogger::Instance()->Get();
  305. gbusinessLog = BusinessLogger::Instance()->Get();
  306. gdigitalTwinLog = DigitalTwinLogger::Instance()->Get();
  307. if(gdriverLog) gdriverLog->Info("=======================Log Begin=========================");
  308. if (gmotionLog) gmotionLog->Info("=======================Log Begin=========================");
  309. if(gcommLog) gcommLog->Info("=======================Log Begin=========================");
  310. if(gbusinessLog) gbusinessLog->Info("=======================Log Begin=========================");
  311. if (gdigitalTwinLog) gdigitalTwinLog->Info("=======================Log Begin=========================");
  312. }
  313. void MachineryECOMDriver::IntallPinDefineFile(const std::string& workpath)
  314. {
  315. auto pindefineFile = ConfigurerMotion::GetPinDefineFileName();
  316. if (pindefineFile != "")
  317. {
  318. auto path = workpath;
  319. if (path[path.size() - 1] != '\\')
  320. {
  321. path = path + "\\";
  322. }
  323. auto pindefineFullPath = path + pindefineFile;
  324. IOInterfaceMapper::Load(pindefineFullPath);
  325. }
  326. }
  327. bool MachineryECOMDriver::GetDeviceConfig(std::string& Cfg)
  328. {
  329. return true;
  330. }
  331. bool MachineryECOMDriver::SetDeviceConfig(std::string Cfg)
  332. {
  333. return true;
  334. }
  335. //-----------------------------------------------------------------------------
  336. // GetIODriver & CreateIODriver
  337. //-----------------------------------------------------------------------------
  338. static MachineryECOMDriver gIODriver;
  339. extern "C" DIOS::Dev::IODriver * __cdecl GetIODriver() // 返回静态对象的引用, 调用者不能删除 !
  340. {
  341. return &gIODriver;
  342. }
  343. extern "C" DIOS::Dev::IODriver * __cdecl CreateIODriver() // 返回新对象, 调用者必须自行删除此对象 !
  344. {
  345. return new MachineryECOMDriver();
  346. }