LogPcTask_Biz.cs 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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. List<IONewModel> ioNewModelList = new List<IONewModel>();
  1075. DateTime recordTime;
  1076. taskList.Add(Task.Factory.StartNew(() =>
  1077. {
  1078. while (true)
  1079. {
  1080. if (tokenSource.IsCancellationRequested)
  1081. {
  1082. break;
  1083. }
  1084. if (IOPLC is null)
  1085. {
  1086. continue;
  1087. }
  1088. {
  1089. OperateResult<short[]> ioRet = melsec_net.ReadInt16(IOPLC.PlcItemAddressType + IOPLC.PlcItemStartAddress, (ushort)IOPLC.PlcItemAddressLength);
  1090. List<PlcItemModel> ioItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == IOPLC.PlcItemType).ToList();
  1091. if (!ioRet.IsSuccess || ioRet.Content.Length <= 0 || ioItemLst is null || ioItemLst.Count <= 0)
  1092. {
  1093. continue;
  1094. }
  1095. recordTime = DateTime.Now;
  1096. ioNewModelList.Clear();
  1097. foreach (PlcItemModel plcItem in ioItemLst)
  1098. {
  1099. ParsePlcItemData(plcItem, ioRet.Content, 0);
  1100. //转化为
  1101. IONewModel ioNewModel = new IONewModel();
  1102. ioNewModel.IOName = plcItem.PlcItemCode;
  1103. if (plcItem.PlcItemCode.Contains("Stage"))
  1104. {
  1105. ioNewModel.IOType = "Stage";
  1106. }
  1107. else
  1108. {
  1109. if (plcItem.PlcItemCode.Contains("IrradiatorInput"))
  1110. {
  1111. ioNewModel.IOType = "IrradiatorInput";
  1112. }
  1113. else
  1114. {
  1115. if (plcItem.PlcItemCode.Contains("IrradiatorOutput"))
  1116. {
  1117. ioNewModel.IOType = "IrradiatorOutput";
  1118. }
  1119. }
  1120. }
  1121. ioNewModel.IOValue = Convert.ToInt32(plcItem.Value);
  1122. ioNewModel.RecordTime = recordTime;
  1123. ioNewModelList.Add(ioNewModel);
  1124. }
  1125. //ToDo:把PLC数据插入到数据库中
  1126. DbHelper.Db.Insertable(ioNewModelList).ExecuteCommand();
  1127. }
  1128. Task.Delay(500).Wait();
  1129. }
  1130. }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
  1131. }
  1132. private void ParsePlcItemData(PlcItemModel plcItem, short[] retLst, int startIndex)
  1133. {
  1134. if (plcItem.PlcItemFormat == 0) // 解析为int类型
  1135. {
  1136. if (plcItem.IsFullWord == 1) // 全字解析,不进行二进制拆分
  1137. {
  1138. short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
  1139. if (plcItem.WordSize == 1)
  1140. {
  1141. plcItem.Value = tmpVal[0];
  1142. }
  1143. else if (plcItem.WordSize == 2)
  1144. {
  1145. plcItem.Value = (tmpVal[0] << 16) + tmpVal[1];
  1146. }
  1147. }
  1148. else // 非全字解析,需要对数据进行二进制拆分,根据具体的二进制位数,进行数据转换。
  1149. {
  1150. short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
  1151. string binStr = string.Empty;
  1152. for (int j = 0; j < tmpVal.Length; j++)
  1153. {
  1154. binStr += string.Concat(Convert.ToString(tmpVal[j], 2).PadLeft(16, '0').Reverse());
  1155. }
  1156. string retStr = string.Concat(binStr.Skip(plcItem.BinSubOffset).Take(plcItem.BinSize).ToArray().Reverse());
  1157. plcItem.Value = Convert.ToInt32(retStr, 2);
  1158. }
  1159. }
  1160. else if (plcItem.PlcItemFormat == 1) // 解析为ASCII码字符串, 数据直接转换为对应的ASCII码字符串数据进行存储
  1161. {
  1162. short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
  1163. List<byte> bytes = new();
  1164. for (int j = 0; j < plcItem.WordSize; j++)
  1165. {
  1166. //bytes.AddRange(BitConverter.GetBytes(tmpVal[j]));
  1167. bytes.Add((byte)tmpVal[j]);
  1168. }
  1169. ASCIIEncoding asciiMd = new();
  1170. //Console.WriteLine(BitConverter.ToString(bytes.ToArray()));
  1171. char[] ascii = asciiMd.GetChars(bytes.ToArray());
  1172. string retStr = string.Empty;
  1173. for (int j = 0; j < bytes.Count; j++)
  1174. {
  1175. retStr += ascii[j];
  1176. }
  1177. plcItem.Value = retStr;
  1178. }
  1179. else if (plcItem.PlcItemFormat == 2) // 解析为二进制数据,按照二进制具体位置和位数,重新组装数据
  1180. {
  1181. if (plcItem.IsFullWord == 1) // 全字解析 , 直接存储数据的二进制字符串
  1182. {
  1183. short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
  1184. string binStr = string.Empty;
  1185. for (int j = 0; j < tmpVal.Length; j++)
  1186. {
  1187. binStr += string.Concat(Convert.ToString(tmpVal[j], 2).PadLeft(16, '0').Reverse());
  1188. }
  1189. plcItem.Value = binStr;
  1190. }
  1191. else // 非全字解析 按照二进制具体位置和位数,存储相应的二进制字符串
  1192. {
  1193. short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
  1194. string binStr = string.Empty;
  1195. for (int j = 0; j < tmpVal.Length; j++)
  1196. {
  1197. binStr += string.Concat(Convert.ToString(tmpVal[j], 2).PadLeft(16, '0').Reverse());
  1198. }
  1199. string retStr = string.Concat(binStr.Skip(plcItem.BinSubOffset).Take(plcItem.BinSize).ToArray().Reverse());
  1200. plcItem.Value = retStr;
  1201. }
  1202. }
  1203. }
  1204. /// <summary>
  1205. /// 根据解析出来的PLC点位数据,创建对应的数据库实体对象。
  1206. /// </summary>
  1207. /// <typeparam name="T">对应的数据库实体对象</typeparam>
  1208. /// <param name="plcItemLst">解析出来的PLC点位数据</param>
  1209. /// <returns></returns>
  1210. private static T MapPlcItemToObjectProperty<T>(List<PlcItemModel> plcItemLst) where T : class, new()
  1211. {
  1212. T tmp = new();
  1213. PropertyInfo[] propertys = tmp.GetType().GetProperties();
  1214. foreach (PropertyInfo property in propertys)
  1215. {
  1216. foreach (PlcItemModel item in plcItemLst)
  1217. {
  1218. if (property.Name == item.PlcItemCode)
  1219. {
  1220. if (property.PropertyType.FullName == typeof(DateTime).FullName)
  1221. {
  1222. property.SetValue(tmp, item.Value is null ? new DateTime() : Convert.ToDateTime(item.Value), null);
  1223. }
  1224. else if (property.PropertyType.FullName == typeof(int).FullName)
  1225. {
  1226. property.SetValue(tmp, item.Value is null ? 0 : Convert.ToInt32(item.Value), null);
  1227. }
  1228. else if (property.PropertyType.FullName == typeof(decimal).FullName)
  1229. {
  1230. property.SetValue(tmp, item.Value is null ? 0 : Convert.ToDecimal(item.Value), null);
  1231. }
  1232. else if (property.PropertyType.FullName == typeof(string).FullName)
  1233. {
  1234. property.SetValue(tmp, item.Value is null ? null : item.Value.ToString(), null);
  1235. }
  1236. else if (property.PropertyType.FullName == typeof(bool).FullName || property.PropertyType.FullName == typeof(bool).FullName)
  1237. {
  1238. //property.SetValue(tmp, item.Value is null ? false : Convert.ToBoolean(item.Value), null);
  1239. //VS编辑器建议的简化写法。 非常巧妙!!!!!!
  1240. property.SetValue(tmp, item.Value is not null && Convert.ToBoolean(item.Value), null);
  1241. }
  1242. else if (property.PropertyType.BaseType.FullName == typeof(byte[]).FullName)
  1243. {
  1244. property.SetValue(tmp, item.Value is null ? null : item.Value, null);
  1245. }
  1246. else if (property.PropertyType.BaseType.FullName == typeof(Enum).FullName)
  1247. {
  1248. property.SetValue(tmp, Enum.Parse(property.PropertyType, (item.Value ?? 0).ToString()), null);
  1249. }
  1250. else
  1251. {
  1252. property.SetValue(tmp, item.Value, null);
  1253. }
  1254. break;
  1255. }
  1256. }
  1257. }
  1258. return tmp;
  1259. }
  1260. private Dictionary<string, int> GetNonZeroProperties(object obj)
  1261. {
  1262. Dictionary<string, int> nonZeroProperties = new Dictionary<string, int>();
  1263. PropertyInfo[] properties = obj.GetType().GetProperties();
  1264. foreach (PropertyInfo property in properties)
  1265. {
  1266. if (property.PropertyType == typeof(int))
  1267. {
  1268. int value = (int)property.GetValue(obj);
  1269. if (value != 0)
  1270. {
  1271. nonZeroProperties.Add(property.Name, value);
  1272. }
  1273. }
  1274. }
  1275. return nonZeroProperties;
  1276. }
  1277. // 将8位BCD码转化为整数
  1278. private int BCDToInt(byte bcdValue)
  1279. {
  1280. int result = 0;
  1281. int multiplier = 1;
  1282. while (bcdValue > 0)
  1283. {
  1284. int digit = bcdValue & 0xF;
  1285. result += digit * multiplier;
  1286. bcdValue >>= 4;
  1287. multiplier *= 10;
  1288. }
  1289. return result;
  1290. }
  1291. // 将16位BCD码转化为两个整数
  1292. private void Convert16BitBCDToTwoInts(short bcdValue, out int int1, out int int2)
  1293. {
  1294. // 高8位BCD码
  1295. byte highByte = (byte)((bcdValue >> 8) & 0xFF);
  1296. // 低8位BCD码
  1297. byte lowByte = (byte)(bcdValue & 0xFF);
  1298. // 分别转换高8位和低8位
  1299. int1 = BCDToInt(highByte);
  1300. int2 = BCDToInt(lowByte);
  1301. }
  1302. #endregion
  1303. }
  1304. }