using DotNetty.Codecs.Mqtt.Packets;
using HybirdFrameworkDriver.ModbusTcpMaster;
using HybirdFrameworkDriver.Session;
using HybirdFrameworkServices.Plc;
using Newtonsoft.Json.Linq;
using System.Net.NetworkInformation;
namespace Service.Init.Entity
{
public class PlcInfo
{
#region 字段
///
/// 设备编号
///
private string _devNo = "001";
///
/// 设备名称
///
private string _devname = "换电PLC";
///
/// 服务端连接IP
///
private string _ipaddr = "172.0.20.48";
///
/// 服务端连接端口
///
private int _port = 502;
///
/// 站号
///
private byte _site = 0x01;
///
/// 连接超时时间。单位秒
///
private int _connecttimeout = 10;
///
/// 保持活跃时间。单位秒
///
private int _keepalive = 30;
///
/// 连接状态
///
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;
///
/// plc是否连接
///
public bool Connected { get; set; }
///
/// plc是否处于远程模式
///
public bool RemoteNot
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
{
return RemoteNot;
}
else
{
return false;
}
}
set
{
RemoteNot = value;
DataValidityTime = DateTime.Now;
}
}
///
/// 读入口雷达是否感应触发
///
public bool EntranceRadar
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return EntranceRadar;
}
else
{
return false;
}
}
set
{
EntranceRadar = value;
DataValidityTime = DateTime.Now;
}
}
///
/// 读取通道定位状态(拍照是否OK)
///
public bool ChannelLocationState
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return ChannelLocationState;
}
else
{
return false;
}
}
set
{
ChannelLocationState = value;
DataValidityTime = DateTime.Now;
}
}
///
/// 读取plc任务状态电池拆卸中
///
public bool BatteryDisassembly
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return BatteryDisassembly;
}
else
{
return false;
}
}
set
{
BatteryDisassembly = value;
DataValidityTime = DateTime.Now;
}
}
///
/// 读取plc任务状态电池入库搬运中
///
public bool StorageBatteries
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return StorageBatteries;
}
else
{
return false;
}
}
set
{
StorageBatteries = value;
DataValidityTime = DateTime.Now;
}
}
///
/// 读取plc任务状态电池出库搬运中
///
public bool BatteryDelivery
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return BatteryDelivery;
}
else
{
return false;
}
}
set
{
BatteryDelivery = value;
DataValidityTime = DateTime.Now;
}
}
///
/// 读取plc任务状态电池安装中
///
public bool BatteryInstallation
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return BatteryInstallation;
}
else
{
return false;
}
}
set
{
BatteryInstallation = value;
DataValidityTime = DateTime.Now;
}
}
///
/// 读取plc任务状态电池安装完成
///
public bool BatteryInstallationComplete
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return BatteryInstallationComplete;
}
else
{
return false;
}
}
set
{
BatteryInstallationComplete = value;
DataValidityTime = DateTime.Now;
}
}
///
/// 设置出口灯:绿灯(通知车辆可以驶离)
///
public bool SetExitLightGreen
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return SetExitLightGreen;
}
else
{
return false;
}
}
set
{
SetExitLightGreen = value;
DataValidityTime = DateTime.Now;
}
}
///
/// 读出口雷达是否感应触发
///
public bool ExitRadarSensingTrigger
{
get
{
if ((DateTime.Now - DataValidityTime).Seconds <= 2)
{
return ExitRadarSensingTrigger;
}
else
{
return false;
}
}
set
{
ExitRadarSensingTrigger = value;
DataValidityTime = DateTime.Now;
}
}
///
/// 写入口灯绿
///
///
public bool SendLightGreen()
{
bool bResult = false;
if (Connected)
{
hostToPlc.LightIn.Value = 1000;
bResult = WriteUint16(hostToPlc.LightIn);
}
return bResult;
}
///
/// 下发选包数据给plc(入仓位
///
/// 1:缓存位舱位号 2-20 电池存储舱位
///
public bool SendSelectRuleIn(ushort loc)
{
bool bResult = false;
if (Connected)
{
hostToPlc.EntrySelection.Value = loc;
bResult = WriteUint16(hostToPlc.EntrySelection);
}
return bResult;
}
///
/// 下发选包数据给plc(出仓位
///
/// 1:缓存位舱位号 2-20 电池存储舱位
///
public bool SendSelectRuleOut(ushort loc)
{
bool bResult = false;
if (Connected)
{
hostToPlc.ExitSelection.Value = loc;
bResult = WriteUint16(hostToPlc.ExitSelection);
}
return bResult;
}
///
/// 写入口灯:红灯
///
///
public bool SendLightRed()
{
bool bResult = false;
if (Connected)
{
hostToPlc.LightIn.Value = 1020;
bResult = WriteUint16(hostToPlc.LightIn);
}
return bResult;
}
///
/// 下发plc开启换电任务
///
///
public bool SendTaskType()
{
bool bResult = false;
if (Connected)
{
hostToPlc.TaskType.Value = 1;
bResult = WriteUint16(hostToPlc.TaskType);
}
return bResult;
}
///
/// 设置出口灯:绿灯
///
///
public bool SendLightOutGreen()
{
bool bResult = false;
if (Connected)
{
hostToPlc.LightOut.Value = 1000;
bResult = WriteUint16(hostToPlc.LightOut);
}
return bResult;
}
///
/// 设置出口灯:红灯
///
///
public bool SendLightOutRed()
{
bool bResult = false;
if (Connected)
{
hostToPlc.LightOut.Value = 1020;
bResult = WriteUint16(hostToPlc.LightOut);
}
return bResult;
}
#endregion
public PlcInfo(string channelId, int eqmSn)
{
ChannelId = channelId;
EqmSn = eqmSn;
}
public bool WriteUint16(ModbusProperty value)
{
bool bResult = false;
ModbusSession session = SessionMgr.GetModbusSession(ChannelId);
bResult = session.Write(value);
return bResult;
}
public bool Writeint16(ModbusProperty value)
{
bool bResult = false;
ModbusSession session = SessionMgr.GetModbusSession(ChannelId);
bResult = session.Write(value);
return bResult;
}
}
}