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.

521 lines
15 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using DotNetty.Codecs.Mqtt.Packets;
using HybirdFrameworkDriver.ModbusTcpMaster;
using HybirdFrameworkDriver.Session;
using HybirdFrameworkServices.Plc;
using Newtonsoft.Json.Linq;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Pipelines.Sockets.Unofficial.Arenas;
using System.Net.NetworkInformation;
namespace Service.Init.Entity
{
public class PlcInfo
{
#region 字段
/// <summary>
/// 设备编号
/// </summary>
private string _devNo = "001";
/// <summary>
/// 设备名称
/// </summary>
private string _devname = "换电PLC";
/// <summary>
/// 服务端连接IP
/// </summary>
private string _ipaddr = "172.0.20.48";
/// <summary>
/// 服务端连接端口
/// </summary>
private int _port = 502;
/// <summary>
/// 站号
/// </summary>
private byte _site = 0x01;
/// <summary>
/// 连接超时时间。单位秒
/// </summary>
private int _connecttimeout = 10;
/// <summary>
/// 保持活跃时间。单位秒
/// </summary>
private int _keepalive = 30;
/// <summary>
/// 连接状态
/// </summary>
private ConnectState _connect_state = ConnectState.Disconnect;
#endregion 字段
public string ChannelId;
public int EqmSn;
public HostToPlc hostToPlc = new HostToPlc();
public PlcToHost plcToHost = new PlcToHost();
public PlcFault plcFault = new PlcFault();
#region
int DataTimeSeconds = 2;
DateTime DataValidityTime = DateTime.Now;
/// <summary>
/// plc是否连接
/// </summary>
public bool Connected { get; set; }
/// <summary>
/// plc是否处于远程模式
/// </summary>
public bool RemoteNot
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
{
return RemoteNot;
}
else
{
return false;
}
}
set
{
RemoteNot = value;
DataValidityTime = DateTime.Now;
}
}
/// <summary>
/// 读入口雷达是否感应触发
/// </summary>
public bool EntranceRadar
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return EntranceRadar;
}
else
{
return false;
}
}
set
{
EntranceRadar = value;
DataValidityTime = DateTime.Now;
}
}
/// <summary>
/// 读取通道定位状态(拍照是否OK)
/// </summary>
public bool ChannelLocationState
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return ChannelLocationState;
}
else
{
return false;
}
}
set
{
ChannelLocationState = value;
DataValidityTime = DateTime.Now;
}
}
/// <summary>
/// 读取plc任务状态电池拆卸中
/// </summary>
public bool BatteryDisassembly
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return BatteryDisassembly;
}
else
{
return false;
}
}
set
{
BatteryDisassembly = value;
DataValidityTime = DateTime.Now;
}
}
/// <summary>
/// 读取plc任务状态电池入库搬运中
/// </summary>
public bool StorageBatteries
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return StorageBatteries;
}
else
{
return false;
}
}
set
{
StorageBatteries = value;
DataValidityTime = DateTime.Now;
}
}
/// <summary>
/// 读取plc任务状态电池出库搬运中
/// </summary>
public bool BatteryDelivery
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return BatteryDelivery;
}
else
{
return false;
}
}
set
{
BatteryDelivery = value;
DataValidityTime = DateTime.Now;
}
}
/// <summary>
/// 读取plc任务状态电池安装中
/// </summary>
public bool BatteryInstallation
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return BatteryInstallation;
}
else
{
return false;
}
}
set
{
BatteryInstallation = value;
DataValidityTime = DateTime.Now;
}
}
/// <summary>
/// 读取plc任务状态电池安装完成
/// </summary>
public bool BatteryInstallationComplete
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return BatteryInstallationComplete;
}
else
{
return false;
}
}
set
{
BatteryInstallationComplete = value;
DataValidityTime = DateTime.Now;
}
}
/// <summary>
/// 设置出口灯:绿灯(通知车辆可以驶离)
/// </summary>
public bool SetExitLightGreen
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return SetExitLightGreen;
}
else
{
return false;
}
}
set
{
SetExitLightGreen = value;
DataValidityTime = DateTime.Now;
}
}
/// <summary>
/// 读出口雷达是否感应触发
/// </summary>
public bool ExitRadarSensingTrigger
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return ExitRadarSensingTrigger;
}
else
{
return false;
}
}
set
{
ExitRadarSensingTrigger = value;
DataValidityTime = DateTime.Now;
}
}
/// <summary>
/// 写入口灯绿
/// </summary>
/// <returns></returns>
public bool SendLightGreen()
{
bool bResult = false;
if (Connected)
{
hostToPlc.LightIn.Value = 1000;
bResult = WriteUint16(hostToPlc.LightIn);
}
return bResult;
}
/// <summary>
/// 下发选包数据给plc(入仓位
/// </summary>
/// <param name="loc">1:缓存位舱位号 2-20 电池存储舱位</param>
/// <returns></returns>
public bool SendSelectRuleIn(ushort loc)
{
bool bResult = false;
if (Connected)
{
hostToPlc.EntrySelection.Value = loc;
bResult = WriteUint16(hostToPlc.EntrySelection);
}
return bResult;
}
/// <summary>
/// 下发选包数据给plc(出仓位
/// </summary>
/// <param name="loc">1:缓存位舱位号 2-20 电池存储舱位</param>
/// <returns></returns>
public bool SendSelectRuleOut(ushort loc)
{
bool bResult = false;
if (Connected)
{
hostToPlc.ExitSelection.Value = loc;
bResult = WriteUint16(hostToPlc.ExitSelection);
}
return bResult;
}
/// <summary>
/// 写入口灯:红灯
/// </summary>
/// <returns></returns>
public bool SendLightRed()
{
bool bResult = false;
if (Connected)
{
hostToPlc.LightIn.Value = 1020;
bResult = WriteUint16(hostToPlc.LightIn);
}
return bResult;
}
/// <summary>
/// 下发plc开启换电任务
/// </summary>
/// <returns></returns>
public bool SendTaskType()
{
bool bResult = false;
if (Connected)
{
hostToPlc.TaskType.Value = 1;
bResult = WriteUint16(hostToPlc.TaskType);
}
return bResult;
}
/// <summary>
/// 设置出口灯:绿灯
/// </summary>
/// <returns></returns>
public bool SendLightOutGreen()
{
bool bResult = false;
if (Connected)
{
hostToPlc.LightOut.Value = 1000;
bResult = WriteUint16(hostToPlc.LightOut);
}
return bResult;
}
/// <summary>
/// 设置出口灯:红灯
/// </summary>
/// <returns></returns>
public bool SendLightOutRed()
{
bool bResult = false;
if (Connected)
{
hostToPlc.LightOut.Value = 1020;
bResult = WriteUint16(hostToPlc.LightOut);
}
return bResult;
}
#endregion
#region 有料检测
/// <summary>
/// 有料检测 1-20仓
/// </summary>
public byte MaterialDetec01 { get; set; }
public byte MaterialDetec02 { get; set; }
public byte MaterialDetec03 { get; set; }
public byte MaterialDetec04 { get; set; }
public byte MaterialDetec05 { get; set; }
public byte MaterialDetec06 { get; set; }
public byte MaterialDetec07 { get; set; }
public byte MaterialDetec08 { get; set; }
public byte MaterialDetec09 { get; set; }
public byte MaterialDetec10 { get; set; }
public byte MaterialDetec11 { get; set; }
public byte MaterialDetec12 { get; set; }
public byte MaterialDetec13 { get; set; }
public byte MaterialDetec14 { get; set; }
public byte MaterialDetec15 { get; set; }
public byte MaterialDetec16 { get; set; }
public byte MaterialDetec17 { get; set; }
public byte MaterialDetec18 { get; set; }
public byte MaterialDetec19 { get; set; }
public byte MaterialDetec20 { get; set; }
#endregion 有料检测
/// <summary>
/// 舱位状态 在位状态0-不在位1-在位2-无效
/// </summary>
/// <returns></returns>
public List<byte> ReturnMaterialDetec()
{
List<byte> ret = new List<byte>
{
MaterialDetec01,MaterialDetec02 ,MaterialDetec03 ,MaterialDetec04 ,MaterialDetec05 ,
MaterialDetec06 ,MaterialDetec07 ,MaterialDetec08 ,MaterialDetec09 ,MaterialDetec10 ,
MaterialDetec11 ,MaterialDetec12 ,MaterialDetec13 ,MaterialDetec14 ,MaterialDetec15 ,
MaterialDetec16 ,MaterialDetec17 ,MaterialDetec18 ,MaterialDetec19 ,MaterialDetec20 ,
};
return ret;
}
/// <summary>
/// 灯光控制:全部打开
/// </summary>
/// <returns></returns>
public bool AllLightsOn()
{
bool bResult = false;
if (Connected)
{
hostToPlc.LightingControl.Value = 1000;
bResult = WriteUint16(hostToPlc.LightIn);
}
return bResult;
}
/// <summary>
/// 灯光控制:全部关闭
/// </summary>
/// <returns></returns>
public bool AllLightsOff()
{
bool bResult = false;
if (Connected)
{
hostToPlc.LightingControl.Value = 1010;
bResult = WriteUint16(hostToPlc.LightIn);
}
return bResult;
}
/// <summary>
/// 消防移仓
/// </summary>
/// <param name="entrySelection">入仓位选择0-无操作、1-缓存位舱位号、2-20电池存储仓位</param>
/// <returns></returns>
public bool FireProtectionRelocation(ushort entrySelection)
{
bool bResult = false;
if (Connected)
{
hostToPlc.TaskType.Value = 5;
hostToPlc.EntrySelection.Value = entrySelection;
hostToPlc.ExitSelection.Value = 5;
hostToPlc.BatteryPackType.Value = 0;
//TODO:: 连续写多个寄存器 是否影响速度
bool bResultTaskType = WriteUint16(hostToPlc.TaskType);
bool bResultEntrySelection = WriteUint16(hostToPlc.EntrySelection);
bool bResultExitSelection = WriteUint16(hostToPlc.ExitSelection);
bool bResultBatteryPackType = WriteUint16(hostToPlc.BatteryPackType);
if (bResultTaskType && bResultEntrySelection && bResultExitSelection && bResultBatteryPackType)
{
bResult = true;
}
}
return bResult;
}
public PlcInfo(string channelId, int eqmSn)
{
ChannelId = channelId;
EqmSn = eqmSn;
}
public bool WriteUint16(ModbusProperty<UInt16> value)
{
bool bResult = false;
ModbusSession session = SessionMgr.GetModbusSession(ChannelId);
bResult = session.Write<UInt16>(value);
return bResult;
}
public bool Writeint16(ModbusProperty<Int16> value)
{
bool bResult = false;
ModbusSession session = SessionMgr.GetModbusSession(ChannelId);
bResult = session.Write<Int16>(value);
return bResult;
}
}
}