LogPcTask_Biz.cs 61 KB

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