using Autofac;
using HslCommunication;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Utils;
using Service.Execute.Model;
using Service.Plc.Msg;
namespace Service.Plc.Client;
public class PlcMgr
{
public static PlcClient? PlcClient;
public static readonly HostToPlc HostToPlcData = new HostToPlc();
public static readonly PlcToHost PlcToHostData = new PlcToHost();
public static readonly PlcFault PlcFaultData = new PlcFault();
///
/// 数据有效时间。单位秒
///
static int DataTimeSeconds = 20;
///
/// 数据刷新时间
///
public static DateTime DataValidityTime = DateTime.Now;
public static bool Init()
{
if (PlcClient == null)
{
PlcClient = AppInfo.Container.Resolve();
}
return PlcClient.Connect();
}
///
/// 写入口灯
/// 红灯:1020
/// 绿灯:1000
///
///
///
public static bool WriteEntranceLamp(byte value)
{
if (PlcClient != null)
{
HostToPlcData.LightIn.Value = value;
return PlcClient.WriteValue(HostToPlcData.LightIn);
}
return false;
}
///
/// 是否远程模式
///
///
public static bool IsRemote()
{
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
{
return (PlcToHostData.ModeControl.Value == 1010 && PlcToHostData.RemoteLocalControlState.Value == 1010)
? true
: false;
}
else
{
return false;
}
}
///
/// 入口雷达检测
///
///
public static bool EntranceRadar()
{
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
{
return PlcToHostData.RadarStatesIn.Value == 1000 ? true : false;
}
else
{
return false;
}
}
///
/// 出口雷达检测
///
///
public static bool ExitRadar()
{
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
{
return PlcToHostData.RadarStatesOut.Value == 1000 ? true : false;
}
else
{
return false;
}
}
///
/// 写出口灯
/// 红灯:1020
/// 绿灯:1000
///
///
///
public static bool WriteExistLamp(int data)
{
if (PlcClient != null)
{
HostToPlc writeHostToPlc = new HostToPlc();
writeHostToPlc.LightOut.Value = (ushort)data;
return PlcClient.WriteValue(writeHostToPlc.LightOut);
}
return false;
}
///
/// 下发选包
///
/// 入仓位,仓位号
/// 出仓位,仓位号
///
public static bool DistributeBinNo(string inBinNo, string outBinNo)
{
if (PlcClient != null)
{
HostToPlc writeHostToPlc = new HostToPlc();
writeHostToPlc.EntrySelection.Value = Convert.ToUInt16(inBinNo);
writeHostToPlc.ExitSelection.Value = Convert.ToUInt16(outBinNo);
bool write1 = PlcClient.WriteValue(writeHostToPlc.EntrySelection);
bool write2 = PlcClient.WriteValue(writeHostToPlc.ExitSelection);
return write1 && write2;
}
return false;
}
public static bool ReadTaskStatus(ushort taskNo)
{
if (PlcClient != null)
{
return PlcToHostData.TaskType.Value == taskNo;
}
return false;
}
///
/// plc上使能
///
///
public static bool HoldOn()
{
HostToPlc writeHostToPlc = new HostToPlc();
writeHostToPlc.TaskEnablement.Value = 1000;
return PlcClient.WriteValue(writeHostToPlc.TaskEnablement);
}
///
/// 下发任务 plc 需要一起下发
///
///
public static bool DistributeTask(ushort inBinNo, ushort outBinNo, ushort taskNo)
{
DistributeTask distributeTask = new()
{
TaskNo = taskNo,
InBinNo = inBinNo,
OutBinNo = outBinNo,
BatteryPackType = 0,
};
byte[] encode = ModelConvert.Encode(distributeTask);
//TODO::現在手動處理encode
for (int i = 0; i < encode.Length; i++)
{
if (i % 2 == 0)
{
var temp = encode[i + 1];
encode[i + 1] = encode[i];
encode[i] = temp;
}
}
//需要将数据转换成byte数组
OperateResult operateResult = PlcClient.Write("x=16;111",encode);
return operateResult.IsSuccess;
}
///
/// 下发启动换电
///
///
public static bool StartSwapping()
{
if (PlcClient != null)
{
HostToPlc writeHostToPlc = new HostToPlc();
writeHostToPlc.TaskType.Value = (ushort)1;
// writeHostToPlc.TaskEnablement.Value = (ushort)1000;
bool write1 = PlcClient.WriteValue(writeHostToPlc.TaskType);
//bool write2 = PlcClient.WriteValue(writeHostToPlc.TaskEnablement);
return write1;
}
return false;
}
static bool heart = false;
///
/// 发送心跳
///
///
public static bool SendHear()
{
if (PlcClient != null)
{
heart = !heart;
HostToPlc writeHostToPlc = new HostToPlc();
writeHostToPlc.ProtocolVersion1.Value = 2;
writeHostToPlc.ProtocolVersion2.Value = 0;
writeHostToPlc.ProtocolVersion3.Value = 1;
writeHostToPlc.CommunicationDiagnosis.Value = (ushort)(heart ? 1 : 0);
writeHostToPlc.Years.Value = (ushort)DateTime.Now.Year;
writeHostToPlc.Month.Value = (ushort)DateTime.Now.Month;
writeHostToPlc.Day.Value = (ushort)DateTime.Now.Day;
writeHostToPlc.Hour.Value = (ushort)DateTime.Now.Hour;
writeHostToPlc.Points.Value = (ushort)DateTime.Now.Minute;
writeHostToPlc.Seconds.Value = (ushort)DateTime.Now.Second;
bool write01 = PlcClient.WriteValue(writeHostToPlc.ProtocolVersion1);
bool write02 = PlcClient.WriteValue(writeHostToPlc.ProtocolVersion2);
bool write03 = PlcClient.WriteValue(writeHostToPlc.ProtocolVersion3);
bool write0 = PlcClient.WriteValue(writeHostToPlc.CommunicationDiagnosis);
bool write1 = PlcClient.WriteValue(writeHostToPlc.Years);
bool write2 = PlcClient.WriteValue(writeHostToPlc.Month);
bool write3 = PlcClient.WriteValue(writeHostToPlc.Day);
bool write4 = PlcClient.WriteValue(writeHostToPlc.Hour);
bool write5 = PlcClient.WriteValue(writeHostToPlc.Points);
bool write6 = PlcClient.WriteValue(writeHostToPlc.Seconds);
return write1 && write2 && write3 && write4 && write5 && write6;
}
return true;
}
///
/// 查看拍照状态
///
///
public static ushort ChannelStatus()
{
if (PlcClient != null)
{
return PlcToHostData.ChannelLocationState.Value;
}
return 0;
}
///
/// 读plc任务状态
///
/// 6未连接
public static int ReadPlcTaskStatus()
{
if (PlcClient != null)
{
return PlcToHostData.TaskStates.Value;
}
return (-1);
}
}