123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425 |
- using B20UVLog.Models;
- using B20UVLog.Pages;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using TFT_MelsecMcNet;
- namespace B20UVLog
- {
- public class LogPcTask_Biz
- {
- #region 单例模式
- /// <summary>
- /// 单例模式对象
- /// </summary>
- private static LogPcTask_Biz _instance = null;
- private static readonly object lockObj = new object();
- /// <summary>
- /// 单例模式方法
- /// </summary>
- public static LogPcTask_Biz Instance
- {
- get
- {
- if (_instance == null)
- {
- lock (lockObj)
- {
- if (_instance == null)
- {
- _instance = new LogPcTask_Biz();
- }
- }
- }
- return _instance;
- }
- }
- #endregion
- #region 全局变量
-
- public MelsecMcNet melsec_net = null;
-
- /// <summary>
- /// 日志头部
- /// </summary>
- private readonly string LogHeadText = "日志采集系统LogPc主业务类 ==>> ";
- private readonly List<Task> taskList = new();
- private readonly int TaskDelayTime = 500;
- private readonly int ReadDelayTime = 5000;
- private readonly CancellationTokenSource tokenSource = new();
- private PlcItemTypeModel AxisDataPLC;
- private PlcItemTypeModel AxisData2PLC;
- private PlcItemTypeModel AxisData3PLC;
- private PlcItemTypeModel AxisData4PLC;
- private PlcItemTypeModel AxisData5PLC;
- private PlcItemTypeModel ServoAxisStautsPLC;
- private PlcItemTypeModel GlassInformationPLC;
- private PlcItemTypeModel LampUseTimePLC;
- private PlcItemTypeModel RecipeBodyPLC;
- private PlcItemTypeModel SYSParaPLC;
- private PlcItemTypeModel RobotInterfaceInPLC;
- private PlcItemTypeModel RobotInterfaceOutPLC;
- private PlcItemTypeModel YMeasureDataPLC;
- private PlcItemTypeModel EQPStatusPLC;
- private PlcItemTypeModel RecipeCountPLC;
- private PlcItemTypeModel LampDataPLC;
- private PlcItemTypeModel AnalogDataPLC;
- private PlcItemTypeModel AlarmPLC;
- private PlcItemTypeModel WarnPLC;
- private PlcItemTypeModel IOPLC;
-
- #endregion
- /// <summary>
- /// Log采集业务类 初始化数据
- /// </summary>
- /// <exception cref="Exception"></exception>
- public void Init()
- {
- AxisDataPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "AxisData");
- AxisData2PLC= DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "AxisData2");
- AxisData3PLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "AxisData3");
- AxisData4PLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "AxisData4");
- AxisData5PLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "AxisData5");
- ServoAxisStautsPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "ServoAxisStatus");
- GlassInformationPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "GlassInformation");
- LampUseTimePLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "LampUseTime");
- RecipeBodyPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "RecipeBody");
- SYSParaPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "SysPara");
- RobotInterfaceInPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "RobotInterfaceIn");
- RobotInterfaceOutPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "RobotInterfaceOut");
- YMeasureDataPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "YMeasureData");
- EQPStatusPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "EQPStatus");
- RecipeCountPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "RecipeCount");
- AnalogDataPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "AnalogData");
- LampDataPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "LampData");
- AlarmPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "Alarm");
- WarnPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "Warn");
- IOPLC = DbHelper.Db.Queryable<PlcItemTypeModel>().First(x => x.PlcItemType == "IO");
- }
- /// <summary>
- /// 连接PLC
- /// </summary>
- /// <returns></returns>
- public bool ConnectPlc()
- {
- try
- {
- melsec_net = new MelsecMcNet(ConfigurationManager.AppSettings["PLCAddr"], Convert.ToInt32(ConfigurationManager.AppSettings["PLCPort"]));
- OperateResult connect = melsec_net.ConnectServer();
- if (connect.IsSuccess)
- {
- ;
- return true;
- }
- else
- {
-
- return false;
- }
- }
- catch (Exception ex)
- {
-
- return false;
- }
- }
- /// <summary>
- /// 断开PLC连接
- /// </summary>
- public void DisConnectPlc()
- {
- // 暂停所有PLC采集线程。
- if (taskList.Count > 0)
- {
- tokenSource?.Cancel();
- foreach (Task item in taskList)
- {
- while (!item.IsCompleted)
- {
- }
- item.Dispose();
- }
- taskList.Clear();
- tokenSource?.Dispose();
-
- }
- OperateResult result = melsec_net?.ConnectClose();
- if (result.IsSuccess)
- {
-
- }
- else
- {
-
- }
- melsec_net = null;
- }
- #region PLC数据采集主业务线程
- public void StartPlcLogMonitor()
- {
- AxisDataLogTask();
- AxisData2LogTask();
- AxisData3LogTask();
- AxisData4LogTask();
- AxisData5LogTask();
- GlassInformationLogTask();
- LampUserTimeLogTask();
- RecipeBodyLogTask();
- SYSParaLogTask();
- RobotInPLCLogTask();
- RobotOutPLCLogTask();
- YMeasureDataLogTask();
- EQPStatusLogTask();
- RecipeBodyLogTask();
- AnalogDataLogTask();
- LampDataLogTask();
- AlarmLogTask();
- WarnLogTask();
- IOLogTask();
- }
- private void AxisDataLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
-
- if (AxisDataPLC is null)
- {
- continue;
- }
- OperateResult<bool> isAxisData = melsec_net.ReadBool(AxisDataPLC.ReportFlagAddress);
- if (isAxisData.IsSuccess && isAxisData.Content)
- {
- OperateResult<short[]> axisDataRet = melsec_net.ReadInt16(AxisDataPLC.PlcItemAddressType + AxisDataPLC.PlcItemStartAddress, (ushort)AxisDataPLC.PlcItemAddressLength);
- List<PlcItemModel> axisDataPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AxisDataPLC.PlcItemType).ToList();
- if (!axisDataRet.IsSuccess || axisDataRet.Content.Length <= 0 || axisDataPlcItemLst is null || axisDataPlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in axisDataPlcItemLst)
- {
- ParsePlcItemData(plcItem, axisDataRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- AxisDataModel axisData = MapPlcItemToObjectProperty<AxisDataModel>(axisDataPlcItemLst);
-
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(AxisDataPLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(AxisDataPLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(AxisDataPLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void AxisData2LogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (AxisData2PLC is null)
- {
- continue;
- }
- OperateResult<bool> isAxisData = melsec_net.ReadBool(AxisData2PLC.ReportFlagAddress);
- if (isAxisData.IsSuccess && isAxisData.Content)
- {
- OperateResult<short[]> axisDatatRet = melsec_net.ReadInt16(AxisData2PLC.PlcItemAddressType + AxisData2PLC.PlcItemStartAddress, (ushort)AxisData2PLC.PlcItemAddressLength);
- List<PlcItemModel> axisDataPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AxisData2PLC.PlcItemType).ToList();
- if (!axisDatatRet.IsSuccess || axisDatatRet.Content.Length <= 0 || axisDataPlcItemLst is null || axisDataPlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in axisDataPlcItemLst)
- {
- ParsePlcItemData(plcItem, axisDatatRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- AxisData2 axisData = MapPlcItemToObjectProperty<AxisData2>(axisDataPlcItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(AxisData2PLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(AxisData2PLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(AxisData2PLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void AxisData3LogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (AxisData3PLC is null)
- {
- continue;
- }
- OperateResult<bool> isAxisData = melsec_net.ReadBool(AxisData3PLC.ReportFlagAddress);
- if (isAxisData.IsSuccess && isAxisData.Content)
- {
- OperateResult<short[]> axisDataRet = melsec_net.ReadInt16(AxisData3PLC.PlcItemAddressType + AxisData3PLC.PlcItemStartAddress, (ushort)AxisData3PLC.PlcItemAddressLength);
- List<PlcItemModel> axisDataPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AxisData3PLC.PlcItemType).ToList();
- if (!axisDataRet.IsSuccess || axisDataRet.Content.Length <= 0 || axisDataPlcItemLst is null || axisDataPlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in axisDataPlcItemLst)
- {
- ParsePlcItemData(plcItem, axisDataRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- AxisData3 axisData = MapPlcItemToObjectProperty<AxisData3>(axisDataPlcItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(AxisData3PLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(AxisData3PLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(AxisData3PLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void AxisData4LogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (AxisData4PLC is null)
- {
- continue;
- }
- OperateResult<bool> isAxisData = melsec_net.ReadBool(AxisData4PLC.ReportFlagAddress);
- if (isAxisData.IsSuccess && isAxisData.Content)
- {
- OperateResult<short[]> axisDataRet = melsec_net.ReadInt16(AxisData4PLC.PlcItemAddressType + AxisData4PLC.PlcItemStartAddress, (ushort)AxisData4PLC.PlcItemAddressLength);
- List<PlcItemModel> axisDataPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AxisData4PLC.PlcItemType).ToList();
- if (!axisDataRet.IsSuccess || axisDataRet.Content.Length <= 0 || axisDataPlcItemLst is null || axisDataPlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in axisDataPlcItemLst)
- {
- ParsePlcItemData(plcItem, axisDataRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- AxisData4 axisData = MapPlcItemToObjectProperty<AxisData4>(axisDataPlcItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(AxisData4PLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(AxisData4PLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(AxisData4PLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void AxisData5LogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (AxisData5PLC is null)
- {
- continue;
- }
- OperateResult<bool> isAxisData = melsec_net.ReadBool(AxisData5PLC.ReportFlagAddress);
- if (isAxisData.IsSuccess && isAxisData.Content)
- {
- OperateResult<short[]> axisDataRet = melsec_net.ReadInt16(AxisData5PLC.PlcItemAddressType + AxisData5PLC.PlcItemStartAddress, (ushort)AxisData5PLC.PlcItemAddressLength);
- List<PlcItemModel> axisDataPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AxisData5PLC.PlcItemType).ToList();
- if (!axisDataRet.IsSuccess || axisDataRet.Content.Length <= 0 || axisDataPlcItemLst is null || axisDataPlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in axisDataPlcItemLst)
- {
- ParsePlcItemData(plcItem, axisDataRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- AxisData5 axisData = MapPlcItemToObjectProperty<AxisData5>(axisDataPlcItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(AxisData5PLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(AxisData5PLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(AxisData5PLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- /// <summary>
- /// servoAxis
- /// </summary>
- private void ServoAxisStatusLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (ServoAxisStautsPLC is null)
- {
- continue;
- }
- OperateResult<bool> isServoAxis = melsec_net.ReadBool(ServoAxisStautsPLC.ReportFlagAddress);
- if (isServoAxis.IsSuccess && isServoAxis.Content)
- {
- OperateResult<short[]> servoAxisRet = melsec_net.ReadInt16(ServoAxisStautsPLC.PlcItemAddressType + ServoAxisStautsPLC.PlcItemStartAddress, (ushort)ServoAxisStautsPLC.PlcItemAddressLength);
- List<PlcItemModel> servoAxisPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == ServoAxisStautsPLC.PlcItemType).ToList();
- if (!servoAxisRet.IsSuccess || servoAxisRet.Content.Length <= 0 || servoAxisPlcItemLst is null || servoAxisPlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in servoAxisPlcItemLst)
- {
- ParsePlcItemData(plcItem, servoAxisRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- ServoAxisStatusModel axisData = MapPlcItemToObjectProperty<ServoAxisStatusModel>(servoAxisPlcItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(ServoAxisStautsPLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(ServoAxisStautsPLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(ServoAxisStautsPLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void GlassInformationLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (GlassInformationPLC is null)
- {
- continue;
- }
- OperateResult<bool> isServoAxis = melsec_net.ReadBool(GlassInformationPLC.ReportFlagAddress);
- if (isServoAxis.IsSuccess && isServoAxis.Content)
- {
- OperateResult<short[]> glassInfoRet = melsec_net.ReadInt16(GlassInformationPLC.PlcItemAddressType + GlassInformationPLC.PlcItemStartAddress, (ushort)GlassInformationPLC.PlcItemAddressLength);
- List<PlcItemModel> servoAxisPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == GlassInformationPLC.PlcItemType).ToList();
- if (!glassInfoRet.IsSuccess || glassInfoRet.Content.Length <= 0 || servoAxisPlcItemLst is null || servoAxisPlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in servoAxisPlcItemLst)
- {
- ParsePlcItemData(plcItem, glassInfoRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- GlassInformationModel glassInfoData = MapPlcItemToObjectProperty<GlassInformationModel>(servoAxisPlcItemLst);
- glassInfoData.LordingTime = DateTime.Now;
- glassInfoData.UnlordingTime = DateTime.Now;
- glassInfoData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(glassInfoData).ExecuteCommand();
- melsec_net.Write(GlassInformationPLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(GlassInformationPLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(GlassInformationPLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void LampUserTimeLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (LampUseTimePLC is null)
- {
- continue;
- }
- OperateResult<bool> isLampUse = melsec_net.ReadBool(LampUseTimePLC.ReportFlagAddress);
- if (isLampUse.IsSuccess && isLampUse.Content)
- {
- OperateResult<short[]> glassInfoRet = melsec_net.ReadInt16(LampUseTimePLC.PlcItemAddressType + LampUseTimePLC.PlcItemStartAddress, (ushort)LampUseTimePLC.PlcItemAddressLength);
- List<PlcItemModel> glassInfoPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == LampUseTimePLC.PlcItemType).ToList();
- if (!glassInfoRet.IsSuccess || glassInfoRet.Content.Length <= 0 || glassInfoPlcItemLst is null || glassInfoPlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in glassInfoPlcItemLst)
- {
- ParsePlcItemData(plcItem, glassInfoRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- LampUseTimeModel axisData = MapPlcItemToObjectProperty<LampUseTimeModel>(glassInfoPlcItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(LampUseTimePLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(LampUseTimePLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(LampUseTimePLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void RecipeBodyLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (RecipeBodyPLC is null)
- {
- continue;
- }
- OperateResult<bool> isLampUse = melsec_net.ReadBool(RecipeBodyPLC.ReportFlagAddress);
- if (isLampUse.IsSuccess && isLampUse.Content)
- {
- OperateResult<short[]> recipeBodyRet = melsec_net.ReadInt16(RecipeBodyPLC.PlcItemAddressType + RecipeBodyPLC.PlcItemStartAddress, (ushort)RecipeBodyPLC.PlcItemAddressLength);
- List<PlcItemModel> reciprBodyPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == RecipeBodyPLC.PlcItemType).ToList();
- if (!recipeBodyRet.IsSuccess || recipeBodyRet.Content.Length <= 0 || reciprBodyPlcItemLst is null || reciprBodyPlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in reciprBodyPlcItemLst)
- {
- ParsePlcItemData(plcItem, recipeBodyRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- RecipeBodyModel axisData = MapPlcItemToObjectProperty<RecipeBodyModel>(reciprBodyPlcItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(RecipeBodyPLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(RecipeBodyPLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(RecipeBodyPLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void SYSParaLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (SYSParaPLC is null)
- {
- continue;
- }
- OperateResult<bool> isSysPara = melsec_net.ReadBool(SYSParaPLC.ReportFlagAddress);
- if (isSysPara.IsSuccess && isSysPara.Content)
- {
- OperateResult<short[]> sysParaRet = melsec_net.ReadInt16(SYSParaPLC.PlcItemAddressType + SYSParaPLC.PlcItemStartAddress, (ushort)SYSParaPLC.PlcItemAddressLength);
- List<PlcItemModel> sysparaItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == SYSParaPLC.PlcItemType).ToList();
- if (!sysParaRet.IsSuccess || sysParaRet.Content.Length <= 0 || sysparaItemLst is null || sysparaItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in sysparaItemLst)
- {
- ParsePlcItemData(plcItem, sysParaRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- SysParaModel axisData = MapPlcItemToObjectProperty<SysParaModel>(sysparaItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(SYSParaPLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(SYSParaPLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(SYSParaPLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void RobotInPLCLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (RobotInterfaceInPLC is null)
- {
- continue;
- }
- OperateResult<bool> isRobotIn = melsec_net.ReadBool(RobotInterfaceInPLC.ReportFlagAddress);
- if (isRobotIn.IsSuccess && isRobotIn.Content)
- {
- OperateResult<short[]> robotInRet = melsec_net.ReadInt16(RobotInterfaceInPLC.PlcItemAddressType + RobotInterfaceInPLC.PlcItemStartAddress, (ushort)RobotInterfaceInPLC.PlcItemAddressLength);
- List<PlcItemModel> robotInPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == RobotInterfaceInPLC.PlcItemType).ToList();
- if (!robotInRet.IsSuccess || robotInRet.Content.Length <= 0 || robotInPlcItemLst is null || robotInPlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in robotInPlcItemLst)
- {
- ParsePlcItemData(plcItem, robotInRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- RobotInterfaceInModel axisData = MapPlcItemToObjectProperty<RobotInterfaceInModel>(robotInPlcItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(RobotInterfaceInPLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isRobotInReply = melsec_net.ReadBool(RobotInterfaceInPLC.ReplyFlagAddress);
- if (isRobotInReply.IsSuccess && isRobotInReply.Content)
- {
- melsec_net.Write(RobotInterfaceInPLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void RobotOutPLCLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (RobotInterfaceOutPLC is null)
- {
- continue;
- }
- OperateResult<bool> isRobotOut = melsec_net.ReadBool(RobotInterfaceOutPLC.ReportFlagAddress);
- if (isRobotOut.IsSuccess && isRobotOut.Content)
- {
- OperateResult<short[]> robotInRet = melsec_net.ReadInt16(RobotInterfaceOutPLC.PlcItemAddressType + RobotInterfaceOutPLC.PlcItemStartAddress, (ushort)RobotInterfaceOutPLC.PlcItemAddressLength);
- List<PlcItemModel> robotInPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == RobotInterfaceOutPLC.PlcItemType).ToList();
- if (!robotInRet.IsSuccess || robotInRet.Content.Length <= 0 || robotInPlcItemLst is null || robotInPlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in robotInPlcItemLst)
- {
- ParsePlcItemData(plcItem, robotInRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- RobotInterfaceOutModel axisData = MapPlcItemToObjectProperty<RobotInterfaceOutModel>(robotInPlcItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(RobotInterfaceOutPLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isRobotInReply = melsec_net.ReadBool(RobotInterfaceOutPLC.ReplyFlagAddress);
- if (isRobotInReply.IsSuccess && isRobotInReply.Content)
- {
- melsec_net.Write(RobotInterfaceOutPLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void YMeasureDataLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (YMeasureDataPLC is null)
- {
- continue;
- }
- OperateResult<bool> isYMeasure = melsec_net.ReadBool(YMeasureDataPLC.ReportFlagAddress);
- if (isYMeasure.IsSuccess && isYMeasure.Content)
- {
- OperateResult<short[]> yMeasureRet = melsec_net.ReadInt16(YMeasureDataPLC.PlcItemAddressType + YMeasureDataPLC.PlcItemStartAddress, (ushort)YMeasureDataPLC.PlcItemAddressLength);
- List<PlcItemModel> glassInfoPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == YMeasureDataPLC.PlcItemType).ToList();
- if (!yMeasureRet.IsSuccess || yMeasureRet.Content.Length <= 0 || glassInfoPlcItemLst is null || glassInfoPlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in glassInfoPlcItemLst)
- {
- ParsePlcItemData(plcItem, yMeasureRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- YMeasureDataModel axisData = MapPlcItemToObjectProperty<YMeasureDataModel>(glassInfoPlcItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(YMeasureDataPLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(YMeasureDataPLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(YMeasureDataPLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void EQPStatusLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (EQPStatusPLC is null)
- {
- continue;
- }
- OperateResult<bool> isEQPStatus = melsec_net.ReadBool(EQPStatusPLC.ReportFlagAddress);
- if (isEQPStatus.IsSuccess && isEQPStatus.Content)
- {
- OperateResult<short[]> eqpStatusRet = melsec_net.ReadInt16(EQPStatusPLC.PlcItemAddressType + EQPStatusPLC.PlcItemStartAddress, (ushort)EQPStatusPLC.PlcItemAddressLength);
- List<PlcItemModel> eqpPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == EQPStatusPLC.PlcItemType).ToList();
- if (!eqpStatusRet.IsSuccess || eqpStatusRet.Content.Length <= 0 || eqpPlcItemLst is null || eqpPlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in eqpPlcItemLst)
- {
- ParsePlcItemData(plcItem, eqpStatusRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- EQPStatusModel axisData = MapPlcItemToObjectProperty<EQPStatusModel>(eqpPlcItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(EQPStatusPLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(EQPStatusPLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(EQPStatusPLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void RecipeCountLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (RecipeCountPLC is null)
- {
- continue;
- }
- OperateResult<bool> isRecipeCount = melsec_net.ReadBool(RecipeCountPLC.ReportFlagAddress);
- if (isRecipeCount.IsSuccess && isRecipeCount.Content)
- {
- OperateResult<short[]> recipeRet = melsec_net.ReadInt16(RecipeCountPLC.PlcItemAddressType + RecipeCountPLC.PlcItemStartAddress, (ushort)RecipeCountPLC.PlcItemAddressLength);
- List<PlcItemModel> recipePlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == RecipeCountPLC.PlcItemType).ToList();
- if (!recipeRet.IsSuccess || recipeRet.Content.Length <= 0 || recipePlcItemLst is null || recipePlcItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in recipePlcItemLst)
- {
- ParsePlcItemData(plcItem, recipeRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- RecipeCountModel axisData = MapPlcItemToObjectProperty<RecipeCountModel>(recipePlcItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(RecipeCountPLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(RecipeCountPLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(RecipeCountPLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void AnalogDataLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (AnalogDataPLC is null)
- {
- continue;
- }
-
- {
-
- List<PlcItemModel> analogPlcItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AnalogDataPLC.PlcItemType).ToList();
- foreach(var item in analogPlcItemLst)
- {
- item.Value= melsec_net.ReadInt16(item.PlcAddr).Content;
-
- }
-
- //foreach (PlcItemModel plcItem in analogPlcItemLst)
- //{
- // ParsePlcItemData(plcItem, analogDataRet.Content, 0);
- //}
- //ToDo:把PLC数据插入到数据库中
- AnalogDataModel analogData = MapPlcItemToObjectProperty<AnalogDataModel>(analogPlcItemLst);
- //几个字段特殊读取
- //OperateResult<short> stageMain = melsec_net.ReadInt16("D16640");
- //analogData.StageMainCDA= stageMain.Content;
- //OperateResult<short> irradiator = melsec_net.ReadInt16("D16642");
- //analogData.IrradiatorMainCDA = irradiator.Content;
- //OperateResult<short> z1Current = melsec_net.ReadInt16("D16650");
- //analogData.Z1Current = z1Current.Content;
- //OperateResult<short> z2Current = melsec_net.ReadInt16("D16652");
- //analogData.Z2Current = z2Current.Content;
- //OperateResult<short> z3Current = melsec_net.ReadInt16("D16654");
- //analogData.Z3Current = z3Current.Content;
- //OperateResult<short> z4Current = melsec_net.ReadInt16("D16656");
- //analogData.Z4Current = z4Current.Content;
- analogData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(analogData).ExecuteCommand();
-
- }
-
- Task.Delay(ReadDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void LampDataLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (LampDataPLC is null)
- {
- continue;
- }
- {
- //OperateResult<short[]> lampDataRet = melsec_net.ReadInt16(LampDataPLC.PlcItemAddressType + LampDataPLC.PlcItemStartAddress, (ushort)AnalogDataPLC.PlcItemAddressLength);
- List<PlcItemModel> lampDataItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == LampDataPLC.PlcItemType).ToList();
- foreach (var item in lampDataItemLst)
- {
- item.Value = melsec_net.ReadInt16(item.PlcAddr).Content;
- }
- //ToDo:把PLC数据插入到数据库中
- LampDataModel lampData = MapPlcItemToObjectProperty<LampDataModel>(lampDataItemLst);
- //几个字段特殊读取
- lampData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(lampData).ExecuteCommand();
- }
- Task.Delay(ReadDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void AlarmLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (AlarmPLC is null)
- {
- continue;
- }
- OperateResult<bool> isAlarm = melsec_net.ReadBool(AlarmPLC.ReportFlagAddress);
- if (isAlarm.IsSuccess && isAlarm.Content)
- {
- OperateResult<short[]> alarmRet = melsec_net.ReadInt16(AlarmPLC.PlcItemAddressType + AlarmPLC.PlcItemStartAddress, (ushort)AlarmPLC.PlcItemAddressLength);
- List<PlcItemModel> alarmItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == AlarmPLC.PlcItemType).ToList();
- if (!alarmRet.IsSuccess || alarmRet.Content.Length <= 0 || alarmItemLst is null || alarmItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in alarmItemLst)
- {
- ParsePlcItemData(plcItem, alarmRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- AlarmModel alarmData = MapPlcItemToObjectProperty<AlarmModel>(alarmItemLst);
- alarmData.AlarmDescribe = "测试报警";
- alarmData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(alarmData).ExecuteCommand();
- melsec_net.Write(AlarmPLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(AlarmPLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(AlarmPLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void WarnLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (WarnPLC is null)
- {
- continue;
- }
- OperateResult<bool> isLampUse = melsec_net.ReadBool(WarnPLC.ReportFlagAddress);
- if (isLampUse.IsSuccess && isLampUse.Content)
- {
- OperateResult<short[]> warnRet = melsec_net.ReadInt16(WarnPLC.PlcItemAddressType + WarnPLC.PlcItemStartAddress, (ushort)WarnPLC.PlcItemAddressLength);
- List<PlcItemModel> warnItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == WarnPLC.PlcItemType).ToList();
- if (!warnRet.IsSuccess || warnRet.Content.Length <= 0 || warnItemLst is null || warnItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in warnItemLst)
- {
- ParsePlcItemData(plcItem, warnRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- WarnModel warnData = MapPlcItemToObjectProperty<WarnModel>(warnItemLst);
- warnData.WarnDescribe = "测试警告";
- warnData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(warnData).ExecuteCommand();
- melsec_net.Write(WarnPLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(WarnPLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(WarnPLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void IOLogTask()
- {
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (IOPLC is null)
- {
- continue;
- }
- OperateResult<bool> isLampUse = melsec_net.ReadBool(IOPLC.ReportFlagAddress);
- if (isLampUse.IsSuccess && isLampUse.Content)
- {
- OperateResult<short[]> ioRet = melsec_net.ReadInt16(IOPLC.PlcItemAddressType + IOPLC.PlcItemStartAddress, (ushort)IOPLC.PlcItemAddressLength);
- List<PlcItemModel> ioItemLst = DbHelper.Db.Queryable<PlcItemModel>().Where(x => x.PlcItemType == IOPLC.PlcItemType).ToList();
- if (!ioRet.IsSuccess || ioRet.Content.Length <= 0 || ioItemLst is null || ioItemLst.Count <= 0)
- {
- continue;
- }
- foreach (PlcItemModel plcItem in ioItemLst)
- {
- ParsePlcItemData(plcItem, ioRet.Content, 0);
- }
- //ToDo:把PLC数据插入到数据库中
- IOModel axisData = MapPlcItemToObjectProperty<IOModel>(ioItemLst);
- axisData.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(axisData).ExecuteCommand();
- melsec_net.Write(IOPLC.ReplyFlagAddress, true);
- }
- else
- {
- OperateResult<bool> isJobEventReply = melsec_net.ReadBool(IOPLC.ReplyFlagAddress);
- if (isJobEventReply.IsSuccess && isJobEventReply.Content)
- {
- melsec_net.Write(IOPLC.ReplyFlagAddress, false);
- }
- }
- Task.Delay(TaskDelayTime).Wait();
- }
- }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
- }
- private void ParsePlcItemData(PlcItemModel plcItem, short[] retLst, int startIndex)
- {
- if (plcItem.PlcItemFormat == 0) // 解析为int类型
- {
- if (plcItem.IsFullWord == 1) // 全字解析,不进行二进制拆分
- {
- short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
- if (plcItem.WordSize == 1)
- {
- plcItem.Value = tmpVal[0];
- }
- else if (plcItem.WordSize == 2)
- {
- plcItem.Value = (tmpVal[0] << 16) + tmpVal[1];
- }
- }
- else // 非全字解析,需要对数据进行二进制拆分,根据具体的二进制位数,进行数据转换。
- {
- short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
- string binStr = string.Empty;
- for (int j = 0; j < tmpVal.Length; j++)
- {
- binStr += string.Concat(Convert.ToString(tmpVal[j], 2).PadLeft(16, '0').Reverse());
- }
- string retStr = string.Concat(binStr.Skip(plcItem.BinSubOffset).Take(plcItem.BinSize).ToArray().Reverse());
- plcItem.Value = Convert.ToInt32(retStr, 2);
- }
- }
- else if (plcItem.PlcItemFormat == 1) // 解析为ASCII码字符串, 数据直接转换为对应的ASCII码字符串数据进行存储
- {
- short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
- List<byte> bytes = new();
- for (int j = 0; j < plcItem.WordSize; j++)
- {
- //bytes.AddRange(BitConverter.GetBytes(tmpVal[j]));
- bytes.Add((byte)tmpVal[j]);
- }
- ASCIIEncoding asciiMd = new();
- //Console.WriteLine(BitConverter.ToString(bytes.ToArray()));
- char[] ascii = asciiMd.GetChars(bytes.ToArray());
- string retStr = string.Empty;
- for (int j = 0; j < bytes.Count; j++)
- {
- retStr += ascii[j];
- }
- plcItem.Value = retStr;
- }
- else if (plcItem.PlcItemFormat == 2) // 解析为二进制数据,按照二进制具体位置和位数,重新组装数据
- {
- if (plcItem.IsFullWord == 1) // 全字解析 , 直接存储数据的二进制字符串
- {
- short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
- string binStr = string.Empty;
- for (int j = 0; j < tmpVal.Length; j++)
- {
- binStr += string.Concat(Convert.ToString(tmpVal[j], 2).PadLeft(16, '0').Reverse());
- }
- plcItem.Value = binStr;
- }
- else // 非全字解析 按照二进制具体位置和位数,存储相应的二进制字符串
- {
- short[] tmpVal = retLst.Skip(startIndex + plcItem.WordSubOffset).Take(plcItem.WordSize).ToArray();
- string binStr = string.Empty;
- for (int j = 0; j < tmpVal.Length; j++)
- {
- binStr += string.Concat(Convert.ToString(tmpVal[j], 2).PadLeft(16, '0').Reverse());
- }
- string retStr = string.Concat(binStr.Skip(plcItem.BinSubOffset).Take(plcItem.BinSize).ToArray().Reverse());
- plcItem.Value = retStr;
- }
- }
- }
- /// <summary>
- /// 根据解析出来的PLC点位数据,创建对应的数据库实体对象。
- /// </summary>
- /// <typeparam name="T">对应的数据库实体对象</typeparam>
- /// <param name="plcItemLst">解析出来的PLC点位数据</param>
- /// <returns></returns>
- private static T MapPlcItemToObjectProperty<T>(List<PlcItemModel> plcItemLst) where T : class, new()
- {
- T tmp = new();
- PropertyInfo[] propertys = tmp.GetType().GetProperties();
- foreach (PropertyInfo property in propertys)
- {
- foreach (PlcItemModel item in plcItemLst)
- {
- if (property.Name == item.PlcItemCode)
- {
- if (property.PropertyType.FullName == typeof(DateTime).FullName)
- {
- property.SetValue(tmp, item.Value is null ? new DateTime() : Convert.ToDateTime(item.Value), null);
- }
- else if (property.PropertyType.FullName == typeof(int).FullName)
- {
- property.SetValue(tmp, item.Value is null ? 0 : Convert.ToInt32(item.Value), null);
- }
- else if (property.PropertyType.FullName == typeof(decimal).FullName)
- {
- property.SetValue(tmp, item.Value is null ? 0 : Convert.ToDecimal(item.Value), null);
- }
- else if (property.PropertyType.FullName == typeof(string).FullName)
- {
- property.SetValue(tmp, item.Value is null ? null : item.Value.ToString(), null);
- }
- else if (property.PropertyType.FullName == typeof(bool).FullName || property.PropertyType.FullName == typeof(bool).FullName)
- {
- //property.SetValue(tmp, item.Value is null ? false : Convert.ToBoolean(item.Value), null);
- //VS编辑器建议的简化写法。 非常巧妙!!!!!!
- property.SetValue(tmp, item.Value is not null && Convert.ToBoolean(item.Value), null);
- }
- else if (property.PropertyType.BaseType.FullName == typeof(byte[]).FullName)
- {
- property.SetValue(tmp, item.Value is null ? null : item.Value, null);
- }
- else if (property.PropertyType.BaseType.FullName == typeof(Enum).FullName)
- {
- property.SetValue(tmp, Enum.Parse(property.PropertyType, (item.Value ?? 0).ToString()), null);
- }
- else
- {
- property.SetValue(tmp, item.Value, null);
- }
- break;
- }
- }
- }
- return tmp;
- }
- #endregion
- }
- }
|