DIOS.Dev.SyncBoxe.2000ST.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. #include "stdafx.h"
  2. #include "FileVersion.hpp"
  3. #include "DIOS.Dev.SyncBoxe.2000ST.h"
  4. #include "Helper.JSON.hpp"
  5. #include "CommonFun.h"
  6. #include "common_api.h"
  7. using namespace DIOS::Dev::Detail::SYNBOX;
  8. namespace nsSYN = DIOS::Dev::Detail::SYNBOX;
  9. #pragma warning (disable:4244) // warning C4244: “初始化”: 从“double”转换到“float”,可能丢失数据
  10. #pragma warning (disable:4305) // warning C4305: “参数”: 从“double”到“float”截断
  11. #pragma warning (disable:4267) // warning C4267 : “初始化”: 从“size_t”转换到“int”,可能丢失数据
  12. using namespace DIOS::Dev::Detail::SYNBOX;
  13. namespace nsSYN = DIOS::Dev::Detail::SYNBOX;
  14. Log4CPP::Logger* mLog::gLogger = nullptr;
  15. ////-----------------------------------------------------------------------------
  16. //// DynBoxDevice
  17. ////-----------------------------------------------------------------------------
  18. nsSYN::SyncBoxDevice::SyncBoxDevice(std::shared_ptr <IOEventCenter> center, nsSCF::SCF SCF, string configfile) : super(center, SCF)
  19. {
  20. assert(EventCenter);
  21. m_DynBoxDevice.reset(new DynBoxDevice(center, SCF, configfile));
  22. m_MSGUnit.reset(new nsDetail::MSGUnit(center, SyncConsoleUnitType));
  23. m_CollimatorDevice.reset(new CollimatorDevice(center, SCF));
  24. Register();
  25. OnCallBack();
  26. }
  27. nsSYN::SyncBoxDevice::~SyncBoxDevice()
  28. {
  29. }
  30. std::string nsSYN::SyncBoxDevice::GetGUID() const
  31. {
  32. mLog::FINFO("\n===============GetGUID : {$} ===================\n", SyncConsoleUnitType);
  33. return SyncConsoleUnitType;
  34. }
  35. void nsSYN::SyncBoxDevice::Register()
  36. {
  37. auto Disp = &Dispatch;
  38. m_DynBoxDevice->Register(Disp);
  39. m_CollimatorDevice->Register(Disp);
  40. Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; });
  41. }
  42. void nsSYN::SyncBoxDevice::OnCallBack()
  43. {
  44. mLog::FINFO("SyncBoxDevice::OnCallBack in");
  45. auto HWNotProcess = [](const char* value, int length) -> void
  46. {
  47. mLog::FERROR("\n This commands didn't need to process!\n");
  48. };
  49. auto HWSignal = [this](const char* value, int length) -> void
  50. {
  51. assert(value);
  52. char cChannelValue;
  53. cChannelValue = value[2]; //status : on /off
  54. string strChannel = ((string)value).substr(0, 2);
  55. bool bStatus = ChartoInt(cChannelValue);
  56. mLog::FINFO("HWSignal:{$}",value);
  57. m_DynBoxDevice->DealtheSignal(strChannel.c_str(), bStatus);
  58. };
  59. auto HWCAN = [this](const char* value, int length)
  60. {
  61. char data_can[MAX_COMMAND_LEN] = { 0 };
  62. for (int i = 0; i < length; i++)
  63. {
  64. if (value[i] == 0x30)
  65. data_can[i] = 0x0;
  66. else if (value[i] == 0x61)
  67. data_can[i] = 0x0a;
  68. else if (value[i] == 0x64)
  69. data_can[i] = 0x0d;
  70. else
  71. data_can[i] = value[i];
  72. }
  73. string strcmd = data_can;
  74. string sCmdID = strcmd.substr(2, 2);
  75. //DealReceiveData(sCmdID, &data_can[4], length - 4);
  76. };
  77. // 有部分前缀是包含关系, 长的包含短的, 例如 KVS 包含了 KV.
  78. // 因此长的在前面, 短的在后面
  79. // !!! Device 是个短寿命对象, 而 arFrame 是静态变量 !!!
  80. // !!! 因此, 在添加到 arFrame 之前, 务必先清零 !!!
  81. arFrame.clear();
  82. arFrame.push_back(tFrameMapping("GR", 2, HWCAN));
  83. arFrame.push_back(tFrameMapping("CR", 2, HWCAN));
  84. arFrame.push_back(tFrameMapping("TB", 2, HWSignal));
  85. arFrame.push_back(tFrameMapping("WB", 2, HWSignal));
  86. arFrame.push_back(tFrameMapping("GT", 2, HWSignal));
  87. arFrame.push_back(tFrameMapping("GW", 2, HWSignal));
  88. arFrame.push_back(tFrameMapping("PT", 2, HWSignal));
  89. arFrame.push_back(tFrameMapping("PW", 2, HWSignal));
  90. arFrame.push_back(tFrameMapping("ER", 2, HWSignal));
  91. arFrame.push_back(tFrameMapping("RT", 2, HWSignal));
  92. arFrame.push_back(tFrameMapping("RW", 2, HWSignal));
  93. arFrame.push_back(tFrameMapping("OT", 2, HWSignal));
  94. arFrame.push_back(tFrameMapping("OW", 2, HWSignal));
  95. arFrame.push_back(tFrameMapping("PR", 2, HWSignal));
  96. arFrame.push_back(tFrameMapping("FT", 2, HWSignal));
  97. arFrame.push_back(tFrameMapping("FW", 2, HWSignal));
  98. arFrame.push_back(tFrameMapping("TT", 2, HWSignal));
  99. arFrame.push_back(tFrameMapping("TW", 2, HWSignal));
  100. }
  101. //-----------------------------------------------------------------------------
  102. // DynBoxDriver
  103. //-----------------------------------------------------------------------------
  104. nsSYN::DynBoxDriver::DynBoxDriver ()
  105. {
  106. m_bDemoConnected = false;
  107. m_pAttribute.reset(new ResDataObject());
  108. m_pDescription.reset(new ResDataObject());
  109. #ifdef _WIN64
  110. g_strAppPath = GetProcessDirectory() + R"(OEMDrivers\SyncBox\2000T\DIOS.Dev.SyncBoxe.2000ST64.dll)";
  111. #else
  112. g_strAppPath = GetProcessDirectory() + R"(OEMDrivers\SyncBox\2000T\DIOS.Dev.SyncBoxe.2000ST.dll)";
  113. #endif
  114. }
  115. nsSYN::DynBoxDriver::~DynBoxDriver ()
  116. {
  117. }
  118. auto nsSYN::DynBoxDriver::CreateDevice (int index) -> std::unique_ptr <IODevice>
  119. {
  120. mLog::FINFO("Enter CreateDevice()");
  121. if (!m_SCF.isConnected())
  122. {
  123. mLog::FERROR("CreateDevice but scf no connect");
  124. return nullptr;
  125. }
  126. if (index == 0)
  127. {
  128. m_p2000tDevice = new SyncBoxDevice(EventCenter, m_SCF, m_ConfigFileName);
  129. auto dev = std::unique_ptr <IODevice>(new IODevice(m_p2000tDevice));
  130. return dev;
  131. }
  132. return nullptr;
  133. }
  134. void nsSYN::DynBoxDriver::FireNotify (int code, std::string key, std::string content)
  135. {
  136. EventCenter->OnNotify (code, key, content);
  137. }
  138. void nsSYN::DynBoxDriver::Prepare ()
  139. {
  140. string strLogPath = GetProcessDirectory() + R"(\OEMDrivers\SyncBox\Conf\Log4CPP.Config.SYN.xml)";
  141. Log4CPP::GlobalContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "SYN.2000T");
  142. auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str());
  143. if (!rc)
  144. {
  145. printf("\n Load log configfile failed!\n");
  146. }
  147. mLog::gLogger = Log4CPP::LogManager::GetLogger("SYN.2000T");
  148. m_SCFDllName = GetConnectDLL(m_ConfigFileName);
  149. super::Prepare ();
  150. }
  151. bool nsSYN::DynBoxDriver::Connect ()
  152. {
  153. ResDataObject Connection = GetConnectParam(m_ConfigFileName);
  154. mLog::FINFO("connections:{$}, ConfigFileName: {$}", Connection.encode(), m_ConfigFileName);
  155. auto erCode = m_SCF.Connect(Connection.encode(), &nsSYN::DynBoxDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000);
  156. mLog::FINFO("scf connect code {$}", erCode);
  157. if (erCode != SCF_ERR::SCF_SUCCEED)
  158. {
  159. mLog::FINFO("connect scf failed");
  160. return false;
  161. }
  162. mLog::FINFO("Connect: make success");
  163. CanShakeHand();
  164. //Sleep(1000);
  165. //SetCANState(true);//open can
  166. //Sleep(1000);
  167. ReadVersion();
  168. //Sleep(200);
  169. auto rc = super::Connect();
  170. if (!rc)
  171. return false;
  172. mLog::FINFO("connect scf ok");
  173. return true;
  174. }
  175. void nsSYN::DynBoxDriver::Disconnect()
  176. {
  177. super::Disconnect();
  178. m_SCF.Disconnect();
  179. m_bDemoConnected = false;
  180. }
  181. bool nsSYN::DynBoxDriver::isConnected() const
  182. {
  183. if (super::isConnected())
  184. {
  185. return true;
  186. }
  187. else
  188. {
  189. return false;
  190. }
  191. }
  192. std::string nsSYN::DynBoxDriver::DriverProbe ()
  193. {
  194. ResDataObject r_config, HardwareInfo;
  195. if (r_config.loadFile (m_ConfigFileName.c_str ()))
  196. {
  197. HardwareInfo.add ("MajorID", r_config ["CONFIGURATION"] ["MajorID"]);
  198. HardwareInfo.add ("MinorID", r_config ["CONFIGURATION"] ["MinorID"]);
  199. HardwareInfo.add ("VendorID", r_config ["CONFIGURATION"] ["VendorID"]);
  200. HardwareInfo.add ("ProductID", r_config ["CONFIGURATION"] ["ProductID"]);
  201. HardwareInfo.add ("SerialID", r_config ["CONFIGURATION"] ["SerialID"]);
  202. }
  203. else
  204. {
  205. HardwareInfo.add("MajorID", "SYN");
  206. HardwareInfo.add("MinorID", "Dr");
  207. HardwareInfo.add("VendorID", "ECOM");
  208. HardwareInfo.add("ProductID", "2000T");
  209. HardwareInfo.add("SerialID", "1234");
  210. }
  211. string ret = HardwareInfo.encode ();
  212. return ret;
  213. }
  214. bool nsSYN::DynBoxDriver::GetDeviceConfig(std::string& Cfg)
  215. {
  216. Cfg = m_DeviceConfig.encode();
  217. printf("GetDeviceConfig over : %s \n", Cfg.c_str());
  218. return true;
  219. }
  220. bool nsSYN::DynBoxDriver::SetDeviceConfig(std::string Cfg)
  221. {
  222. mLog::FINFO("--Func-- SetDeviceConfig {$}\n", Cfg.c_str());
  223. ResDataObject DeviceConfig;
  224. DeviceConfig.decode(Cfg.c_str());
  225. ResDataObject DescriptionTempEx;
  226. DescriptionTempEx = DeviceConfig["DeviceConfig"]["Attribute"];
  227. mLog::FDEBUG("Attribute:{$}", DescriptionTempEx.encode());
  228. bool bSaveFile = false; //true:重新保存配置文件
  229. string strAccess = "";
  230. for (int i = 0; i < DescriptionTempEx.size(); i++)
  231. {
  232. string strKey = DescriptionTempEx.GetKey(i);
  233. mLog::FINFO("{$}", strKey.c_str());
  234. try
  235. {
  236. if (m_pAttribute->GetFirstOf(strKey.c_str()) >= 0)
  237. {
  238. strAccess = (string)(*m_pDescription)[strKey.c_str()]["Access"];
  239. if ("RW" == strAccess)
  240. {
  241. //修改对应配置,在其他单元的配置项要同时调用其修改函数修改真实值
  242. //1. 修改内存中的值,用于给上层发消息
  243. (*m_pAttribute)[strKey.c_str()] = DescriptionTempEx[i];
  244. //2. 拿到Innerkey
  245. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  246. mLog::FINFO("nConfigInfoCount {$}", nConfigInfoCount);
  247. string strTemp = ""; //存储AttributeKey
  248. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  249. {
  250. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  251. if (strTemp == strKey)
  252. {
  253. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  254. break;
  255. }
  256. }
  257. //3. 修改配置文件中的值
  258. if (SetDeviceConfigValue(m_Configurations, strTemp.c_str(), 1, DescriptionTempEx[i]))
  259. {
  260. mLog::FDEBUG("SetDeviceConfigValue over");
  261. bSaveFile = true;
  262. }
  263. }
  264. else
  265. {
  266. mLog::FINFO("{$} is not a RW configuration item", strKey.c_str());
  267. }
  268. }
  269. else
  270. {
  271. mLog::FINFO("without this attribute {$}", strKey.c_str());
  272. }
  273. }
  274. catch (ResDataObjectExption& e)
  275. {
  276. mLog::FERROR("SetDriverConfig crashed: {$}", e.what());
  277. return false;
  278. }
  279. }
  280. if (bSaveFile)
  281. {
  282. //3. 重新保存配置文件
  283. SaveConfigFile(true);
  284. }
  285. return true;
  286. }
  287. bool nsSYN::DynBoxDriver::SaveConfigFile(bool bSendNotify)
  288. {
  289. m_ConfigAll["CONFIGURATION"] = m_Configurations;
  290. bool bRt = m_ConfigAll.SaveFile(m_ConfigFileName.c_str());
  291. mLog::FINFO("SaveConfigFile over {$}", bRt);
  292. return true;
  293. }
  294. bool nsSYN::DynBoxDriver::GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue)
  295. {
  296. strValue = "";
  297. string strTemp = pInnerKey;
  298. if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取
  299. {
  300. int pos = 0;
  301. ResDataObject resTemp = config;
  302. while ((pos = strTemp.find_first_of(',')) != string::npos)
  303. {
  304. string Key = strTemp.substr(0, pos);
  305. string TempValue = resTemp[Key.c_str()].encode();
  306. resTemp.clear();
  307. resTemp.decode(TempValue.c_str());
  308. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  309. }
  310. if (strTemp != "")
  311. {
  312. strValue = (string)resTemp[strTemp.c_str()];
  313. }
  314. else
  315. {
  316. strValue = (string)resTemp;
  317. }
  318. }
  319. return true;
  320. }
  321. bool nsSYN::DynBoxDriver::SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue)
  322. {
  323. string strTemp = pInnerKey;
  324. mLog::FDEBUG("Begin to change {$} item value to {$}", pInnerKey, szValue);
  325. if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取
  326. {
  327. try {
  328. int pos = 0;
  329. ResDataObject* resTemp = &config;
  330. while ((pos = strTemp.find_first_of(',')) != string::npos)
  331. {
  332. string Key = strTemp.substr(0, pos);
  333. resTemp = &(*resTemp)[Key.c_str()];
  334. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  335. }
  336. if (strTemp != "")
  337. {
  338. (*resTemp)[strTemp.c_str()] = szValue;
  339. }
  340. else
  341. {
  342. *resTemp = szValue;
  343. }
  344. }
  345. catch (ResDataObjectExption& e)
  346. {
  347. mLog::FERROR("SetDriverConfigvalue crashed: {$}", e.what());
  348. return false;
  349. }
  350. }
  351. return true;
  352. }
  353. std::string nsSYN::DynBoxDriver::GetResource()
  354. {
  355. mLog::FINFO("Enter GetResource()");
  356. ResDataObject r_config, temp;
  357. if (!temp.loadFile(m_ConfigFileName.c_str()))
  358. {
  359. return "";
  360. }
  361. m_ConfigAll = temp;
  362. r_config = temp["CONFIGURATION"];
  363. m_Configurations = r_config;
  364. ResDataObject DescriptionTemp;
  365. ResDataObject ListTemp;
  366. string strTemp = ""; //用于读取字符串配置信息
  367. string strIndex = ""; //用于读取配置信息中的List项
  368. int nTemp = -1; //用于读取整型配置信息
  369. char sstream[10] = { 0 }; //用于转换值
  370. string strValue = ""; //用于存储配置的值
  371. string strType = ""; //用于存储配置的类型 int/float/string...
  372. /***
  373. * 1. 通过循环,将所有配置项写到pDeviceConfig
  374. * 2. 记录配置项的内部key以及配置类型,类型对应了不同配置文件路径,用于读写真实值
  375. ***/
  376. try
  377. {
  378. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  379. //mLog::FINFO(g_pFPDCtrlLog, "ConfigInfo Count: {$}", nConfigInfoCount);
  380. m_pAttribute->clear();
  381. m_pDescription->clear();
  382. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  383. {
  384. DescriptionTemp.clear();
  385. ListTemp.clear();
  386. //AttributeType
  387. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Type"];
  388. DescriptionTemp.add(ConfKey::AttributeType, strTemp.c_str());
  389. //DescriptionTemp= m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"];
  390. //mLog::FINFO(g_pFPDCtrlLog, "--> {$}: {$}", AttributeType, strTemp.c_str());
  391. strType = strTemp; //记录配置项的类型
  392. //AttributeKey
  393. //1. 根据AttributeType,内部key和配置路径,拿到当前的真实值
  394. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  395. nTemp = (int)m_Configurations["ConfigToolInfo"][nInfoIndex]["PathID"];
  396. GetDeviceConfigValue(r_config, strTemp.c_str(), nTemp, strValue);
  397. //2. 赋值
  398. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  399. if ("int" == strType)
  400. {
  401. (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str()));
  402. //mLog::FINFO(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), atoi(strValue.c_str()));
  403. }
  404. else if ("float" == strType)
  405. {
  406. (*m_pAttribute).add(strTemp.c_str(), atof(strValue.c_str()));
  407. //mLog::FINFO(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), atof(strValue.c_str()));
  408. }
  409. else //其它先按string类型处理
  410. {
  411. (*m_pAttribute).add(strTemp.c_str(), strValue.c_str());
  412. //mLog::FINFO(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), strValue.c_str());
  413. }
  414. //AttributeAccess
  415. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Access"];
  416. DescriptionTemp.add(ConfKey::AttributeAccess, strTemp.c_str());
  417. //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeAccess, strTemp.c_str());
  418. //AttributeRangeMin
  419. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMin"];
  420. if (strTemp != "") //不需要的配置项为空
  421. {
  422. DescriptionTemp.add(ConfKey::AttributeRangeMin, strTemp.c_str());
  423. //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeRangeMin, strTemp.c_str());
  424. }
  425. //AttributeRangeMax
  426. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMax"];
  427. if (strTemp != "") //不需要的配置项为空
  428. {
  429. DescriptionTemp.add(ConfKey::AttributeRangeMax, strTemp.c_str());
  430. //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeRangeMax, strTemp.c_str());
  431. }
  432. //AttributeList
  433. nTemp = m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListNum"];
  434. if (nTemp > 0) //ListNum不大于0时说明不需要list配置
  435. {
  436. for (int nListIndex = 0; nListIndex < nTemp; nListIndex++)
  437. {
  438. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListInfo"][nListIndex];
  439. //sprintf_s(sstream, "{$}", nListIndex);
  440. auto temKey = std::to_string(nListIndex);
  441. ListTemp.add(temKey.c_str(), strTemp.c_str());
  442. //mLog::FINFO(g_pFPDCtrlLog, "list {$}: {$}", nListIndex, strTemp.c_str());
  443. }
  444. DescriptionTemp.add(ConfKey::AttributeList, ListTemp.encode());
  445. }
  446. //AttributeRequired
  447. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Required"];
  448. DescriptionTemp.add(ConfKey::AttributeRequired, strTemp.c_str());
  449. //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeRequired, strTemp.c_str());
  450. //AttributeDefaultValue
  451. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["DefaultValue"];
  452. if (strTemp != "") //不需要的配置项为空
  453. {
  454. DescriptionTemp.add(ConfKey::AttributeDefaultValue, strTemp.c_str());
  455. //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeDefaultValue, strTemp.c_str());
  456. }
  457. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  458. (*m_pDescription).add(strTemp.c_str(), DescriptionTemp);
  459. }
  460. }
  461. catch (ResDataObjectExption& e)
  462. {
  463. mLog::FERROR("Get config error: {$}", e.what());
  464. return "";
  465. }
  466. ResDataObject resDeviceResource;
  467. resDeviceResource.add(ConfKey::DiosAttribute, (*m_pAttribute));
  468. resDeviceResource.add(ConfKey::DiosDescription, (*m_pDescription));
  469. ResDataObject DescriptionTempEx;
  470. DescriptionTempEx.add(ConfKey::DiosConfig, resDeviceResource);
  471. m_DeviceConfig.clear();
  472. m_DeviceConfig = DescriptionTempEx;
  473. string res = DescriptionTempEx.encode();
  474. mLog::FDEBUG("get resource over {$}", DescriptionTempEx.encode());
  475. printf("get resource over : %s \n", DescriptionTempEx.encode());
  476. return res;
  477. }
  478. std::string nsSYN::DynBoxDriver::DeviceProbe ()
  479. {
  480. ResDataObject r_config, HardwareInfo;
  481. if (r_config.loadFile (m_ConfigFileName.c_str ()))
  482. {
  483. HardwareInfo.add ("MajorID", r_config ["CONFIGURATION"] ["MajorID"]);
  484. HardwareInfo.add ("MinorID", r_config ["CONFIGURATION"] ["MinorID"]);
  485. HardwareInfo.add ("VendorID", r_config ["CONFIGURATION"] ["VendorID"]);
  486. HardwareInfo.add ("ProductID", r_config ["CONFIGURATION"] ["ProductID"]);
  487. HardwareInfo.add ("SerialID", r_config ["CONFIGURATION"] ["SerialID"]);
  488. }
  489. else
  490. {
  491. HardwareInfo.add ("MajorID", "SYN");
  492. HardwareInfo.add ("MinorID", "Dr");
  493. HardwareInfo.add ("VendorID", "ECOM");
  494. HardwareInfo.add ("ProductID", "2000T");
  495. HardwareInfo.add ("SerialID", "1234");
  496. }
  497. string ret = HardwareInfo.encode ();
  498. return ret;
  499. }
  500. void nsSYN::DynBoxDriver::Dequeue (const char * Packet, DWORD Length)
  501. {
  502. DecodeFrame(Packet, Length);
  503. }
  504. PACKET_RET nsSYN::DynBoxDriver::callbackPackageProcess (const char * RecData, DWORD nLength, DWORD& PacketLength)
  505. {
  506. if (nLength < 1)
  507. {
  508. mLog::FERROR("nLength < 1, nLength=={$} \n", nLength);
  509. return PACKET_USELESS;
  510. }
  511. for (DWORD i = 0; i < nLength - 1; i++)
  512. {
  513. if (RecData [i] == 0x0d && RecData [i + 1] == 0x0a)
  514. {
  515. PacketLength = i + 2;
  516. char strtemp [100] = { 0 };
  517. memcpy (strtemp, RecData, i);
  518. strtemp [PacketLength + 1] = 0;
  519. mLog::FINFO("2000T ==IN ==:{$}\n", strtemp);
  520. return PACKET_ISPACKET;
  521. }
  522. else if (RecData [i] == 0x03)
  523. {
  524. mLog::FERROR("(Hex)0x03 ,len={$} \n", i);
  525. PacketLength = i + 1;
  526. return PACKET_USELESS;
  527. }
  528. }
  529. return PACKET_NOPACKET;
  530. }
  531. //-----------------------------------------------------------------------------
  532. //
  533. //-----------------------------------------------------------------------------
  534. void nsSYN::DynBoxDriver::CanShakeHand()
  535. {
  536. int nTimeout = 100;
  537. char cCmd[MAX_COMMAND_LEN] = "EC";// "SECA00";
  538. int nCmdSize = 2;// 6;
  539. FormatCmd(cCmd, nCmdSize);
  540. string strLog;
  541. CmdtoString(cCmd, nCmdSize, strLog);
  542. mLog::FINFO( "[Send:{$}]", strLog.c_str());
  543. mLog::FINFO("CanShakeHand ==OUT==: {$} ", cCmd);
  544. int ret = 0;
  545. m_SCF.Lock(msTimeOut_Lock)
  546. .SendPacket(cCmd, nCmdSize, nTimeout, ret);
  547. Sleep(nTimeout);
  548. }
  549. void nsSYN::DynBoxDriver::ReadVersion()
  550. {
  551. int nTimeout = 100;
  552. char cCmd[MAX_COMMAND_LEN] = "VR?";// "SECA00";
  553. int nCmdSize = 3;// 6;
  554. FormatCmd(cCmd, nCmdSize);
  555. string strLog;
  556. CmdtoString(cCmd, nCmdSize, strLog);
  557. mLog::FINFO("[Send:{$}]", strLog.c_str());
  558. mLog::FINFO("CanShakeHand ==OUT==: {$} ", cCmd);
  559. int ret = 0;
  560. m_SCF.Lock(msTimeOut_Lock)
  561. .SendPacket(cCmd, nCmdSize, nTimeout, ret);
  562. Sleep(nTimeout);
  563. }
  564. void nsSYN::DynBoxDriver::SetCANState(bool state)
  565. {
  566. mLog::FINFO("Enter SetCANState()");
  567. int nTimeout = 100;
  568. ResDataObject temp;
  569. mLog::FINFO("ConfigFileName [{$}]", m_ConfigFileName);
  570. if (!temp.loadFile(m_ConfigFileName.c_str()))
  571. return;
  572. auto ResDYNConfig = temp["CONFIGURATION"]["DeviceConfig"];
  573. m_Configurations = ResDYNConfig;
  574. TransJsonText(m_Configurations);
  575. char cCmd[MAX_COMMAND_LEN] = { 0 };
  576. string CANProtocol = (string)m_Configurations["CANPortocol"];
  577. mLog::FINFO("CANProtocol [{$}]", CANProtocol);
  578. string CANBaud = (string)m_Configurations["CANBaud"];
  579. mLog::FINFO("CANBaud [{$}]", CANBaud);
  580. int nCmdSize = 0;
  581. if (CANProtocol == "1")
  582. {
  583. cCmd[0] = 'G';
  584. }
  585. else
  586. {
  587. cCmd[0] = 'C';
  588. }
  589. if (state)
  590. {
  591. cCmd[1] = 'O';
  592. mLog::FINFO("OpenCAN");
  593. cCmd[2] = CANBaud[0];
  594. string CANFilter = (string)m_Configurations["CANFilter"];
  595. if (CANFilter.length() >= 4)
  596. {
  597. cCmd[3] = atoi(CANFilter.substr(0,2).c_str());
  598. cCmd[4] = atoi(CANFilter.substr(2, 4).c_str());
  599. }
  600. else
  601. {
  602. cCmd[3] = 0xFF;
  603. cCmd[4] = 0x00;
  604. }
  605. nCmdSize = 5;
  606. }
  607. else
  608. {
  609. cCmd[1] = 'C';
  610. cCmd[2] = '0';
  611. mLog::FINFO("CloseCAN");
  612. nCmdSize = 3;
  613. }
  614. FormatCmd(cCmd, nCmdSize);
  615. string strLog;
  616. CmdtoString(cCmd, nCmdSize, strLog);
  617. mLog::FINFO("[Send:{$}]", strLog.c_str());
  618. //printf("[Send:%s]\n", strLog.c_str());
  619. mLog::FINFO("SetCANState==OUT==: {$} ", cCmd);
  620. int ret = 0;
  621. m_SCF.Lock(msTimeOut_Lock)
  622. .SendPacket(cCmd, nCmdSize, nTimeout, ret);
  623. Sleep(nTimeout);
  624. }
  625. //-----------------------------------------------------------------------------
  626. // GetIODriver & CreateIODriver
  627. //-----------------------------------------------------------------------------
  628. static nsSYN::DynBoxDriver gIODriver;
  629. extern "C" DIOS::Dev::IODriver * __cdecl GetIODriver () // 返回静态对象的引用, 调用者不能删除 !
  630. {
  631. return &gIODriver;
  632. }
  633. extern "C" DIOS::Dev::IODriver * __cdecl CreateIODriver () // 返回新对象, 调用者必须自行删除此对象 !
  634. {
  635. return new nsSYN::DynBoxDriver ();
  636. }