12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550 |
- using B20UVLog.Models;
- using B20UVLog.Pages;
- using NetTaste;
- 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;
- //字典字段
- private List<DictModel> axleAlarm=new List<DictModel>();
- private List<DictModel> axleWarn = new List<DictModel>();
- private List<DictModel> deviceAlarm = new List<DictModel>();
- private List<DictModel> deviceWarn = new List<DictModel>();
- #endregion
- /// <summary>
- /// Log采集业务类 初始化数据
- /// </summary>
- /// <exception cref="Exception"></exception>
- public void Init()
- {
- var viewdata = DbHelper.Db.Queryable<AxisDataAllModel>().ToList();
- 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");
- //获取字典中的所有字段
- axleAlarm = DbHelper.Db.Queryable<DictModel>().ToList().FindAll(x=>x.Type=="Alarm"&&x.Code=="Axle");
- axleWarn = DbHelper.Db.Queryable<DictModel>().ToList().FindAll(x => x.Type == "Warn" && x.Code == "Axle");
- deviceAlarm = DbHelper.Db.Queryable<DictModel>().ToList().FindAll(x => x.Type == "Alarm" && x.Code == "Device");
- deviceWarn = DbHelper.Db.Queryable<DictModel>().ToList().FindAll(x => x.Type == "Warn" && x.Code == "Warn");
- }
- /// <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();
- ServoAxisStatusLogTask();
- 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数据插入到数据库中
- ServoAxisStautsOriginal servoOrignal = MapPlcItemToObjectProperty<ServoAxisStautsOriginal>(servoAxisPlcItemLst);
- //ServoAxisStautsOriginal 将不为0的字段转为ServoAxisStautsModel
- var nonZeroProperties = GetNonZeroProperties(servoOrignal);
- foreach (var property in nonZeroProperties)
- {
- string name = property.Key;
- int value = property.Value;
- ServoAxisStatusModel servoAxisStatusModel = new ServoAxisStatusModel();
- if(name.Contains("Warn"))
- {
- servoAxisStatusModel.AxisName = name.Substring(0, 4);
- servoAxisStatusModel.WarningCode = value;
- servoAxisStatusModel.WarningDescribe =axleWarn?.FirstOrDefault(x=>x.Key==value.ToString().Trim())?.Value;
- }
- else
- {
- if (name.Contains("Alarm"))
- {
- servoAxisStatusModel.AxisName = name.Substring(0, 4);
- servoAxisStatusModel.AlarmCode = value;
- servoAxisStatusModel.AlarmDescribe = axleAlarm?.FirstOrDefault(x => x.Key == value.ToString().Trim())?.Value;
- }
- }
- servoAxisStatusModel.RecordTime = DateTime.Now;
- DbHelper.Db.Insertable(servoAxisStatusModel).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);
-
- //读取时间
- OperateResult<short[]> glassInfoRetTime = melsec_net.ReadInt16("D16100", 6);
- if (!glassInfoRetTime.IsSuccess || glassInfoRetTime.Content.Length <= 0)
- {
- continue;
- }
- Convert16BitBCDToTwoInts(glassInfoRetTime.Content[0], out int yy1, out int mm1);
- Convert16BitBCDToTwoInts(glassInfoRetTime.Content[1], out int dd1, out int hh1);
- Convert16BitBCDToTwoInts(glassInfoRetTime.Content[2], out int ff1, out int ss1);
- Convert16BitBCDToTwoInts(glassInfoRetTime.Content[3], out int yy2, out int mm2);
- Convert16BitBCDToTwoInts(glassInfoRetTime.Content[4], out int dd2, out int hh2);
- Convert16BitBCDToTwoInts(glassInfoRetTime.Content[5], out int ff2, out int ss2);
- glassInfoData.LordingTime = new DateTime(yy1, mm1, dd1, hh1, ff1, ss1);
- glassInfoData.UnlordingTime = new DateTime(yy2, mm2, dd2, hh2, ff2, ss2);
- 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 = deviceAlarm?.FirstOrDefault(x=>x.Key.Trim()==alarmData.AlarmCode.ToString())?.Value;
- 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 = deviceWarn?.FirstOrDefault(x => x.Key.Trim() == warnData.WarnCode.ToString())?.Value;
- 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()
- {
-
- List<IONewModel> ioNewModelList = new List<IONewModel>();
- DateTime recordTime;
- taskList.Add(Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (tokenSource.IsCancellationRequested)
- {
- break;
- }
- if (IOPLC is null)
- {
- continue;
- }
-
- {
- 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;
- }
- recordTime = DateTime.Now;
- ioNewModelList.Clear();
- foreach (PlcItemModel plcItem in ioItemLst)
- {
- ParsePlcItemData(plcItem, ioRet.Content, 0);
- //转化为
- IONewModel ioNewModel = new IONewModel();
- ioNewModel.IOName = plcItem.PlcItemCode;
- if (plcItem.PlcItemCode.Contains("Stage"))
- {
- ioNewModel.IOType = "Stage";
- }
- else
- {
- if (plcItem.PlcItemCode.Contains("IrradiatorInput"))
- {
- ioNewModel.IOType = "IrradiatorInput";
- }
- else
- {
- if (plcItem.PlcItemCode.Contains("IrradiatorOutput"))
- {
- ioNewModel.IOType = "IrradiatorOutput";
- }
- }
- }
- ioNewModel.IOValue = Convert.ToInt32(plcItem.Value);
- ioNewModel.RecordTime = recordTime;
- ioNewModelList.Add(ioNewModel);
- }
-
-
- //ToDo:把PLC数据插入到数据库中
-
- DbHelper.Db.Insertable(ioNewModelList).ExecuteCommand();
-
- }
-
- Task.Delay(500).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;
- }
- private Dictionary<string, int> GetNonZeroProperties(object obj)
- {
- Dictionary<string, int> nonZeroProperties = new Dictionary<string, int>();
- PropertyInfo[] properties = obj.GetType().GetProperties();
- foreach (PropertyInfo property in properties)
- {
- if (property.PropertyType == typeof(int))
- {
- int value = (int)property.GetValue(obj);
- if (value != 0)
- {
- nonZeroProperties.Add(property.Name, value);
- }
- }
- }
- return nonZeroProperties;
- }
- // 将8位BCD码转化为整数
- private int BCDToInt(byte bcdValue)
- {
- int result = 0;
- int multiplier = 1;
- while (bcdValue > 0)
- {
- int digit = bcdValue & 0xF;
- result += digit * multiplier;
- bcdValue >>= 4;
- multiplier *= 10;
- }
- return result;
- }
- // 将16位BCD码转化为两个整数
- private void Convert16BitBCDToTwoInts(short bcdValue, out int int1, out int int2)
- {
- // 高8位BCD码
- byte highByte = (byte)((bcdValue >> 8) & 0xFF);
- // 低8位BCD码
- byte lowByte = (byte)(bcdValue & 0xFF);
- // 分别转换高8位和低8位
- int1 = BCDToInt(highByte);
- int2 = BCDToInt(lowByte);
- }
- #endregion
- }
- }
|