using HslCommunication;
using HslCommunication.ModBus;
using Monitor.LogService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Module.FireCtrl.ModBusTcp
{
///
/// 消防设备通讯
///
public class FireModbusTool
{
#region 定义锁
private object lockObj = new object(); //线程同步锁
#endregion 定义锁
#region 字段属性
///
/// 服务端连接IP
///
private string _ipaddr = "172.0.50.100";
///
/// 服务端连接IP
///
public string F_IpAddr
{
get
{
return _ipaddr;
}
set
{
lock (lockObj)
{
_ipaddr = value;
}
}
}
///
/// 服务端连接端口
///
private int _port = 404;
///
/// 服务端连接端口
///
public int F_Port
{
get
{
return _port;
}
set
{
lock (lockObj)
{
_port = value;
}
}
}
///
/// 从站号
///
private byte _site = 0x01;
///
/// 从站号
///
public byte F_Site
{
get
{
return _site;
}
set
{
lock (lockObj)
{
_site = value;
}
}
}
///
/// 通讯是否已连接
///
private bool _net_connected = false;
///
/// 通讯是否已连接
///
public bool F_NetConnected
{
get
{
return _net_connected;
}
set
{
lock (lockObj)
{
_net_connected = value;
}
}
}
///
/// ModbusTcp客户端
///
private ModbusTcpNet _client;
///
/// ModbusTcp客户端
///
public ModbusTcpNet F_UpsClient
{
get
{
return _client;
}
set
{
lock (lockObj)
{
_client = value;
}
}
}
///
/// 设备编号
///
private string _eqm_code = "F001";
///
/// 设备编号
///
public string F_EqmCode
{
get
{
return _eqm_code;
}
set
{
lock (lockObj)
{
_eqm_code = value;
}
}
}
///
/// 设备名称
///
private string _eqm_name = "消防设备";
///
/// 设备名称
///
public string F_EqmName
{
get
{
return _eqm_name;
}
set
{
lock (lockObj)
{
_eqm_name = value;
}
}
}
///
/// 决定去读取参数
///
private bool _is_read = true;
///
/// 决定去读取参数
///
public bool F_IsRead
{
get
{
return _is_read;
}
set
{
lock (lockObj)
{
_is_read = value;
}
}
}
///
/// 故障码
///
private int _fault_code = 0;
///
/// 故障码
///
public int F_FaultCode
{
get
{
return _fault_code;
}
set
{
lock (lockObj)
{
_fault_code = value;
}
}
}
///
/// 故障报警列表
///
private List _fault_alaram_no_list = new List();
///
/// 故障报警列表
///
public List F_FaultAlarmNoList
{
get
{
return _fault_alaram_no_list;
}
set
{
lock (lockObj)
{
_fault_alaram_no_list = value;
}
}
}
///
/// 消防系统状态值
///
private MFireStatusValue _fire_status_value;
///
/// 消防设备状态值
///
public MFireStatusValue F_FireStatusValue
{
get
{
return _fire_status_value;
}
set
{
_fire_status_value = value;
}
}
///
/// 故障信息值
///
private List _fault_informations = new List();
///
/// 消防故障信息值
///
public List F_FaultInformations
{
get
{
return _fault_informations;
}
set
{
_fault_informations = value;
}
}
///
/// 传感器信息
///
private List _sensor_informations = new List();
///
/// 消防传感器信息值
///
public List F_SensorInformations
{
get
{
return _sensor_informations;
}
set
{
_sensor_informations = value;
}
}
///
/// 分区信息
///
private MPartitionValue _partition;
///
/// 消防分区信息值
///
public MPartitionValue F_Partition
{
get
{
return _partition;
}
set
{
_partition = value;
}
}
///
/// 消防灭火器状态信息值
///
private MFireExtinguisher _fire_extinguisher;
///
/// 消防灭火器状态信息值
///
public MFireExtinguisher F_FireExtinguisher
{
get
{
return _fire_extinguisher;
}
set
{
_fire_extinguisher = value;
}
}
#endregion 字段属性
#region 类结构体
public FireModbusTool()
{
if (!HslCommunication.Authorization.SetAuthorizationCode("975a841d-7346-489a-80eb-3b68c8b17df1"))
{
}
if (_client == null)
{
_client = new ModbusTcpNet(_ipaddr, _port);
}
}
///
/// 类结构体
///
/// 服务端连接IP
/// 服务端连接端口
public FireModbusTool(string ipAddr, int port)
{
_ipaddr = ipAddr;
_port = port;
if (_client == null)
{
_client = new ModbusTcpNet(_ipaddr, _port);
}
}
///
/// 类结构体
///
/// 服务端连接IP
/// 服务端连接端口
/// 从站号
public FireModbusTool(string ipAddr, int port, byte site)
{
_ipaddr = ipAddr;
_port = port;
_site = site;
if (_client == null)
{
_client = new ModbusTcpNet(_ipaddr, _port);
}
}
///
/// 类结构体
///
/// 服务端连接IP
/// 服务端连接端口
/// 从站号
/// 设备编号
public FireModbusTool(string ipAddr, int port, byte site, string devNO)
{
_ipaddr = ipAddr;
_port = port;
_site = site;
_eqm_code = devNO;
if (_client == null)
{
_client = new ModbusTcpNet(_ipaddr, _port);
}
}
///
/// 类结构体
///
/// 服务端连接IP
/// 服务端连接端口
/// 从站号
/// 设备编号
/// 设备名称
public FireModbusTool(string ipaddr, int port, byte site, string devno, string devname)
{
_ipaddr = ipaddr;
_port = port;
_site = site;
_eqm_code = devno;
_eqm_name = devname;
if (_client == null)
{
_client = new ModbusTcpNet(_ipaddr, _port);
}
}
#endregion 类结构体
#region 设备连接
///
/// ModbusTcp连接
///
public void Connect()
{
try
{
if (_client == null)
{
_client = new ModbusTcpNet(_ipaddr, _port, _site);
}
if (_client != null)
{
_client = new ModbusTcpNet(_ipaddr, _port, _site);
OperateResult OptRet = _client.ConnectServer();
if (OptRet.IsSuccess)
{
_net_connected = true;
StartFireStateThread();
}
else
{
_net_connected = false;
}
}
}
catch (Exception ex)
{
Log.LogInstance.WriteLog("连接失败" + ex.ToString(), LogType.Run, "电表Log");
}
}
///
/// ModbusTcp连接断开
///
public void DisConnect()
{
try
{
OperateResult OptRet = _client.ConnectClose();
if (OptRet.IsSuccess)
{
_net_connected = false;
}
}
catch (Exception ex)
{
Log.LogInstance.WriteLog("断开连接失败" + ex.ToString(), LogType.Run, "电表Log");
}
}
#endregion 设备连接
#region 数据采集
///
/// 开始采集消防状态值线程
///
public void StartFireStateThread()
{
Thread readParamValueThread = new Thread(ReadParamValueFunc);
readParamValueThread.IsBackground = true;
readParamValueThread.Start();
}
///
/// 读取Ups状态方法
///
private void ReadParamValueFunc()
{
while (_is_read)
{
if (_net_connected)
{
#region 系统状态
OperateResult statusValue = _client.Read("x=1;0", 17);
_fire_status_value = new MFireStatusValue();
if (statusValue.IsSuccess)
{
byte[] results_16 = statusValue.Content;
_fire_status_value.F_Alarm = BitsConvertUtils.ByteToBitValue(results_16, 0, 0);
_fire_status_value.F_Fault = BitsConvertUtils.ByteToBitValue(results_16, 0, 1);
_fire_status_value.F_Silencers = BitsConvertUtils.ByteToBitValue(results_16, 0, 2);
_fire_status_value.F_SelfTest = BitsConvertUtils.ByteToBitValue(results_16, 0, 3);
_fire_status_value.F_AutomaticMode = BitsConvertUtils.ByteToBitValue(results_16, 0, 7);
_fire_status_value.F_ManualMode = BitsConvertUtils.ByteToBitValue(results_16, 1, 0);
_fire_status_value.F_StartManually = BitsConvertUtils.ByteToBitValue(results_16, 1, 1);
_fire_status_value.F_ManualEmergencyStop = BitsConvertUtils.ByteToBitValue(results_16, 1, 2);
_fire_status_value.F_AudibleVisualStatus = BitsConvertUtils.ByteToBitValue(results_16, 1, 7);
_fire_status_value.F_DeflatedState = BitsConvertUtils.ByteToBitValue(results_16, 2, 0);
//new FireValueToRedis().StoredFireStatusValue(_eqm_code, _fire_status_value);
}
#endregion
#region 传感器信息
OperateResult sensorInformation = _client.Read("x=4;0", 96);//6个传感器,一个传感器1占16个字
_sensor_informations = new List();
if (sensorInformation.IsSuccess)
{
byte[] results_2048 = sensorInformation.Content;
for (int i = 0; i < results_2048.Length / 32; i++)
{
MSensorInformation _sensor_inform = new MSensorInformation();
_sensor_inform.F_Obligate = _client.ByteTransform.TransUInt16(results_2048, 0 + i * 32);
_sensor_inform.F_ProtectiveArea = _client.ByteTransform.TransUInt16(results_2048, 2 + i * 32);
_sensor_inform.F_DeviceType = _client.ByteTransform.TransUInt16(results_2048, 4 + i * 32);
_sensor_inform.F_DetectorID = _client.ByteTransform.TransUInt16(results_2048, 6 + i * 32);
_sensor_inform.F_AlarmLevel = _client.ByteTransform.TransUInt16(results_2048, 8 + i * 32);
_sensor_inform.F_Temperature = _client.ByteTransform.TransUInt16(results_2048, 10 + i * 32);
_sensor_inform.F_CO = _client.ByteTransform.TransUInt16(results_2048, 12 + i * 32);
_sensor_inform.F_VOC = _client.ByteTransform.TransUInt16(results_2048, 14 + i * 32);
_sensor_inform.F_Fumes = _client.ByteTransform.TransUInt16(results_2048, 16 + i * 32);
_sensor_inform.F_Obligate01 = _client.ByteTransform.TransUInt16(results_2048, 18 + i * 32);
_sensor_inform.F_Obligate02 = _client.ByteTransform.TransUInt16(results_2048, 20 + i * 32);
_sensor_inform.F_Obligate03 = _client.ByteTransform.TransUInt16(results_2048, 22 + i * 32);
_sensor_inform.F_Obligate04 = _client.ByteTransform.TransUInt16(results_2048, 24 + i * 32);
_sensor_inform.F_Obligate05 = _client.ByteTransform.TransUInt16(results_2048, 26 + i * 32);
_sensor_inform.F_Obligate06 = _client.ByteTransform.TransUInt16(results_2048, 28 + i * 32);
_sensor_inform.F_Obligate07 = _client.ByteTransform.TransUInt16(results_2048, 30 + i * 32);
_sensor_informations.Add(_sensor_inform);
}
//new FireValueToRedis().StoredFireSensorInfo(_eqm_code, _sensor_informations);
}
#endregion
#region 分区信息
OperateResult partition = _client.Read("x=4;2048", 16); //有1个分区,
_partition = new MPartitionValue();
if (partition.IsSuccess)
{
byte[] results_16 = partition.Content;
_partition.F_Obligate = _client.ByteTransform.TransUInt16(results_16, 0);
_partition.F_ProtectiveArea = _client.ByteTransform.TransUInt16(results_16, 2);
_partition.F_AlarmLevel = _client.ByteTransform.TransUInt16(results_16, 4);
_partition.F_StartManually = _client.ByteTransform.TransUInt16(results_16, 12);
_partition.F_EmergencyStop = _client.ByteTransform.TransUInt16(results_16, 14);
_partition.F_BootControl = _client.ByteTransform.TransUInt16(results_16, 16);
_partition.F_Delay = _client.ByteTransform.TransUInt16(results_16, 18);
_partition.F_DelayTime = _client.ByteTransform.TransUInt16(results_16, 20);
_partition.F_StartSpraying = _client.ByteTransform.TransUInt16(results_16, 22);
//new FireValueToRedis().StoredFirePartitionValue(_eqm_code, _partition);
}
#endregion
#region 灭火器信息
OperateResult fireExtinguisherStatus = _client.Read("x=4;4096", 16);//读灭火器状态,实际有6个灭火器
_fire_extinguisher = new MFireExtinguisher();
if (fireExtinguisherStatus.IsSuccess)
{
byte[] results_16 = fireExtinguisherStatus.Content;
_fire_extinguisher.F_FireExtinguisherValue01 = _client.ByteTransform.TransUInt16(results_16, 0);
_fire_extinguisher.F_FireExtinguisherValue02 = _client.ByteTransform.TransUInt16(results_16, 2);
_fire_extinguisher.F_FireExtinguisherValue03 = _client.ByteTransform.TransUInt16(results_16, 4);
_fire_extinguisher.F_FireExtinguisherValue04 = _client.ByteTransform.TransUInt16(results_16, 6);
_fire_extinguisher.F_FireExtinguisherValue05 = _client.ByteTransform.TransUInt16(results_16, 8);
_fire_extinguisher.F_FireExtinguisherValue06 = _client.ByteTransform.TransUInt16(results_16, 10);
//new FireValueToRedis().StoredFireExtinguisher(_eqm_code, _fire_extinguisher);
}
#endregion
#region 故障信息
OperateResult faultInformation = _client.Read("x=3;256", 384);
_fault_informations = new List();
if (faultInformation.IsSuccess)
{
byte[] results_384 = faultInformation.Content;
for (int i = 0; i < results_384.Length / 6; i++)
{
MFaultInformation fault_inform = new MFaultInformation();
fault_inform.F_ObligateHigh = results_384[1 + i * 6];
fault_inform.F_ObligateLow = results_384[0 + i * 6];
fault_inform.F_ProtectiveArea = results_384[3 + i * 6];
fault_inform.F_DeviceType = results_384[2 + i * 6];
fault_inform.F_DeviceNumber = results_384[5 + i * 6];
fault_inform.F_FaultCode = results_384[4 + i * 6];
_fault_informations.Add(fault_inform);
}
//new FireValueToRedis().StoredFireFaultInfo(_eqm_code, _fault_informations);
}
#endregion
}
Thread.Sleep(500);
}
}
#endregion 数据采集
}
}