DIOS.Dev.SyncBox.FullUCB.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. // DIOS.Dev.GEN.DEMO.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "FileVersion.hpp"
  5. #include "DIOS.Dev.SyncBox.FullUCB.h"
  6. #include "Helper.JSON.hpp"
  7. #include "common_api.h"
  8. using namespace DIOS::Dev::Detail::SYNBOX;
  9. namespace nsSYN = DIOS::Dev::Detail::SYNBOX;
  10. #pragma warning (disable:4244) // warning C4244: “初始化”: 从“double”转换到“float”,可能丢失数据
  11. #pragma warning (disable:4305) // warning C4305: “参数”: 从“double”到“float”截断
  12. #pragma warning (disable:4267) // warning C4267 : “初始化”: 从“size_t”转换到“int”,可能丢失数据
  13. using namespace DIOS::Dev::Detail::SYNBOX;
  14. namespace nsSYN = DIOS::Dev::Detail::SYNBOX;
  15. #define COM_Deliver 0
  16. nsSYN::SyncBoxDevice* nsSYN::SyncBoxDevice::m_SingleExample = nullptr;
  17. //-----------------------------------------------------------------------------
  18. // 串口处理类
  19. //-----------------------------------------------------------------------------
  20. Log4CPP::Logger* mLog::gLogger = nullptr;
  21. #if COM_Deliver
  22. static nsSerialGPM::CDeliverModule m_tDelivermodule;
  23. static int m_nCMDType_Directly{ 0 };
  24. static int m_nCMDType_WaitTime{ 0 };
  25. static int m_nCMDType_WaitSelf{ 0 };
  26. void nsSYN::ProcessClientData(const char* pData, unsigned long nDataLength, void* lparam)
  27. {
  28. SyncBoxDevice* pCurSyn = (SyncBoxDevice*)lparam;
  29. pCurSyn->HWSend(pData, nDataLength);
  30. }
  31. void nsSYN::WriteLog(const char* pData, nsSerialGPM::LOG_V2_LEVEL level)
  32. {
  33. switch (level)
  34. {
  35. case nsSerialGPM::LOG_V2_FATAL:
  36. case nsSerialGPM::LOG_V2_ERROR:
  37. mLog::FERROR(pData);
  38. break;
  39. case nsSerialGPM::LOG_V2_WARNING:
  40. mLog::FWARN(pData);
  41. break;
  42. case nsSerialGPM::LOG_V2_DEBUG:
  43. mLog::FDEBUG(pData);
  44. break;
  45. case nsSerialGPM::LOG_V2_INFO:
  46. mLog::FINFO(pData);
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. RET_STATUS nsSYN::HWSendDirectly(char* strCommand, int lengh, int headLengh)
  53. {
  54. return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_Directly, headLengh);
  55. }
  56. RET_STATUS nsSYN::HWSendWaittimeCMD(char* strCommand, int lengh, int headLengh)
  57. {
  58. return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_WaitTime, headLengh);
  59. }
  60. RET_STATUS nsSYN::HWSendWaitSelfCMD(char* strCommand, int lengh, int headLengh)
  61. {
  62. return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_WaitSelf, headLengh);
  63. }
  64. #else
  65. RET_STATUS nsSYN::HWSendNormal(char* strCommand, int lengh, int headLengh)
  66. {
  67. RET_STATUS res = RET_STATUS::RET_SUCCEED;
  68. if (SyncBoxDevice::m_SingleExample)
  69. {
  70. res = SyncBoxDevice::m_SingleExample->HWSend(strCommand, lengh);
  71. SetWaitAction(strCommand);
  72. }
  73. return res;
  74. }
  75. #endif // COM_Deliver
  76. ////-----------------------------------------------------------------------------
  77. //// SynBoxDevice
  78. ////-----------------------------------------------------------------------------
  79. nsSYN::SyncBoxDevice::SyncBoxDevice(std::shared_ptr <IOEventCenter> center, nsSCF::SCF SCF, string configfile) : super(center, SCF)
  80. {
  81. assert(EventCenter);
  82. m_SingleExample = this;
  83. //add by wxx:自动获取版本号并更新到日志
  84. string version;
  85. if (GetVersion(version, hMyModule))
  86. mLog::FINFO("--Func-- driver prepare : version:{$}\n", version.c_str());
  87. else
  88. mLog::FINFO("--Func-- driver prepare, v3.0.0.0 \n");
  89. m_DeviceFullUCB.reset(new DeviceFullUCB(center,configfile));
  90. m_MSGUnit.reset(new nsDetail::MSGUnit(center, SyncConsoleUnitType));
  91. #if COM_Deliver
  92. m_tDelivermodule.InitSendModle(this, &ProcessClientData, WriteLog);
  93. m_nCMDType_Directly = m_tDelivermodule.SetPriority(false, true);
  94. m_nCMDType_WaitTime = m_tDelivermodule.SetPriority(false, false, false, true, 100);
  95. m_nCMDType_WaitSelf = m_tDelivermodule.SetPriority(false, false, 0, false, 0, true, 500);
  96. m_tDelivermodule.SetACKCMDHead(ackCMD, 2);
  97. mLog::FINFO("m_nCMDType_Directly[{$}] m_nCMDType_WaitTime[{$}] m_nCMDType_WaitSelf[{$}]",
  98. m_nCMDType_Directly, m_nCMDType_WaitTime, m_nCMDType_WaitSelf);
  99. #endif // COM_Deliver
  100. Register();
  101. OnCallBack();
  102. }
  103. nsSYN::SyncBoxDevice::~SyncBoxDevice()
  104. {
  105. m_SingleExample = nullptr;
  106. mLog::FINFO("\n===============log end ===================\n");
  107. }
  108. std::string nsSYN::SyncBoxDevice::GetGUID() const
  109. {
  110. mLog::FINFO("\n===============GetGUID : {$} ===================\n", SyncConsoleUnitType);
  111. return SyncConsoleUnitType;
  112. }
  113. void nsSYN::SyncBoxDevice::Register()
  114. {
  115. auto Disp = &Dispatch;
  116. m_DeviceFullUCB->Register(Disp);
  117. Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; });
  118. auto fun_SetDirectExpSignal = [this](auto in, auto& out)
  119. {
  120. auto value = JSONTo <int>(in);
  121. return m_DeviceFullUCB->SetDirectExpSignal(value);
  122. };
  123. Disp->Action.Push("SetDirectExpSignal", fun_SetDirectExpSignal);
  124. auto fun_SetPulseWidth = [this](auto in, auto& out)
  125. {
  126. auto value = JSONTo <int>(in);
  127. return m_DeviceFullUCB->SetPulseWidth(value);
  128. };
  129. Disp->Action.Push("SetPulseWidth", fun_SetPulseWidth);
  130. }
  131. void nsSYN::SyncBoxDevice::OnCallBack()
  132. {
  133. auto HWNotProcess = [](const char* value, int length) -> void
  134. {
  135. mLog::FERROR("This commands didn't need to process!\n");
  136. };
  137. auto HWSignal = [this](const char* value, int length)
  138. {
  139. assert(value);
  140. //mLog::FDEBUG("HWSignal:[{$}]", value);
  141. #if COM_Deliver
  142. m_tDelivermodule.CheckReceive(value, 5);
  143. #endif // COM_Deliver
  144. char cChannelValue;
  145. cChannelValue = value[9]; //status : on /off
  146. string strChannel(value,7,2);
  147. int bStatus = ChartoInt(cChannelValue);
  148. if (m_DeviceFullUCB)
  149. {
  150. m_DeviceFullUCB->DealtheSignal(strChannel, bStatus);
  151. }
  152. };
  153. auto HWCAN = [this](const char* value, int length) -> void
  154. {
  155. string sCmdID = ((string)value).substr(8, 3);
  156. int nlen = ChartoInt(value[11]) * 256 + ChartoInt(value[12]);
  157. };
  158. arFrame.clear();
  159. //通信(握手)测试
  160. arFrame["SEC"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  161. //读取版本号
  162. arFrame["SVR"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  163. //产品序列号写入
  164. arFrame["SSN"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  165. //蜂鸣器提示功能
  166. arFrame["SSP"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  167. //输出端口初始化
  168. arFrame["SIT"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  169. //单片机冷启动
  170. arFrame["SRE"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  171. //出厂恢复(放在软件高级配置工具里)
  172. arFrame["SFR"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  173. //EEPROM区域COPY(放在测试工具里)
  174. arFrame["SEE"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  175. //工作状态查询命令
  176. arFrame["SRF"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  177. //清除工作状态存储区
  178. arFrame["SCF"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  179. //ABS输出命令
  180. arFrame["SAS"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  181. //ADC开启/关闭命令
  182. arFrame["SAD"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  183. //ADC读取命令
  184. arFrame["SRA"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  185. //AEC开启/关闭功能
  186. arFrame["SAE"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  187. //PT_STOP时间读取功能
  188. arFrame["SPT"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  189. //接收CAN数据
  190. arFrame["SCR"] = tFrameMapping(HWCAN, tFrameMapping::OverType_directly, false);
  191. //发送CAN数据
  192. arFrame["STL"] = tFrameMapping(HWCAN, tFrameMapping::OverType_directly, false);
  193. //CAN通讯设置
  194. arFrame["SCS"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  195. //PWM波形属性设置
  196. arFrame["SPS"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  197. //PWM波形输出使能
  198. arFrame["SPE"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  199. //曝光模式切换
  200. arFrame["SMS"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  201. //通道属性设置
  202. arFrame["SCP"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  203. //通道属性读取确认
  204. arFrame["SCD"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  205. //通道属性版本号写入
  206. arFrame["SPW"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  207. //通道属性版本号读出
  208. arFrame["SPR"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  209. //读取通道状态
  210. arFrame["SRS"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  211. //输入通道自动返回状态
  212. arFrame["SAI"] = tFrameMapping(HWSignal, tFrameMapping::OverType_WaitACK, false);
  213. //通道输出设置
  214. arFrame["SWS"] = tFrameMapping(HWSignal, tFrameMapping::OverType_WaitACK, false);
  215. //arFrame["CO"] = tFrameMapping(HWNotProcess, tFrameMapping::OverType_directly, false);
  216. }
  217. RET_STATUS nsSYN::SyncBoxDevice::HWSend(const char* strCommand, int nCMDLengh, int nTimeOut)
  218. {
  219. if (true)
  220. {
  221. string strLog(strCommand);
  222. strLog.at(strLog.length()-1) = 0x03;
  223. strLog.at(strLog.length() - 2) = 0x04;
  224. mLog::FDEBUG("==OUT==: [{$}] \n", strLog.c_str());
  225. }
  226. if (!m_SCF)
  227. return RET_STATUS::RET_FAILED;
  228. int retLength;
  229. m_SCF.Lock(msTimeOut_Lock)
  230. .SendPacket(strCommand, nCMDLengh, nTimeOut, retLength);
  231. //Sleep(nTimeOut);
  232. return RET_STATUS::RET_SUCCEED;
  233. }
  234. //V3新方法
  235. void nsSYN::SyncBoxDevice::SubscribeSelf(dios_mqtt_connection* conn)
  236. {
  237. mLog::FDEBUG("SubscribeSelf");
  238. //订阅GEN所有Action
  239. if (nullptr != conn)
  240. {
  241. SubscribeTopic(conn, "DIOS/DEVICE/SyncBox/Action/#");
  242. }
  243. else
  244. {
  245. mLog::FERROR("SubscribeSelf conn is empty");
  246. }
  247. }
  248. //-----------------------------------------------------------------------------
  249. // CAN口处理
  250. //-----------------------------------------------------------------------------
  251. void nsSYN::SyncBoxDriver::HandshakeTest()
  252. {
  253. int nTimeout = 100;
  254. char cCmd[FullUCB_Com_NormalLen] = "SECA00";
  255. int nCmdSize = 6;
  256. FormatCmd(cCmd, nCmdSize);
  257. mLog::FINFO("CanShakeHand ==OUT==: [{$}] ", cCmd);
  258. int ret = 0;
  259. m_SCF.Lock(msTimeOut_Lock)
  260. .SendPacket(cCmd, nCmdSize, nTimeout, ret);
  261. Sleep(nTimeout);
  262. }
  263. //-----------------------------------------------------------------------------
  264. // SyncBoxDriver
  265. //-----------------------------------------------------------------------------
  266. nsSYN::SyncBoxDriver::SyncBoxDriver()
  267. {
  268. m_bConnected = false;
  269. m_pAttribute.reset(new ResDataObject());
  270. m_pDescription.reset(new ResDataObject());
  271. }
  272. nsSYN::SyncBoxDriver::~SyncBoxDriver()
  273. {
  274. mLog::Close();
  275. mLog::gLogger = nullptr;
  276. }
  277. auto nsSYN::SyncBoxDriver::CreateDevice(int index) -> std::unique_ptr <IODevice>
  278. {
  279. mLog::FINFO("CreateDevice: index ={$}", index);
  280. if (!m_SCF.isConnected())
  281. {
  282. mLog::FERROR("CreateDevice: but scf no connect");
  283. return nullptr;
  284. }
  285. #if 0 //测试时不需要强校验握手
  286. if (!m_bConnected)
  287. {
  288. mLog::FERROR("CreateDevice: not recv Hand Shake");
  289. return nullptr;
  290. }
  291. #endif // 0
  292. if (index == 0)
  293. {
  294. m_pDYNDevice = new SyncBoxDevice(EventCenter, m_SCF, m_ConfigFileName);
  295. auto dev = std::unique_ptr <IODevice>(new IODevice(m_pDYNDevice));
  296. ResDataObject r_config;
  297. return dev;
  298. }
  299. return nullptr;
  300. }
  301. void nsSYN::SyncBoxDriver::FireNotify(int code, std::string key, std::string content)
  302. {
  303. EventCenter->OnNotify(code, key, content);
  304. }
  305. bool SyncBoxDriver::DriverEntry(std::string CfgFileName)
  306. {
  307. m_ConfigFileName = CfgFileName;
  308. mLog::FINFO("ConfigFileName {$}", m_ConfigFileName);
  309. return true;
  310. }
  311. void nsSYN::SyncBoxDriver::Prepare()
  312. {
  313. string strLogPath = GetProcessDirectory() + R"(\OEMDrivers\SyncBox\Conf\Log4CPP.Config.SYN.xml)";
  314. mLog::FINFO("strLogPath {$}", strLogPath);
  315. Log4CPP::GlobalContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "SYN.FULLUCB");
  316. auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str());
  317. if (!rc)
  318. {
  319. printf("Load log configfile failed!\n");
  320. }
  321. mLog::gLogger = Log4CPP::LogManager::GetLogger("SYN.FULLUCB");
  322. m_SCFDllName = GetConnectDLL(m_ConfigFileName);
  323. super::Prepare();
  324. }
  325. bool nsSYN::SyncBoxDriver::Connect()
  326. {
  327. ResDataObject Connection = GetConnectParam(m_ConfigFileName);
  328. mLog::FINFO("connections:{$} \n", Connection.encode());
  329. auto erCode = m_SCF.Connect(Connection.encode(), &nsSYN::SyncBoxDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000);
  330. if (erCode != SCF_ERR::SCF_SUCCEED)
  331. {
  332. mLog::FERROR("scf connect failed");
  333. return false;
  334. }
  335. mLog::FINFO("Connect: make success");
  336. Sleep(200);
  337. auto rc = super::Connect();
  338. if (!rc) return false;
  339. HandshakeTest();
  340. Sleep(1000);
  341. mLog::FINFO("Connect: scf ok");
  342. return true;
  343. }
  344. bool nsSYN::SyncBoxDriver::ReConnection(nsSCF::SCF& DevSCF)
  345. {
  346. super::Disconnect();
  347. mLog::FINFO("ReConnection:SCF Disconnect");
  348. ResDataObject Connection = GetConnectParam(m_ConfigFileName);
  349. mLog::FINFO("ReConnection:{$}", Connection.encode());
  350. auto erCode = m_SCF.Connect(Connection.encode(), &nsSYN::SyncBoxDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000);
  351. if (erCode == SCF_ERR::SCF_SUCCEED)
  352. {
  353. Sleep(1000);
  354. auto rc = super::Connect();
  355. if (!rc)
  356. {
  357. mLog::FINFO("ReConnection:super Connect failed");
  358. }
  359. else
  360. {
  361. DevSCF = m_SCF;
  362. return true;
  363. }
  364. }
  365. else
  366. {
  367. mLog::FINFO("ReConnection failed");
  368. }
  369. return false;
  370. }
  371. void nsSYN::SyncBoxDriver::Disconnect()
  372. {
  373. super::Disconnect();
  374. m_SCF.Disconnect();
  375. m_pDYNDevice = nullptr;
  376. m_bConnected = false;
  377. mLog::FINFO("Disconnect ok");
  378. }
  379. bool nsSYN::SyncBoxDriver::isConnected() const
  380. {
  381. mLog::FINFO("isConnected");
  382. if (super::isConnected())
  383. {
  384. return true;
  385. }
  386. else
  387. {
  388. return false;
  389. }
  390. }
  391. void nsSYN::SyncBoxDriver::Dequeue(const char* Packet, DWORD Length)
  392. {
  393. if (!m_bConnected)
  394. {
  395. if (strncmp(Packet,":SEC",4) == 0)
  396. {
  397. m_bConnected = true;
  398. mLog::FDEBUG("Dequeue:get Hand Shake");
  399. }
  400. }
  401. #if 0
  402. if (strncmp(Packet, "EC", 2) == 0)
  403. mLog::FDEBUG("Dequeue[EC],arFrameLengh[{$}]", arFrame.size());
  404. #endif
  405. DecodeFrame(Packet, Length);
  406. }
  407. PACKET_RET nsSYN::SyncBoxDriver::callbackPackageProcess(const char* RecData, DWORD nLength, DWORD& PacketLength)
  408. {
  409. //判断是否是整包
  410. /*
  411. 指令编码格式:
  412. +命令头(0x3A)
  413. +命令对象(S表示同步盒,P表示电脑,其他的的再定)
  414. +命令字节(由2个字节组合形成)
  415. +数据格式(‘A’表示ASIIC码,‘B’表示二进制码,‘C’表示混合或其他模式)
  416. +数据长度(由2个字节组合,数据区可以(0~255))
  417. +数据区(根据数据长度发送的结果或数据)
  418. +校验和(由2个字节组成,详见注1)
  419. +结束位1(0x0D)+结束位2(0x0A)
  420. */
  421. #if 0
  422. char tempData[3]{ 0 };
  423. tempData[0] = RecData[0];
  424. if (nLength >= 2)
  425. tempData[1] = RecData[1];
  426. if (strncmp(tempData, "EC", 2) == 0)
  427. mLog::FDEBUG("RecData[{$}],lengh[{$}]", tempData, nLength);
  428. #endif
  429. int bHasHead = 0;
  430. if (nLength < 3)
  431. {
  432. //mLog::FERROR ("nLength[{$}] < 3", nLength);
  433. PacketLength = 0;
  434. return PACKET_NOPACKET;
  435. }
  436. else if (nLength >= FullUCB_Com_NormalLen)
  437. {
  438. mLog::FERROR("nLength[{$}] > [{$}] ", nLength, FullUCB_Com_NormalLen);
  439. PacketLength = nLength;
  440. return PACKET_USELESS;
  441. }
  442. for (DWORD i = 0; i < nLength - 1; i++)
  443. {
  444. //寻找包头
  445. if (RecData[i] == FullUCB_Com_STX)
  446. {
  447. if (i != 0) //包头之前的数据格式不对,全部扔掉
  448. {
  449. PacketLength = i;
  450. char strtemp[FullUCB_Com_NormalLen] = { 0 };
  451. memcpy(strtemp, RecData, PacketLength);
  452. mLog::FERROR("==IN unknown format data ==:[{$}],UselessDataLength={$},TotalLength={$} \n", strtemp, PacketLength, nLength);
  453. return PACKET_USELESS;
  454. }
  455. else
  456. {
  457. if (RecData[i] == FullUCB_Com_STX)
  458. {
  459. bHasHead = 1;
  460. }
  461. }
  462. }
  463. //寻找包尾
  464. if (RecData[i] == FullUCB_Com_ETX && RecData[i + 1] == FullUCB_Com_EOT)
  465. {
  466. PacketLength = i + 2;
  467. char strtemp[FullUCB_Com_NormalLen] = { 0 };
  468. memcpy(strtemp, RecData + 1, i - 1);
  469. strtemp[PacketLength + 1] = 0;
  470. if (1 == bHasHead)
  471. {
  472. if (strncmp(strtemp, "SWS", 3) == 0 && strtemp[6] == 'O')
  473. {
  474. mLog::FINFO("==IN==: [{$}] not use", strtemp);
  475. #if !COM_Deliver
  476. CheckWaitState(RecData);
  477. #endif
  478. return PACKET_USELESS;
  479. }
  480. else
  481. {
  482. mLog::FINFO("==IN==: [{$}]", strtemp);
  483. #if !COM_Deliver
  484. CheckWaitState(RecData);
  485. #endif
  486. return PACKET_ISPACKET;
  487. }
  488. }
  489. else
  490. {
  491. mLog::FERROR("==IN==:cmd[{$}][{$}] useless", PacketLength, RecData);
  492. return PACKET_USELESS;
  493. }
  494. }
  495. }
  496. if (bHasHead)
  497. {
  498. PacketLength = 0;
  499. }
  500. return PACKET_NOPACKET;
  501. }
  502. //Web config 专用
  503. bool nsSYN::SyncBoxDriver::GetDeviceConfig(std::string& Cfg)
  504. {
  505. Cfg = m_DeviceConfig.encode();
  506. mLog::FINFO("Cfg {$}", Cfg);
  507. return true;
  508. }
  509. bool nsSYN::SyncBoxDriver::SetDeviceConfig(std::string Cfg)
  510. {
  511. mLog::FINFO("--Func-- SetDeviceConfig {$}\n", Cfg.c_str());
  512. ResDataObject DeviceConfig;
  513. DeviceConfig.decode(Cfg.c_str());
  514. ResDataObject DescriptionTempEx;
  515. DescriptionTempEx = DeviceConfig["DeviceConfig"]["Attribute"];
  516. mLog::FDEBUG("Attribute:{$}", DescriptionTempEx.encode());
  517. bool bSaveFile = false; //true:重新保存配置文件
  518. string strAccess = "";
  519. for (int i = 0; i < DescriptionTempEx.size(); i++)
  520. {
  521. string strKey = DescriptionTempEx.GetKey(i);
  522. mLog::FINFO("{$}", strKey.c_str());
  523. try
  524. {
  525. if (m_pAttribute->GetFirstOf(strKey.c_str()) >= 0)
  526. {
  527. strAccess = (string)(*m_pDescription)[strKey.c_str()]["Access"];
  528. if ("RW" == strAccess)
  529. {
  530. //修改对应配置,在其他单元的配置项要同时调用其修改函数修改真实值
  531. //1. 修改内存中的值,用于给上层发消息
  532. (*m_pAttribute)[strKey.c_str()] = DescriptionTempEx[i];
  533. //2. 拿到Innerkey
  534. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  535. mLog::FINFO("nConfigInfoCount {$}", nConfigInfoCount);
  536. string strTemp = ""; //存储AttributeKey
  537. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  538. {
  539. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  540. if (strTemp == strKey)
  541. {
  542. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  543. break;
  544. }
  545. }
  546. //3. 修改配置文件中的值
  547. if (SetDeviceConfigValue(m_Configurations, strTemp.c_str(), 1, DescriptionTempEx[i]))
  548. {
  549. mLog::FDEBUG("SetDeviceConfigValue over");
  550. bSaveFile = true;
  551. }
  552. }
  553. else
  554. {
  555. mLog::FINFO("{$} is not a RW configuration item", strKey.c_str());
  556. }
  557. }
  558. else
  559. {
  560. mLog::FINFO("without this attribute {$}", strKey.c_str());
  561. }
  562. }
  563. catch (ResDataObjectExption& e)
  564. {
  565. mLog::FERROR("SetDriverConfig crashed: {$}", e.what());
  566. return false;
  567. }
  568. }
  569. if (bSaveFile)
  570. {
  571. //3. 重新保存配置文件
  572. SaveConfigFile(true);
  573. }
  574. return true;
  575. }
  576. bool nsSYN::SyncBoxDriver::SaveConfigFile(bool bSendNotify)
  577. {
  578. m_ConfigAll["CONFIGURATION"] = m_Configurations;
  579. bool bRt = m_ConfigAll.SaveFile(m_ConfigFileName.c_str());
  580. mLog::FINFO("SaveConfigFile over {$}", bRt);
  581. return true;
  582. }
  583. bool nsSYN::SyncBoxDriver::GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue)
  584. {
  585. strValue = "";
  586. string strTemp = pInnerKey;
  587. if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取
  588. {
  589. int pos = 0;
  590. ResDataObject resTemp = config;
  591. while ((pos = strTemp.find_first_of(',')) != string::npos)
  592. {
  593. string Key = strTemp.substr(0, pos);
  594. string TempValue = resTemp[Key.c_str()].encode();
  595. resTemp.clear();
  596. resTemp.decode(TempValue.c_str());
  597. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  598. }
  599. if (strTemp != "")
  600. {
  601. strValue = (string)resTemp[strTemp.c_str()];
  602. }
  603. else
  604. {
  605. strValue = (string)resTemp;
  606. }
  607. }
  608. return true;
  609. }
  610. bool nsSYN::SyncBoxDriver::SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue)
  611. {
  612. string strTemp = pInnerKey;
  613. mLog::FDEBUG("Begin to change {$} item value to {$}", pInnerKey, szValue);
  614. if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取
  615. {
  616. try {
  617. int pos = 0;
  618. ResDataObject* resTemp = &config;
  619. while ((pos = strTemp.find_first_of(',')) != string::npos)
  620. {
  621. string Key = strTemp.substr(0, pos);
  622. resTemp = &(*resTemp)[Key.c_str()];
  623. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  624. }
  625. if (strTemp != "")
  626. {
  627. (*resTemp)[strTemp.c_str()] = szValue;
  628. }
  629. else
  630. {
  631. *resTemp = szValue;
  632. }
  633. }
  634. catch (ResDataObjectExption& e)
  635. {
  636. mLog::FERROR("SetDriverConfigvalue crashed: {$}", e.what());
  637. return false;
  638. }
  639. }
  640. return true;
  641. }
  642. std::string nsSYN::SyncBoxDriver::GetResource()
  643. {
  644. ResDataObject r_config, temp;
  645. if (!temp.loadFile(m_ConfigFileName.c_str()))
  646. {
  647. return "";
  648. }
  649. m_ConfigAll = temp;
  650. r_config = temp["CONFIGURATION"];
  651. m_Configurations = r_config;
  652. ResDataObject DescriptionTemp;
  653. ResDataObject ListTemp;
  654. string strTemp = ""; //用于读取字符串配置信息
  655. string strIndex = ""; //用于读取配置信息中的List项
  656. int nTemp = -1; //用于读取整型配置信息
  657. char sstream[10] = { 0 }; //用于转换值
  658. string strValue = ""; //用于存储配置的值
  659. string strType = ""; //用于存储配置的类型 int/float/string...
  660. /***
  661. * 1. 通过循环,将所有配置项写到pDeviceConfig
  662. * 2. 记录配置项的内部key以及配置类型,类型对应了不同配置文件路径,用于读写真实值
  663. ***/
  664. try
  665. {
  666. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  667. //mLog::FINFO(g_pFPDCtrlLog, "ConfigInfo Count: {$}", nConfigInfoCount);
  668. m_pAttribute->clear();
  669. m_pDescription->clear();
  670. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  671. {
  672. DescriptionTemp.clear();
  673. ListTemp.clear();
  674. //AttributeType
  675. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Type"];
  676. DescriptionTemp.add(ConfKey::AttributeType, strTemp.c_str());
  677. //DescriptionTemp= m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"];
  678. //mLog::FINFO(g_pFPDCtrlLog, "--> {$}: {$}", AttributeType, strTemp.c_str());
  679. strType = strTemp; //记录配置项的类型
  680. //AttributeKey
  681. //1. 根据AttributeType,内部key和配置路径,拿到当前的真实值
  682. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  683. nTemp = (int)m_Configurations["ConfigToolInfo"][nInfoIndex]["PathID"];
  684. GetDeviceConfigValue(r_config, strTemp.c_str(), nTemp, strValue);
  685. //2. 赋值
  686. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  687. if ("int" == strType)
  688. {
  689. (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str()));
  690. //mLog::FINFO(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), atoi(strValue.c_str()));
  691. }
  692. else if ("float" == strType)
  693. {
  694. (*m_pAttribute).add(strTemp.c_str(), atof(strValue.c_str()));
  695. //mLog::FINFO(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), atof(strValue.c_str()));
  696. }
  697. else //其它先按string类型处理
  698. {
  699. (*m_pAttribute).add(strTemp.c_str(), strValue.c_str());
  700. //mLog::FINFO(g_pFPDCtrlLog, "Key {$}: {$}", strTemp.c_str(), strValue.c_str());
  701. }
  702. //AttributeAccess
  703. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Access"];
  704. DescriptionTemp.add(ConfKey::AttributeAccess, strTemp.c_str());
  705. //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeAccess, strTemp.c_str());
  706. //AttributeRangeMin
  707. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMin"];
  708. if (strTemp != "") //不需要的配置项为空
  709. {
  710. DescriptionTemp.add(ConfKey::AttributeRangeMin, strTemp.c_str());
  711. //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeRangeMin, strTemp.c_str());
  712. }
  713. //AttributeRangeMax
  714. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMax"];
  715. if (strTemp != "") //不需要的配置项为空
  716. {
  717. DescriptionTemp.add(ConfKey::AttributeRangeMax, strTemp.c_str());
  718. //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeRangeMax, strTemp.c_str());
  719. }
  720. //AttributeList
  721. nTemp = m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListNum"];
  722. if (nTemp > 0) //ListNum不大于0时说明不需要list配置
  723. {
  724. for (int nListIndex = 0; nListIndex < nTemp; nListIndex++)
  725. {
  726. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListInfo"][nListIndex];
  727. //sprintf_s(sstream, "{$}", nListIndex);
  728. auto temKey = std::to_string(nListIndex);
  729. ListTemp.add(temKey.c_str(), strTemp.c_str());
  730. //mLog::FINFO(g_pFPDCtrlLog, "list {$}: {$}", nListIndex, strTemp.c_str());
  731. }
  732. DescriptionTemp.add(ConfKey::AttributeList, ListTemp.encode());
  733. }
  734. //AttributeRequired
  735. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Required"];
  736. DescriptionTemp.add(ConfKey::AttributeRequired, strTemp.c_str());
  737. //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeRequired, strTemp.c_str());
  738. //AttributeDefaultValue
  739. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["DefaultValue"];
  740. if (strTemp != "") //不需要的配置项为空
  741. {
  742. DescriptionTemp.add(ConfKey::AttributeDefaultValue, strTemp.c_str());
  743. //mLog::FINFO(g_pFPDCtrlLog, "{$}: {$}", AttributeDefaultValue, strTemp.c_str());
  744. }
  745. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  746. (*m_pDescription).add(strTemp.c_str(), DescriptionTemp);
  747. }
  748. }
  749. catch (ResDataObjectExption& e)
  750. {
  751. mLog::FERROR("Get config error: {$}", e.what());
  752. return "";
  753. }
  754. ResDataObject resDeviceResource;
  755. resDeviceResource.add(ConfKey::DiosAttribute, (*m_pAttribute));
  756. resDeviceResource.add(ConfKey::DiosDescription, (*m_pDescription));
  757. ResDataObject DescriptionTempEx;
  758. DescriptionTempEx.add(ConfKey::DiosConfig, resDeviceResource);
  759. m_DeviceConfig.clear();
  760. m_DeviceConfig = DescriptionTempEx;
  761. string res = DescriptionTempEx.encode();
  762. mLog::FDEBUG("get resource over {$}", DescriptionTempEx.encode());
  763. return res;
  764. }
  765. std::string nsSYN::SyncBoxDriver::DeviceProbe()
  766. {
  767. ResDataObject r_config, HardwareInfo;
  768. if (r_config.loadFile(m_ConfigFileName.c_str()))
  769. {
  770. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  771. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  772. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  773. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  774. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  775. }
  776. else
  777. {
  778. HardwareInfo.add("MajorID", "SYN");
  779. HardwareInfo.add("MinorID", "Dr");
  780. HardwareInfo.add("VendorID", "ECOM");
  781. HardwareInfo.add("ProductID", "FULLUCB");
  782. HardwareInfo.add("SerialID", "1234");
  783. }
  784. string ret = HardwareInfo.encode();
  785. return ret;
  786. }
  787. std::string nsSYN::SyncBoxDriver::DriverProbe()
  788. {
  789. ResDataObject r_config, HardwareInfo;
  790. if (r_config.loadFile(m_ConfigFileName.c_str()))
  791. {
  792. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  793. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  794. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  795. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  796. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  797. }
  798. else
  799. {
  800. HardwareInfo.add("MajorID", "SYN");
  801. HardwareInfo.add("MinorID", "Dr");
  802. HardwareInfo.add("VendorID", "ECOM");
  803. HardwareInfo.add("ProductID", "FULLUCB");
  804. HardwareInfo.add("SerialID", "1234");
  805. }
  806. string ret = HardwareInfo.encode();
  807. return ret;
  808. }
  809. //-----------------------------------------------------------------------------
  810. // GetIODriver & CreateIODriver
  811. //-----------------------------------------------------------------------------
  812. static nsSYN::SyncBoxDriver gIODriver;
  813. extern "C" DIOS::Dev::IODriver * __cdecl GetIODriver () // 返回静态对象的引用, 调用者不能删除 !
  814. {
  815. return &gIODriver;
  816. }
  817. extern "C" DIOS::Dev::IODriver * __cdecl CreateIODriver () // 返回新对象, 调用者必须自行删除此对象 !
  818. {
  819. return new nsSYN::SyncBoxDriver ();
  820. }