DIOS.Dev.SyncBoxe.DYN.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. // DIOS.Dev.GEN.DEMO.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "FileVersion.hpp"
  5. #include "DIOS.Dev.SyncBoxe.DYN.h"
  6. #include "Helper.JSON.hpp"
  7. #include "CommonFun.h"
  8. // "SimpleLog.hpp"
  9. #include "common_api.h"
  10. //SimpleLog* g_SLogger = NULL;
  11. using namespace DIOS::Dev::Detail::SYNBOX;
  12. namespace nsSYN = DIOS::Dev::Detail::SYNBOX;
  13. #pragma warning (disable:4244) // warning C4244: “初始化”: 从“double”转换到“float”,可能丢失数据
  14. #pragma warning (disable:4305) // warning C4305: “参数”: 从“double”到“float”截断
  15. #pragma warning (disable:4267) // warning C4267 : “初始化”: 从“size_t”转换到“int”,可能丢失数据
  16. #ifdef _WIN64
  17. #ifdef _DEBUG
  18. static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64D.dll";
  19. #else
  20. static const auto COM_SCFDllName = "Dios.Dev.SerialSCFX64.dll";
  21. #endif
  22. #endif
  23. #ifdef _WIN64
  24. #ifdef _DEBUG
  25. static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64D.dll";
  26. #else
  27. static const auto TCP_SCFDllName = "Dios.Dev.TcpipSCFX64.dll";
  28. #endif
  29. #endif
  30. using namespace DIOS::Dev::Detail::SYNBOX;
  31. namespace nsSYN = DIOS::Dev::Detail::SYNBOX;
  32. Log4CPP::Logger* mLog::gLogger = nullptr;
  33. ////-----------------------------------------------------------------------------
  34. //// DynBoxDevice
  35. ////-----------------------------------------------------------------------------
  36. nsSYN::SyncBoxDevice::SyncBoxDevice(std::shared_ptr <IOEventCenter> center, nsSCF::SCF SCF) : super(center, SCF)
  37. {
  38. assert(EventCenter);
  39. m_DynBoxDevice.reset(new DynBoxDevice(center,SCF));
  40. m_MSGUnit.reset(new nsDetail::MSGUnit(center, SyncConsoleUnitType));
  41. m_CollimatorDevice.reset(new CollimatorDevice(center, SCF));
  42. Register();
  43. OnCallBack();
  44. }
  45. nsSYN::SyncBoxDevice::~SyncBoxDevice()
  46. {
  47. }
  48. std::string nsSYN::SyncBoxDevice::GetGUID() const
  49. {
  50. mLog::Info("\n===============GetGUID : {$} ===================\n", SyncConsoleUnitType);
  51. return SyncConsoleUnitType;
  52. }
  53. void nsSYN::SyncBoxDevice::Register()
  54. {
  55. auto Disp = &Dispatch;
  56. m_DynBoxDevice->Register(Disp);
  57. auto fun_SimulateHandSwitchSignal = [this](auto in, auto& out)
  58. {
  59. string value[5];
  60. ResDataObject json;
  61. mLog::Error("in [{$}]", in.c_str());
  62. json.decode(in.c_str());
  63. int paramCount = json.GetKeyCount("P0");
  64. for (int i = 0; i < paramCount; i++)
  65. {
  66. value[i] = (const char*)json[i];
  67. }
  68. return m_DynBoxDevice->SimulateHandSwitchSignal(value[0], atoi(value[1].c_str()));
  69. };
  70. Disp->Action.Push("SimulateHandSwitchSignal", fun_SimulateHandSwitchSignal);
  71. m_CollimatorDevice->Register(Disp);
  72. Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; });
  73. }
  74. void nsSYN::SyncBoxDevice::OnCallBack()
  75. {
  76. //m_DynBoxDevice->OnCallBack();
  77. //m_CollimatorDevice->OnCallback();
  78. auto HWNotProcess = [](const char* value, int length) -> void
  79. {
  80. mLog::Error("\n This commands didn't need to process!\n");
  81. };
  82. auto HWSignal = [this](const char* value, int length)
  83. {
  84. assert(value);
  85. char cChannelValue;
  86. cChannelValue = value[5]; //status : on /off
  87. string strChannel = ((string)value).substr(3, 2);
  88. bool bStatus = ChartoInt(cChannelValue);
  89. m_DynBoxDevice->DealtheSignal(strChannel.c_str(), bStatus);
  90. };
  91. auto HWCAN = [this](const char* value, int length) -> void
  92. {
  93. string sCmdID = ((string)value).substr(8, 3);
  94. int nlen = ChartoInt(value[11]) * 256 + ChartoInt(value[12]);
  95. m_CollimatorDevice->DealReceiveData(sCmdID, &value[13], nlen);
  96. };
  97. // 有部分前缀是包含关系, 长的包含短的, 例如 KVS 包含了 KV.
  98. // 因此长的在前面, 短的在后面
  99. // !!! Device 是个短寿命对象, 而 arFrame 是静态变量 !!!
  100. // !!! 因此, 在添加到 arFrame 之前, 务必先清零 !!!
  101. arFrame.clear();
  102. arFrame.push_back(tFrameMapping("SRA", 3, HWNotProcess));
  103. arFrame.push_back(tFrameMapping("SEC", 3, HWNotProcess));
  104. arFrame.push_back(tFrameMapping("SPT", 3, HWNotProcess));
  105. arFrame.push_back(tFrameMapping("SRS", 3, HWNotProcess));
  106. arFrame.push_back(tFrameMapping("SAI", 3, HWSignal));
  107. arFrame.push_back(tFrameMapping("SWS", 3, HWSignal));
  108. arFrame.push_back(tFrameMapping("SCR", 3, HWCAN));
  109. arFrame.push_back(tFrameMapping("STL", 3, HWCAN));
  110. }
  111. //V3新方法
  112. void nsSYN::SyncBoxDevice::SubscribeSelf(dios_mqtt_connection* conn)
  113. {
  114. mLog::Debug("SubscribeSelf");
  115. //订阅GEN所有Action
  116. if (nullptr != conn)
  117. {
  118. SubscribeTopic(conn, "DIOS/DEVICE/SyncBox/Action/#");
  119. }
  120. else
  121. {
  122. mLog::Error("SubscribeSelf conn is empty");
  123. }
  124. }
  125. //-----------------------------------------------------------------------------
  126. // DynBoxDriver
  127. //-----------------------------------------------------------------------------
  128. nsSYN::DynBoxDriver::DynBoxDriver ()
  129. {
  130. m_bDemoMode = false;
  131. m_bDemoConnected = false;
  132. m_pAttribute.reset(new ResDataObject());
  133. m_pDescription.reset(new ResDataObject());
  134. #ifdef _WIN64
  135. g_strAppPath = GetProcessDirectory() + R"(OEMDrivers\SyncBox\DYN\DIOS.Dev.SyncBoxe.DYN64.dll)";
  136. #else
  137. g_strAppPath = GetProcessDirectory() + R"(OEMDrivers\SyncBox\DYN\DIOS.Dev.SyncBoxe.DYN.dll)";
  138. #endif
  139. }
  140. nsSYN::DynBoxDriver::~DynBoxDriver ()
  141. {
  142. }
  143. auto nsSYN::DynBoxDriver::CreateDevice (int index) -> std::unique_ptr <IODevice>
  144. {
  145. if (m_bDemoMode)
  146. {
  147. if (! m_bDemoConnected) return nullptr;
  148. auto dev = std::unique_ptr <IODevice>(new IODevice(new SyncBoxDevice(EventCenter, m_SCF)));
  149. return dev;
  150. }
  151. if (!m_SCF.isConnected())
  152. return nullptr;
  153. ResDataObject r_config;
  154. //pSDCDevice = new nsSYN::DynBoxDevice(EventCenter,m_SCF);
  155. pSDCDevice=new SyncBoxDevice(EventCenter, m_SCF);
  156. //auto pSDCDevice = std::unique_ptr <DynBoxDevice>(new DynBoxDevice(EventCenter, m_SCF));
  157. auto dev = std::unique_ptr <IODevice>(new IODevice(pSDCDevice));
  158. if (r_config.loadFile(m_ConfigFileName.c_str()))
  159. {
  160. pSDCDevice->m_DynBoxDevice->ResDYNConfig = r_config["CONFIGURATION"]["DeviceConfig"];
  161. pSDCDevice->m_DynBoxDevice->m_bSynUpdataGenerStatus = r_config["CONFIGURATION"]["SynUpdataGenerStatus"];
  162. pSDCDevice->m_CollimatorDevice->ResDYNConfig = r_config["CONFIGURATION"]["DeviceConfig"];
  163. //pSDCDevice->m_DynBoxDevice->m_DefaultExpFrameRate = r_config["CONFIGURATION"]["DefaultExpFrameRate"];
  164. //pSDCDevice->m_DynBoxDevice->m_DefaultExpTimes = r_config["CONFIGURATION"]["DefaultExpTimes"];
  165. try
  166. {
  167. pSDCDevice->m_DynBoxDevice->m_DefaultExpFrameRate = r_config["CONFIGURATION"]["DefaultExpFrameRate"];
  168. }
  169. catch (ResDataObjectExption& e)
  170. {
  171. mLog::Error("Read DefaultExpFrameRate failed: {$}", e.what());
  172. }
  173. try
  174. {
  175. pSDCDevice->m_DynBoxDevice->m_DefaultExpTimes = r_config["CONFIGURATION"]["DefaultExpTimes"];
  176. }
  177. catch (ResDataObjectExption& e)
  178. {
  179. mLog::Error("Read DefaultExpTimes failed: {$}", e.what());
  180. }
  181. }
  182. pSDCDevice->m_CollimatorDevice->SetCollimatorMode(6);
  183. return dev;
  184. }
  185. void nsSYN::DynBoxDriver::FireNotify (int code, std::string key, std::string content)
  186. {
  187. EventCenter->OnNotify (code, key, content);
  188. }
  189. void nsSYN::DynBoxDriver::Prepare ()
  190. {
  191. string strLogPath = GetProcessDirectory() + R"(\OEMDrivers\SyncBox\Conf\Log4CPP.Config.SYN.xml)";
  192. Log4CPP::GlobalContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "SYN.DYN");
  193. auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str());
  194. if (!rc)
  195. {
  196. printf("\n Load log configfile failed!\n");
  197. }
  198. mLog::gLogger = Log4CPP::LogManager::GetLogger("SYN.DYN");
  199. ResDataObject r_config;
  200. if (r_config.loadFile (m_ConfigFileName.c_str ()))
  201. {
  202. m_bDemoMode = (atoi)(((string)r_config ["CONFIGURATION"] ["IsDemo"]).c_str ());
  203. ResDataObject Connection = r_config ["CONFIGURATION"] ["connections"] [0];
  204. if ((string)Connection ["type"] == "COM")
  205. m_SCFDllName = COM_SCFDllName;
  206. else
  207. m_SCFDllName = TCP_SCFDllName;
  208. }
  209. //add by wxx:自动获取版本号并更新到日志
  210. string version;
  211. ResDataObject TempConfig = r_config["CONFIGURATION"];
  212. if (GetVersion(version,TempConfig))
  213. mLog::Info("--Func-- driver prepare : version:{$}\n", version.c_str());
  214. else
  215. mLog::Info("--Func-- driver prepare, v1.0.0.1 \n");
  216. super::Prepare ();
  217. }
  218. bool nsSYN::DynBoxDriver::Connect ()
  219. {
  220. if (m_bDemoMode)
  221. {
  222. m_bDemoConnected = true;
  223. return true;// SCF_ERR::SCF_SUCCEED;
  224. }
  225. ResDataObject r_config;
  226. if (!r_config.loadFile(m_ConfigFileName.c_str()))
  227. return false;// SCF_ERR::SCF_OPEN_FAILED;
  228. ResDataObject Connection = r_config["CONFIGURATION"]["connections"][0];
  229. mLog::Info("connections:{$} \n", Connection.encode());
  230. auto erCode = m_SCF.Connect(Connection.encode(), &nsSYN::DynBoxDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000);
  231. if (erCode != SCF_ERR::SCF_SUCCEED)
  232. return false; // erCode;
  233. CanShakeHand();
  234. Sleep(200);
  235. SetCANState(true);//open can
  236. auto rc = super::Connect();
  237. if (!rc)
  238. return false;// 0;
  239. //return (erCode == SCF_ERR::SCF_SUCCEED);
  240. return true;// SCF_ERR::SCF_SUCCEED;
  241. }
  242. void nsSYN::DynBoxDriver::Disconnect()
  243. {
  244. super::Disconnect();
  245. m_SCF.Disconnect();
  246. m_bDemoConnected = false;
  247. }
  248. bool nsSYN::DynBoxDriver::isConnected() const
  249. {
  250. if (m_bDemoMode)
  251. return m_bDemoConnected;
  252. else
  253. return super::isConnected();
  254. }
  255. std::string nsSYN::DynBoxDriver::DriverProbe ()
  256. {
  257. ResDataObject r_config, HardwareInfo;
  258. if (r_config.loadFile (m_ConfigFileName.c_str ()))
  259. {
  260. HardwareInfo.add ("MajorID", r_config ["CONFIGURATION"] ["MajorID"]);
  261. HardwareInfo.add ("MinorID", r_config ["CONFIGURATION"] ["MinorID"]);
  262. HardwareInfo.add ("VendorID", r_config ["CONFIGURATION"] ["VendorID"]);
  263. HardwareInfo.add ("ProductID", r_config ["CONFIGURATION"] ["ProductID"]);
  264. HardwareInfo.add ("SerialID", r_config ["CONFIGURATION"] ["SerialID"]);
  265. }
  266. else
  267. {
  268. HardwareInfo.add("MajorID", "SYN");
  269. HardwareInfo.add("MinorID", "Dr");
  270. HardwareInfo.add("VendorID", "ECOM");
  271. HardwareInfo.add("ProductID", "DYN");
  272. HardwareInfo.add("SerialID", "1234");
  273. }
  274. string ret = HardwareInfo.encode ();
  275. return ret;
  276. }
  277. bool nsSYN::DynBoxDriver::GetDeviceConfig(std::string& Cfg)
  278. {
  279. Cfg = m_DeviceConfig.encode();
  280. printf("GetDeviceConfig over");
  281. return true;
  282. }
  283. bool nsSYN::DynBoxDriver::SetDeviceConfig(std::string Cfg)
  284. {
  285. mLog::Info("--Func-- SetDeviceConfig {$}\n", Cfg.c_str());
  286. ResDataObject DeviceConfig;
  287. DeviceConfig.decode(Cfg.c_str());
  288. ResDataObject DescriptionTempEx;
  289. DescriptionTempEx = DeviceConfig["DeviceConfig"]["Attribute"];
  290. mLog::Debug("Attribute:{$}", DescriptionTempEx.encode());
  291. bool bSaveFile = false; //true:重新保存配置文件
  292. string strAccess = "";
  293. for (int i = 0; i < DescriptionTempEx.size(); i++)
  294. {
  295. string strKey = DescriptionTempEx.GetKey(i);
  296. mLog::Info("{$}", strKey.c_str());
  297. try
  298. {
  299. if (m_pAttribute->GetFirstOf(strKey.c_str()) >= 0)
  300. {
  301. strAccess = (string)(*m_pDescription)[strKey.c_str()]["Access"];
  302. if ("RW" == strAccess)
  303. {
  304. //修改对应配置,在其他单元的配置项要同时调用其修改函数修改真实值
  305. //1. 修改内存中的值,用于给上层发消息
  306. (*m_pAttribute)[strKey.c_str()] = DescriptionTempEx[i];
  307. //2. 拿到Innerkey
  308. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  309. mLog::Info("nConfigInfoCount {$}", nConfigInfoCount);
  310. string strTemp = ""; //存储AttributeKey
  311. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  312. {
  313. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  314. if (strTemp == strKey)
  315. {
  316. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  317. break;
  318. }
  319. }
  320. //3. 修改配置文件中的值
  321. if (SetDeviceConfigValue(m_Configurations, strTemp.c_str(), 1, DescriptionTempEx[i]))
  322. {
  323. mLog::Debug("SetDeviceConfigValue over");
  324. bSaveFile = true;
  325. }
  326. }
  327. else
  328. {
  329. mLog::Info("{$} is not a RW configuration item", strKey.c_str());
  330. }
  331. }
  332. else
  333. {
  334. mLog::Info("without this attribute {$}", strKey.c_str());
  335. }
  336. }
  337. catch (ResDataObjectExption& e)
  338. {
  339. mLog::Error("SetDriverConfig crashed: {$}", e.what());
  340. return false;
  341. }
  342. }
  343. if (bSaveFile)
  344. {
  345. //3. 重新保存配置文件
  346. SaveConfigFile(true);
  347. }
  348. return true;
  349. }
  350. bool nsSYN::DynBoxDriver::SaveConfigFile(bool bSendNotify)
  351. {
  352. m_ConfigAll["CONFIGURATION"] = m_Configurations;
  353. bool bRt = m_ConfigAll.SaveFile(m_ConfigFileName.c_str());
  354. mLog::Info("SaveConfigFile over {$}", bRt);
  355. return true;
  356. }
  357. bool nsSYN::DynBoxDriver::GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue)
  358. {
  359. strValue = "";
  360. string strTemp = pInnerKey;
  361. if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取
  362. {
  363. int pos = 0;
  364. ResDataObject resTemp = config;
  365. while ((pos = strTemp.find_first_of(',')) != string::npos)
  366. {
  367. string Key = strTemp.substr(0, pos);
  368. string TempValue = resTemp[Key.c_str()].encode();
  369. resTemp.clear();
  370. resTemp.decode(TempValue.c_str());
  371. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  372. }
  373. if (strTemp != "")
  374. {
  375. strValue = (string)resTemp[strTemp.c_str()];
  376. }
  377. else
  378. {
  379. strValue = (string)resTemp;
  380. }
  381. }
  382. return true;
  383. }
  384. bool nsSYN::DynBoxDriver::SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue)
  385. {
  386. string strTemp = pInnerKey;
  387. mLog::Debug("Begin to change {$} item value to {$}", pInnerKey, szValue);
  388. if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取
  389. {
  390. try {
  391. int pos = 0;
  392. ResDataObject* resTemp = &config;
  393. while ((pos = strTemp.find_first_of(',')) != string::npos)
  394. {
  395. string Key = strTemp.substr(0, pos);
  396. resTemp = &(*resTemp)[Key.c_str()];
  397. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  398. }
  399. if (strTemp != "")
  400. {
  401. (*resTemp)[strTemp.c_str()] = szValue;
  402. }
  403. else
  404. {
  405. *resTemp = szValue;
  406. }
  407. }
  408. catch (ResDataObjectExption& e)
  409. {
  410. mLog::Error("SetDriverConfigvalue crashed: {$}", e.what());
  411. return false;
  412. }
  413. }
  414. return true;
  415. }
  416. std::string nsSYN::DynBoxDriver::GetResource()
  417. {
  418. ResDataObject r_config, temp;
  419. if (!temp.loadFile(m_ConfigFileName.c_str()))
  420. {
  421. return "";
  422. }
  423. m_ConfigAll = temp;
  424. r_config = temp["CONFIGURATION"];
  425. m_Configurations = r_config;
  426. ResDataObject DescriptionTemp;
  427. ResDataObject ListTemp;
  428. string strTemp = ""; //用于读取字符串配置信息
  429. string strIndex = ""; //用于读取配置信息中的List项
  430. int nTemp = -1; //用于读取整型配置信息
  431. char sstream[10] = { 0 }; //用于转换值
  432. string strValue = ""; //用于存储配置的值
  433. string strType = ""; //用于存储配置的类型 int/float/string...
  434. /***
  435. * 1. 通过循环,将所有配置项写到pDeviceConfig
  436. * 2. 记录配置项的内部key以及配置类型,类型对应了不同配置文件路径,用于读写真实值
  437. ***/
  438. try
  439. {
  440. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  441. //mLog::Info(g_pFPDCtrlLog, "ConfigInfo Count: {$}", nConfigInfoCount);
  442. m_pAttribute->clear();
  443. m_pDescription->clear();
  444. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  445. {
  446. DescriptionTemp.clear();
  447. ListTemp.clear();
  448. //AttributeType
  449. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Type"];
  450. DescriptionTemp.add(ConfKey::AttributeType, strTemp.c_str());
  451. //DescriptionTemp= m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"];
  452. //mLog::Info(g_pFPDCtrlLog, "--> {$}: {$}", AttributeType, strTemp.c_str());
  453. strType = strTemp; //记录配置项的类型
  454. //AttributeKey
  455. //1. 根据AttributeType,内部key和配置路径,拿到当前的真实值
  456. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  457. nTemp = (int)m_Configurations["ConfigToolInfo"][nInfoIndex]["PathID"];
  458. GetDeviceConfigValue(r_config, strTemp.c_str(), nTemp, strValue);
  459. //2. 赋值
  460. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  461. if ("int" == strType)
  462. {
  463. (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str()));
  464. //mLog::Info(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), atoi(strValue.c_str()));
  465. }
  466. else if ("float" == strType)
  467. {
  468. (*m_pAttribute).add(strTemp.c_str(), atof(strValue.c_str()));
  469. //mLog::Info(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), atof(strValue.c_str()));
  470. }
  471. else //其它先按string类型处理
  472. {
  473. (*m_pAttribute).add(strTemp.c_str(), strValue.c_str());
  474. //mLog::Info(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), strValue.c_str());
  475. }
  476. //AttributeAccess
  477. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Access"];
  478. DescriptionTemp.add(ConfKey::AttributeAccess, strTemp.c_str());
  479. //mLog::Info(g_pFPDCtrlLog, "{$}: {$}", AttributeAccess, strTemp.c_str());
  480. //AttributeRangeMin
  481. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMin"];
  482. if (strTemp != "") //不需要的配置项为空
  483. {
  484. DescriptionTemp.add(ConfKey::AttributeRangeMin, strTemp.c_str());
  485. //mLog::Info(g_pFPDCtrlLog, "{$}: {$}", AttributeRangeMin, strTemp.c_str());
  486. }
  487. //AttributeRangeMax
  488. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMax"];
  489. if (strTemp != "") //不需要的配置项为空
  490. {
  491. DescriptionTemp.add(ConfKey::AttributeRangeMax, strTemp.c_str());
  492. //mLog::Info(g_pFPDCtrlLog, "{$}: {$}", AttributeRangeMax, strTemp.c_str());
  493. }
  494. //AttributeList
  495. nTemp = m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListNum"];
  496. if (nTemp > 0) //ListNum不大于0时说明不需要list配置
  497. {
  498. for (int nListIndex = 0; nListIndex < nTemp; nListIndex++)
  499. {
  500. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListInfo"][nListIndex];
  501. //sprintf_s(sstream, "{$}", nListIndex);
  502. auto temKey = std::to_string(nListIndex);
  503. ListTemp.add(temKey.c_str(), strTemp.c_str());
  504. //mLog::Info(g_pFPDCtrlLog, "list {$}: {$}", nListIndex, strTemp.c_str());
  505. }
  506. DescriptionTemp.add(ConfKey::AttributeList, ListTemp.encode());
  507. }
  508. //AttributeRequired
  509. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Required"];
  510. DescriptionTemp.add(ConfKey::AttributeRequired, strTemp.c_str());
  511. //mLog::Info(g_pFPDCtrlLog, "{$}: {$}", AttributeRequired, strTemp.c_str());
  512. //AttributeDefaultValue
  513. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["DefaultValue"];
  514. if (strTemp != "") //不需要的配置项为空
  515. {
  516. DescriptionTemp.add(ConfKey::AttributeDefaultValue, strTemp.c_str());
  517. //mLog::Info(g_pFPDCtrlLog, "{$}: {$}", AttributeDefaultValue, strTemp.c_str());
  518. }
  519. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  520. (*m_pDescription).add(strTemp.c_str(), DescriptionTemp);
  521. }
  522. }
  523. catch (ResDataObjectExption& e)
  524. {
  525. mLog::Error("Get config error: {$}", e.what());
  526. return "";
  527. }
  528. ResDataObject resDeviceResource;
  529. resDeviceResource.add(ConfKey::DiosAttribute, (*m_pAttribute));
  530. resDeviceResource.add(ConfKey::DiosDescription, (*m_pDescription));
  531. ResDataObject DescriptionTempEx;
  532. DescriptionTempEx.add(ConfKey::DiosConfig, resDeviceResource);
  533. m_DeviceConfig.clear();
  534. m_DeviceConfig = DescriptionTempEx;
  535. string res = DescriptionTempEx.encode();
  536. //mLog::Debug("get resource over {$}", DescriptionTempEx.encode());
  537. //printf("get resource over : %s \n", DescriptionTempEx.encode());
  538. return res;
  539. }
  540. //std::string nsSYN::DynBoxDriver::GetResource ()
  541. //{
  542. // ResDataObject temp;
  543. // if (!temp.loadFile (m_ConfigFileName.c_str ()))
  544. // return std::string ();
  545. //
  546. // auto r_config = temp ["CONFIGURATION"];
  547. // for (auto &Item : m_ConfigInfo)
  548. // {
  549. // string key = Item.GetKey ();
  550. // if (key == ConfKey::DiosType)
  551. // {
  552. // Item.SetCurrentValue (((string)r_config ["VendorID"]).c_str ());
  553. // }
  554. // else if (key == ConfKey::DiosModel)
  555. // {
  556. // Item.SetCurrentValue (((string)r_config ["ProductID"]).c_str ());
  557. // }
  558. // else if (key == ConfKey::DiosSCFType)
  559. // {
  560. // Item.SetCurrentValue (((string)r_config ["connections"] [0] ["type"]).c_str ());
  561. // }
  562. // else if (key == ConfKey::DiosSCFPort || key == ConfKey::DiosSCFBaudrate || key == ConfKey::DiosSCFBytesize
  563. // || key == ConfKey::DiosSCFParity || key == ConfKey::DiosSCFStopbits || key == ConfKey::DiosSCFIP)
  564. // {
  565. // if (r_config ["connections"] [0].GetFirstOf (key.c_str ()) >= 0)
  566. // {
  567. // Item.SetCurrentValue (((string)r_config ["connections"] [0] [key.c_str ()]).c_str ());
  568. // }
  569. // }
  570. // }
  571. // ResDataObject resAttr, resDescription;
  572. // for (auto Item : m_ConfigInfo)
  573. // {
  574. // resAttr.add (Item.GetKey (), Item.GetCurrentValue ());
  575. // resDescription.add (Item.GetKey (), Item.GetDescription ());
  576. // }
  577. // ResDataObject resDeviceResource;
  578. // resDeviceResource.add (ConfKey::DiosAttribute, resAttr);
  579. // resDeviceResource.add (ConfKey::DiosDescription, resDescription);
  580. // m_DeviceConfig = resDeviceResource;
  581. // string res = resDeviceResource.encode ();
  582. // mLog::Info("resDeviceResource :{$} \n", resDeviceResource.encode ());
  583. // return res;
  584. //}
  585. std::string nsSYN::DynBoxDriver::DeviceProbe ()
  586. {
  587. ResDataObject r_config, HardwareInfo;
  588. if (r_config.loadFile (m_ConfigFileName.c_str ()))
  589. {
  590. HardwareInfo.add ("MajorID", r_config ["CONFIGURATION"] ["MajorID"]);
  591. HardwareInfo.add ("MinorID", r_config ["CONFIGURATION"] ["MinorID"]);
  592. HardwareInfo.add ("VendorID", r_config ["CONFIGURATION"] ["VendorID"]);
  593. HardwareInfo.add ("ProductID", r_config ["CONFIGURATION"] ["ProductID"]);
  594. HardwareInfo.add ("SerialID", r_config ["CONFIGURATION"] ["SerialID"]);
  595. }
  596. else
  597. {
  598. HardwareInfo.add ("MajorID", "SYN");
  599. HardwareInfo.add ("MinorID", "Dr");
  600. HardwareInfo.add ("VendorID", "ECOM");
  601. HardwareInfo.add ("ProductID", "DYN");
  602. HardwareInfo.add ("SerialID", "1234");
  603. }
  604. string ret = HardwareInfo.encode ();
  605. return ret;
  606. }
  607. void nsSYN::DynBoxDriver::Dequeue (const char * Packet, DWORD Length)
  608. {
  609. DecodeFrame (Packet, Length);
  610. }
  611. PACKET_RET nsSYN::DynBoxDriver::callbackPackageProcess (const char * RecData, DWORD nLength, DWORD& PacketLength)
  612. {
  613. //判断是否是整包
  614. /*
  615. 这个是回调函数,我收到的数据会需要这个回调函数帮我判断是否是整个的包;
  616. 如果有整个的包,返回值为true,在PacketLength处返回给我整个包的长度,我再截取后放入缓存供上层使用;
  617. 如果缓存中的数据没有整个的数据包,那么返回false
  618. */
  619. if (nLength < 1)
  620. {
  621. mLog::Error("nLength < 1, nLength=={$} \n", nLength);
  622. return PACKET_USELESS;
  623. }
  624. for (DWORD i = 0; i < nLength - 1; i++)
  625. {
  626. if (RecData [i] == 0x0d && RecData [i + 1] == 0x0a)
  627. {
  628. PacketLength = i + 2;
  629. char strtemp [100] = { 0 };
  630. memcpy (strtemp, RecData, i);
  631. strtemp [PacketLength + 1] = 0;
  632. mLog::Info("==IN ==:{$}\n", strtemp);
  633. //printf("==IN(Hex) ==:");
  634. //for (int j = 0; j < i; j++)
  635. //{
  636. // printf("%02X ", RecData[j]);
  637. //}
  638. //printf("\n");
  639. return PACKET_ISPACKET;
  640. }
  641. else if (RecData [i] == 0x03)
  642. {
  643. mLog::Error("(Hex)0x03 ,len={$} \n", i);
  644. PacketLength = i + 1;
  645. return PACKET_USELESS;
  646. }
  647. }
  648. return PACKET_NOPACKET;
  649. }
  650. //-----------------------------------------------------------------------------
  651. //
  652. //-----------------------------------------------------------------------------
  653. void nsSYN::DynBoxDriver::CanShakeHand()
  654. {
  655. int nTimeout = 100;
  656. char cCmd[MAX_COMMAND_LEN] = "SECA00";
  657. int nCmdSize = 6;
  658. FormatCmd(cCmd, nCmdSize);
  659. string strLog;
  660. CmdtoString(cCmd, nCmdSize, strLog);
  661. mLog::Info( "[Send:{$}]", strLog.c_str());
  662. mLog::Info("CanShakeHand ==OUT==: {$} \n", cCmd);
  663. int ret = 0;
  664. m_SCF.Lock(msTimeOut_Lock)
  665. .SendPacket(cCmd, nCmdSize, nTimeout, ret);
  666. Sleep(nTimeout);
  667. }
  668. void nsSYN::DynBoxDriver::SetCANState(bool state)
  669. {
  670. int nTimeout = 100;
  671. ResDataObject r_config;
  672. if (!r_config.loadFile(m_ConfigFileName.c_str())) return;
  673. ResDataObject ResDYNConfig = r_config["CONFIGURATION"]["DeviceConfig"];
  674. char cCmd[MAX_COMMAND_LEN] = "SCSA13";
  675. string CANProtocol = (string)ResDYNConfig["CANPortocol"];
  676. string CANBaud = (string)ResDYNConfig["CANBaud"];
  677. string CANEnable;
  678. if (state)
  679. {
  680. CANEnable = "1";
  681. mLog::Info( "OpenCAN");
  682. }
  683. else
  684. {
  685. CANEnable = "0";
  686. mLog::Info( "CloseCAN");
  687. }
  688. string CANFilter = (string)ResDYNConfig["CANFilter"];
  689. vector <int> vecFilter;
  690. if (!CANFilter.empty())
  691. {
  692. while (!CANFilter.empty())
  693. {
  694. string strTemp = GetContentFromString(CANFilter, ',');
  695. vecFilter.push_back(String2Hex(strTemp));
  696. }
  697. }
  698. INT32 nIDS = 0, nFilter = 0;
  699. int nSize = vecFilter.size();
  700. if (nSize > 0)
  701. {
  702. //int nIDStemp = vecFilter[0], nFiltertemp = vecFilter[0];
  703. int nIDStemp = 0xFFFFFFFF, nFiltertemp = 0xFFFFFFFF;
  704. for (int n = 0; n < nSize; n++)
  705. {
  706. nIDStemp &= vecFilter[n];
  707. }
  708. if (nSize > 1)
  709. {
  710. nFiltertemp = 0;
  711. const int num = 11;
  712. int ntem[num];
  713. for (int m = 0; m < num; m++)
  714. {
  715. ntem[m] = 1;
  716. int nvalue = (vecFilter[0] >> (num - 1 - m)) & 0x01;
  717. for (int l = 1; l < nSize; l++)
  718. {
  719. if (nvalue != ((vecFilter[l] >> (num - 1 - m)) & 0x01))
  720. {
  721. ntem[m] = 0;
  722. break;
  723. }
  724. }
  725. nFiltertemp += (ntem[m] << (num - 1 - m));
  726. }
  727. }
  728. nIDS = nIDStemp << 21;
  729. nFilter = (nFiltertemp) << 21;
  730. }
  731. string strtemp;
  732. char cFilter[16];
  733. for (int i = 0; i < 8; i++)
  734. {
  735. int nres = ((nIDS & (0xF << ((7 - i) * 4))) >> ((7 - i) * 4)) & 0x0F;
  736. if (nres > 9)
  737. {
  738. cFilter[i] = char(0x30 + nres + 7);
  739. }
  740. else
  741. {
  742. cFilter[i] = char(0x30 + nres);
  743. }
  744. }
  745. for (int i = 0; i < 8; i++)
  746. {
  747. int nres = ((nFilter & (0xF << ((7 - i) * 4))) >> ((7 - i) * 4)) & 0x0F;
  748. if (nres > 9)
  749. {
  750. cFilter[i + 8] = char(0x30 + nres + 7);
  751. }
  752. else
  753. {
  754. cFilter[i + 8] = char(0x30 + nres);
  755. }
  756. }
  757. cCmd[6] = CANProtocol[0];
  758. cCmd[7] = CANBaud[0];
  759. for (int i = 0; i < 16; i++)
  760. {
  761. cCmd[8 + i] = cFilter[i];
  762. //cCmd[8 + i] = '0';
  763. }
  764. cCmd[24] = CANEnable[0];
  765. int nCmdSize = 25;
  766. FormatCmd(cCmd, nCmdSize);
  767. string strLog;
  768. CmdtoString(cCmd, nCmdSize, strLog);
  769. mLog::Info( "[Send:{$}]", strLog.c_str());
  770. mLog::Info("==OUT==: {$} \n", cCmd);
  771. int ret = 0;
  772. m_SCF.Lock(msTimeOut_Lock)
  773. .SendPacket(cCmd, nCmdSize, nTimeout, ret);
  774. Sleep(nTimeout);
  775. }
  776. //-----------------------------------------------------------------------------
  777. // GetIODriver & CreateIODriver
  778. //-----------------------------------------------------------------------------
  779. static nsSYN::DynBoxDriver gIODriver;
  780. extern "C" DIOS::Dev::IODriver * __cdecl GetIODriver () // 返回静态对象的引用, 调用者不能删除 !
  781. {
  782. return &gIODriver;
  783. }
  784. extern "C" DIOS::Dev::IODriver * __cdecl CreateIODriver () // 返回新对象, 调用者必须自行删除此对象 !
  785. {
  786. return new nsSYN::DynBoxDriver ();
  787. }