DIOS.Dev.Mech.HanQingNormal.cpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213
  1. #include "stdafx.h"
  2. #include <assert.h>
  3. #include <cmath>
  4. #include "Helper.JSON.hpp"
  5. #include "DIOS.Dev.Mech.HanQingNormal.hpp"
  6. #include "PacketAnalizer.h"
  7. #include "Generator.BasicMoulds.hpp"
  8. #define MY_PI 3.14159265358979323846
  9. using namespace DIOS::Dev::Detail::MECH;
  10. namespace nsMech = DIOS::Dev::Detail::MECH;
  11. namespace nsGEN = DIOS::Dev::Detail::Generator;
  12. //关闭无关警告
  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. #pragma warning (disable:4805) // warning C4805: “!=”: 在操作中将类型“bool”与类型“int”混合不安全
  17. //设置相关常量
  18. #define HanQingNormal_TIMEOUTVALUE 100
  19. #define HanQingNormal_LoopDefHBTime 500
  20. static const int msTimeOut_Lock = 200; //通讯接口锁定时间
  21. #define HanQingNormal_Com_NormalLen 128
  22. //#define HanQingNormal_STX 0x02
  23. #define HanQingNormal_ETX 0x03
  24. //DiosV3消息
  25. #define Dios_V3 1
  26. #define HanQingNormal_GenClientName "HanQing_Gen"
  27. #define HanQingNormal_ClientTimeOut 5000
  28. Log4CPP::Logger* mLog::gLogger = nullptr;
  29. static bool glo_StitchWarningFlag{ false };
  30. /*
  31. *串口/以太网通信帧格式:
  32. <Command><Data><ETX><Checksum>
  33. 解释:
  34. <Command> = 2 ASCII 命令名称
  35. <Data> = n ASCII 命令参数
  36. <ETX> = 1 ASCII 0x03 帧尾
  37. <CSUM> = 校验和
  38. 串口通信设置:
  39. . 9600 波特率
  40. . No Parity
  41. . 8 Data Bits
  42. . 1 Stop Bit
  43. . 无握手过程
  44. */
  45. //生成命令校验和:cmdData 为 <CMD> [<,> <ARG>]...<,> 其长度要多出3个字符用于存放 CSUM、ETX
  46. char ComputeCheckSum(char* cmdData, int& size)
  47. {
  48. if (size + 2 >= HanQingNormal_Com_NormalLen)
  49. {
  50. return 0x00;
  51. }
  52. cmdData[size++] = HanQingNormal_ETX;
  53. int AsciiSum = 0;
  54. for (int i = 0; i < size; i++) //每字节相加
  55. {
  56. AsciiSum += cmdData[i];
  57. }
  58. cmdData[size++] = (char)AsciiSum;
  59. //mLog::Debug("ComputeCheckSum:[0x{$:x}]", AsciiSum);
  60. return (char)AsciiSum;
  61. }
  62. //指令操作关联结构
  63. nsMech::tFrameMapItem::tFrameMapItem()
  64. {
  65. m_fFun = NULL;
  66. }
  67. nsMech::tFrameMapItem::tFrameMapItem(cbFun f)
  68. {
  69. m_fFun = f;
  70. }
  71. nsMech::tFrameMapItem& nsMech::tFrameMapItem::operator =(const tFrameMapItem& value)
  72. {
  73. m_fFun = value.m_fFun;
  74. return *this;
  75. }
  76. //响应操作对照表
  77. auto cmdCmpFun = [](const string& a, const string& b) {
  78. int minLengh = a.length() < b.length() ? a.length() : b.length();
  79. return a.compare(0, minLengh, b, 0, minLengh) < 0;
  80. };
  81. static std::map <string, tFrameMapItem, decltype(cmdCmpFun)> arFrame(cmdCmpFun);
  82. //查找响应操作对照表执行对应操作
  83. static char CMDData[HanQingNormal_Com_NormalLen] = { 0 };
  84. static bool DecodeFrame(const char* strFrame, int length)
  85. {
  86. auto found = arFrame.find(strFrame);//此处pr用来在arFrame中找到对于的包头
  87. if (found == arFrame.end())
  88. {
  89. return false;
  90. }
  91. string
  92. memset(CMDData, 0, HanQingNormal_Com_NormalLen - 1);
  93. int cmdHead = found->first.length();
  94. memcpy(CMDData, strFrame + cmdHead, length - cmdHead - 2);
  95. found->second.m_fFun(CMDData, length - cmdHead);//第二个参数 不重要
  96. memcpy(CMDData, strFrame, cmdHead);
  97. arFrame["FFF"].m_fFun(CMDData,3);
  98. return true;
  99. }
  100. //指令拆分
  101. void StringSpliTokeyValue(string& strdata, const char* delimiter, string& key, string& value)
  102. {
  103. std::size_t findPos = strdata.find(delimiter);
  104. if (findPos != string::npos)
  105. {
  106. key = strdata.substr(0, findPos);
  107. value = strdata.substr(findPos + 1, strdata.length() - findPos);
  108. }
  109. }
  110. void DataSpliToMap(const char* strdata, const char* delimiter, map<string, string>& arry)
  111. {
  112. string strtemp = strdata;
  113. string strName = strtemp;
  114. std::size_t oldPos = 0;
  115. std::size_t findPos = strtemp.find(delimiter);
  116. while (findPos != string::npos)
  117. {
  118. strName = strtemp.substr(oldPos, findPos - oldPos);
  119. {
  120. string key, value;
  121. StringSpliTokeyValue(strName, ":", key, value);
  122. arry[key] = value;
  123. }
  124. if (findPos != strtemp.length())
  125. {
  126. oldPos = findPos + 1;
  127. findPos = strtemp.find(delimiter, oldPos);
  128. if (findPos == string::npos)
  129. {
  130. strName = strtemp.substr(oldPos, strtemp.length() - oldPos);
  131. break;
  132. }
  133. }
  134. else
  135. {
  136. break;
  137. }
  138. }
  139. strName.erase(strName.length()-2);
  140. {
  141. string key, value;
  142. StringSpliTokeyValue(strName, ":", key, value);
  143. arry[key] = value;
  144. }
  145. }
  146. //-----------------------------------------------------------------------------
  147. // HanQingNormalDevice
  148. //-----------------------------------------------------------------------------
  149. int nsMech::HanQingNormalDevice::m_iLoopTime = HanQingNormal_LoopDefHBTime;
  150. nsMech::HanQingNormalDevice::HanQingNormalDevice(std::shared_ptr <IOEventCenter> center, nsSCF::SCF SCF, string configfile /*= ""*/):super(center, SCF)
  151. {
  152. assert(EventCenter);
  153. string version;
  154. if (GetVersion(version, hMyModule))
  155. mLog::Debug("\n===============log begin : version:{$} ===================\n", version.c_str());
  156. else
  157. mLog::Debug("\n===============log begin : version:0.0.0.0 ===================\n");
  158. ResDataObject Conftemp;
  159. if (!configfile.empty())
  160. {
  161. Conftemp.loadFile(configfile.c_str());
  162. m_MechConfig = Conftemp["CONFIGURATION"];
  163. TransJsonText(m_MechConfig);
  164. }
  165. //Mechanical
  166. m_MECHUnit.m_MechState.reset(new MechStateMould(AttrKey::MACHINE_STATUS_STANDBY, AttrKey::MACHINE_STATUS_SHUTDOWN, AttrKey::MACHINE_STATUS_SERVICE, 1));
  167. m_MECHUnit.m_SID.reset(new SIDMould(123.5, 0, 10000, 1));
  168. m_MECHUnit.m_TubeAngle.reset(new TubeAngleMould(0, -120, 120, 1));
  169. float minHeight = 64.4, maxHeight = 170.2;//标装37.1~174.7,V2配置46.5~170.2,球管中心是最低64.4,最高210.9
  170. if (m_MechConfig.GetKeyCount("MechanicalHeight") > 0)
  171. {
  172. if (m_MechConfig["MechanicalHeight"].GetKeyCount("min") > 0)
  173. minHeight = m_MechConfig["MechanicalHeight"]["min"];
  174. if (m_MechConfig["MechanicalHeight"].GetKeyCount("max") > 0)
  175. maxHeight = m_MechConfig["MechanicalHeight"]["max"];
  176. mLog::Debug("config:MechanicalHeight[min:{$},max:{$}]", minHeight, maxHeight);
  177. }
  178. m_MECHUnit.m_TubeHeight.reset(new TubeHeightMould(0, minHeight, maxHeight, 1));
  179. m_MECHUnit.m_FID.reset(new FIDMould(0, minHeight, maxHeight, 1));
  180. //拼接
  181. m_MECHUnit.m_StitchingState.reset(new StitchingStateMould(AttrKey::STITCHSTATE_INIT, AttrKey::STITCHSTATE_ERR, AttrKey::STITCHSTATE_MAX, 1));
  182. m_MECHUnit.m_StitchHeight.reset(new StitchHeightMould(0, minHeight, maxHeight, 1));
  183. m_MECHUnit.m_StitchLength.reset(new StitchLengthMould(0, 0, 1000, 1));
  184. m_MECHUnit.m_StitchOverLap.reset(new StitchOverLapMould(0, 0, 1000, 1));
  185. m_MECHUnit.m_StitchPID.reset(new StitchPIDMould(0, 0, 1000, 1));
  186. m_MECHUnit.m_StitchDirection.reset(new StitchDirectionMould(AttrKey::STITCHDIR_NULL, AttrKey::STITCHDIR_NULL, AttrKey::STITCHDIR_MAX, 1));
  187. m_MECHUnit.m_StitchType.reset(new StitchTypeMould(AttrKey::STITCHTYPE_NULL, AttrKey::STITCHTYPE_NULL, AttrKey::STITCHTYPE_MAX, 1));
  188. m_MECHUnit.m_StitchStepLength.reset(new StitchStepLengthMould(10, 0, 1000, 1));
  189. m_MECHUnit.m_FPDPosition.reset(new FPDPositionMould(0, 0, 100, 1));
  190. m_MECHUnit.m_TotalImageCount.reset(new TotalImageCountMould(0, 0, 100, 1));
  191. m_MECHUnit.m_CurrentImageNumber.reset(new CurrentImageNumberMould(100, -1, 100, 1));
  192. if (m_MechConfig.GetKeyCount("FPDLengh") > 0)
  193. {
  194. m_nFPDLengh = m_MechConfig["FPDLengh"];
  195. mLog::Debug("config:FPDLengh[{$}]", m_nFPDLengh);
  196. }
  197. //上报错误消息
  198. m_MSGUnit.reset(new nsDetail::MSGUnit(center, MechUnitType));
  199. //串口数据处理类初始化
  200. m_tDelivermodule.InitSendModle(this ,&ProcessClientData, WriteLog);
  201. m_nCMDType_WaitTime = m_tDelivermodule.SetPriority(false, false, false, true, 100);
  202. m_nCMDType_WaitACK = m_tDelivermodule.SetPriority(false, false, 3, false, 0, true, 1000);
  203. mLog::Debug("m_nCMDType_WaitTime[{$}] m_nCMDType_WaitACK[{$}]", m_nCMDType_WaitTime, m_nCMDType_WaitACK);
  204. //将机架可以对外提供的指令注册集进行补充
  205. Register();
  206. //配置响应操作对照表
  207. OnCallBack();
  208. //设置初始参数
  209. RefreshData();
  210. m_hExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  211. m_hMoveBeginEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  212. //启动硬件状态轮询进程
  213. StartLoopThread();
  214. }
  215. nsMech::HanQingNormalDevice::~HanQingNormalDevice()
  216. {
  217. if (m_pGenClient)
  218. {
  219. if (!m_pGenClient->IsClosed())
  220. {
  221. m_pGenClient->Close();
  222. }
  223. delete m_pGenClient;
  224. m_pGenClient = nullptr;
  225. }
  226. ResetEvent(m_hMoveBeginEvent);
  227. SetEvent(m_hExitEvent);
  228. Sleep(m_iLoopTime);
  229. m_tDelivermodule.EixtSendModle();
  230. mLog::Debug("\n===============log end ===================\n");
  231. }
  232. std::string HanQingNormalDevice::GetGUID() const
  233. {
  234. mLog::Debug("\n===============GetGUID : {$} ===================\n", StitchMechUnitType);
  235. return StitchMechUnitType;
  236. }
  237. RET_STATUS nsMech::HanQingNormalDevice::RefreshData()
  238. {
  239. GetUnitStatus();
  240. #if Dios_V3
  241. m_pGenClient = new LogicClient(HanQingNormal_GenClientName, m_strCurTransaction, "NSQ", false);
  242. if (m_pGenClient->Open("DIOS/DEVICE/Generator", ACTION + NOTIFY_MSG + NOTIFY_UPDATE,"", HanQingNormal_ClientTimeOut))
  243. {
  244. mLog::Debug("Dios_V3 Create {$} Client success", HanQingNormal_GenClientName);
  245. m_pGenClient->SubScribeTopic("DIOS/DEVICE/Generator/+/+/+/Notify/KV");
  246. m_pGenClient->SubScribeTopic("DIOS/DEVICE/Generator/+/+/+/Notify/MA");
  247. m_pGenClient->SubScribeTopic("DIOS/DEVICE/Generator/+/+/+/Notify/MS");
  248. m_pGenClient->SubScribeTopic("DIOS/DEVICE/Generator/+/+/+/Notify/MAS");
  249. m_pGenClient->SubScribeTopic("DIOS/DEVICE/Generator/+/+/+/Notify/FOCUS");
  250. m_pGenClient->SubScribeTopic("DIOS/DEVICE/Generator/+/+/+/Notify/TECHMODE");
  251. m_pGenClient->SubScribeTopic("DIOS/DEVICE/Generator/+/+/+/Notify/AECDENSITY");
  252. m_pGenClient->SubScribeTopic("DIOS/DEVICE/Generator/+/+/+/Notify/AECFIELD");
  253. m_pGenClient->SubScribeTopic("DIOS/DEVICE/Generator/+/+/+/Notify/AECFILM");
  254. }
  255. else
  256. {
  257. mLog::Debug("Dios_V3 Create {$} Client failed", HanQingNormal_GenClientName);
  258. }
  259. #endif
  260. return RET_STATUS::RET_SUCCEED;
  261. }
  262. void nsMech::HanQingNormalDevice::Register()
  263. {
  264. auto Disp = &Dispatch;
  265. superMech::RegisterNormalControl(Disp);
  266. superMech::RegisterStitchingControl(Disp);
  267. superMech::RegisterExtraScreenControl(Disp);
  268. superMech::RegisterExamInfo(Disp);
  269. Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; });
  270. auto fun_SendStitchState = [this](auto in, auto& out)
  271. {
  272. FireNotify(AttrKey::STITCHINGSTATE, m_MECHUnit.m_StitchingState->Get());
  273. return RET_STATUS::RET_SUCCEED;
  274. };
  275. Disp->Action.Push("SendStitchState", fun_SendStitchState);
  276. }
  277. void nsMech::HanQingNormalDevice::FireNotify(std::string key, std::string content)
  278. {
  279. EventCenter->OnNotify(1, key, content);
  280. }
  281. void nsMech::HanQingNormalDevice::FireNotify(std::string key, const int content)
  282. {
  283. std::string str = std::format("{:d}", content);
  284. EventCenter->OnNotify(1, key, str);
  285. }
  286. void nsMech::HanQingNormalDevice::FireNotify(std::string key, const float content)
  287. {
  288. std::string str = std::format("{:f}", content);
  289. EventCenter->OnNotify(1, key, str);
  290. }
  291. bool nsMech::HanQingNormalDevice::HasErrorMessage()
  292. {
  293. if (!m_MSGUnit->JSGet().empty())
  294. {
  295. return true;
  296. }
  297. else if (glo_StitchWarningFlag)
  298. {
  299. return true;
  300. }
  301. return false;
  302. }
  303. void nsMech::HanQingNormalDevice::FireErrorMessage(const bool Act, const int Code, const char* ResInfo)
  304. {
  305. string ErrorCode("Mech_ERR_");
  306. ErrorCode += std::to_string(Code);
  307. int level = OTC_MG_REGULATION_LEVEL::REG_ERRO;
  308. if (Act)
  309. {
  310. mLog::Error("add {$}:{$}", ErrorCode.c_str(), ResInfo);
  311. m_MSGUnit->AddErrorMessage(ErrorCode.c_str(), level, ResInfo);
  312. }
  313. else
  314. {
  315. mLog::Error("del {$}:{$}", ErrorCode.c_str(), ResInfo);
  316. if (Code == 0)
  317. m_MSGUnit->DelErrorMessage("0", level, ResInfo);
  318. else
  319. m_MSGUnit->DelErrorMessage(ErrorCode.c_str(), level, ResInfo);
  320. }
  321. }
  322. void nsMech::HanQingNormalDevice::FireWarnMessage(const bool Act, const int Code, const char* ResInfo)
  323. {
  324. string ErrorCode("Mech_WAR_");
  325. ErrorCode += std::to_string(Code);
  326. int level = OTC_MG_REGULATION_LEVEL::REG_WARN;
  327. if (Act)
  328. {
  329. glo_StitchWarningFlag = true;
  330. mLog::Error("add {$}:{$}", ErrorCode.c_str(), ResInfo);
  331. m_MSGUnit->AddWarnMessage(ErrorCode.c_str(), level, ResInfo);
  332. }
  333. else
  334. {
  335. glo_StitchWarningFlag = false;
  336. mLog::Error("del {$}:{$}", ErrorCode.c_str(), ResInfo);
  337. m_MSGUnit->DelWarnMessage(ErrorCode.c_str(), level, ResInfo);
  338. }
  339. }
  340. RET_STATUS nsMech::HanQingNormalDevice::HWSend(const char* strCommand, int lengh, bool reSend, int nTimeOut)
  341. {
  342. if (!m_SCF)
  343. return RET_STATUS::RET_FAILED;
  344. char strSendCommand[HanQingNormal_Com_NormalLen] = { 0 };
  345. int copyCode = memcpy_s(strSendCommand, HanQingNormal_Com_NormalLen, strCommand, lengh);
  346. if (copyCode)
  347. {
  348. mLog::Error("copy cmd[{$}][{$}] error[{$}]", strCommand, lengh, copyCode);
  349. return RET_STATUS::RET_FAILED;
  350. }
  351. ComputeCheckSum(strSendCommand, lengh);
  352. if (strncmp(strSendCommand, "TT?", 3) != 0)
  353. {
  354. if (reSend)
  355. {
  356. mLog::Debug("==OUT==: [{$}] wait for ACK \n", strSendCommand);
  357. }
  358. else
  359. {
  360. mLog::Debug("==OUT==: [{$}] \n", strSendCommand);
  361. }
  362. }
  363. int retLength;
  364. m_SCF.Lock(msTimeOut_Lock)
  365. .SendPacket(strSendCommand, lengh, nTimeOut, retLength);
  366. //Sleep(nTimeOut);
  367. return RET_STATUS::RET_SUCCEED;
  368. }
  369. void HanQingNormalDevice::OnCallBack()
  370. {
  371. mLog::Debug("Enter OnCallBack");
  372. auto HWNotProcess = [this](const char* value, int length) -> void
  373. {
  374. //mLog::Warn(" This commands didn't need to process!");
  375. m_tDelivermodule.CheckReceive(value, length);
  376. };
  377. auto HW_WS = [this](const char* value, int length) -> void
  378. {
  379. if (m_pGenClient != NULL)
  380. {
  381. if (!m_pGenClient->IsClosed())
  382. {
  383. ResDataObject Request, Response;
  384. Request.add("P0", 0);
  385. m_pGenClient->Action(nsGEN::ActionKey::SetValue_WORKSTATION, Request, Response, HanQingNormal_ClientTimeOut);
  386. mLog::Debug("execute[{$}:{$}] to GEN", nsGEN::ActionKey::SetValue_WORKSTATION, 0);
  387. }
  388. else
  389. {
  390. mLog::Debug("{$} Client is Close", HanQingNormal_GenClientName);
  391. }
  392. }
  393. else
  394. {
  395. mLog::Debug("{$} Client is NULL", HanQingNormal_GenClientName);
  396. }
  397. };
  398. auto HW_KV = [this](const char* value, int length) -> void
  399. {
  400. if (m_pGenClient != NULL)
  401. {
  402. if (!m_pGenClient->IsClosed())
  403. {
  404. ResDataObject Request, Response;
  405. if (value[0] == '+')
  406. {
  407. m_pGenClient->Action(nsGEN::ActionKey::IncParam_KV, Request, Response, HanQingNormal_ClientTimeOut);
  408. mLog::Debug("execute[IncParam_KV] to GEN");
  409. }
  410. else if (value[0] == '-')
  411. {
  412. m_pGenClient->Action(nsGEN::ActionKey::DecParam_KV, Request, Response, HanQingNormal_ClientTimeOut);
  413. mLog::Debug("execute[DecParam_KV] to GEN");
  414. }
  415. }
  416. else
  417. {
  418. mLog::Debug("{$} Client is Close", HanQingNormal_GenClientName);
  419. }
  420. }
  421. else
  422. {
  423. mLog::Debug("{$} Client is NULL", HanQingNormal_GenClientName);
  424. }
  425. };
  426. auto HW_MA = [this](const char* value, int length) -> void
  427. {
  428. if (m_pGenClient != NULL)
  429. {
  430. if (!m_pGenClient->IsClosed())
  431. {
  432. ResDataObject Request, Response;
  433. if (value[0] == '+')
  434. {
  435. m_pGenClient->Action(nsGEN::ActionKey::IncParam_MA, Request, Response, HanQingNormal_ClientTimeOut);
  436. mLog::Debug("execute[IncParam_MA] to GEN");
  437. }
  438. else if (value[0] == '-')
  439. {
  440. m_pGenClient->Action(nsGEN::ActionKey::DecParam_MA, Request, Response, HanQingNormal_ClientTimeOut);
  441. mLog::Debug("execute[DecParam_MA] to GEN");
  442. }
  443. }
  444. else
  445. {
  446. mLog::Debug("{$} Client is Close", HanQingNormal_GenClientName);
  447. }
  448. }
  449. else
  450. {
  451. mLog::Debug("{$} Client is NULL", HanQingNormal_GenClientName);
  452. }
  453. };
  454. auto HW_MS = [this](const char* value, int length) -> void
  455. {
  456. if (m_pGenClient != NULL)
  457. {
  458. if (!m_pGenClient->IsClosed())
  459. {
  460. ResDataObject Request, Response;
  461. if (value[0] == '+')
  462. {
  463. m_pGenClient->Action(nsGEN::ActionKey::IncParam_MS, Request, Response, HanQingNormal_ClientTimeOut);
  464. mLog::Debug("execute[IncParam_MS] to GEN");
  465. }
  466. else if (value[0] == '-')
  467. {
  468. m_pGenClient->Action(nsGEN::ActionKey::DecParam_MS, Request, Response, HanQingNormal_ClientTimeOut);
  469. mLog::Debug("execute[DecParam_MS] to GEN");
  470. }
  471. }
  472. else
  473. {
  474. mLog::Debug("{$} Client is Close", HanQingNormal_GenClientName);
  475. }
  476. }
  477. else
  478. {
  479. mLog::Debug("{$} Client is NULL", HanQingNormal_GenClientName);
  480. }
  481. };
  482. auto HW_MAS = [this](const char* value, int length) -> void
  483. {
  484. if (m_pGenClient != NULL)
  485. {
  486. if (!m_pGenClient->IsClosed())
  487. {
  488. ResDataObject Request, Response;
  489. if (value[0] == '+')
  490. {
  491. m_pGenClient->Action(nsGEN::ActionKey::IncParam_MAS, Request, Response, HanQingNormal_ClientTimeOut);
  492. mLog::Debug("execute[IncParam_MAS] to GEN");
  493. }
  494. else if (value[0] == '-')
  495. {
  496. m_pGenClient->Action(nsGEN::ActionKey::DecParam_MAS, Request, Response, HanQingNormal_ClientTimeOut);
  497. mLog::Debug("execute[DecParam_MAS] to GEN");
  498. }
  499. }
  500. else
  501. {
  502. mLog::Debug("{$} Client is Close", HanQingNormal_GenClientName);
  503. }
  504. }
  505. else
  506. {
  507. mLog::Debug("{$} Client is NULL", HanQingNormal_GenClientName);
  508. }
  509. };
  510. auto HW_FO = [this](const char* value, int length) -> void
  511. {
  512. if (m_pGenClient != NULL)
  513. {
  514. if (!m_pGenClient->IsClosed())
  515. {
  516. ResDataObject Request, Response;
  517. Request.add("P0", value);
  518. m_pGenClient->Action(nsGEN::ActionKey::SetValue_FOCUS, Request, Response, HanQingNormal_ClientTimeOut);
  519. mLog::Debug("execute[{$}:{$}] to GEN", nsGEN::ActionKey::SetValue_FOCUS, value);
  520. }
  521. else
  522. {
  523. mLog::Debug("{$} Client is Close", HanQingNormal_GenClientName);
  524. }
  525. }
  526. else
  527. {
  528. mLog::Debug("{$} Client is NULL", HanQingNormal_GenClientName);
  529. }
  530. };
  531. auto HW_AECField = [this](const char* value, int length) -> void
  532. {
  533. if (m_pGenClient != NULL)
  534. {
  535. if (!m_pGenClient->IsClosed())
  536. {
  537. ResDataObject Request, Response;
  538. Request.add("P0", value);
  539. m_pGenClient->Action(nsGEN::ActionKey::SetValue_AECFIELD, Request, Response, HanQingNormal_ClientTimeOut);
  540. mLog::Debug("execute[{$}:{$}] to GEN", nsGEN::ActionKey::SetValue_AECFIELD, value);
  541. }
  542. else
  543. {
  544. mLog::Debug("{$} Client is Close", HanQingNormal_GenClientName);
  545. }
  546. }
  547. else
  548. {
  549. mLog::Debug("{$} Client is NULL", HanQingNormal_GenClientName);
  550. }
  551. };
  552. auto HW_AECDensity = [this](const char* value, int length) -> void
  553. {
  554. if (m_pGenClient != NULL)
  555. {
  556. if (!m_pGenClient->IsClosed())
  557. {
  558. ResDataObject Request, Response;
  559. Request.add("P0", value);
  560. m_pGenClient->Action(nsGEN::ActionKey::SetValue_AECDENSITY, Request, Response, HanQingNormal_ClientTimeOut);
  561. mLog::Debug("execute[{$}:{$}] to GEN", nsGEN::ActionKey::SetValue_AECDENSITY, value);
  562. }
  563. else
  564. {
  565. mLog::Debug("{$} Client is Close", HanQingNormal_GenClientName);
  566. }
  567. }
  568. else
  569. {
  570. mLog::Debug("{$} Client is NULL", HanQingNormal_GenClientName);
  571. }
  572. };
  573. auto HW_Techmode = [this](const char* value, int length) -> void
  574. {
  575. if (m_pGenClient != NULL)
  576. {
  577. if (!m_pGenClient->IsClosed())
  578. {
  579. ResDataObject Request, Response;
  580. Request.add("P0", value);
  581. m_pGenClient->Action(nsGEN::ActionKey::SetValue_TECHMODE, Request, Response, HanQingNormal_ClientTimeOut);
  582. mLog::Debug("execute[{$}:{$}] to GEN", nsGEN::ActionKey::SetValue_TECHMODE, value);
  583. //switch (atoi(value))
  584. //{
  585. // case 0://MA\MS
  586. // {
  587. // SetMA(m_fMA);
  588. // SetMS(m_fMS);
  589. // }break;
  590. // case 1://MAS
  591. // {
  592. // SetMAS(m_fMAS);
  593. // }break;
  594. // case 2://AEC
  595. // {
  596. // SetMA(m_fMA);
  597. // SetMS(m_fMS);
  598. // }break;
  599. //}
  600. }
  601. else
  602. {
  603. mLog::Debug("{$} Client is Close", HanQingNormal_GenClientName);
  604. }
  605. }
  606. else
  607. {
  608. mLog::Debug("{$} Client is NULL", HanQingNormal_GenClientName);
  609. }
  610. };
  611. auto HW_PatientSize = [this](const char* value, int length) -> void
  612. {
  613. string size{"Middle"};
  614. switch (atoi(value))
  615. {
  616. case 1:
  617. {size = "Small"; }break;
  618. case 2:
  619. {size = "Middle"; }break;
  620. case 3:
  621. {size = "Large"; }break;
  622. case 4:
  623. {size = "Child"; }break;
  624. default:
  625. break;
  626. }
  627. FireNotify(AttrKey::PATIENTSIZE, size);
  628. };
  629. auto HW_Error = [this](const char* value, int length) -> void
  630. {
  631. if(length <= 0)
  632. FireErrorMessage(true,1, value);
  633. else
  634. FireErrorMessage(false, 1, value);
  635. };
  636. auto HW_Status = [this](const char* value, int length) -> void
  637. {
  638. map<string, string> data;
  639. DataSpliToMap(value,"|", data);
  640. for (auto item : data)
  641. {
  642. if (item.first == "FP")
  643. {
  644. int data = atoi(item.second.c_str());
  645. if (m_MECHUnit.m_StitchingState->Get() == AttrKey::STITCHSTATE_REACH_INIT)
  646. {
  647. //给印度发货的机架不带红外定位,DellWork的带
  648. //if (data == 0)
  649. //{
  650. // if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_START))
  651. // FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_START);
  652. // m_MECHUnit.m_strInfo = "Stitch Start";
  653. // mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  654. // FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  655. //}
  656. //else
  657. //{
  658. // FireErrorMessage(true,3,"The movement of the mechanical system was interrupted, Please EXIT the current stitching and RE-START the stitching exam");
  659. //}
  660. }
  661. //else
  662. //{
  663. // mLog::Error("stitching:not in [Stitch Standby]");
  664. //}
  665. }
  666. else if (item.first == "FH")//球管的高度值
  667. {
  668. float data = atof(item.second.c_str()) / 10.0f;
  669. if (m_MECHUnit.m_TubeHeight->Update(data))
  670. {
  671. mLog::Debug("update TubeHeight[{$}]", data);
  672. FireNotify(AttrKey::TUBEHEIGHT, data);
  673. }
  674. }
  675. else if (item.first == "FG")//球管的角度
  676. {
  677. float data = atof(item.second.c_str());
  678. if (m_MECHUnit.m_TubeAngle->Update(data))
  679. {
  680. mLog::Debug("update TubeAngle[{$}]", data);
  681. FireNotify(AttrKey::TUBEANGLE, data);
  682. }
  683. }
  684. else if (item.first == "FX")//X轴的距离(SID的值)
  685. {
  686. float data = atof(item.second.c_str())/10.0f;
  687. if (m_MECHUnit.m_SID->Update(data))
  688. {
  689. mLog::Debug("update SID[{$}]", data);
  690. FireNotify(AttrKey::SID, data);
  691. }
  692. }
  693. else if (item.first == "FU")//胸片架Bucky的高度值
  694. {
  695. float data = atof(item.second.c_str()) / 10.0f;
  696. if (m_MECHUnit.m_FID->Update(data))
  697. {
  698. mLog::Debug("update FID[{$}]", data);
  699. FireNotify(AttrKey::FID, data);
  700. if (m_MECHUnit.m_CurrentImageNumber->Get() == 0)
  701. {
  702. mLog::Debug("Stitching Check: TubeHeight[current:{$},min:{$},max:{$}]",
  703. data, m_MECHUnit.m_StitchHeight->GetLimitMin(), m_MECHUnit.m_StitchHeight->GetLimitMax());
  704. if (m_MECHUnit.m_StitchDirection->Get() == AttrKey::STITCHDIR_TopToBottom)
  705. {
  706. if (m_MECHUnit.m_StitchLength->Get() > (data - m_MECHUnit.m_StitchHeight->GetLimitMin()))
  707. {
  708. mLog::Debug("StitchLength[{$}] > CurrentHeight[{$}]-MinHeight[{$}]",
  709. m_MECHUnit.m_StitchLength->Get(), data, m_MECHUnit.m_StitchHeight->GetLimitMin());
  710. FireWarnMessage(true, 4, "too low.raise the machine height");
  711. }
  712. else if (HasErrorMessage())
  713. {
  714. FireWarnMessage(false, 0, "clear all Mech error");
  715. }
  716. }
  717. else if (m_MECHUnit.m_StitchDirection->Get() == AttrKey::STITCHDIR_BottomToTop)
  718. {
  719. if (m_MECHUnit.m_StitchLength->Get() > (m_MECHUnit.m_StitchHeight->GetLimitMax() - data))
  720. {
  721. mLog::Debug("StitchLength[{$}] > MaxHeight[{$}]-CurrentHeight[{$}]",
  722. m_MECHUnit.m_StitchLength->Get(), m_MECHUnit.m_StitchHeight->GetLimitMax(), data);
  723. FireWarnMessage(true, 5, "too high,reduce the machine height");
  724. }
  725. else if (HasErrorMessage())
  726. {
  727. FireWarnMessage(false, 0, "clear all Mech error");
  728. }
  729. }
  730. }
  731. }
  732. }
  733. else if (item.first == "FV")//Y轴的距离
  734. {
  735. float data = atof(item.second.c_str()) / 10.0f;
  736. //wxx
  737. }
  738. }
  739. };
  740. auto HW_SM = [this](const char* value, int length) -> void
  741. {
  742. int status = m_MECHUnit.m_StitchingState->Get();
  743. if (status >= AttrKey::STITCHSTATE_INIT && status < AttrKey::STITCHSTATE_END)
  744. {
  745. int data = atoi(value);
  746. int status = -1;
  747. switch (data)
  748. {
  749. case 1: //启动运动
  750. {
  751. status = AttrKey::STITCHSTATE_MOVING;
  752. m_MECHUnit.m_strInfo = "Stitch Moving";
  753. }break;
  754. case 2: //停止完成
  755. {
  756. if (m_MECHUnit.m_CurrentImageNumber->Get() == 0)
  757. {
  758. if (!m_bIsBeginFlag)//用于拼接计划
  759. {
  760. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_STANDBY))
  761. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_STANDBY);
  762. m_MECHUnit.m_strInfo = "Stitch Standby";
  763. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  764. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  765. }
  766. else
  767. {
  768. switch (m_MECHUnit.m_StitchType->Get())
  769. {
  770. case AttrKey::STITCHTYPE_AutomaticTranslation:
  771. case AttrKey::STITCHTYPE_ManualTranslation:
  772. {}break;
  773. case AttrKey::STITCHTYPE_AutomaticAngle:
  774. case AttrKey::STITCHTYPE_ManualAngle:
  775. {
  776. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_READY))
  777. {
  778. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_READY);
  779. }
  780. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  781. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  782. }break;
  783. }
  784. }
  785. status = AttrKey::STITCHSTATE_REACH_INIT;
  786. m_MECHUnit.m_strInfo = "Stitch Reach to the initial position";
  787. }
  788. else
  789. {
  790. status = AttrKey::STITCHSTATE_REACH_NEXT;
  791. m_MECHUnit.m_strInfo = "Stitch Reach to the next position";
  792. }
  793. }break;
  794. case 3: //退出拼接模式
  795. {
  796. status = AttrKey::STITCHSTATE_CANCEL;
  797. m_MECHUnit.m_strInfo = "Stitch quit";
  798. }break;
  799. case 4: //运动超限停止
  800. {
  801. status = AttrKey::STITCHSTATE_ERR;
  802. m_MECHUnit.m_strInfo = "get error";
  803. FireErrorMessage(true, 2, "get Mech error");
  804. }break;
  805. default:
  806. break;
  807. }
  808. if(m_MECHUnit.m_StitchingState->Update(status))
  809. FireNotify(AttrKey::STITCHINGSTATE, status);
  810. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  811. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  812. if (data == 2)
  813. {
  814. if (m_MECHUnit.m_CurrentImageNumber->Get() != 0)
  815. {
  816. m_MECHUnit.m_strInfo = "Stitch Ready";
  817. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_READY))
  818. {
  819. Sleep(2000);
  820. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_READY);
  821. }
  822. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  823. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  824. }
  825. }
  826. }
  827. };
  828. // 有部分前缀是包含关系, 长的包含短的, 因此长的在前面, 短的在后面
  829. // Device 是个短寿命对象, 而 arFrame 是静态变量 !!!
  830. // 因此, 在添加到 arFrame 之前, 务必先清零 !!!
  831. arFrame.clear();
  832. arFrame["OR"] = tFrameMapItem(HW_WS);
  833. arFrame["KV"] = tFrameMapItem(HW_KV);
  834. arFrame["MA"] = tFrameMapItem(HW_MA);
  835. arFrame["MS"] = tFrameMapItem(HW_MS);
  836. arFrame["MX"] = tFrameMapItem(HW_MAS);
  837. arFrame["FO"] = tFrameMapItem(HW_FO);
  838. arFrame["FI"] = tFrameMapItem(HW_AECField);
  839. arFrame["FD"] = tFrameMapItem(HW_AECDensity);
  840. arFrame["ET"] = tFrameMapItem(HW_Techmode);
  841. arFrame["PS"] = tFrameMapItem(HW_PatientSize);
  842. arFrame["ER"] = tFrameMapItem(HW_Error);
  843. arFrame[">TT|"] = tFrameMapItem(HW_Status);
  844. arFrame["SM"] = tFrameMapItem(HW_SM);
  845. arFrame["FFF"] = tFrameMapItem(HWNotProcess);//命令格式:校验和错误
  846. }
  847. //串口数据处理类对外接口
  848. void __stdcall nsMech::HanQingNormalDevice::ProcessClientData(const char* pData, unsigned long nDataLength, void* lparam)
  849. {
  850. HanQingNormalDevice* pCurMech = (HanQingNormalDevice*)lparam;
  851. pCurMech->HWSend(pData, nDataLength);
  852. }
  853. void __stdcall nsMech::HanQingNormalDevice::WriteLog(const char* pData, nsSerialGPM::LOG_V2_LEVEL level)
  854. {
  855. switch (level)
  856. {
  857. case nsSerialGPM::LOG_V2_FATAL:
  858. case nsSerialGPM::LOG_V2_ERROR:
  859. mLog::Error(pData);
  860. break;
  861. case nsSerialGPM::LOG_V2_WARNING:
  862. mLog::Warn(pData);
  863. break;
  864. case nsSerialGPM::LOG_V2_DEBUG:
  865. mLog::Debug(pData);
  866. break;
  867. case nsSerialGPM::LOG_V2_INFO:
  868. mLog::Debug(pData);
  869. break;
  870. default:
  871. break;
  872. }
  873. }
  874. RET_STATUS nsMech::HanQingNormalDevice::HWSendWaittimeCMD(char* strCommand, int lengh, int headLengh)
  875. {
  876. return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_WaitTime, headLengh);
  877. }
  878. RET_STATUS nsMech::HanQingNormalDevice::HWSendWaitACKCMD(char* strCommand, int lengh, int headLengh)
  879. {
  880. return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_WaitACK, headLengh);
  881. }
  882. bool nsMech::HanQingNormalDevice::StartLoopThread()
  883. {
  884. mLog::Debug("Enter Start HardwareStatus Thread ");
  885. if (m_pHardwareStatusThread == NULL)
  886. {
  887. DWORD m_HardwareStatusID;
  888. m_pHardwareStatusThread = CreateThread(0, 0, HardwareStatusThread, this, 0, &m_HardwareStatusID);
  889. if (m_pHardwareStatusThread == NULL)
  890. {
  891. mLog::Error("Start HardwareStatus Thread Failed");
  892. return false;
  893. }
  894. }
  895. mLog::Debug("Enter Start GenStatus Thread ");
  896. if (m_pGenStatusThread == NULL)
  897. {
  898. DWORD m_GenStatusID;
  899. m_pGenStatusThread = CreateThread(0, 0, GenStatusThread, this, 0, &m_GenStatusID);
  900. if (m_pGenStatusThread == NULL)
  901. {
  902. mLog::Error("Start GenStatus Thread Failed");
  903. return false;
  904. }
  905. }
  906. return true;
  907. }
  908. DWORD nsMech::HanQingNormalDevice::HardwareStatusThread(LPVOID pParam)
  909. {
  910. HanQingNormalDevice* pCurMech = (HanQingNormalDevice*)pParam;
  911. if (pCurMech == NULL)
  912. {
  913. return false;
  914. }
  915. mLog::Debug("HardwareStatusThread start");
  916. if ((int)pCurMech->m_MechConfig["loopTime"] >= 0)
  917. {
  918. pCurMech->m_iLoopTime = (int)pCurMech->m_MechConfig["loopTime"];
  919. }
  920. mLog::Debug("loopTime = {$}", pCurMech->m_iLoopTime);
  921. while (pCurMech->m_iLoopTime)
  922. {
  923. DWORD dwResult = WaitForSingleObject(pCurMech->m_hExitEvent, pCurMech->m_iLoopTime);
  924. switch (dwResult)
  925. {
  926. case WAIT_OBJECT_0:
  927. {
  928. mLog::Debug("get exit event, HardwareStatusThread stop");
  929. return true;
  930. }break;
  931. case WAIT_TIMEOUT:
  932. {
  933. if (pCurMech->m_bLoopFlag.load())
  934. {
  935. pCurMech->GetUnitStatus();
  936. }
  937. }break;
  938. }
  939. }
  940. mLog::Debug("HardwareStatusThread stop");
  941. return true;
  942. }
  943. DWORD nsMech::HanQingNormalDevice::GenStatusThread(LPVOID pParam)
  944. {
  945. HanQingNormalDevice* pCurMech = (HanQingNormalDevice*)pParam;
  946. if (pCurMech == NULL)
  947. {
  948. return false;
  949. }
  950. mLog::Debug("GenStatusThread start");
  951. try {
  952. if (!pCurMech->m_pGenClient)
  953. {
  954. mLog::Error("Cannot find Notify Clinet[{$}]", HanQingNormal_GenClientName);
  955. return true;
  956. }
  957. vector<HANDLE> waitList; //事件等待列表
  958. waitList.push_back(pCurMech->m_hExitEvent);
  959. waitList.push_back(pCurMech->m_pGenClient->GetNotifyHandle());
  960. while (WaitForSingleObject(pCurMech->m_hExitEvent, 50) != WAIT_OBJECT_0)
  961. {
  962. DWORD wait_ret = WaitForMultipleObjects((DWORD)waitList.size(), waitList.data(), FALSE, INFINITE);
  963. switch (wait_ret)
  964. {
  965. case WAIT_TIMEOUT:
  966. {
  967. }break;
  968. case WAIT_OBJECT_0://exit thread
  969. {
  970. mLog::Debug("get exit event, GenStatusThread stop");
  971. return true;
  972. }break;
  973. case WAIT_OBJECT_0 + 1://pCurMech->m_pGenClient
  974. {
  975. while (pCurMech->m_pGenClient->IsDataArrived())
  976. {
  977. ResDataObject NotifyData;
  978. PACKET_CMD notifycmd = pCurMech->m_pGenClient->ReadNotify(NotifyData);
  979. switch (notifycmd)
  980. {
  981. case PACKET_CMD_OPEN:
  982. {}break;
  983. case PACKET_CMD_CLOSE:
  984. {}break;
  985. case PACKET_CMD_GET:
  986. {}break;
  987. case PACKET_CMD_UPDATE://属性更新
  988. {
  989. ResDataObject resDevicePath;
  990. pCurMech->m_pGenClient->GetFilePath(resDevicePath);
  991. string strPath = (string)resDevicePath;
  992. string strKey = PacketAnalizer::GetPacketKey(&NotifyData);
  993. string strValue = (string)NotifyData["CONTEXT"];
  994. string strTopic = (string)NotifyData["TOPIC"];
  995. if (strTopic.find("demo") == string::npos)
  996. {
  997. mLog::Debug("get Notify[{$}:{$}] from [{$}]", strKey.c_str(), strValue.c_str(), strPath.c_str());
  998. pCurMech->GenNotify(strKey, strValue);
  999. }
  1000. }break;
  1001. case PACKET_CMD_ADD://设备上报错误
  1002. {}break;
  1003. case PACKET_CMD_DEL://设备消除错误
  1004. {}break;
  1005. case PACKET_CMD_EXE:
  1006. {}break;
  1007. case PACKET_CMD_DATA:
  1008. {}break;
  1009. case PACKET_CMD_MSG:
  1010. {}break;
  1011. case PACKET_CMD_ONLINE:
  1012. {}break;
  1013. case PACKET_CMD_PART_UPDATE:
  1014. {}break;
  1015. default:
  1016. break;
  1017. }
  1018. }
  1019. }break;
  1020. default:
  1021. break;
  1022. }
  1023. }
  1024. }
  1025. catch (...)
  1026. {
  1027. mLog::FERROR("ReadForDeviceEvents has crash");
  1028. }
  1029. mLog::Debug("GenStatusThread stop");
  1030. return true;
  1031. }
  1032. //检查信息接口
  1033. RET_STATUS nsMech::HanQingNormalDevice::SetStudyInfo(ResDataObject& pParam)
  1034. {
  1035. auto& P0 = pParam[0];
  1036. SetPatientInfo(P0);
  1037. return RET_STATUS::RET_SUCCEED;
  1038. }
  1039. RET_STATUS nsMech::HanQingNormalDevice::SetViewInfo(ResDataObject& pParam)
  1040. {
  1041. return RET_STATUS::RET_SUCCEED;
  1042. }
  1043. RET_STATUS nsMech::HanQingNormalDevice::SetPatientInfo(ResDataObject& pParam)
  1044. {
  1045. ECOM_PATIENT Info;
  1046. Info.strPatientName = (string)pParam["PatientName"];
  1047. Info.strSex = (string)pParam["Sex"];
  1048. Info.strAge = (string)pParam["Age"];
  1049. Info.strPatientID = (string)pParam["PatientID"];
  1050. Info.strPatientSize = (string)pParam["PatientSize"];
  1051. Info.strStudyDescription = (string)pParam["StudyDescription"];
  1052. Info.strAccessionNumber = (string)pParam["AccessionNumber"];
  1053. mLog::Debug("Enter SetPatientInfo:[Name:{$}],[Sex:{$}],[Age:{$}],[ID:{$}],[Size:{$}],[Description:{$}],[AccessionNumber:{$}]",
  1054. Info.strPatientName.c_str(), Info.strSex.c_str(), Info.strAge.c_str(), Info.strPatientID.c_str(), Info.strPatientSize.c_str(),
  1055. Info.strStudyDescription.c_str(), Info.strAccessionNumber.c_str());
  1056. char temp[50] = { 0 };
  1057. //PatientName
  1058. sprintf_s(temp, "PNM%-20s", Info.strPatientName.c_str());
  1059. HWSendWaittimeCMD(temp, strlen(temp));
  1060. //Sex
  1061. sprintf_s(temp, "GEM%c", Info.strSex.at(0));
  1062. HWSendWaittimeCMD(temp, strlen(temp));
  1063. //Age
  1064. int nAge = atoi(Info.strAge.c_str());
  1065. sprintf_s(temp, "AGE%03dY", nAge);
  1066. HWSendWaittimeCMD(temp, strlen(temp));
  1067. //PatientID
  1068. sprintf_s(temp, "PID%-20s", Info.strPatientID.c_str());
  1069. HWSendWaittimeCMD(temp, strlen(temp));
  1070. //PatientSizeint
  1071. int nPS = 2;
  1072. if (Info.strPatientSize == "Small")
  1073. {
  1074. nPS = 1;
  1075. }
  1076. else if (Info.strPatientSize == "Middle")
  1077. {
  1078. nPS = 2;
  1079. }
  1080. else if (Info.strPatientSize == "Large")
  1081. {
  1082. nPS = 3;
  1083. }
  1084. else if (Info.strPatientSize == "Child")
  1085. {
  1086. nPS = 4;
  1087. }
  1088. sprintf_s(temp, "PS%1d", nPS);
  1089. HWSendWaittimeCMD(temp, strlen(temp));
  1090. //StudyDescription
  1091. sprintf_s(temp, "DES%-20s", Info.strStudyDescription.c_str());
  1092. HWSendWaittimeCMD(temp, strlen(temp));
  1093. //AccessionNumber
  1094. sprintf_s(temp, "ACC%-20s", Info.strAccessionNumber.c_str());
  1095. HWSendWaittimeCMD(temp, strlen(temp));
  1096. return RET_STATUS::RET_SUCCEED;
  1097. }
  1098. //机架通用接口
  1099. RET_STATUS nsMech::HanQingNormalDevice::Reset()
  1100. {
  1101. mLog::Debug("Enter Reset");
  1102. FireErrorMessage(false, 0, "clear all Mech error");
  1103. mLog::Debug("End Reset");
  1104. return RET_STATUS::RET_SUCCEED;
  1105. }
  1106. RET_STATUS nsMech::HanQingNormalDevice::SetTechParamsInfo(ResDataObject& pParam)
  1107. {
  1108. return RET_STATUS::RET_SUCCEED;
  1109. }
  1110. RET_STATUS nsMech::HanQingNormalDevice::SetPositionNumber(int pn)
  1111. {
  1112. mLog::Debug("Enter SetPositionNumber:[{$}]", pn);
  1113. char temp[50] = { 0 };
  1114. sprintf_s(temp, "PN%03d", pn);
  1115. return HWSendWaittimeCMD(temp, strlen(temp));
  1116. }
  1117. RET_STATUS nsMech::HanQingNormalDevice::SetSID(float value)
  1118. {
  1119. return RET_STATUS::RET_SUCCEED;
  1120. }
  1121. RET_STATUS nsMech::HanQingNormalDevice::SetSOD(float value)
  1122. {
  1123. return RET_STATUS::RET_SUCCEED;
  1124. }
  1125. RET_STATUS nsMech::HanQingNormalDevice::MoveMech(string& value)
  1126. {
  1127. return RET_STATUS::RET_SUCCEED;
  1128. }
  1129. RET_STATUS nsMech::HanQingNormalDevice::StopMech(string& value)
  1130. {
  1131. return RET_STATUS::RET_SUCCEED;
  1132. }
  1133. RET_STATUS nsMech::HanQingNormalDevice::SetGrid(AttrKey::MECH_GRIDSTATE GridState)
  1134. {
  1135. return RET_STATUS::RET_SUCCEED;
  1136. }
  1137. //遮光器
  1138. RET_STATUS nsMech::HanQingNormalDevice::SetCollimator(ECOM_COLLIMATOR_INFO& curCollimator)
  1139. {
  1140. return RET_STATUS::RET_SUCCEED;
  1141. }
  1142. RET_STATUS nsMech::HanQingNormalDevice::SetFilter(int nFilter)
  1143. {
  1144. return RET_STATUS::RET_SUCCEED;
  1145. }
  1146. //拼接相应接口
  1147. RET_STATUS nsMech::HanQingNormalDevice::BeginStitching()
  1148. {
  1149. if (m_MECHUnit.m_MechState->Update(AttrKey::MACHINE_STATUS_STITCH))
  1150. FireNotify(AttrKey::MACHSTATE, m_MECHUnit.m_MechState->Get());
  1151. mLog::Debug("Enter BeginStitching");
  1152. char temp[50] = { 0 };
  1153. m_bIsBeginFlag = true;
  1154. switch (m_MECHUnit.m_StitchType->Get())
  1155. {
  1156. case AttrKey::STITCHTYPE_AutomaticTranslation:
  1157. case AttrKey::STITCHTYPE_ManualTranslation:
  1158. {
  1159. sprintf_s(temp, "CM8");
  1160. HWSendWaittimeCMD(temp, strlen(temp));
  1161. if (m_MECHUnit.m_StitchingState->Get() == AttrKey::STITCHSTATE_REACH_INIT)
  1162. {
  1163. m_MECHUnit.m_strInfo = "Stitch Ready";
  1164. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_READY))
  1165. {
  1166. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_READY);
  1167. }
  1168. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  1169. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  1170. }
  1171. else
  1172. {
  1173. mLog::Error("stitching:[{$}]not in [Stitch Init]", m_MECHUnit.m_StitchingState->Get());
  1174. }
  1175. }break;
  1176. case AttrKey::STITCHTYPE_AutomaticAngle:
  1177. case AttrKey::STITCHTYPE_ManualAngle:
  1178. {
  1179. sprintf_s(temp, "CM7");
  1180. HWSendWaittimeCMD(temp, strlen(temp));
  1181. {
  1182. if (m_MECHUnit.m_TotalImageCount->Get() <= 2)
  1183. {
  1184. sprintf_s(temp, "DT1");
  1185. }
  1186. else
  1187. {
  1188. sprintf_s(temp, "DT2");
  1189. }
  1190. HWSendWaittimeCMD(temp, strlen(temp));
  1191. sprintf_s(temp, "MD%04d", (int)m_MECHUnit.m_StitchStepLength->Get() * 10);
  1192. HWSendWaittimeCMD(temp, strlen(temp));
  1193. }
  1194. }break;
  1195. default:
  1196. return RET_STATUS::RET_FAILED;
  1197. }
  1198. return RET_STATUS::RET_SUCCEED;
  1199. }
  1200. RET_STATUS nsMech::HanQingNormalDevice::EndStitching()
  1201. {
  1202. if (m_MECHUnit.m_MechState->Update(AttrKey::MACHINE_STATUS_STANDBY))
  1203. FireNotify(AttrKey::MACHSTATE, m_MECHUnit.m_MechState->Get());
  1204. mLog::Debug("Enter EndStitching");
  1205. char temp[50] = { 0 };
  1206. sprintf_s(temp, "CM0");
  1207. HWSendWaittimeCMD(temp, strlen(temp));
  1208. //轮询标志
  1209. m_bLoopFlag = false;
  1210. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_END))
  1211. {
  1212. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_END);
  1213. m_MECHUnit.m_strInfo = "Stitch Finish";
  1214. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  1215. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  1216. }
  1217. return RET_STATUS::RET_SUCCEED;
  1218. }
  1219. RET_STATUS nsMech::HanQingNormalDevice::SetupStitching(string& value)
  1220. {
  1221. char temp[50] = { 0 };
  1222. ResDataObject json, P0;
  1223. m_bIsBeginFlag = false;
  1224. json.decode(value.c_str());
  1225. if (json.GetKeyCount("P0") > 0)
  1226. {
  1227. P0 = json[0];
  1228. }
  1229. else
  1230. P0 = json;
  1231. int nInitHeight = atoi(((string)P0[AttrKey::STITCHHEIGHT]).c_str());
  1232. int nTargetLength = atoi(((string)P0[AttrKey::STITCHLENGTH]).c_str());
  1233. int nOverlap = atoi(((string)P0[AttrKey::STITCHOVERLAP]).c_str());
  1234. int nSID = atoi(((string)P0[AttrKey::STITCHSID]).c_str());
  1235. int nTID = atoi(((string)P0[AttrKey::STITCHTID]).c_str());
  1236. int nPID = atoi(((string)P0[AttrKey::STITCHPID]).c_str());
  1237. int nDirection = atoi(((string)P0[AttrKey::STITCHDIRECTION]).c_str());
  1238. int nStitchType = atoi(((string)P0[AttrKey::STITCHTYPE]).c_str());
  1239. mLog::Debug("Enter SetupStitching:[InitHeight:{$},TotalLength:{$},Overlap:{$},SID:{$},TID:{$},PID:{$},Direction:{$},StitchType:{$}]",
  1240. nInitHeight, nTargetLength, nOverlap, nSID, nTID, nPID, nDirection, nStitchType);
  1241. m_MECHUnit.m_StitchHeight->Update(nInitHeight);
  1242. m_MECHUnit.m_StitchOverLap->Update(nOverlap);
  1243. m_MECHUnit.m_StitchDirection->Update(nDirection);
  1244. m_MECHUnit.m_StitchType->Update(nStitchType);
  1245. if (m_MECHUnit.m_CurrentImageNumber->Update(0))
  1246. FireNotify(AttrKey::CURRENTIMAGENUMBER, 0);
  1247. switch (m_MECHUnit.m_StitchType->Get())
  1248. {
  1249. case AttrKey::STITCHTYPE_AutomaticTranslation:
  1250. case AttrKey::STITCHTYPE_ManualTranslation:
  1251. {
  1252. //总长度校验
  1253. int maxLength = m_MECHUnit.m_TubeHeight->GetLimitMax() - m_MECHUnit.m_TubeHeight->GetLimitMin();
  1254. if (nTargetLength > (maxLength-5))
  1255. {
  1256. mLog::Warn("StitchingLengh[{$}] too big [height min:{$},height max:{$}],need change to[{$}]",
  1257. nTargetLength, m_MECHUnit.m_TubeHeight->GetLimitMax(),m_MECHUnit.m_TubeHeight->GetLimitMin(), maxLength - 5);
  1258. nTargetLength = maxLength - 5;
  1259. }
  1260. //总张数计算
  1261. int totalImage = std::ceil((float)(nTargetLength - nOverlap) / (float)(m_nFPDLengh - nOverlap));
  1262. m_MECHUnit.m_TotalImageCount->Update(totalImage);
  1263. FireNotify(AttrKey::TOTALIMAGECOUNT, totalImage);
  1264. //计算步长
  1265. float tempStepLength = (float)nTargetLength / (float)totalImage;
  1266. if (m_MECHUnit.m_StitchStepLength->Update(tempStepLength))
  1267. FireNotify(AttrKey::STITCHSTEPLENGTH, tempStepLength);
  1268. mLog::Debug("compute TransStitching [total ImageCount:{$},StepLength:{$}]",
  1269. totalImage, tempStepLength);
  1270. //更新总长
  1271. m_MECHUnit.m_StitchLength->Update(nTargetLength);
  1272. }break;
  1273. case AttrKey::STITCHTYPE_AutomaticAngle:
  1274. case AttrKey::STITCHTYPE_ManualAngle:
  1275. {
  1276. //手动查询SID
  1277. GetUnitStatus(true);
  1278. //总长度校验
  1279. int maxLength = m_MECHUnit.m_TubeHeight->GetLimitMax() - m_MECHUnit.m_TubeHeight->GetLimitMin();
  1280. float minStitchLenth = m_nFPDLengh * 2 - nOverlap;
  1281. float maxStitchLenth = m_nFPDLengh * 3 - nOverlap * 2;
  1282. if (maxStitchLenth > (maxLength - 5))
  1283. {
  1284. mLog::Warn("StitchingLengh[{$}] too big [height min:{$},height max:{$}],need change to[{$}]",
  1285. maxStitchLenth, m_MECHUnit.m_TubeHeight->GetLimitMax(), m_MECHUnit.m_TubeHeight->GetLimitMin(), maxLength - 5);
  1286. minStitchLenth = (maxLength - 5) - (maxStitchLenth - minStitchLenth);
  1287. maxStitchLenth = maxLength - 5;
  1288. }
  1289. //2、3张时角度范围
  1290. float minStitchAngle = 2 * atan((float)minStitchLenth /2.0f / m_MECHUnit.m_SID->Get()) * 180.0f / MY_PI;
  1291. float maxStitchAngle = 2 * atan((float)maxStitchLenth /2.0f / m_MECHUnit.m_SID->Get()) * 180.0f / MY_PI;
  1292. mLog::Debug("compute AngleStitching limit[FPDLengh:{$},OverLap:{$}, SID:{$},minLengh:{$}, maxLengh:{$}]->angle[min:{$},max:{$}]",
  1293. m_nFPDLengh, nOverlap, m_MECHUnit.m_SID->Get(), minStitchLenth, maxStitchLenth, minStitchAngle, maxStitchAngle);
  1294. if (maxStitchAngle > 25)
  1295. {
  1296. float tempMinAngle = minStitchAngle - (maxStitchAngle - 25);
  1297. float tempMaxAngle = 25;
  1298. mLog::Warn("Stitching Angle too big, need change angle[min:{$},max:{$}]",tempMinAngle, tempMaxAngle);
  1299. minStitchAngle = tempMinAngle;
  1300. maxStitchAngle = tempMaxAngle;
  1301. }
  1302. //设置参数的总角度
  1303. if (nTargetLength <= minStitchAngle)
  1304. {
  1305. m_MECHUnit.m_StitchLength->Update(minStitchAngle);
  1306. float tempStepLength = (float)minStitchAngle / 2;
  1307. if (m_MECHUnit.m_StitchStepLength->Update(tempStepLength))
  1308. FireNotify(AttrKey::STITCHSTEPLENGTH, tempStepLength);
  1309. m_MECHUnit.m_TotalImageCount->Update(2);
  1310. FireNotify(AttrKey::TOTALIMAGECOUNT, 2);
  1311. mLog::Debug("compute AngleStitching [total ImageCount:{$},StepLength:{$}]",
  1312. 2, tempStepLength);
  1313. }
  1314. else
  1315. {
  1316. m_MECHUnit.m_StitchLength->Update(maxStitchAngle);
  1317. //计算步长
  1318. float tempStepLength = (float)maxStitchAngle / 3;
  1319. if (m_MECHUnit.m_StitchStepLength->Update(tempStepLength))
  1320. FireNotify(AttrKey::STITCHSTEPLENGTH, tempStepLength);
  1321. m_MECHUnit.m_TotalImageCount->Update(3);
  1322. FireNotify(AttrKey::TOTALIMAGECOUNT, 3);
  1323. mLog::Debug("compute AngleStitching [total ImageCount:{$},StepLength:{$}]",
  1324. 3, tempStepLength);
  1325. }
  1326. }break;
  1327. default:
  1328. return RET_STATUS::RET_FAILED;
  1329. }
  1330. //先退出
  1331. {
  1332. sprintf_s(temp, "CM0");
  1333. HWSendWaittimeCMD(temp, strlen(temp));
  1334. }
  1335. //设置对齐位
  1336. if (nSID <= 150 && (nStitchType == AttrKey::STITCHTYPE_AutomaticTranslation || nStitchType == AttrKey::STITCHTYPE_ManualTranslation))
  1337. {
  1338. sprintf_s(temp, "CM1");
  1339. HWSendWaittimeCMD(temp, strlen(temp));
  1340. }
  1341. else
  1342. {
  1343. sprintf_s(temp, "CM2");
  1344. HWSendWaittimeCMD(temp, strlen(temp));
  1345. }
  1346. //轮询标志
  1347. m_bLoopFlag = true;
  1348. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_INIT))
  1349. {
  1350. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_INIT);
  1351. m_MECHUnit.m_strInfo = "Stitch Init";
  1352. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  1353. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  1354. }
  1355. return RET_STATUS::RET_SUCCEED;
  1356. }
  1357. //notice the URS that the last acquired image is accepted by host. Move to the next stitching target position.
  1358. RET_STATUS nsMech::HanQingNormalDevice::AcceptStitchingImage()
  1359. {
  1360. mLog::Debug("Enter AcceptStitchingImage");
  1361. m_bIsBeginFlag = false;
  1362. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_Accept_Image))
  1363. {
  1364. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_Accept_Image);
  1365. m_MECHUnit.m_strInfo = "Stitch Accept Current image";
  1366. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  1367. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  1368. }
  1369. int Currentnum = m_MECHUnit.m_CurrentImageNumber->Get() + 1;
  1370. if (m_MECHUnit.m_CurrentImageNumber->Update(Currentnum))
  1371. {
  1372. mLog::Debug("stitching current image[{$}:{$}]", m_MECHUnit.m_CurrentImageNumber->Get(), m_MECHUnit.m_TotalImageCount->Get());
  1373. FireNotify(AttrKey::CURRENTIMAGENUMBER, Currentnum);
  1374. }
  1375. if (m_MECHUnit.m_CurrentImageNumber->Get() >= m_MECHUnit.m_TotalImageCount->Get())
  1376. {
  1377. EndStitching();
  1378. }
  1379. else
  1380. {
  1381. char temp[50] = { 0 };
  1382. switch (m_MECHUnit.m_StitchType->Get())
  1383. {
  1384. case AttrKey::STITCHTYPE_AutomaticTranslation:
  1385. case AttrKey::STITCHTYPE_ManualTranslation:
  1386. {
  1387. //运动的步距aaaa mm
  1388. sprintf_s(temp, "MD%04d", (int)m_MECHUnit.m_StitchStepLength->Get() * 10);
  1389. HWSendWaittimeCMD(temp, strlen(temp));
  1390. //移动方向
  1391. switch (m_MECHUnit.m_StitchDirection->Get())
  1392. {
  1393. case AttrKey::STITCHDIR_TopToBottom:
  1394. case AttrKey::STITCHDIR_leftToRight:
  1395. {
  1396. sprintf_s(temp, "DT1");
  1397. HWSendWaittimeCMD(temp, strlen(temp));
  1398. }break;
  1399. case AttrKey::STITCHDIR_BottomToTop:
  1400. case AttrKey::STITCHDIR_RightToLeft:
  1401. {
  1402. sprintf_s(temp, "DT2");
  1403. HWSendWaittimeCMD(temp, strlen(temp));
  1404. }break;
  1405. }
  1406. //启动运动
  1407. sprintf_s(temp, "SM1");
  1408. HWSendWaittimeCMD(temp, strlen(temp));
  1409. }break;
  1410. case AttrKey::STITCHTYPE_AutomaticAngle:
  1411. case AttrKey::STITCHTYPE_ManualAngle:
  1412. {
  1413. sprintf_s(temp, "SM1");
  1414. HWSendWaittimeCMD(temp, strlen(temp));
  1415. }break;
  1416. default:
  1417. return RET_STATUS::RET_FAILED;
  1418. }
  1419. }
  1420. return RET_STATUS::RET_SUCCEED;
  1421. }
  1422. //notice the URS that the last acquired image is rejected by host. Keep current position.
  1423. RET_STATUS nsMech::HanQingNormalDevice::RejectStitchingImage()
  1424. {
  1425. mLog::Debug("Enter RejectStitchingImage");
  1426. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_Reject_Image))
  1427. {
  1428. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_Reject_Image);
  1429. m_MECHUnit.m_strInfo = "Stitch Reject Current Image";
  1430. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  1431. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  1432. }
  1433. return RET_STATUS::RET_SUCCEED;
  1434. }
  1435. //Cancel stitching procedure.
  1436. RET_STATUS nsMech::HanQingNormalDevice::CancelStitching()
  1437. {
  1438. if (m_MECHUnit.m_MechState->Update(AttrKey::MACHINE_STATUS_STANDBY))
  1439. FireNotify(AttrKey::MACHSTATE, m_MECHUnit.m_MechState->Get());
  1440. mLog::Debug("Enter CancelStitching");
  1441. char temp[50] = { 0 };
  1442. sprintf_s(temp, "CM0");
  1443. HWSendWaittimeCMD(temp, strlen(temp));
  1444. //轮询标志
  1445. m_bLoopFlag = false;
  1446. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_CANCEL))
  1447. {
  1448. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_CANCEL);
  1449. m_MECHUnit.m_strInfo = "Stitch Cancel";
  1450. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  1451. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  1452. }
  1453. return RET_STATUS::RET_SUCCEED;
  1454. }
  1455. //Note the URS that stitching is finished.
  1456. RET_STATUS nsMech::HanQingNormalDevice::CompleteStitching()
  1457. {
  1458. if (m_MECHUnit.m_MechState->Update(AttrKey::MACHINE_STATUS_STANDBY))
  1459. FireNotify(AttrKey::MACHSTATE, m_MECHUnit.m_MechState->Get());
  1460. mLog::Debug("Enter CompleteStitching");
  1461. char temp[50] = { 0 };
  1462. sprintf_s(temp, "CM0");
  1463. HWSendWaittimeCMD(temp, strlen(temp));
  1464. //轮询标志
  1465. m_bLoopFlag = false;
  1466. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_END))
  1467. {
  1468. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_END);
  1469. m_MECHUnit.m_strInfo = "Stitch Finish";
  1470. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  1471. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  1472. }
  1473. return RET_STATUS::RET_SUCCEED;
  1474. }
  1475. //New extra view, request the positioner to move to a position to take an extra image.
  1476. RET_STATUS nsMech::HanQingNormalDevice::NewExtraView()
  1477. {
  1478. mLog::Debug("Enter NewExtraView");
  1479. return RET_STATUS::RET_SUCCEED;
  1480. }
  1481. RET_STATUS nsMech::HanQingNormalDevice::RepeatStitching()
  1482. {
  1483. mLog::Debug("Enter RepeatStitching");
  1484. return RET_STATUS::RET_SUCCEED;
  1485. }
  1486. RET_STATUS nsMech::HanQingNormalDevice::SetAutoPosiitonNo(int nPN)
  1487. {
  1488. mLog::Debug("Enter SetAutoPosiitonNo:[{$}]", nPN);
  1489. return RET_STATUS::RET_SUCCEED;
  1490. }
  1491. //牛头
  1492. RET_STATUS nsMech::HanQingNormalDevice::SetExpEnable(bool nExpEnabled)
  1493. {
  1494. return RET_STATUS::RET_SUCCEED;
  1495. }
  1496. RET_STATUS nsMech::HanQingNormalDevice::SetWS(int nWS)
  1497. {
  1498. mLog::Debug("Enter SetWS:[{$}]", nWS);
  1499. if (m_nWS == nWS)return RET_STATUS::RET_SUCCEED;
  1500. m_nWS = nWS;
  1501. char temp[50] = { 0 };
  1502. sprintf_s(temp, "OR%1d", nWS);
  1503. return HWSendWaittimeCMD(temp, strlen(temp));
  1504. }
  1505. RET_STATUS nsMech::HanQingNormalDevice::SetKV(int nKV)
  1506. {
  1507. mLog::Debug("Enter SetKV:[{$}]", nKV);
  1508. if(m_nKV == nKV)return RET_STATUS::RET_SUCCEED;
  1509. m_nKV = nKV;
  1510. char temp[50] = { 0 };
  1511. sprintf_s(temp, "KV%03d", nKV);
  1512. return HWSendWaittimeCMD(temp, strlen(temp));
  1513. }
  1514. RET_STATUS nsMech::HanQingNormalDevice::SetMA(float fMA)
  1515. {
  1516. mLog::Debug("Enter SetMA:[{$}]", fMA);
  1517. if (m_fMA == fMA)return RET_STATUS::RET_SUCCEED;
  1518. m_fMA = fMA;
  1519. char temp[50] = { 0 };
  1520. sprintf_s(temp, "MA%05d", (int)(fMA * 10));
  1521. return HWSendWaittimeCMD(temp, strlen(temp));
  1522. }
  1523. RET_STATUS nsMech::HanQingNormalDevice::SetMS(float fMS)
  1524. {
  1525. mLog::Debug("Enter SetMS:[{$}]", fMS);
  1526. if (m_fMS == fMS)return RET_STATUS::RET_SUCCEED;
  1527. m_fMS = fMS;
  1528. char temp[50] = { 0 };
  1529. sprintf_s(temp, "MS%05d", (int)(fMS * 10));
  1530. return HWSendWaittimeCMD(temp, strlen(temp));
  1531. }
  1532. RET_STATUS nsMech::HanQingNormalDevice::SetMAS(float fMAS)
  1533. {
  1534. mLog::Debug("Enter SetMAS:[{$}]", fMAS);
  1535. if (m_fMAS == fMAS)return RET_STATUS::RET_SUCCEED;
  1536. m_fMAS = fMAS;
  1537. char temp[50] = { 0 };
  1538. sprintf_s(temp, "MX%05d", (int)(fMAS * 10));
  1539. return HWSendWaittimeCMD(temp, strlen(temp));
  1540. }
  1541. RET_STATUS nsMech::HanQingNormalDevice::SetFO(int nFO)
  1542. {
  1543. mLog::Debug("Enter SetFO:[{$}]", nFO);
  1544. if (m_nFocus == nFO)return RET_STATUS::RET_SUCCEED;
  1545. m_nFocus = nFO;
  1546. char temp[50] = { 0 };
  1547. sprintf_s(temp, "FO%1d", nFO);
  1548. return HWSendWaittimeCMD(temp, strlen(temp));
  1549. }
  1550. RET_STATUS nsMech::HanQingNormalDevice::SetTechMode(int nET)
  1551. {
  1552. mLog::Debug("Enter SetTechMode:[{$}]", nET);
  1553. if (m_nET == nET)return RET_STATUS::RET_SUCCEED;
  1554. m_nET = nET;
  1555. char temp[50] = { 0 };
  1556. sprintf_s(temp, "ET%1d", nET);
  1557. return HWSendWaittimeCMD(temp, strlen(temp));
  1558. }
  1559. RET_STATUS nsMech::HanQingNormalDevice::SetAECField(int nAECFieldSel)
  1560. {
  1561. mLog::Debug("Enter SetAECField:[{$}]", nAECFieldSel);
  1562. if (m_nAECField == nAECFieldSel)return RET_STATUS::RET_SUCCEED;
  1563. m_nAECField = nAECFieldSel;
  1564. char temp[50] = { 0 };
  1565. sprintf_s(temp, "FI%03d", nAECFieldSel);
  1566. return HWSendWaittimeCMD(temp, strlen(temp));
  1567. return RET_STATUS::RET_SUCCEED;
  1568. }
  1569. RET_STATUS nsMech::HanQingNormalDevice::SetDensity(float nAECDensity)
  1570. {
  1571. mLog::Debug("Enter SetDensity:[{$}]", nAECDensity);
  1572. if (m_nAECDensity == nAECDensity)return RET_STATUS::RET_SUCCEED;
  1573. m_nAECDensity = nAECDensity;
  1574. char temp[50] = { 0 };
  1575. sprintf_s(temp, "FD%+04d", (int)nAECDensity);
  1576. return HWSendWaittimeCMD(temp, strlen(temp));
  1577. }
  1578. RET_STATUS nsMech::HanQingNormalDevice::SetBodySize(string strSize)
  1579. {
  1580. mLog::Debug("Enter SetBodySize:[{$}]", strSize.c_str());
  1581. char temp[50] = { 0 };
  1582. int nPS = 2;
  1583. if (strSize == "Small")
  1584. {
  1585. nPS = 1;
  1586. }
  1587. else if (strSize == "Middle")
  1588. {
  1589. nPS = 2;
  1590. }
  1591. else if (strSize == "Large")
  1592. {
  1593. nPS = 3;
  1594. }
  1595. else if (strSize == "Child")
  1596. {
  1597. nPS = 4;
  1598. }
  1599. if (m_nBodySize == nPS)return RET_STATUS::RET_SUCCEED;
  1600. m_nBodySize = nPS;
  1601. sprintf_s(temp, "PS%1d", m_nBodySize);
  1602. HWSendWaittimeCMD(temp, strlen(temp));
  1603. }
  1604. //HanQingNormal特有
  1605. RET_STATUS nsMech::HanQingNormalDevice::GetUnitStatus(bool sync)
  1606. {
  1607. //mLog::Debug("Enter GetUnitStatus");
  1608. char temp[50] = { 0 };
  1609. sprintf_s(temp, "TT?");
  1610. if(sync)
  1611. return HWSendWaitACKCMD(temp, strlen(temp));
  1612. else
  1613. return HWSendWaittimeCMD(temp, strlen(temp));
  1614. }
  1615. RET_STATUS nsMech::HanQingNormalDevice::GenNotify(string& key, string& value)
  1616. {
  1617. if (key == nsGEN::AttrKey::WORKSTATION)
  1618. {
  1619. SetWS(atoi(value.c_str()));
  1620. }
  1621. else if (key == nsGEN::AttrKey::KV)
  1622. {
  1623. SetKV(atoi(value.c_str()));
  1624. }
  1625. else if (key == nsGEN::AttrKey::MA)
  1626. {
  1627. SetMA(atof(value.c_str()));
  1628. }
  1629. else if (key == nsGEN::AttrKey::MS)
  1630. {
  1631. SetMS(atof(value.c_str()));
  1632. }
  1633. else if (key == nsGEN::AttrKey::MAS)
  1634. {
  1635. SetMAS(atof(value.c_str()));
  1636. }
  1637. else if (key == nsGEN::AttrKey::FOCUS)
  1638. {
  1639. SetFO(atoi(value.c_str()));
  1640. }
  1641. else if (key == nsGEN::AttrKey::TECHMODE)
  1642. {
  1643. SetTechMode(atoi(value.c_str()));
  1644. }
  1645. else if (key == nsGEN::AttrKey::AECDENSITY)
  1646. {
  1647. SetDensity(atof(value.c_str()));
  1648. }
  1649. else if (key == nsGEN::AttrKey::AECFIELD)
  1650. {
  1651. SetAECField(atoi(value.c_str()));
  1652. }
  1653. return RET_STATUS::RET_SUCCEED;
  1654. }
  1655. //-----------------------------------------------------------------------------
  1656. // HanQingNormalDriver
  1657. //-----------------------------------------------------------------------------
  1658. nsMech::HanQingNormalDriver::HanQingNormalDriver()
  1659. {
  1660. m_pAttribute.reset(new ResDataObject());
  1661. m_pDescription.reset(new ResDataObject());
  1662. }
  1663. nsMech::HanQingNormalDriver::~HanQingNormalDriver()
  1664. {
  1665. }
  1666. void nsMech::HanQingNormalDriver::FireNotify(int code, std::string key, std::string content)
  1667. {
  1668. mLog::Debug("Enter FireNotify");
  1669. EventCenter->OnNotify(code, key, content);
  1670. }
  1671. void nsMech::HanQingNormalDriver::Dequeue(const char* Packet, DWORD Length)
  1672. {
  1673. DecodeFrame(Packet, Length);
  1674. }
  1675. static char strRcvTemp[HanQingNormal_Com_NormalLen] = { 0 };
  1676. PACKET_RET nsMech::HanQingNormalDriver::callbackPackageProcess(const char* RecData, DWORD nLength, DWORD& PacketLength)
  1677. {
  1678. #if 0
  1679. if (nLength > 1)
  1680. {
  1681. mLog::Error("receive data_len[{$}]", nLength);
  1682. for (int i = 0; i < nLength; i++)
  1683. {
  1684. if (i != nLength - 1)
  1685. {
  1686. mLog::Error("receive data[{$}][{$}]", i, RecData[i]);
  1687. }
  1688. else
  1689. {
  1690. mLog::Error("receive data[{$}][{$}]", i, RecData[i]);
  1691. }
  1692. }
  1693. }
  1694. #endif
  1695. if (nLength < 1)
  1696. {
  1697. PacketLength = 0;
  1698. mLog::Error("nLength too small, nLength=={$}", nLength);
  1699. return PACKET_USELESS;
  1700. }
  1701. else if (nLength > HanQingNormal_Com_NormalLen - 1)
  1702. {
  1703. PacketLength = nLength;
  1704. mLog::Error("nLength too big, nLength=={$}", nLength);
  1705. return PACKET_USELESS;
  1706. }
  1707. memset(strRcvTemp, 0, HanQingNormal_Com_NormalLen);
  1708. for (DWORD i = 0; i < nLength; i++)
  1709. {
  1710. //寻找包尾
  1711. if (RecData[i] == HanQingNormal_ETX)
  1712. {
  1713. PacketLength = i + 2; //because ETX+Checksum
  1714. if (i >= 3) //正常指令:CMDid(2BYTE) + DATA(1BYTE) + ETX(1BYTE) + Checksum(1BYTE)
  1715. {
  1716. memcpy_s(strRcvTemp, HanQingNormal_Com_NormalLen - 1, RecData, i); //长度:-1 排除ETX+Checksum
  1717. if (strncmp(strRcvTemp, ">TT", 3) != 0)
  1718. mLog::Info("==IN==:[{$}]", strRcvTemp);
  1719. return PACKET_ISPACKET;
  1720. }
  1721. else //空指令
  1722. {
  1723. memcpy_s(strRcvTemp, HanQingNormal_Com_NormalLen - 1, RecData, PacketLength); //空数据,格式正确但无有效命令
  1724. mLog::Error("==IN uselss data==:[{$}]", strRcvTemp);
  1725. return PACKET_USELESS;
  1726. }
  1727. }
  1728. }
  1729. return PACKET_NOPACKET;
  1730. }
  1731. void nsMech::HanQingNormalDriver::Prepare()
  1732. {
  1733. string strLogPath = GetProcessDirectory() + R"(\OEMDrivers\Mechanical\Conf\Log4CPP.Config.MECH.xml)";
  1734. //Log4CPP::ThreadContext::Map::Set("LogFileName", "Mech.HanQingNormal");
  1735. Log4CPP::GlobalContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "Mech.HanQingNormal");
  1736. auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str());
  1737. mLog::gLogger = Log4CPP::LogManager::GetLogger("Mech.HanQingNormal");
  1738. m_SCFDllName = GetConnectDLL(m_ConfigFileName);
  1739. super::Prepare();
  1740. }
  1741. bool nsMech::HanQingNormalDriver::Connect()
  1742. {
  1743. mLog::Debug("Enter Connect");
  1744. ResDataObject Connection = GetConnectParam(m_ConfigFileName);
  1745. mLog::Info("connections:{$} \n", Connection.encode());
  1746. auto erCode = m_SCF.Connect(Connection.encode(), &nsMech::HanQingNormalDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000);
  1747. if (erCode != SCF_ERR::SCF_SUCCEED)
  1748. return false; //return erCode;
  1749. auto rc = super::Connect();
  1750. if (!rc)
  1751. return false; //return 0;
  1752. return true; //return SCF_ERR::SCF_SUCCEED;
  1753. }
  1754. void nsMech::HanQingNormalDriver::Disconnect()
  1755. {
  1756. mLog::Debug("Enter Disconnect");
  1757. super::Disconnect();
  1758. m_SCF.Disconnect();
  1759. }
  1760. bool nsMech::HanQingNormalDriver::isConnected() const
  1761. {
  1762. return super::isConnected();
  1763. }
  1764. auto nsMech::HanQingNormalDriver::CreateDevice(int index) -> std::unique_ptr <IODevice>
  1765. {
  1766. mLog::Debug("Enter CreateDevice[{$}]", index);
  1767. //mLog::Info("Enter CreateDevice, index={$}", index);
  1768. if (index == 0)
  1769. {
  1770. auto dev = std::unique_ptr<IODevice>(new IODevice(new HanQingNormalDevice(EventCenter, m_SCF, m_ConfigFileName)));
  1771. return dev;
  1772. }
  1773. //mLog::Error("unknown index");
  1774. unique_ptr <IODevice> dev;
  1775. return dev;
  1776. }
  1777. std::string nsMech::HanQingNormalDriver::DriverProbe()
  1778. {
  1779. printf("line= %d,%s %s\n", __LINE__, __FUNCTION__, m_ConfigFileName.c_str());
  1780. ResDataObject r_config, HardwareInfo;
  1781. if (r_config.loadFile(m_ConfigFileName.c_str()))
  1782. {
  1783. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  1784. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  1785. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  1786. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  1787. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  1788. }
  1789. else
  1790. {
  1791. HardwareInfo.add("MajorID", "Machine");
  1792. HardwareInfo.add("MinorID", "Dr");
  1793. HardwareInfo.add("VendorID", "ECOM");
  1794. HardwareInfo.add("ProductID", "HF");
  1795. HardwareInfo.add("SerialID", "1234");
  1796. }
  1797. string ret = HardwareInfo.encode();
  1798. printf("line= %d,%s %s\n", __LINE__, __FUNCTION__, m_ConfigFileName.c_str());
  1799. return ret;
  1800. }
  1801. std::string nsMech::HanQingNormalDriver::GetResource()
  1802. {
  1803. ResDataObject r_config, temp;
  1804. if (!temp.loadFile(m_ConfigFileName.c_str()))
  1805. {
  1806. return "";
  1807. }
  1808. m_ConfigAll = temp;
  1809. r_config = temp["CONFIGURATION"];
  1810. m_Configurations = r_config;
  1811. ResDataObject DescriptionTemp;
  1812. ResDataObject DescriptionSend;
  1813. ResDataObject m_DescriptionSend;
  1814. ResDataObject ListTemp;
  1815. string strTemp = ""; //用于读取字符串配置信息
  1816. string strIndex = ""; //用于读取配置信息中的List项
  1817. int nTemp = -1; //用于读取整型配置信息
  1818. char sstream[10] = { 0 }; //用于转换值
  1819. string strValue = ""; //用于存储配置的值
  1820. string strType = ""; //用于存储配置的类型 int/float/string...
  1821. try
  1822. {
  1823. //便利ConfigToolInfo 中 所有的AttributeInfo 属性段
  1824. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  1825. m_pAttribute->clear();
  1826. m_pDescription->clear();
  1827. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  1828. {
  1829. DescriptionTemp.clear();
  1830. DescriptionSend.clear();
  1831. ListTemp.clear();
  1832. //AttributeType
  1833. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Type"];
  1834. DescriptionTemp.add(ConfKey::DiosType, strTemp.c_str());//DiosGeneratorAttribute
  1835. DescriptionSend.add(ConfKey::DiosType, strTemp.c_str());//DiosGeneratorAttribute
  1836. strType = strTemp; //记录配置项的类型
  1837. //AttributeKey
  1838. //1. 根据AttributeType,内部key和配置路径,拿到当前的真实值
  1839. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  1840. nTemp = (int)m_Configurations["ConfigToolInfo"][nInfoIndex]["PathID"];
  1841. GetDeviceConfigValue(r_config, strTemp.c_str(), nTemp, strValue); //得到strValue的值
  1842. //2. 赋值
  1843. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  1844. if ("int" == strType)
  1845. {
  1846. (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str()));
  1847. }
  1848. else if ("float" == strType)
  1849. {
  1850. (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str()));
  1851. }
  1852. else //其它先按string类型处理
  1853. {
  1854. (*m_pAttribute).add(strTemp.c_str(), strValue.c_str());
  1855. }
  1856. //AttributeAccess
  1857. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Access"];
  1858. DescriptionTemp.add(ConfKey::DiosAccess, strTemp.c_str());
  1859. DescriptionSend.add(ConfKey::DiosAccess, strTemp.c_str());
  1860. //AttributeRangeMin
  1861. //strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMin"];
  1862. //if (strTemp != "") //不需要的配置项为空
  1863. //{
  1864. // DescriptionTemp.add(ConfKey::DiosRangeMin, strTemp.c_str());
  1865. //}
  1866. ////AttributeRangeMax
  1867. //strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMax"];
  1868. //if (strTemp != "") //不需要的配置项为空
  1869. //{
  1870. // DescriptionTemp.add(ConfKey::DiosRangeMax, strTemp.c_str());
  1871. //}
  1872. //AttributeList
  1873. nTemp = m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListNum"];
  1874. if (nTemp > 0) //ListNum不大于0时说明不需要list配置
  1875. {
  1876. for (int nListIndex = 0; nListIndex < nTemp; nListIndex++)
  1877. {
  1878. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListInfo"][nListIndex];
  1879. auto temKey = std::to_string(nListIndex);
  1880. ListTemp.add(temKey.c_str(), strTemp.c_str());
  1881. }
  1882. DescriptionTemp.add(ConfKey::DiosList, ListTemp);
  1883. DescriptionSend.add(ConfKey::DiosList, ListTemp.encode());
  1884. }
  1885. //AttributeRequired
  1886. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Required"];
  1887. DescriptionTemp.add(ConfKey::DiosRequired, strTemp.c_str());
  1888. DescriptionSend.add(ConfKey::DiosRequired, strTemp.c_str());
  1889. //AttributeDefaultValue
  1890. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["DefaultValue"];
  1891. if (strTemp != "") //不需要的配置项为空
  1892. {
  1893. DescriptionTemp.add(ConfKey::DiosDefaultValue, strTemp.c_str());
  1894. DescriptionSend.add(ConfKey::DiosDefaultValue, strTemp.c_str());
  1895. }
  1896. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  1897. (*m_pDescription).add(strTemp.c_str(), DescriptionTemp);
  1898. m_DescriptionSend.add(strTemp.c_str(), DescriptionSend.encode());
  1899. }
  1900. }
  1901. catch (ResDataObjectExption& e)
  1902. {
  1903. //mLog::Error("Get config error: {$}", e.what());
  1904. return "";
  1905. }
  1906. ResDataObject resDeviceResource;
  1907. resDeviceResource.add(ConfKey::DiosAttribute, (*m_pAttribute));
  1908. resDeviceResource.add(ConfKey::DiosDescription, (*m_pDescription));
  1909. ResDataObject DescriptionTempEx;
  1910. DescriptionTempEx.add(ConfKey::DiosConfig, resDeviceResource);
  1911. m_DeviceConfig.clear();
  1912. m_DeviceConfig = DescriptionTempEx;
  1913. //mLog::Debug("local ************* get resource over {$}", DescriptionTempEx.encode());
  1914. printf("local ************* get resource over %s \n", DescriptionTempEx.encode());
  1915. resDeviceResource.clear();
  1916. resDeviceResource.add(ConfKey::DiosAttribute, (*m_pAttribute));
  1917. resDeviceResource.add(ConfKey::DiosDescription, m_DescriptionSend);
  1918. DescriptionTempEx.clear();
  1919. DescriptionTempEx.add(ConfKey::DiosConfig, resDeviceResource);
  1920. m_DeviceConfigSend.clear();
  1921. m_DeviceConfigSend = DescriptionTempEx;
  1922. string res = m_DeviceConfigSend.encode();
  1923. //mLog::Debug("get resource over {$}", DescriptionTempEx.encode());
  1924. printf("************* get resource over %s \n", DescriptionTempEx.encode());
  1925. return res;
  1926. }
  1927. std::string nsMech::HanQingNormalDriver::DeviceProbe()
  1928. {
  1929. ResDataObject r_config, HardwareInfo;
  1930. if (r_config.loadFile(m_ConfigFileName.c_str()))
  1931. {
  1932. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  1933. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  1934. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  1935. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  1936. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  1937. }
  1938. else
  1939. {
  1940. HardwareInfo.add("MajorID", "Machine");
  1941. HardwareInfo.add("MinorID", "Dr");
  1942. HardwareInfo.add("VendorID", "ECOM");
  1943. HardwareInfo.add("ProductID", "HF");
  1944. HardwareInfo.add("SerialID", "1234");
  1945. }
  1946. string ret = HardwareInfo.encode();
  1947. return ret;
  1948. }
  1949. bool nsMech::HanQingNormalDriver::GetDeviceConfig(std::string& Cfg)
  1950. {
  1951. Cfg = m_DeviceConfigSend.encode();
  1952. printf("GetDeviceConfig over , %s", Cfg.c_str());
  1953. return true;
  1954. }
  1955. bool nsMech::HanQingNormalDriver::SetDeviceConfig(std::string Cfg)
  1956. {
  1957. printf("\n--Func-- SetDeviceConfig %s\n", Cfg.c_str());
  1958. ResDataObject DeviceConfig;
  1959. DeviceConfig.decode(Cfg.c_str());
  1960. ResDataObject DescriptionTempEx;
  1961. DescriptionTempEx = DeviceConfig["DeviceConfig"]["Attribute"];
  1962. //mLog::Debug("Attribute:{$}", DescriptionTempEx.encode());
  1963. bool bSaveFile = false; //true:重新保存配置文件
  1964. string strAccess = "";
  1965. for (int i = 0; i < DescriptionTempEx.size(); i++)
  1966. {
  1967. string strKey = DescriptionTempEx.GetKey(i);
  1968. ////mLog::Info("{$}", strKey.c_str());
  1969. printf("%s\n", strKey.c_str());
  1970. try
  1971. {
  1972. if (m_pAttribute->GetFirstOf(strKey.c_str()) >= 0)
  1973. {
  1974. strAccess = (string)(*m_pDescription)[strKey.c_str()]["Access"];
  1975. if ("RW" == strAccess)
  1976. {
  1977. //修改对应配置,在其他单元的配置项要同时调用其修改函数修改真实值
  1978. //1. 修改内存中的值,用于给上层发消息
  1979. (*m_pAttribute)[strKey.c_str()] = DescriptionTempEx[i];
  1980. //2. 拿到Innerkey
  1981. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  1982. ////mLog::Info("nConfigInfoCount {$}", nConfigInfoCount);
  1983. string strTemp = ""; //存储AttributeKey
  1984. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  1985. {
  1986. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  1987. if (strTemp == strKey)
  1988. {
  1989. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  1990. break;
  1991. }
  1992. }
  1993. //3. 修改配置文件中的值
  1994. if (SetDeviceConfigValue(m_Configurations, strTemp.c_str(), 1, DescriptionTempEx[i]))
  1995. {
  1996. //mLog::Debug("SetDeviceConfigValue over");
  1997. bSaveFile = true;
  1998. }
  1999. }
  2000. else
  2001. {
  2002. ////mLog::Info("{$} is not a RW configuration item", strKey.c_str());
  2003. }
  2004. }
  2005. else
  2006. {
  2007. ////mLog::Info("without this attribute {$}", strKey.c_str());
  2008. }
  2009. }
  2010. catch (ResDataObjectExption& e)
  2011. {
  2012. printf("\nSetDriverConfig crashed: %s\n", e.what());
  2013. //mLog::Error("SetDriverConfig crashed: {$}", e.what());
  2014. return false;
  2015. }
  2016. }
  2017. if (bSaveFile)
  2018. {
  2019. //3. 重新保存配置文件
  2020. SaveConfigFile(true);
  2021. }
  2022. return true;
  2023. }
  2024. bool nsMech::HanQingNormalDriver::SaveConfigFile(bool bSendNotify)
  2025. {
  2026. m_ConfigAll["CONFIGURATION"] = m_Configurations;
  2027. bool bRt = m_ConfigAll.SaveFile(m_ConfigFileName.c_str());
  2028. ////mLog::Info("SaveConfigFile over {$}", bRt);
  2029. return true;
  2030. }
  2031. bool nsMech::HanQingNormalDriver::GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue)
  2032. {
  2033. printf("line= %d,%s,pInnerKey=%s,nPathID=%d\n", __LINE__, __FUNCTION__, pInnerKey, nPathID);
  2034. strValue = "";
  2035. string strTemp = pInnerKey;
  2036. if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取
  2037. {
  2038. int pos = 0;
  2039. ResDataObject resTemp = config;
  2040. while ((pos = strTemp.find_first_of(',')) != string::npos)
  2041. {
  2042. string Key = strTemp.substr(0, pos);
  2043. string TempValue = resTemp[Key.c_str()].encode();
  2044. //printf("-TempValue=== %s\n", TempValue.c_str());
  2045. resTemp.clear();
  2046. resTemp.decode(TempValue.c_str());
  2047. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  2048. //printf("-************--%s\n", strTemp.c_str());
  2049. }
  2050. if (strTemp != "")
  2051. {
  2052. strValue = (string)resTemp[strTemp.c_str()];
  2053. }
  2054. else
  2055. {
  2056. strValue = (string)resTemp;
  2057. }
  2058. }
  2059. //printf("------------%s\n", strValue.c_str());
  2060. return true;
  2061. }
  2062. bool nsMech::HanQingNormalDriver::SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue)
  2063. {
  2064. string strTemp = pInnerKey;
  2065. //mLog::Debug("Begin to change {$} item value to {$}", pInnerKey, szValue);
  2066. printf("\n Begin to change {%s} item value to {%s}\n", pInnerKey, szValue);
  2067. if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取
  2068. {
  2069. try {
  2070. int pos = 0;
  2071. ResDataObject* resTemp = &config;
  2072. while ((pos = strTemp.find_first_of(',')) != string::npos)
  2073. {
  2074. string Key = strTemp.substr(0, pos);
  2075. resTemp = &(*resTemp)[Key.c_str()];
  2076. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  2077. }
  2078. if (strTemp != "")
  2079. {
  2080. (*resTemp)[strTemp.c_str()] = szValue;
  2081. }
  2082. else
  2083. {
  2084. *resTemp = szValue;
  2085. }
  2086. }
  2087. catch (ResDataObjectExption& e)
  2088. {
  2089. //mLog::Error("SetDriverConfigvalue crashed: {$}", e.what());
  2090. return false;
  2091. }
  2092. }
  2093. return true;
  2094. }
  2095. //模块对外接口
  2096. static nsMech::HanQingNormalDriver gIODriver;
  2097. extern "C" DIOS::Dev::IODriver * __cdecl GetIODriver() // 返回静态对象的引用, 调用者不能删除 !
  2098. {
  2099. return &gIODriver;
  2100. }
  2101. extern "C" DIOS::Dev::IODriver * __cdecl CreateIODriver() // 返回新对象, 调用者必须自行删除此对象 !
  2102. {
  2103. return new nsMech::HanQingNormalDriver();
  2104. }