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.

269 lines
10 KiB

7 months ago
using Common.Const;
using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Const;
using HybirdFrameworkDriver.ModbusTcpMaster;
7 months ago
using log4net;
using Repository.Station;
using Service.Plc.Msg;
namespace Service.Plc.Client;
/// <summary>
7 months ago
///
/// </summary>
[Scope]
public class PlcClient : ModbusTcpMaster
{
7 months ago
private static readonly ILog Log = LogManager.GetLogger(typeof(PlcClient));
private long TaskNo = 0;
7 months ago
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);
}
7 months ago
public PlcClient()
{
ReadAction = BatchRead;
Ip = "172.0.20.66";
Port = 502;
Duration = 1000;
AutoReConnect = true;
7 months ago
ByteSeq = EndingConst.ByteSeq.BA;
WordSeq = EndingConst.WordSeq.CD;
}
5 months ago
private bool BatchRead(ModbusTcpMaster master)
{
5 months ago
bool connectResult = false;
try
{
5 months ago
Log.Info("Plc read start");
if (master != null)
{
var bytes01 = master.BatchReadHolderRegister(1, 115);
if (bytes01 != null)
{
5 months ago
connectResult = true;
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)
{
5 months ago
connectResult = true;
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)
{
5 months ago
connectResult = true;
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);
}
5 months ago
Log.Info("Plc read finish");
}
5 months ago
}
catch (Exception e)
{
Log.Info($"Plc read error:{e}");
throw;
7 months ago
}
5 months ago
return connectResult;
7 months ago
}
private void SaveAlarmInfo(List<string> lstAlarm)
7 months ago
{
string plc = "plc";
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
7 months ago
}
else
{
#region 没报警把已处理记录更新并删除实时报警
7 months ago
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)
7 months ago
BinInfoRepository.Update(
it =>
new BinInfo()
{
Exists = 0,
BatteryNo = "-1",
Soc = (decimal)-1,
Soe = (decimal)-1,
Soh = (decimal)-1,
7 months ago
},
it => it.No == binNo);
else
7 months ago
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);
}
5 months ago
}