You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
287 lines
11 KiB
287 lines
11 KiB
using Common.Const;
|
|
using Entity.DbModel.Station;
|
|
using HybirdFrameworkCore.Autofac.Attribute;
|
|
using HybirdFrameworkCore.Const;
|
|
using HybirdFrameworkDriver.ModbusTcpMaster;
|
|
using log4net;
|
|
using Repository.Station;
|
|
using Service.Plc.Msg;
|
|
|
|
namespace Service.Plc.Client;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[Scope]
|
|
public class PlcClient : ModbusTcpMaster
|
|
{
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(PlcClient));
|
|
|
|
private long TaskNo=0;
|
|
public BinInfoRepository BinInfoRepository { get; set; }
|
|
public EquipAlarmDefineRepository EquipAlarmDefineRepository { get; set; }
|
|
public EquipAlarmRecordRepository EquipAlarmRecordRepository { get; set; }
|
|
public EquipAlarmProcessRecordRepository EquipAlarmProcessRecordRepository { get; set; }
|
|
|
|
|
|
//修改当前任务
|
|
public void ExChangeTaskNo(long taskNo)
|
|
{
|
|
Interlocked.Exchange(ref TaskNo, taskNo);
|
|
}
|
|
|
|
//重置当前任务
|
|
public void ResetTaskNo()
|
|
{
|
|
Interlocked.Exchange(ref TaskNo, 0);
|
|
}
|
|
|
|
//读取当前任务
|
|
public long ReadTaskNo()
|
|
{
|
|
|
|
return Interlocked.Read( ref TaskNo);
|
|
}
|
|
|
|
public PlcClient()
|
|
{
|
|
ReadAction = BatchRead;
|
|
Ip = "172.0.20.66";
|
|
Port = 502;
|
|
Duration = 1000;
|
|
AutoReConnect = true;
|
|
ByteSeq = EndingConst.ByteSeq.BA;
|
|
WordSeq= EndingConst.WordSeq.CD;
|
|
}
|
|
|
|
private void BatchRead(ModbusTcpMaster master)
|
|
{
|
|
var bytes01 = master.BatchReadHolderRegister(1, 115);
|
|
if (bytes01 != null)
|
|
{
|
|
PlcMgr.DataValidityTime = DateTime.Now;
|
|
|
|
ModbusDecoder.Decode<HostToPlc>(bytes01, PlcMgr.HostToPlcData, EndingConst.ByteSeq.AB,
|
|
EndingConst.WordSeq.DC);
|
|
}
|
|
|
|
var bytes02 = master.BatchReadHolderRegister(201, 222);
|
|
if (bytes02 != null)
|
|
{
|
|
PlcMgr.DataValidityTime = DateTime.Now;
|
|
ModbusDecoder.Decode<PlcToHost>(bytes02, PlcMgr.PlcToHostData, EndingConst.ByteSeq.AB,
|
|
EndingConst.WordSeq.DC);
|
|
|
|
UpdateBinInfoCache(PlcMgr.PlcToHostData.MaterialDetec01.Value, "1");
|
|
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec02.Value, "2");
|
|
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec03.Value, "3");
|
|
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec04.Value, "4");
|
|
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec05.Value, "5");
|
|
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec06.Value, "6");
|
|
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec07.Value, "7");
|
|
UpdateBinInfo(PlcMgr.PlcToHostData.MaterialDetec08.Value, "8");
|
|
|
|
}
|
|
|
|
var bytes03 = master.BatchReadHolderRegister(701, 32);
|
|
if (bytes03 != null)
|
|
{
|
|
PlcMgr.LastPlcFaultData = PlcMgr.PlcFaultData;
|
|
PlcMgr.DataValidityTime = DateTime.Now;
|
|
ModbusDecoder.Decode<PlcFault>(bytes03, PlcMgr.PlcFaultData, EndingConst.ByteSeq.AB,
|
|
EndingConst.WordSeq.CD);
|
|
List<string> alarm = new List<string>()
|
|
{
|
|
PlcMgr.PlcFaultData.ErrorCode01.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode02.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode03.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode04.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode05.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode06.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode07.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode08.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode09.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode10.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode11.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode12.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode13.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode14.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode15.Value.ToString(),
|
|
PlcMgr.PlcFaultData.ErrorCode16.Value.ToString(),
|
|
};
|
|
SaveAlarmInfo(alarm);
|
|
}
|
|
}
|
|
|
|
private void SaveAlarmInfo(List<string> lstAlarm)
|
|
{
|
|
string plc = "plc";
|
|
/*if (errorCode != 0)
|
|
{
|
|
EquipAlarmDefine? alarmDefine = EquipAlarmDefineRepository.SelectByEquipCodeAndErrorCode((int)EquipmentType.Plc, plc, errorCode.ToString());
|
|
if (alarmDefine == null)
|
|
{
|
|
Log.Error($"error code {errorCode} has no define");
|
|
return;
|
|
}
|
|
|
|
EquipAlarmRecord record = new EquipAlarmRecord()
|
|
{
|
|
EquipTypeCode = alarmDefine.EquipTypeCode,
|
|
EquipCode = alarmDefine.EquipCode,
|
|
ErrorCode = alarmDefine.ErrorCode,
|
|
ErrorLevel = alarmDefine.ErrorLevel,
|
|
ErrorMsg = alarmDefine.ErrorMsg,
|
|
ProcessMethod = alarmDefine.ProcessMethod
|
|
};
|
|
EquipAlarmRecordRepository.Insert(record);
|
|
}
|
|
else
|
|
{
|
|
EquipAlarmRecord? alarmRecord = EquipAlarmRecordRepository.SelectByEquipCodeAndErrorCode((int)EquipmentType.Plc, plc, errorCode.ToString());
|
|
if (alarmRecord != null)
|
|
{
|
|
EquipAlarmProcessRecord processRecord = new EquipAlarmProcessRecord()
|
|
{
|
|
EquipTypeCode = alarmRecord.EquipTypeCode,
|
|
EquipCode = alarmRecord.EquipCode,
|
|
ErrorCode =alarmRecord.EquipCode,
|
|
ErrorLevel = alarmRecord.ErrorLevel,
|
|
ErrorMsg = alarmRecord.ErrorMsg,
|
|
ProcessMethod = alarmRecord.ProcessMethod,
|
|
ProcessTime = DateTime.Now
|
|
};
|
|
EquipAlarmProcessRecordRepository.Insert(processRecord);
|
|
EquipAlarmRecordRepository.DeleteById(alarmRecord.Id);
|
|
}
|
|
}*/
|
|
|
|
if (lstAlarm.Count > 0)
|
|
{
|
|
#region 有报警比较两边差异,新出现的报警就添加,消失的报警就处理并记录
|
|
|
|
var lstEquipAlarmRecord=EquipAlarmRecordRepository.QueryListByClause(i => i.EquipTypeCode == 3);
|
|
var sqllstAlarm=lstEquipAlarmRecord.Select(obj => obj.ErrorCode).ToList();
|
|
|
|
// 找出实时报警中存在但数据库中不存在的元素
|
|
List<string> uniqueToList1 = lstAlarm.Except(sqllstAlarm).ToList();
|
|
|
|
// 找出数据库中存在但实时报警中不存在的元素
|
|
List<string> uniqueToList2 = sqllstAlarm.Except(lstAlarm).ToList();
|
|
|
|
if (uniqueToList1.Count > 0)
|
|
{//这里要添加新的报警数据
|
|
foreach (var errorCode in uniqueToList1)
|
|
{
|
|
EquipAlarmDefine? alarmDefine = EquipAlarmDefineRepository.SelectByEquipCodeAndErrorCode((int)EquipmentType.Plc, plc, errorCode);
|
|
if (alarmDefine != null)
|
|
{
|
|
EquipAlarmRecord record = new EquipAlarmRecord()
|
|
{
|
|
EquipTypeCode = alarmDefine.EquipTypeCode,
|
|
EquipCode = alarmDefine.EquipCode,
|
|
ErrorCode = alarmDefine.ErrorCode,
|
|
ErrorLevel = alarmDefine.ErrorLevel,
|
|
ErrorMsg = alarmDefine.ErrorMsg,
|
|
ProcessMethod = alarmDefine.ProcessMethod,
|
|
StartTime= DateTime.Now
|
|
};
|
|
EquipAlarmRecordRepository.Insert(record);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (uniqueToList2.Count > 0)
|
|
{//这些是要清除实时报警,并且处理记录的。
|
|
// 使用LINQ找出ErrorCode在uniqueToList2中的EquipAlarmRecord对象
|
|
List<EquipAlarmRecord> filteredObjectList = lstEquipAlarmRecord
|
|
.Where(obj => uniqueToList2.Contains(obj.ErrorCode))
|
|
.ToList();
|
|
foreach (var VARIABLE in filteredObjectList)
|
|
{
|
|
EquipAlarmProcessRecord EquipAlarmProcessRecord = new EquipAlarmProcessRecord();
|
|
EquipAlarmProcessRecord.EquipTypeCode = VARIABLE.EquipTypeCode;
|
|
EquipAlarmProcessRecord.EquipCode = VARIABLE.EquipCode;
|
|
EquipAlarmProcessRecord.ErrorCode = VARIABLE.ErrorCode;
|
|
EquipAlarmProcessRecord.ErrorLevel = VARIABLE.ErrorLevel;
|
|
EquipAlarmProcessRecord.ErrorMsg = VARIABLE.ErrorMsg;
|
|
EquipAlarmProcessRecord.ProcessMethod = VARIABLE.ProcessMethod;
|
|
EquipAlarmProcessRecord.StartTime = VARIABLE.StartTime;
|
|
EquipAlarmProcessRecord.ProcessTime=DateTime.Now;
|
|
|
|
EquipAlarmProcessRecordRepository.Insert(EquipAlarmProcessRecord);
|
|
}
|
|
EquipAlarmRecordRepository.Delete(filteredObjectList);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
else
|
|
{
|
|
#region 没报警把已处理记录更新并删除实时报警
|
|
|
|
var lstEquipAlarmRecord=EquipAlarmRecordRepository.QueryListByClause(i=>i.EquipTypeCode==3);
|
|
if (lstEquipAlarmRecord.Count > 0)
|
|
{
|
|
foreach (var VARIABLE in lstEquipAlarmRecord)
|
|
{
|
|
EquipAlarmProcessRecord EquipAlarmProcessRecord = new EquipAlarmProcessRecord();
|
|
EquipAlarmProcessRecord.EquipTypeCode = VARIABLE.EquipTypeCode;
|
|
EquipAlarmProcessRecord.EquipCode = VARIABLE.EquipCode;
|
|
EquipAlarmProcessRecord.ErrorCode = VARIABLE.ErrorCode;
|
|
EquipAlarmProcessRecord.ErrorLevel = VARIABLE.ErrorLevel;
|
|
EquipAlarmProcessRecord.ErrorMsg = VARIABLE.ErrorMsg;
|
|
EquipAlarmProcessRecord.ProcessMethod = VARIABLE.ProcessMethod;
|
|
EquipAlarmProcessRecord.StartTime = VARIABLE.StartTime;
|
|
EquipAlarmProcessRecord.ProcessTime=DateTime.Now;
|
|
|
|
EquipAlarmProcessRecordRepository.Insert(EquipAlarmProcessRecord);
|
|
}
|
|
}
|
|
EquipAlarmRecordRepository.Delete(lstEquipAlarmRecord);
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
}
|
|
|
|
private void UpdateBinInfo(int exists, string binNo)
|
|
{
|
|
if (exists == 0)
|
|
BinInfoRepository.Update(
|
|
it =>
|
|
new BinInfo(){
|
|
Exists = 0,
|
|
BatteryNo = "-1",
|
|
Soc = (decimal)-1,
|
|
Soe = (decimal)-1,
|
|
Soh = (decimal)-1,
|
|
},
|
|
it => it.No == binNo);
|
|
else
|
|
|
|
BinInfoRepository.Update(it => it.Exists == 1,
|
|
it => it.No == binNo);
|
|
|
|
}
|
|
|
|
private void UpdateBinInfoCache(int exists, string binNo)
|
|
{
|
|
if (exists == 0)
|
|
BinInfoRepository.Update(
|
|
it =>
|
|
new BinInfo(){
|
|
Exists = 0,
|
|
|
|
},
|
|
it => it.No == binNo);
|
|
else
|
|
|
|
BinInfoRepository.Update(it => it.Exists == 1,
|
|
it => it.No == binNo);
|
|
|
|
}
|
|
}
|