LogPcTask_Biz.cs 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  1. using B20UVLog.Models;
  2. using B20UVLog.Pages;
  3. using NetTaste;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using TFT_MelsecMcNet;
  12. namespace B20UVLog
  13. {
  14. public class LogPcTask_Biz
  15. {
  16. #region 单例模式
  17. /// <summary>
  18. /// 单例模式对象
  19. /// </summary>
  20. private static LogPcTask_Biz _instance = null;
  21. private static readonly object lockObj = new object();
  22. /// <summary>
  23. /// 单例模式方法
  24. /// </summary>
  25. public static LogPcTask_Biz Instance
  26. {
  27. get
  28. {
  29. if (_instance == null)
  30. {
  31. lock (lockObj)
  32. {
  33. if (_instance == null)
  34. {
  35. _instance = new LogPcTask_Biz();
  36. }
  37. }
  38. }
  39. return _instance;
  40. }
  41. }
  42. #endregion
  43. #region 全局变量
  44. public MelsecMcNet melsec_net = null;
  45. /// <summary>
  46. /// 日志头部
  47. /// </summary>
  48. private readonly string LogHeadText = "日志采集系统LogPc主业务类 ==>> ";
  49. private readonly List<Task> taskList = new();
  50. private readonly int TaskDelayTime = 500;
  51. private readonly int ReadDelayTime = 5000;
  52. private readonly CancellationTokenSource tokenSource = new();
  53. private PlcItemTypeModel AxisDataPLC;
  54. private PlcItemTypeModel AxisData2PLC;
  55. private PlcItemTypeModel AxisData3PLC;
  56. private PlcItemTypeModel AxisData4PLC;
  57. private PlcItemTypeModel AxisData5PLC;
  58. private PlcItemTypeModel ServoAxisStautsPLC;
  59. private PlcItemTypeModel GlassInformationPLC;
  60. private PlcItemTypeModel LampUseTimePLC;
  61. private PlcItemTypeModel RecipeBodyPLC;
  62. private PlcItemTypeModel SYSParaPLC;
  63. private PlcItemTypeModel RobotInterfaceInPLC;
  64. private PlcItemTypeModel RobotInterfaceOutPLC;
  65. private PlcItemTypeModel YMeasureDataPLC;
  66. private PlcItemTypeModel EQPStatusPLC;
  67. private PlcItemTypeModel RecipeCountPLC;
  68. private PlcItemTypeModel LampDataPLC;
  69. private PlcItemTypeModel AnalogDataPLC;
  70. private PlcItemTypeModel AlarmPLC;
  71. private PlcItemTypeModel WarnPLC;
  72. private PlcItemTypeModel IOPLC;
  73. //字典字段
  74. private List<DictModel> axleAlarm=new List<DictModel>();
  75. private List<DictModel> axleWarn = new List<DictModel>();
  76. private List<DictModel> deviceAlarm = new List<DictModel>();
  77. private List<DictModel> deviceWarn = new List<DictModel>();
  78. #endregion
  79. /// <summary>
  80. /// Log采集业务类 初始化数据
  81. /// </summary>
  82. /// <exception cref="Exception"></exception>
  83. public void Init()
  84. {
  85. AxisDataPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "AxisData");
  86. AxisData2PLC= DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "AxisData2");
  87. AxisData3PLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "AxisData3");
  88. AxisData4PLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "AxisData4");
  89. AxisData5PLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "AxisData5");
  90. ServoAxisStautsPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "ServoAxisStatus");
  91. GlassInformationPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "GlassInformation");
  92. LampUseTimePLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "LampUseTime");
  93. RecipeBodyPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "RecipeBody");
  94. SYSParaPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "SysPara");
  95. RobotInterfaceInPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "RobotInterfaceIn");
  96. RobotInterfaceOutPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "RobotInterfaceOut");
  97. YMeasureDataPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "YMeasureData");
  98. EQPStatusPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "EQPStatus");
  99. RecipeCountPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "RecipeCount");
  100. AnalogDataPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "AnalogData");
  101. LampDataPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "LampData");
  102. AlarmPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "Alarm");
  103. WarnPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "Warn");
  104. IOPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "IO");
  105. //获取字典中的所有字段
  106. axleAlarm = DbHelper.Db.Queryable<DictModel>().ToList().FindAll(x=>x.Type=="Alarm"&&x.Code=="Axle");
  107. axleWarn = DbHelper.Db.Queryable<DictModel>().ToList().FindAll(x => x.Type == "Warn" && x.Code == "Axle");
  108. deviceAlarm = DbHelper.Db.Queryable<DictModel>().ToList().FindAll(x => x.Type == "Alarm" && x.Code == "Device");
  109. deviceWarn = DbHelper.Db.Queryable<DictModel>().ToList().FindAll(x => x.Type == "Warn" && x.Code == "Warn");
  110. }
  111. /// <summary>
  112. /// 连接PLC
  113. /// </summary>
  114. /// <returns></returns>
  115. public bool ConnectPlc()
  116. {
  117. try
  118. {
  119. melsec_net = new MelsecMcNet(ConfigurationManager.AppSettings["PLCAddr"], Convert.ToInt32(ConfigurationManager.AppSettings["PLCPort"]));
  120. OperateResult connect = melsec_net.ConnectServer();
  121. if (connect.IsSuccess)
  122. {
  123. ;
  124. return true;
  125. }
  126. else
  127. {
  128. return false;
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. return false;
  134. }
  135. }
  136. /// <summary>
  137. /// 断开PLC连接
  138. /// </summary>
  139. public void DisConnectPlc()
  140. {
  141. // 暂停所有PLC采集线程。
  142. if (taskList.Count > 0)
  143. {
  144. tokenSource?.Cancel();
  145. foreach (Task item in taskList)
  146. {
  147. while (!item.IsCompleted)
  148. {
  149. }
  150. item.Dispose();
  151. }
  152. taskList.Clear();
  153. tokenSource?.Dispose();
  154. }
  155. OperateResult result = melsec_net?.ConnectClose();
  156. if (result.IsSuccess)
  157. {
  158. }
  159. else
  160. {
  161. }
  162. melsec_net = null;
  163. }
  164. #region PLC数据采集主业务线程
  165. public void StartPlcLogMonitor()
  166. {
  167. AxisDataLogTask();
  168. AxisData2LogTask();
  169. AxisData3LogTask();
  170. AxisData4LogTask();
  171. AxisData5LogTask();
  172. ServoAxisStatusLogTask();
  173. GlassInformationLogTask();
  174. LampUserTimeLogTask();
  175. RecipeBodyLogTask();
  176. SYSParaLogTask();
  177. RobotInPLCLogTask();
  178. RobotOutPLCLogTask();
  179. YMeasureDataLogTask();
  180. EQPStatusLogTask();
  181. RecipeBodyLogTask();
  182. AnalogDataLogTask();
  183. LampDataLogTask();
  184. AlarmLogTask();
  185. WarnLogTask();
  186. IOLogTask();
  187. }
  188. private void AxisDataLogTask()
  189. {
  190. taskList.Add(Task.Factory.StartNew(() =>
  191. {
  192. while (true)
  193. {
  194. if (tokenSource.IsCancellationRequested)
  195. {
  196. break;
  197. }
  198. if (AxisDataPLC is null)
  199. {
  200. continue;
  201. }
  202. OperateResult<bool> isAxisData = melsec_net.ReadBool(AxisDataPLC.ReportFlagAddress);
  203. if (isAxisData.IsSuccess && isAxisData.Content)
  204. {
  205. OperateResult<short[]> axisDataRet = melsec_net.ReadInt16(AxisDataPLC.PlcItemAddressType + AxisDataPLC.PlcItemStartAddress, (ushort)AxisDataPLC.PlcItemAddressLength);
  206. List<PlcItemModel> axisDataPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AxisDataPLC.PlcItemType).ToList();
  207. if (!axisDataRet.IsSuccess || axisDataRet.Content.Length <= 0 || axisDataPlcItemLst is null || axisDataPlcItemLst.Count <= 0)
  208. {
  209. continue;
  210. }
  211. foreach (PlcItemModel plcItem in axisDataPlcItemLst)
  212. {
  213. ParsePlcItemData(plcItem, axisDataRet.Content, 0);
  214. }
  215. //ToDo:把PLC数据插入到数据库中
  216. AxisDataModel axisData = MapPlcItemToObjectProperty<AxisDataModel>(axisDataPlcItemLst);
  217. axisData.RecordTime = DateTime.Now;
  218. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  219. melsec_net.Write(AxisDataPLC.ReplyFlagAddress, true);
  220. }
  221. else
  222. {
  223. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(AxisDataPLC.ReplyFlagAddress);
  224. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  225. {
  226. melsec_net.Write(AxisDataPLC.ReplyFlagAddress, false);
  227. }
  228. }
  229. Task.Delay(TaskDelayTime).Wait();
  230. }
  231. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  232. }
  233. private void AxisData2LogTask()
  234. {
  235. taskList.Add(Task.Factory.StartNew(() =>
  236. {
  237. while (true)
  238. {
  239. if (tokenSource.IsCancellationRequested)
  240. {
  241. break;
  242. }
  243. if (AxisData2PLC is null)
  244. {
  245. continue;
  246. }
  247. OperateResult<bool> isAxisData = melsec_net.ReadBool(AxisData2PLC.ReportFlagAddress);
  248. if (isAxisData.IsSuccess && isAxisData.Content)
  249. {
  250. OperateResult<short[]> axisDatatRet = melsec_net.ReadInt16(AxisData2PLC.PlcItemAddressType + AxisData2PLC.PlcItemStartAddress, (ushort)AxisData2PLC.PlcItemAddressLength);
  251. List<PlcItemModel> axisDataPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AxisData2PLC.PlcItemType).ToList();
  252. if (!axisDatatRet.IsSuccess || axisDatatRet.Content.Length <= 0 || axisDataPlcItemLst is null || axisDataPlcItemLst.Count <= 0)
  253. {
  254. continue;
  255. }
  256. foreach (PlcItemModel plcItem in axisDataPlcItemLst)
  257. {
  258. ParsePlcItemData(plcItem, axisDatatRet.Content, 0);
  259. }
  260. //ToDo:把PLC数据插入到数据库中
  261. AxisData2 axisData = MapPlcItemToObjectProperty<AxisData2>(axisDataPlcItemLst);
  262. axisData.RecordTime = DateTime.Now;
  263. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  264. melsec_net.Write(AxisData2PLC.ReplyFlagAddress, true);
  265. }
  266. else
  267. {
  268. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(AxisData2PLC.ReplyFlagAddress);
  269. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  270. {
  271. melsec_net.Write(AxisData2PLC.ReplyFlagAddress, false);
  272. }
  273. }
  274. Task.Delay(TaskDelayTime).Wait();
  275. }
  276. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  277. }
  278. private void AxisData3LogTask()
  279. {
  280. taskList.Add(Task.Factory.StartNew(() =>
  281. {
  282. while (true)
  283. {
  284. if (tokenSource.IsCancellationRequested)
  285. {
  286. break;
  287. }
  288. if (AxisData3PLC is null)
  289. {
  290. continue;
  291. }
  292. OperateResult<bool> isAxisData = melsec_net.ReadBool(AxisData3PLC.ReportFlagAddress);
  293. if (isAxisData.IsSuccess && isAxisData.Content)
  294. {
  295. OperateResult<short[]> axisDataRet = melsec_net.ReadInt16(AxisData3PLC.PlcItemAddressType + AxisData3PLC.PlcItemStartAddress, (ushort)AxisData3PLC.PlcItemAddressLength);
  296. List<PlcItemModel> axisDataPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AxisData3PLC.PlcItemType).ToList();
  297. if (!axisDataRet.IsSuccess || axisDataRet.Content.Length <= 0 || axisDataPlcItemLst is null || axisDataPlcItemLst.Count <= 0)
  298. {
  299. continue;
  300. }
  301. foreach (PlcItemModel plcItem in axisDataPlcItemLst)
  302. {
  303. ParsePlcItemData(plcItem, axisDataRet.Content, 0);
  304. }
  305. //ToDo:把PLC数据插入到数据库中
  306. AxisData3 axisData = MapPlcItemToObjectProperty<AxisData3>(axisDataPlcItemLst);
  307. axisData.RecordTime = DateTime.Now;
  308. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  309. melsec_net.Write(AxisData3PLC.ReplyFlagAddress, true);
  310. }
  311. else
  312. {
  313. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(AxisData3PLC.ReplyFlagAddress);
  314. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  315. {
  316. melsec_net.Write(AxisData3PLC.ReplyFlagAddress, false);
  317. }
  318. }
  319. Task.Delay(TaskDelayTime).Wait();
  320. }
  321. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  322. }
  323. private void AxisData4LogTask()
  324. {
  325. taskList.Add(Task.Factory.StartNew(() =>
  326. {
  327. while (true)
  328. {
  329. if (tokenSource.IsCancellationRequested)
  330. {
  331. break;
  332. }
  333. if (AxisData4PLC is null)
  334. {
  335. continue;
  336. }
  337. OperateResult<bool> isAxisData = melsec_net.ReadBool(AxisData4PLC.ReportFlagAddress);
  338. if (isAxisData.IsSuccess && isAxisData.Content)
  339. {
  340. OperateResult<short[]> axisDataRet = melsec_net.ReadInt16(AxisData4PLC.PlcItemAddressType + AxisData4PLC.PlcItemStartAddress, (ushort)AxisData4PLC.PlcItemAddressLength);
  341. List<PlcItemModel> axisDataPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AxisData4PLC.PlcItemType).ToList();
  342. if (!axisDataRet.IsSuccess || axisDataRet.Content.Length <= 0 || axisDataPlcItemLst is null || axisDataPlcItemLst.Count <= 0)
  343. {
  344. continue;
  345. }
  346. foreach (PlcItemModel plcItem in axisDataPlcItemLst)
  347. {
  348. ParsePlcItemData(plcItem, axisDataRet.Content, 0);
  349. }
  350. //ToDo:把PLC数据插入到数据库中
  351. AxisData4 axisData = MapPlcItemToObjectProperty<AxisData4>(axisDataPlcItemLst);
  352. axisData.RecordTime = DateTime.Now;
  353. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  354. melsec_net.Write(AxisData4PLC.ReplyFlagAddress, true);
  355. }
  356. else
  357. {
  358. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(AxisData4PLC.ReplyFlagAddress);
  359. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  360. {
  361. melsec_net.Write(AxisData4PLC.ReplyFlagAddress, false);
  362. }
  363. }
  364. Task.Delay(TaskDelayTime).Wait();
  365. }
  366. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  367. }
  368. private void AxisData5LogTask()
  369. {
  370. taskList.Add(Task.Factory.StartNew(() =>
  371. {
  372. while (true)
  373. {
  374. if (tokenSource.IsCancellationRequested)
  375. {
  376. break;
  377. }
  378. if (AxisData5PLC is null)
  379. {
  380. continue;
  381. }
  382. OperateResult<bool> isAxisData = melsec_net.ReadBool(AxisData5PLC.ReportFlagAddress);
  383. if (isAxisData.IsSuccess && isAxisData.Content)
  384. {
  385. OperateResult<short[]> axisDataRet = melsec_net.ReadInt16(AxisData5PLC.PlcItemAddressType + AxisData5PLC.PlcItemStartAddress, (ushort)AxisData5PLC.PlcItemAddressLength);
  386. List<PlcItemModel> axisDataPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AxisData5PLC.PlcItemType).ToList();
  387. if (!axisDataRet.IsSuccess || axisDataRet.Content.Length <= 0 || axisDataPlcItemLst is null || axisDataPlcItemLst.Count <= 0)
  388. {
  389. continue;
  390. }
  391. foreach (PlcItemModel plcItem in axisDataPlcItemLst)
  392. {
  393. ParsePlcItemData(plcItem, axisDataRet.Content, 0);
  394. }
  395. //ToDo:把PLC数据插入到数据库中
  396. AxisData5 axisData = MapPlcItemToObjectProperty<AxisData5>(axisDataPlcItemLst);
  397. axisData.RecordTime = DateTime.Now;
  398. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  399. melsec_net.Write(AxisData5PLC.ReplyFlagAddress, true);
  400. }
  401. else
  402. {
  403. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(AxisData5PLC.ReplyFlagAddress);
  404. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  405. {
  406. melsec_net.Write(AxisData5PLC.ReplyFlagAddress, false);
  407. }
  408. }
  409. Task.Delay(TaskDelayTime).Wait();
  410. }
  411. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  412. }
  413. /// <summary>
  414. /// servoAxis
  415. /// </summary>
  416. private void ServoAxisStatusLogTask()
  417. {
  418. taskList.Add(Task.Factory.StartNew(() =>
  419. {
  420. while (true)
  421. {
  422. if (tokenSource.IsCancellationRequested)
  423. {
  424. break;
  425. }
  426. if (ServoAxisStautsPLC is null)
  427. {
  428. continue;
  429. }
  430. OperateResult<bool> isServoAxis = melsec_net.ReadBool(ServoAxisStautsPLC.ReportFlagAddress);
  431. if (isServoAxis.IsSuccess && isServoAxis.Content)
  432. {
  433. OperateResult<short[]> servoAxisRet = melsec_net.ReadInt16(ServoAxisStautsPLC.PlcItemAddressType + ServoAxisStautsPLC.PlcItemStartAddress, (ushort)ServoAxisStautsPLC.PlcItemAddressLength);
  434. List<PlcItemModel> servoAxisPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == ServoAxisStautsPLC.PlcItemType).ToList();
  435. if (!servoAxisRet.IsSuccess || servoAxisRet.Content.Length <= 0 || servoAxisPlcItemLst is null || servoAxisPlcItemLst.Count <= 0)
  436. {
  437. continue;
  438. }
  439. foreach (PlcItemModel plcItem in servoAxisPlcItemLst)
  440. {
  441. ParsePlcItemData(plcItem, servoAxisRet.Content, 0);
  442. }
  443. //ToDo:把PLC数据插入到数据库中
  444. ServoAxisStautsOriginal servoOrignal = MapPlcItemToObjectProperty<ServoAxisStautsOriginal>(servoAxisPlcItemLst);
  445. //ServoAxisStautsOriginal 将不为0的字段转为ServoAxisStautsModel
  446. var nonZeroProperties = GetNonZeroProperties(servoOrignal);
  447. foreach (var property in nonZeroProperties)
  448. {
  449. string name = property.Key;
  450. int value = property.Value;
  451. ServoAxisStatusModel servoAxisStatusModel = new ServoAxisStatusModel();
  452. if(name.Contains("Warn"))
  453. {
  454. servoAxisStatusModel.AxisName = name.Substring(0, 4);
  455. servoAxisStatusModel.WarningCode = value;
  456. servoAxisStatusModel.WarningDescribe =axleWarn?.FirstOrDefault(x=>x.Key==value.ToString().Trim())?.Value;
  457. }
  458. else
  459. {
  460. if (name.Contains("Alarm"))
  461. {
  462. servoAxisStatusModel.AxisName = name.Substring(0, 4);
  463. servoAxisStatusModel.AlarmCode = value;
  464. servoAxisStatusModel.AlarmDescribe = axleAlarm?.FirstOrDefault(x => x.Key == value.ToString().Trim())?.Value;
  465. }
  466. }
  467. servoAxisStatusModel.RecordTime = DateTime.Now;
  468. DbHelper.Db.Insertable(servoAxisStatusModel).ExecuteCommand();
  469. melsec_net.Write(ServoAxisStautsPLC.ReplyFlagAddress, true);
  470. }
  471. }
  472. else
  473. {
  474. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(ServoAxisStautsPLC.ReplyFlagAddress);
  475. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  476. {
  477. melsec_net.Write(ServoAxisStautsPLC.ReplyFlagAddress, false);
  478. }
  479. }
  480. Task.Delay(TaskDelayTime).Wait();
  481. }
  482. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  483. }
  484. private void GlassInformationLogTask()
  485. {
  486. taskList.Add(Task.Factory.StartNew(() =>
  487. {
  488. while (true)
  489. {
  490. if (tokenSource.IsCancellationRequested)
  491. {
  492. break;
  493. }
  494. if (GlassInformationPLC is null)
  495. {
  496. continue;
  497. }
  498. OperateResult<bool> isServoAxis = melsec_net.ReadBool(GlassInformationPLC.ReportFlagAddress);
  499. if (isServoAxis.IsSuccess && isServoAxis.Content)
  500. {
  501. OperateResult<short[]> glassInfoRet = melsec_net.ReadInt16(GlassInformationPLC.PlcItemAddressType + GlassInformationPLC.PlcItemStartAddress, (ushort)GlassInformationPLC.PlcItemAddressLength);
  502. List<PlcItemModel> servoAxisPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == GlassInformationPLC.PlcItemType).ToList();
  503. if (!glassInfoRet.IsSuccess || glassInfoRet.Content.Length <= 0 || servoAxisPlcItemLst is null || servoAxisPlcItemLst.Count <= 0)
  504. {
  505. continue;
  506. }
  507. foreach (PlcItemModel plcItem in servoAxisPlcItemLst)
  508. {
  509. ParsePlcItemData(plcItem, glassInfoRet.Content, 0);
  510. }
  511. //ToDo:把PLC数据插入到数据库中
  512. GlassInformationModel glassInfoData = MapPlcItemToObjectProperty<GlassInformationModel>(servoAxisPlcItemLst);
  513. //读取时间
  514. OperateResult<short[]> glassInfoRetTime = melsec_net.ReadInt16("D16100", 6);
  515. if (!glassInfoRetTime.IsSuccess || glassInfoRetTime.Content.Length <= 0)
  516. {
  517. continue;
  518. }
  519. Convert16BitBCDToTwoInts(glassInfoRetTime.Content[0], out int yy1, out int mm1);
  520. Convert16BitBCDToTwoInts(glassInfoRetTime.Content[1], out int dd1, out int hh1);
  521. Convert16BitBCDToTwoInts(glassInfoRetTime.Content[2], out int ff1, out int ss1);
  522. Convert16BitBCDToTwoInts(glassInfoRetTime.Content[3], out int yy2, out int mm2);
  523. Convert16BitBCDToTwoInts(glassInfoRetTime.Content[4], out int dd2, out int hh2);
  524. Convert16BitBCDToTwoInts(glassInfoRetTime.Content[5], out int ff2, out int ss2);
  525. glassInfoData.LordingTime = new DateTime(yy1, mm1, dd1, hh1, ff1, ss1);
  526. glassInfoData.UnlordingTime = new DateTime(yy2, mm2, dd2, hh2, ff2, ss2);
  527. glassInfoData.RecordTime = DateTime.Now;
  528. DbHelper.Db.Insertable(glassInfoData).ExecuteCommand();
  529. melsec_net.Write(GlassInformationPLC.ReplyFlagAddress, true);
  530. }
  531. else
  532. {
  533. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(GlassInformationPLC.ReplyFlagAddress);
  534. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  535. {
  536. melsec_net.Write(GlassInformationPLC.ReplyFlagAddress, false);
  537. }
  538. }
  539. Task.Delay(TaskDelayTime).Wait();
  540. }
  541. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  542. }
  543. private void LampUserTimeLogTask()
  544. {
  545. taskList.Add(Task.Factory.StartNew(() =>
  546. {
  547. while (true)
  548. {
  549. if (tokenSource.IsCancellationRequested)
  550. {
  551. break;
  552. }
  553. if (LampUseTimePLC is null)
  554. {
  555. continue;
  556. }
  557. OperateResult<bool> isLampUse = melsec_net.ReadBool(LampUseTimePLC.ReportFlagAddress);
  558. if (isLampUse.IsSuccess && isLampUse.Content)
  559. {
  560. OperateResult<short[]> glassInfoRet = melsec_net.ReadInt16(LampUseTimePLC.PlcItemAddressType + LampUseTimePLC.PlcItemStartAddress, (ushort)LampUseTimePLC.PlcItemAddressLength);
  561. List<PlcItemModel> glassInfoPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == LampUseTimePLC.PlcItemType).ToList();
  562. if (!glassInfoRet.IsSuccess || glassInfoRet.Content.Length <= 0 || glassInfoPlcItemLst is null || glassInfoPlcItemLst.Count <= 0)
  563. {
  564. continue;
  565. }
  566. foreach (PlcItemModel plcItem in glassInfoPlcItemLst)
  567. {
  568. ParsePlcItemData(plcItem, glassInfoRet.Content, 0);
  569. }
  570. //ToDo:把PLC数据插入到数据库中
  571. LampUseTimeModel axisData = MapPlcItemToObjectProperty<LampUseTimeModel>(glassInfoPlcItemLst);
  572. axisData.RecordTime = DateTime.Now;
  573. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  574. melsec_net.Write(LampUseTimePLC.ReplyFlagAddress, true);
  575. }
  576. else
  577. {
  578. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(LampUseTimePLC.ReplyFlagAddress);
  579. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  580. {
  581. melsec_net.Write(LampUseTimePLC.ReplyFlagAddress, false);
  582. }
  583. }
  584. Task.Delay(TaskDelayTime).Wait();
  585. }
  586. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  587. }
  588. private void RecipeBodyLogTask()
  589. {
  590. taskList.Add(Task.Factory.StartNew(() =>
  591. {
  592. while (true)
  593. {
  594. if (tokenSource.IsCancellationRequested)
  595. {
  596. break;
  597. }
  598. if (RecipeBodyPLC is null)
  599. {
  600. continue;
  601. }
  602. OperateResult<bool> isLampUse = melsec_net.ReadBool(RecipeBodyPLC.ReportFlagAddress);
  603. if (isLampUse.IsSuccess && isLampUse.Content)
  604. {
  605. OperateResult<short[]> recipeBodyRet = melsec_net.ReadInt16(RecipeBodyPLC.PlcItemAddressType + RecipeBodyPLC.PlcItemStartAddress, (ushort)RecipeBodyPLC.PlcItemAddressLength);
  606. List<PlcItemModel> reciprBodyPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == RecipeBodyPLC.PlcItemType).ToList();
  607. if (!recipeBodyRet.IsSuccess || recipeBodyRet.Content.Length <= 0 || reciprBodyPlcItemLst is null || reciprBodyPlcItemLst.Count <= 0)
  608. {
  609. continue;
  610. }
  611. foreach (PlcItemModel plcItem in reciprBodyPlcItemLst)
  612. {
  613. ParsePlcItemData(plcItem, recipeBodyRet.Content, 0);
  614. }
  615. //ToDo:把PLC数据插入到数据库中
  616. RecipeBodyModel axisData = MapPlcItemToObjectProperty<RecipeBodyModel>(reciprBodyPlcItemLst);
  617. axisData.RecordTime = DateTime.Now;
  618. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  619. melsec_net.Write(RecipeBodyPLC.ReplyFlagAddress, true);
  620. }
  621. else
  622. {
  623. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(RecipeBodyPLC.ReplyFlagAddress);
  624. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  625. {
  626. melsec_net.Write(RecipeBodyPLC.ReplyFlagAddress, false);
  627. }
  628. }
  629. Task.Delay(TaskDelayTime).Wait();
  630. }
  631. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  632. }
  633. private void SYSParaLogTask()
  634. {
  635. taskList.Add(Task.Factory.StartNew(() =>
  636. {
  637. while (true)
  638. {
  639. if (tokenSource.IsCancellationRequested)
  640. {
  641. break;
  642. }
  643. if (SYSParaPLC is null)
  644. {
  645. continue;
  646. }
  647. OperateResult<bool> isSysPara = melsec_net.ReadBool(SYSParaPLC.ReportFlagAddress);
  648. if (isSysPara.IsSuccess && isSysPara.Content)
  649. {
  650. OperateResult<short[]> sysParaRet = melsec_net.ReadInt16(SYSParaPLC.PlcItemAddressType + SYSParaPLC.PlcItemStartAddress, (ushort)SYSParaPLC.PlcItemAddressLength);
  651. List<PlcItemModel> sysparaItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == SYSParaPLC.PlcItemType).ToList();
  652. if (!sysParaRet.IsSuccess || sysParaRet.Content.Length <= 0 || sysparaItemLst is null || sysparaItemLst.Count <= 0)
  653. {
  654. continue;
  655. }
  656. foreach (PlcItemModel plcItem in sysparaItemLst)
  657. {
  658. ParsePlcItemData(plcItem, sysParaRet.Content, 0);
  659. }
  660. //ToDo:把PLC数据插入到数据库中
  661. SysParaModel axisData = MapPlcItemToObjectProperty<SysParaModel>(sysparaItemLst);
  662. axisData.RecordTime = DateTime.Now;
  663. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  664. melsec_net.Write(SYSParaPLC.ReplyFlagAddress, true);
  665. }
  666. else
  667. {
  668. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(SYSParaPLC.ReplyFlagAddress);
  669. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  670. {
  671. melsec_net.Write(SYSParaPLC.ReplyFlagAddress, false);
  672. }
  673. }
  674. Task.Delay(TaskDelayTime).Wait();
  675. }
  676. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  677. }
  678. private void RobotInPLCLogTask()
  679. {
  680. taskList.Add(Task.Factory.StartNew(() =>
  681. {
  682. while (true)
  683. {
  684. if (tokenSource.IsCancellationRequested)
  685. {
  686. break;
  687. }
  688. if (RobotInterfaceInPLC is null)
  689. {
  690. continue;
  691. }
  692. OperateResult<bool> isRobotIn = melsec_net.ReadBool(RobotInterfaceInPLC.ReportFlagAddress);
  693. if (isRobotIn.IsSuccess && isRobotIn.Content)
  694. {
  695. OperateResult<short[]> robotInRet = melsec_net.ReadInt16(RobotInterfaceInPLC.PlcItemAddressType + RobotInterfaceInPLC.PlcItemStartAddress, (ushort)RobotInterfaceInPLC.PlcItemAddressLength);
  696. List<PlcItemModel> robotInPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == RobotInterfaceInPLC.PlcItemType).ToList();
  697. if (!robotInRet.IsSuccess || robotInRet.Content.Length <= 0 || robotInPlcItemLst is null || robotInPlcItemLst.Count <= 0)
  698. {
  699. continue;
  700. }
  701. foreach (PlcItemModel plcItem in robotInPlcItemLst)
  702. {
  703. ParsePlcItemData(plcItem, robotInRet.Content, 0);
  704. }
  705. //ToDo:把PLC数据插入到数据库中
  706. RobotInterfaceInModel axisData = MapPlcItemToObjectProperty<RobotInterfaceInModel>(robotInPlcItemLst);
  707. axisData.RecordTime = DateTime.Now;
  708. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  709. melsec_net.Write(RobotInterfaceInPLC.ReplyFlagAddress, true);
  710. }
  711. else
  712. {
  713. OperateResult<bool> isRobotInReply = melsec_net.ReadBool(RobotInterfaceInPLC.ReplyFlagAddress);
  714. if (isRobotInReply.IsSuccess && isRobotInReply.Content)
  715. {
  716. melsec_net.Write(RobotInterfaceInPLC.ReplyFlagAddress, false);
  717. }
  718. }
  719. Task.Delay(TaskDelayTime).Wait();
  720. }
  721. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  722. }
  723. private void RobotOutPLCLogTask()
  724. {
  725. taskList.Add(Task.Factory.StartNew(() =>
  726. {
  727. while (true)
  728. {
  729. if (tokenSource.IsCancellationRequested)
  730. {
  731. break;
  732. }
  733. if (RobotInterfaceOutPLC is null)
  734. {
  735. continue;
  736. }
  737. OperateResult<bool> isRobotOut = melsec_net.ReadBool(RobotInterfaceOutPLC.ReportFlagAddress);
  738. if (isRobotOut.IsSuccess && isRobotOut.Content)
  739. {
  740. OperateResult<short[]> robotInRet = melsec_net.ReadInt16(RobotInterfaceOutPLC.PlcItemAddressType + RobotInterfaceOutPLC.PlcItemStartAddress, (ushort)RobotInterfaceOutPLC.PlcItemAddressLength);
  741. List<PlcItemModel> robotInPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == RobotInterfaceOutPLC.PlcItemType).ToList();
  742. if (!robotInRet.IsSuccess || robotInRet.Content.Length <= 0 || robotInPlcItemLst is null || robotInPlcItemLst.Count <= 0)
  743. {
  744. continue;
  745. }
  746. foreach (PlcItemModel plcItem in robotInPlcItemLst)
  747. {
  748. ParsePlcItemData(plcItem, robotInRet.Content, 0);
  749. }
  750. //ToDo:把PLC数据插入到数据库中
  751. RobotInterfaceOutModel axisData = MapPlcItemToObjectProperty<RobotInterfaceOutModel>(robotInPlcItemLst);
  752. axisData.RecordTime = DateTime.Now;
  753. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  754. melsec_net.Write(RobotInterfaceOutPLC.ReplyFlagAddress, true);
  755. }
  756. else
  757. {
  758. OperateResult<bool> isRobotInReply = melsec_net.ReadBool(RobotInterfaceOutPLC.ReplyFlagAddress);
  759. if (isRobotInReply.IsSuccess && isRobotInReply.Content)
  760. {
  761. melsec_net.Write(RobotInterfaceOutPLC.ReplyFlagAddress, false);
  762. }
  763. }
  764. Task.Delay(TaskDelayTime).Wait();
  765. }
  766. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  767. }
  768. private void YMeasureDataLogTask()
  769. {
  770. taskList.Add(Task.Factory.StartNew(() =>
  771. {
  772. while (true)
  773. {
  774. if (tokenSource.IsCancellationRequested)
  775. {
  776. break;
  777. }
  778. if (YMeasureDataPLC is null)
  779. {
  780. continue;
  781. }
  782. OperateResult<bool> isYMeasure = melsec_net.ReadBool(YMeasureDataPLC.ReportFlagAddress);
  783. if (isYMeasure.IsSuccess && isYMeasure.Content)
  784. {
  785. OperateResult<short[]> yMeasureRet = melsec_net.ReadInt16(YMeasureDataPLC.PlcItemAddressType + YMeasureDataPLC.PlcItemStartAddress, (ushort)YMeasureDataPLC.PlcItemAddressLength);
  786. List<PlcItemModel> glassInfoPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == YMeasureDataPLC.PlcItemType).ToList();
  787. if (!yMeasureRet.IsSuccess || yMeasureRet.Content.Length <= 0 || glassInfoPlcItemLst is null || glassInfoPlcItemLst.Count <= 0)
  788. {
  789. continue;
  790. }
  791. foreach (PlcItemModel plcItem in glassInfoPlcItemLst)
  792. {
  793. ParsePlcItemData(plcItem, yMeasureRet.Content, 0);
  794. }
  795. //ToDo:把PLC数据插入到数据库中
  796. YMeasureDataModel axisData = MapPlcItemToObjectProperty<YMeasureDataModel>(glassInfoPlcItemLst);
  797. axisData.RecordTime = DateTime.Now;
  798. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  799. melsec_net.Write(YMeasureDataPLC.ReplyFlagAddress, true);
  800. }
  801. else
  802. {
  803. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(YMeasureDataPLC.ReplyFlagAddress);
  804. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  805. {
  806. melsec_net.Write(YMeasureDataPLC.ReplyFlagAddress, false);
  807. }
  808. }
  809. Task.Delay(TaskDelayTime).Wait();
  810. }
  811. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  812. }
  813. private void EQPStatusLogTask()
  814. {
  815. taskList.Add(Task.Factory.StartNew(() =>
  816. {
  817. while (true)
  818. {
  819. if (tokenSource.IsCancellationRequested)
  820. {
  821. break;
  822. }
  823. if (EQPStatusPLC is null)
  824. {
  825. continue;
  826. }
  827. OperateResult<bool> isEQPStatus = melsec_net.ReadBool(EQPStatusPLC.ReportFlagAddress);
  828. if (isEQPStatus.IsSuccess && isEQPStatus.Content)
  829. {
  830. OperateResult<short[]> eqpStatusRet = melsec_net.ReadInt16(EQPStatusPLC.PlcItemAddressType + EQPStatusPLC.PlcItemStartAddress, (ushort)EQPStatusPLC.PlcItemAddressLength);
  831. List<PlcItemModel> eqpPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == EQPStatusPLC.PlcItemType).ToList();
  832. if (!eqpStatusRet.IsSuccess || eqpStatusRet.Content.Length <= 0 || eqpPlcItemLst is null || eqpPlcItemLst.Count <= 0)
  833. {
  834. continue;
  835. }
  836. foreach (PlcItemModel plcItem in eqpPlcItemLst)
  837. {
  838. ParsePlcItemData(plcItem, eqpStatusRet.Content, 0);
  839. }
  840. //ToDo:把PLC数据插入到数据库中
  841. EQPStatusModel axisData = MapPlcItemToObjectProperty<EQPStatusModel>(eqpPlcItemLst);
  842. axisData.RecordTime = DateTime.Now;
  843. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  844. melsec_net.Write(EQPStatusPLC.ReplyFlagAddress, true);
  845. }
  846. else
  847. {
  848. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(EQPStatusPLC.ReplyFlagAddress);
  849. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  850. {
  851. melsec_net.Write(EQPStatusPLC.ReplyFlagAddress, false);
  852. }
  853. }
  854. Task.Delay(TaskDelayTime).Wait();
  855. }
  856. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  857. }
  858. private void RecipeCountLogTask()
  859. {
  860. taskList.Add(Task.Factory.StartNew(() =>
  861. {
  862. while (true)
  863. {
  864. if (tokenSource.IsCancellationRequested)
  865. {
  866. break;
  867. }
  868. if (RecipeCountPLC is null)
  869. {
  870. continue;
  871. }
  872. OperateResult<bool> isRecipeCount = melsec_net.ReadBool(RecipeCountPLC.ReportFlagAddress);
  873. if (isRecipeCount.IsSuccess && isRecipeCount.Content)
  874. {
  875. OperateResult<short[]> recipeRet = melsec_net.ReadInt16(RecipeCountPLC.PlcItemAddressType + RecipeCountPLC.PlcItemStartAddress, (ushort)RecipeCountPLC.PlcItemAddressLength);
  876. List<PlcItemModel> recipePlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == RecipeCountPLC.PlcItemType).ToList();
  877. if (!recipeRet.IsSuccess || recipeRet.Content.Length <= 0 || recipePlcItemLst is null || recipePlcItemLst.Count <= 0)
  878. {
  879. continue;
  880. }
  881. foreach (PlcItemModel plcItem in recipePlcItemLst)
  882. {
  883. ParsePlcItemData(plcItem, recipeRet.Content, 0);
  884. }
  885. //ToDo:把PLC数据插入到数据库中
  886. RecipeCountModel axisData = MapPlcItemToObjectProperty<RecipeCountModel>(recipePlcItemLst);
  887. axisData.RecordTime = DateTime.Now;
  888. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  889. melsec_net.Write(RecipeCountPLC.ReplyFlagAddress, true);
  890. }
  891. else
  892. {
  893. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(RecipeCountPLC.ReplyFlagAddress);
  894. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  895. {
  896. melsec_net.Write(RecipeCountPLC.ReplyFlagAddress, false);
  897. }
  898. }
  899. Task.Delay(TaskDelayTime).Wait();
  900. }
  901. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  902. }
  903. private void AnalogDataLogTask()
  904. {
  905. taskList.Add(Task.Factory.StartNew(() =>
  906. {
  907. while (true)
  908. {
  909. if (tokenSource.IsCancellationRequested)
  910. {
  911. break;
  912. }
  913. if (AnalogDataPLC is null)
  914. {
  915. continue;
  916. }
  917. {
  918. List<PlcItemModel> analogPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AnalogDataPLC.PlcItemType).ToList();
  919. foreach(var item in analogPlcItemLst)
  920. {
  921. item.Value= melsec_net.ReadInt16(item.PlcAddr).Content;
  922. }
  923. //foreach (PlcItemModel plcItem in analogPlcItemLst)
  924. //{
  925. // ParsePlcItemData(plcItem, analogDataRet.Content, 0);
  926. //}
  927. //ToDo:把PLC数据插入到数据库中
  928. AnalogDataModel analogData = MapPlcItemToObjectProperty<AnalogDataModel>(analogPlcItemLst);
  929. //几个字段特殊读取
  930. //OperateResult<short> stageMain = melsec_net.ReadInt16("D16640");
  931. //analogData.StageMainCDA= stageMain.Content;
  932. //OperateResult<short> irradiator = melsec_net.ReadInt16("D16642");
  933. //analogData.IrradiatorMainCDA = irradiator.Content;
  934. //OperateResult<short> z1Current = melsec_net.ReadInt16("D16650");
  935. //analogData.Z1Current = z1Current.Content;
  936. //OperateResult<short> z2Current = melsec_net.ReadInt16("D16652");
  937. //analogData.Z2Current = z2Current.Content;
  938. //OperateResult<short> z3Current = melsec_net.ReadInt16("D16654");
  939. //analogData.Z3Current = z3Current.Content;
  940. //OperateResult<short> z4Current = melsec_net.ReadInt16("D16656");
  941. //analogData.Z4Current = z4Current.Content;
  942. analogData.RecordTime = DateTime.Now;
  943. DbHelper.Db.Insertable(analogData).ExecuteCommand();
  944. }
  945. Task.Delay(ReadDelayTime).Wait();
  946. }
  947. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  948. }
  949. private void LampDataLogTask()
  950. {
  951. taskList.Add(Task.Factory.StartNew(() =>
  952. {
  953. while (true)
  954. {
  955. if (tokenSource.IsCancellationRequested)
  956. {
  957. break;
  958. }
  959. if (LampDataPLC is null)
  960. {
  961. continue;
  962. }
  963. {
  964. //OperateResult<short[]> lampDataRet = melsec_net.ReadInt16(LampDataPLC.PlcItemAddressType + LampDataPLC.PlcItemStartAddress, (ushort)AnalogDataPLC.PlcItemAddressLength);
  965. List<PlcItemModel> lampDataItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == LampDataPLC.PlcItemType).ToList();
  966. foreach (var item in lampDataItemLst)
  967. {
  968. item.Value = melsec_net.ReadInt16(item.PlcAddr).Content;
  969. }
  970. //ToDo:把PLC数据插入到数据库中
  971. LampDataModel lampData = MapPlcItemToObjectProperty<LampDataModel>(lampDataItemLst);
  972. //几个字段特殊读取
  973. lampData.RecordTime = DateTime.Now;
  974. DbHelper.Db.Insertable(lampData).ExecuteCommand();
  975. }
  976. Task.Delay(ReadDelayTime).Wait();
  977. }
  978. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  979. }
  980. private void AlarmLogTask()
  981. {
  982. taskList.Add(Task.Factory.StartNew(() =>
  983. {
  984. while (true)
  985. {
  986. if (tokenSource.IsCancellationRequested)
  987. {
  988. break;
  989. }
  990. if (AlarmPLC is null)
  991. {
  992. continue;
  993. }
  994. OperateResult<bool> isAlarm = melsec_net.ReadBool(AlarmPLC.ReportFlagAddress);
  995. if (isAlarm.IsSuccess && isAlarm.Content)
  996. {
  997. OperateResult<short[]> alarmRet = melsec_net.ReadInt16(AlarmPLC.PlcItemAddressType + AlarmPLC.PlcItemStartAddress, (ushort)AlarmPLC.PlcItemAddressLength);
  998. List<PlcItemModel> alarmItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AlarmPLC.PlcItemType).ToList();
  999. if (!alarmRet.IsSuccess || alarmRet.Content.Length <= 0 || alarmItemLst is null || alarmItemLst.Count <= 0)
  1000. {
  1001. continue;
  1002. }
  1003. foreach (PlcItemModel plcItem in alarmItemLst)
  1004. {
  1005. ParsePlcItemData(plcItem, alarmRet.Content, 0);
  1006. }
  1007. //ToDo:把PLC数据插入到数据库中
  1008. AlarmModel alarmData = MapPlcItemToObjectProperty<AlarmModel>(alarmItemLst);
  1009. alarmData.AlarmDescribe = deviceAlarm?.FirstOrDefault(x=>x.Key.Trim()==alarmData.AlarmCode.ToString())?.Value;
  1010. alarmData.RecordTime = DateTime.Now;
  1011. DbHelper.Db.Insertable(alarmData).ExecuteCommand();
  1012. melsec_net.Write(AlarmPLC.ReplyFlagAddress, true);
  1013. }
  1014. else
  1015. {
  1016. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(AlarmPLC.ReplyFlagAddress);
  1017. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  1018. {
  1019. melsec_net.Write(AlarmPLC.ReplyFlagAddress, false);
  1020. }
  1021. }
  1022. Task.Delay(TaskDelayTime).Wait();
  1023. }
  1024. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  1025. }
  1026. private void WarnLogTask()
  1027. {
  1028. taskList.Add(Task.Factory.StartNew(() =>
  1029. {
  1030. while (true)
  1031. {
  1032. if (tokenSource.IsCancellationRequested)
  1033. {
  1034. break;
  1035. }
  1036. if (WarnPLC is null)
  1037. {
  1038. continue;
  1039. }
  1040. OperateResult<bool> isLampUse = melsec_net.ReadBool(WarnPLC.ReportFlagAddress);
  1041. if (isLampUse.IsSuccess && isLampUse.Content)
  1042. {
  1043. OperateResult<short[]> warnRet = melsec_net.ReadInt16(WarnPLC.PlcItemAddressType + WarnPLC.PlcItemStartAddress, (ushort)WarnPLC.PlcItemAddressLength);
  1044. List<PlcItemModel> warnItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == WarnPLC.PlcItemType).ToList();
  1045. if (!warnRet.IsSuccess || warnRet.Content.Length <= 0 || warnItemLst is null || warnItemLst.Count <= 0)
  1046. {
  1047. continue;
  1048. }
  1049. foreach (PlcItemModel plcItem in warnItemLst)
  1050. {
  1051. ParsePlcItemData(plcItem, warnRet.Content, 0);
  1052. }
  1053. //ToDo:把PLC数据插入到数据库中
  1054. WarnModel warnData = MapPlcItemToObjectProperty<WarnModel>(warnItemLst);
  1055. warnData.WarnDescribe = deviceWarn?.FirstOrDefault(x => x.Key.Trim() == warnData.WarnCode.ToString())?.Value;
  1056. warnData.RecordTime = DateTime.Now;
  1057. DbHelper.Db.Insertable(warnData).ExecuteCommand();
  1058. melsec_net.Write(WarnPLC.ReplyFlagAddress, true);
  1059. }
  1060. else
  1061. {
  1062. OperateResult<bool> isJobEventReply = melsec_net.ReadBool(WarnPLC.ReplyFlagAddress);
  1063. if (isJobEventReply.IsSuccess && isJobEventReply.Content)
  1064. {
  1065. melsec_net.Write(WarnPLC.ReplyFlagAddress, false);
  1066. }
  1067. }
  1068. Task.Delay(TaskDelayTime).Wait();
  1069. }
  1070. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  1071. }
  1072. private void IOLogTask()
  1073. {
  1074. taskList.Add(Task.Factory.StartNew(() =>
  1075. {
  1076. while (true)
  1077. {
  1078. if (tokenSource.IsCancellationRequested)
  1079. {
  1080. break;
  1081. }
  1082. if (IOPLC is null)
  1083. {
  1084. continue;
  1085. }
  1086. {
  1087. OperateResult<short[]> ioRet = melsec_net.ReadInt16(IOPLC.PlcItemAddressType + IOPLC.PlcItemStartAddress, (ushort)IOPLC.PlcItemAddressLength);
  1088. List<PlcItemModel> ioItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == IOPLC.PlcItemType).ToList();
  1089. if (!ioRet.IsSuccess || ioRet.Content.Length <= 0 || ioItemLst is null || ioItemLst.Count <= 0)
  1090. {
  1091. continue;
  1092. }
  1093. foreach (PlcItemModel plcItem in ioItemLst)
  1094. {
  1095. ParsePlcItemData(plcItem, ioRet.Content, 0);
  1096. }
  1097. //ToDo:把PLC数据插入到数据库中
  1098. IOModel axisData = MapPlcItemToObjectProperty<IOModel>(ioItemLst);
  1099. axisData.RecordTime = DateTime.Now;
  1100. DbHelper.Db.Insertable(axisData).ExecuteCommand();
  1101. }
  1102. Task.Delay(500).Wait();
  1103. }
  1104. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  1105. }
  1106. private void ParsePlcItemData(PlcItemModel plcItem, short[] retLst, int startIndex)
  1107. {
  1108. if (plcItem.PlcItemFormat == 0) // 解析为int类型
  1109. {
  1110. if (plcItem.IsFullWord == 1) // 全字解析,不进行二进制拆分
  1111. {
  1112. short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
  1113. if (plcItem.WordSize == 1)
  1114. {
  1115. plcItem.Value = tmpVal[0];
  1116. }
  1117. else if (plcItem.WordSize == 2)
  1118. {
  1119. plcItem.Value = (tmpVal[0] << 16) + tmpVal[1];
  1120. }
  1121. }
  1122. else // 非全字解析,需要对数据进行二进制拆分,根据具体的二进制位数,进行数据转换。
  1123. {
  1124. short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
  1125. string binStr = string.Empty;
  1126. for (int j = 0; j < tmpVal.Length; j++)
  1127. {
  1128. binStr += string.Concat(Convert.ToString(tmpVal[j], 2).PadLeft(16, '0').Reverse());
  1129. }
  1130. string retStr = string.Concat(binStr.Skip(plcItem.BinSubOffset).Take(plcItem.BinSize).ToArray().Reverse());
  1131. plcItem.Value = Convert.ToInt32(retStr, 2);
  1132. }
  1133. }
  1134. else if (plcItem.PlcItemFormat == 1) // 解析为ASCII码字符串, 数据直接转换为对应的ASCII码字符串数据进行存储
  1135. {
  1136. short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
  1137. List<byte> bytes = new();
  1138. for (int j = 0; j < plcItem.WordSize; j++)
  1139. {
  1140. //bytes.AddRange(BitConverter.GetBytes(tmpVal[j]));
  1141. bytes.Add((byte)tmpVal[j]);
  1142. }
  1143. ASCIIEncoding asciiMd = new();
  1144. //Console.WriteLine(BitConverter.ToString(bytes.ToArray()));
  1145. char[] ascii = asciiMd.GetChars(bytes.ToArray());
  1146. string retStr = string.Empty;
  1147. for (int j = 0; j < bytes.Count; j++)
  1148. {
  1149. retStr += ascii[j];
  1150. }
  1151. plcItem.Value = retStr;
  1152. }
  1153. else if (plcItem.PlcItemFormat == 2) // 解析为二进制数据,按照二进制具体位置和位数,重新组装数据
  1154. {
  1155. if (plcItem.IsFullWord == 1) // 全字解析 , 直接存储数据的二进制字符串
  1156. {
  1157. short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
  1158. string binStr = string.Empty;
  1159. for (int j = 0; j < tmpVal.Length; j++)
  1160. {
  1161. binStr += string.Concat(Convert.ToString(tmpVal[j], 2).PadLeft(16, '0').Reverse());
  1162. }
  1163. plcItem.Value = binStr;
  1164. }
  1165. else // 非全字解析 按照二进制具体位置和位数,存储相应的二进制字符串
  1166. {
  1167. short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
  1168. string binStr = string.Empty;
  1169. for (int j = 0; j < tmpVal.Length; j++)
  1170. {
  1171. binStr += string.Concat(Convert.ToString(tmpVal[j], 2).PadLeft(16, '0').Reverse());
  1172. }
  1173. string retStr = string.Concat(binStr.Skip(plcItem.BinSubOffset).Take(plcItem.BinSize).ToArray().Reverse());
  1174. plcItem.Value = retStr;
  1175. }
  1176. }
  1177. }
  1178. /// <summary>
  1179. /// 根据解析出来的PLC点位数据,创建对应的数据库实体对象。
  1180. /// </summary>
  1181. /// <typeparam name="T">对应的数据库实体对象</typeparam>
  1182. /// <param name="plcItemLst">解析出来的PLC点位数据</param>
  1183. /// <returns></returns>
  1184. private static T MapPlcItemToObjectProperty<T>(List<PlcItemModel> plcItemLst) where T : class, new()
  1185. {
  1186. T tmp = new();
  1187. PropertyInfo[] propertys = tmp.GetType().GetProperties();
  1188. foreach (PropertyInfo property in propertys)
  1189. {
  1190. foreach (PlcItemModel item in plcItemLst)
  1191. {
  1192. if (property.Name == item.PlcItemCode)
  1193. {
  1194. if (property.PropertyType.FullName == typeof(DateTime).FullName)
  1195. {
  1196. property.SetValue(tmp, item.Value is null ? new DateTime() : Convert.ToDateTime(item.Value), null);
  1197. }
  1198. else if (property.PropertyType.FullName == typeof(int).FullName)
  1199. {
  1200. property.SetValue(tmp, item.Value is null ? 0 : Convert.ToInt32(item.Value), null);
  1201. }
  1202. else if (property.PropertyType.FullName == typeof(decimal).FullName)
  1203. {
  1204. property.SetValue(tmp, item.Value is null ? 0 : Convert.ToDecimal(item.Value), null);
  1205. }
  1206. else if (property.PropertyType.FullName == typeof(string).FullName)
  1207. {
  1208. property.SetValue(tmp, item.Value is null ? null : item.Value.ToString(), null);
  1209. }
  1210. else if (property.PropertyType.FullName == typeof(bool).FullName || property.PropertyType.FullName == typeof(bool).FullName)
  1211. {
  1212. //property.SetValue(tmp, item.Value is null ? false : Convert.ToBoolean(item.Value), null);
  1213. //VS编辑器建议的简化写法。 非常巧妙!!!!!!
  1214. property.SetValue(tmp, item.Value is not null && Convert.ToBoolean(item.Value), null);
  1215. }
  1216. else if (property.PropertyType.BaseType.FullName == typeof(byte[]).FullName)
  1217. {
  1218. property.SetValue(tmp, item.Value is null ? null : item.Value, null);
  1219. }
  1220. else if (property.PropertyType.BaseType.FullName == typeof(Enum).FullName)
  1221. {
  1222. property.SetValue(tmp, Enum.Parse(property.PropertyType, (item.Value ?? 0).ToString()), null);
  1223. }
  1224. else
  1225. {
  1226. property.SetValue(tmp, item.Value, null);
  1227. }
  1228. break;
  1229. }
  1230. }
  1231. }
  1232. return tmp;
  1233. }
  1234. private Dictionary<string, int> GetNonZeroProperties(object obj)
  1235. {
  1236. Dictionary<string, int> nonZeroProperties = new Dictionary<string, int>();
  1237. PropertyInfo[] properties = obj.GetType().GetProperties();
  1238. foreach (PropertyInfo property in properties)
  1239. {
  1240. if (property.PropertyType == typeof(int))
  1241. {
  1242. int value = (int)property.GetValue(obj);
  1243. if (value != 0)
  1244. {
  1245. nonZeroProperties.Add(property.Name, value);
  1246. }
  1247. }
  1248. }
  1249. return nonZeroProperties;
  1250. }
  1251. // 将8位BCD码转化为整数
  1252. private int BCDToInt(byte bcdValue)
  1253. {
  1254. int result = 0;
  1255. int multiplier = 1;
  1256. while (bcdValue > 0)
  1257. {
  1258. int digit = bcdValue & 0xF;
  1259. result += digit * multiplier;
  1260. bcdValue >>= 4;
  1261. multiplier *= 10;
  1262. }
  1263. return result;
  1264. }
  1265. // 将16位BCD码转化为两个整数
  1266. private void Convert16BitBCDToTwoInts(short bcdValue, out int int1, out int int2)
  1267. {
  1268. // 高8位BCD码
  1269. byte highByte = (byte)((bcdValue >> 8) & 0xFF);
  1270. // 低8位BCD码
  1271. byte lowByte = (byte)(bcdValue & 0xFF);
  1272. // 分别转换高8位和低8位
  1273. int1 = BCDToInt(highByte);
  1274. int2 = BCDToInt(lowByte);
  1275. }
  1276. #endregion
  1277. }
  1278. }