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.

282 lines
13 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.FireControl.Msg;
namespace Service.FireControl.Client;
[Scope]
public class FireControlClient: ModbusTcpMaster
{
private static readonly ILog Log = LogManager.GetLogger(typeof(FireControlClient));
public EquipAlarmDefineRepository EquipAlarmDefineRepository { get; set; }
public EquipAlarmRecordRepository EquipAlarmRecordRepository { get; set; }
public EquipAlarmProcessRecordRepository EquipAlarmProcessRecordRepository { get; set; }
public FireControlClient()
{
ReadAction = BatchRead;
Ip = "172.0.50.100";
Port = 502;
Duration = 1000;
AutoReConnect = true;
ByteSeq = EndingConst.ByteSeq.BA;
WordSeq= EndingConst.WordSeq.CD;
}
private bool BatchRead(ModbusTcpMaster master)
{
bool connectResult = false;
//1电表
//2消防
//3温湿度
Log.Info("fire start read");
#region 电表
var readPowerValue = master.Read("x=3;39424", 2).Content;
if (readPowerValue != null)
{
connectResult = true;
Array.Reverse(readPowerValue);
//ModbusDecoder.Decode<PowerValue>(readPowerValue, FireControlMgr.PowerValue, EndingConst.ByteSeq.BA, EndingConst.WordSeq.DC);
FireControlMgr.PowerValue.FPowerValue.Value=BitConverter.ToSingle(readPowerValue, 0);
}
#endregion
#region 温湿度
var readTem = master.Read("s=3;x=3;0", 2).Content;
if (readTem!=null)
{
connectResult = true;
ModbusDecoder.Decode<TemperatureHumidity>(readTem, FireControlMgr.TemperatureHumidity, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
}
#endregion
var bytes02 = master.ReadCoil("s=2;x=1;0", 17).Content;//0x01
var bytes03 = master.Read("s=2;x=3;1", 3).Content;//0x03 读1 3
var bytes04 = master.Read("s=2;x=3;257", 383).Content;//0x03 读256 383
var bytes05 = master.Read("s=2;x=4;1", 115).Content;//0x04 可以
List<byte> bytes06=new List<byte>();//= master.BatchReadHolderRegister(0x0800, 2048);//0x04 可以
var bytesAdd1 = master.Read("s=2;x=4;2048", 1000).Content;//0x04 可以
var bytesAdd2 = master.Read("s=2;x=4;3048", 1048).Content;//0x04 可以
if (bytesAdd1 != null && bytesAdd2 != null)
{
connectResult = true;
bytes06.AddRange(bytesAdd1);
bytes06.AddRange(bytesAdd2);
}
var bytes07 = master.Read("s=2;x=4;4096", 32).Content;//0x04 可以
var bytes08 = master.Read("s=2;x=5;0", 134).Content;//0x05 写
var bytes09 = master.Read("s=2;x=6;256", 64).Content;//0x06 写
if (bytes02 != null)
{
Log.Info("fire read bytes02 success");
connectResult = true;
FireControlMgr.Subzone2.GiveAnAlarm.Value = bytes02[0];
FireControlMgr.Subzone2.NumberOfFailures.Value = bytes02[1];
FireControlMgr.Subzone2.SoundSuppression.Value = bytes02[2];
FireControlMgr.Subzone2.SelfInspection.Value = bytes02[3];
FireControlMgr.Subzone2.MainPower.Value = bytes02[4];
FireControlMgr.Subzone2.PowerSupplyBackup.Value = bytes02[5];
FireControlMgr.Subzone2.BackupPowerUnderpowered.Value = bytes02[6];
FireControlMgr.Subzone2.AutomaticMode.Value = bytes02[7];
FireControlMgr.Subzone2.ManualMode.Value = bytes02[8];
FireControlMgr.Subzone2.ManualStart.Value = bytes02[9];
FireControlMgr.Subzone2.ManualScram.Value = bytes02[10];
FireControlMgr.Subzone2.StartUpControl.Value = bytes02[11];
FireControlMgr.Subzone2.Delay.Value = bytes02[12];
FireControlMgr.Subzone2.PrimingSpray.Value = bytes02[13];
FireControlMgr.Subzone2.Spray.Value = bytes02[14];
FireControlMgr.Subzone2.AudibleVisualAlarmStatus.Value = bytes02[15];
FireControlMgr.Subzone2.DeflatedDoNotEnter.Value = bytes02[16];
}
if (bytes03 != null)
{
connectResult = true;
ModbusDecoder.Decode<Subzone3>(bytes03, FireControlMgr.Subzone3, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
}
if (bytes04 != null)
{
connectResult = true;
ModbusDecoder.Decode<Subzone4>(bytes04, FireControlMgr.Subzone4, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
List<string> faultList = new List<string>()
{
{FireControlMgr.Subzone4.EquipmentNumber01.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber02.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber03.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber04.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber05.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber06.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber07.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber08.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber09.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber10.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber11.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber12.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber13.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber14.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber15.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber16.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber17.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber18.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber19.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber20.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber21.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber22.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber23.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber24.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber25.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber26.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber27.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber28.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber29.Value.ToString()},
{FireControlMgr.Subzone4.EquipmentNumber30.Value.ToString()},
};
SaveAlarmInfo(faultList);
}
if (bytes05 != null)
{
connectResult = true;
ModbusDecoder.Decode<Subzone5>(bytes05, FireControlMgr.Subzone5, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
}
if (bytes06 != null)
{
connectResult = true;
ModbusDecoder.Decode<Subzone6>(bytes06.ToArray(), FireControlMgr.Subzone6, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
}
if (bytes07 != null)
{
connectResult = true;
ModbusDecoder.Decode<Subzone7>(bytes07, FireControlMgr.Subzone7, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
}
/*if (bytes08 != null)
{
ModbusDecoder.Decode<Subzone8>(bytes08, FireControlMgr.Subzone8, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
}
if (bytes09 != null)
{
ModbusDecoder.Decode<Subzone9>(bytes09, FireControlMgr.Subzone9, EndingConst.ByteSeq.AB, EndingConst.WordSeq.DC);
}*/
return connectResult;
}
private void SaveAlarmInfo(List<string> lstAlarm)
{
if (lstAlarm.Count > 0)
{
#region 有报警比较两边差异,新出现的报警就添加,消失的报警就处理并记录
var lstEquipAlarmRecord = EquipAlarmRecordRepository.QueryListByClause(i => i.EquipTypeCode == (int)EquipmentType.Fire);
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)
{
if (errorCode != "0")
{
EquipAlarmDefine? alarmDefine =
EquipAlarmDefineRepository.QueryByClause(i=>i.EquipTypeCode==(int)EquipmentType.Fire&&i.ErrorCode==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
}
}
}