LogPcTask_Biz.cs 58 KB

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