using System.Text;
using Common.Const;
using DotNetty.Transport.Channels;
using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac.Attribute;
using log4net;
using Repository.Station;
using Service.Fire.Client;
using Service.Fire.Common;
using Service.Fire.Msg.Fire.Req;
using Service.Fire.Msg.Host.Resp;
namespace Service.Fire.Handler
{
///
/// 发送事件
///
/// 1,保存日志到log
///
///
[Order(8)]
[Scope("InstancePerDependency")]
public class DataReqHandler : SimpleChannelInboundHandler, IBaseHandler
{
private static readonly ILog Log = LogManager.GetLogger(typeof(PollingReqHandler));
public EquipAlarmDefineRepository _equipAlarmDefineRepository { get; set; }
public EquipAlarmRecordRepository _equipAlarmRecordRepository { get; set; }
public EquipAlarmProcessRecordRepository _equipAlarmProcessRecordRepository { get; set; }
public DataReqHandler(EquipAlarmDefineRepository equipAlarmDefineRepository,
EquipAlarmRecordRepository equipAlarmRecordRepository,
EquipAlarmProcessRecordRepository equipAlarmProcessRecordRepository)
{
_equipAlarmDefineRepository = equipAlarmDefineRepository;
_equipAlarmRecordRepository = equipAlarmRecordRepository;
_equipAlarmProcessRecordRepository = equipAlarmProcessRecordRepository;
}
protected override void ChannelRead0(IChannelHandlerContext ctx, DataReq msg)
{
if (FireMgr.TryGetClient(ctx.Channel, out var sn, out var client))
{
Log.Info($"receive {msg} from {sn}");
if (msg.Building == 1) //1栋左
{
if (msg.DeviceStatus == 0x05) //火警
{
Troubleshoot("85", true);
}
else if (msg.DeviceStatus == 0x00) //火警消除
{
Troubleshoot("85", false);
}
if (msg.DeviceType == 0xED) //气体现场动作
{
Troubleshoot("86", true);
}
else //报警消除
{
Troubleshoot("86", false);
}
}
else if (msg.Building == 2) //2栋右
{
if (msg.DeviceStatus == 0x05) //火警
{
Troubleshoot("87", true);
}
else if (msg.DeviceStatus == 0x00) //火警消除
{
Troubleshoot("87", false);
}
if (msg.DeviceType == 0xED) //气体现场动作
{
Troubleshoot("88", true);
}
else //报警消除
{
Troubleshoot("85", false);
}
}
#region 预处理
/*
string datatime =
(new DateTime(2000 + msg.Years, msg.Month, msg.Day, msg.Hour, msg.Minute, msg.Seconds)).ToString();
string functionalAttribute = FireConst.FunctionalAttribute(msg.FunctionalAttribute);
string informationType = FireConst.InformationType(msg.InformationType);
string deviceStatus = FireConst.DeviceStatus(msg.DeviceStatus);
string deviceType = FireConst.DeviceType(msg.DeviceType);
string fault = informationType + functionalAttribute + ":" + deviceType + deviceStatus;
string strDecode = "";
for (int i = 0; i < msg.BytesLocationCode.Length; i++)
{
if (msg.BytesLocationCode[i] != 0)
{
string strhex = msg.BytesLocationCode[i].ToString("X");
strDecode += strhex;
}
else
{
break;
}
}
byte[] bytesVelNo = FireConst.ToByteByHexStr(strDecode);
Encoding gbEcoding = Encoding.GetEncoding("gb2312");
string strVelNo = gbEcoding.GetString(bytesVelNo);
*/
#endregion
}
}
///
/// 处理报警方法
///
/// 报警编码
/// 是否存在报警
///
public bool Troubleshoot(string errorCode, bool isAlarm)
{
if (isAlarm) //存在报警
{
EquipAlarmDefine EquipAlarmDefine =
_equipAlarmDefineRepository.QueryByClause(i =>
i.ErrorCode == errorCode && i.EquipTypeCode == (int)EquipmentType.Fire);
if (EquipAlarmDefine!=null)
{
var isfault=_equipAlarmRecordRepository.QueryByClause(i =>
i.ErrorCode == errorCode && i.EquipTypeCode == (int)EquipmentType.Fire);
if (isfault!=null) //判断这个报警是否已经添加了
{
return true;
}
EquipAlarmRecord EquipAlarmRecord = new EquipAlarmRecord()
{
EquipTypeCode = EquipAlarmDefine.EquipTypeCode,
EquipCode = EquipAlarmDefine.EquipCode,
ErrorCode = EquipAlarmDefine.ErrorCode,
ErrorLevel = EquipAlarmDefine.ErrorLevel,
ErrorMsg = EquipAlarmDefine.ErrorMsg,
StartTime = DateTime.Now,
};
_equipAlarmRecordRepository.Insert(EquipAlarmRecord); //添加故障处理
}
}
else
{
EquipAlarmRecord EquipAlarmRecord =
_equipAlarmRecordRepository.QueryByClause(i =>
i.ErrorCode == errorCode && i.EquipTypeCode == (int)EquipmentType.Fire);
if (EquipAlarmRecord != null)
{
EquipAlarmProcessRecord EquipAlarmProcessRecord = new EquipAlarmProcessRecord()
{
EquipTypeCode = EquipAlarmRecord.EquipTypeCode,
EquipCode = EquipAlarmRecord.EquipCode,
ErrorCode = EquipAlarmRecord.ErrorCode,
ErrorLevel = EquipAlarmRecord.ErrorLevel,
ErrorMsg = EquipAlarmRecord.ErrorMsg,
StartTime = EquipAlarmRecord.StartTime,
ProcessTime = DateTime.Now,
};
_equipAlarmProcessRecordRepository.Insert(EquipAlarmProcessRecord); //保存记录
_equipAlarmRecordRepository.Delete(i =>
i.ErrorCode == errorCode && i.EquipTypeCode == (int)EquipmentType.Fire);
}
}
return true;
}
}
}