using Autofac;
using HslCommunication;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Utils;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using Repository.Station;
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 int DataTimeSeconds = 20;
///
/// 数据刷新时间
///
public static DateTime DataValidityTime = DateTime.Now;
public static void Init()
{
if (PlcClient == null)
{
PlcClient = AppInfo.Container.Resolve();
}
Task.Run(() => PlcClient.Connect());
}
///
/// 写入口灯
/// 红灯:1020
/// 绿灯:1000
///
///
///
public static bool WriteEntranceLamp(ushort value)
{
if (PlcClient != null)
{
HostToPlcData.LightIn.Value = value;
return PlcClient.WriteValue(HostToPlcData.LightIn);
}
return false;
}
///
/// 拍照准备
///
///
public static bool PhotoPreparation()
{
if (PlcClient != null)
{
HostToPlcData.PhotoPreparation.Value = 1000;
return PlcClient.WriteValue(HostToPlcData.PhotoPreparation);
}
return false;
}
///
/// 车辆在拍照位
///
///
public static bool TakePhoto()
{
if (PlcClient != null)
{
HostToPlcData.StartPhoto.Value = 1000;
return PlcClient.WriteValue(HostToPlcData.StartPhoto);
}
return false;
}
///
/// 拍照
///
///
public static bool CarInPhotoPosition()
{
if (PlcClient != null)
{
return PlcToHostData.CarInPhotoPosition.Value == 1000;
}
return false;
}
public static bool AirBlowerControl(ushort value)
{
if (PlcClient != null)
{
HostToPlcData.ExhaustFanControl.Value = value;
return PlcClient.WriteValue(HostToPlcData.ExhaustFanControl);
}
return false;
}
public static ushort BlowerStatus()
{
if (PlcClient != null)
{
return PlcToHostData.ExhaustFanStatus.Value;
}
return 0;
}
///
/// 是否远程模式
///
///
public static bool IsRemote()
{
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
{
return PlcToHostData.RemoteLocalControlState.Value == 1010;
}
else
{
return false;
}
}
public static bool IsAuto()
{
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
{
return PlcToHostData.ModeControl.Value == 1010;
}
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;
}
/// 0.无任务 (不允许换电)
/// 1.换电任务
/// 2.移舱任务
/// 3.移出任务
/// 4.移入任务
/// 5.消防任务
public static bool ReadTaskStatus(ushort taskNo)
{
if (PlcClient != null)
{
ResetPlc();
switch (taskNo)
{
case 1:
if (PlcToHostData.StatusPowerChangeProcess.Value != 0 &&
PlcToHostData.StatusPowerChangeProcess.Value != 1000)
{
return true;
}
return false;
case 2:
return PlcToHostData.MovingCondition.Value == 1001;
case 5:
return PlcToHostData.FireFightingTaskStatus.Value == 1000;
default:
return false;
}
}
return false;
}
public static bool ReadSwapReady()
{
return PlcToHostData.WhetherAllowPowerChange.Value == 1010;
}
///
/// plc上使能 1换电,2移仓,3消防
///
///
public static bool HoldOn(int taskType)
{
HostToPlc writeHostToPlc = new HostToPlc();
switch (taskType)
{
case 1:
writeHostToPlc.StartTaskSwap.Value = 1000;
return PlcClient.WriteValue(writeHostToPlc.StartTaskSwap);
case 2:
writeHostToPlc.StartTaskMove.Value = 1000;
return PlcClient.WriteValue(writeHostToPlc.StartTaskMove);
case 3:
writeHostToPlc.StartTaskFire.Value = 1000;
return PlcClient.WriteValue(writeHostToPlc.StartTaskFire);
default:
return false;
}
}
///
/// 下发任务 plc 需要一起下发
///
///
public static bool DistributeTask(ushort inBinNo, ushort outBinNo, ushort taskNo)
{
DistributeTask distributeTask = new()
{
InBinNo = inBinNo,
OutBinNo = outBinNo,
};
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;4", encode);
return operateResult.IsSuccess;
}
///
/// 查看拍照状态
///
///
public static ushort ChannelStatus()
{
if (PlcClient != null)
{
return PlcToHostData.ChannelLocationState.Value;
}
return 0;
}
///
/// 行车回归安全位置
///
///
public static bool PlcCarInit()
{
if (PlcClient != null)
{
HostToPlcData.Regression.Value = 1000;
return PlcClient.WriteValue(HostToPlcData.Regression);
}
return false;
}
///
/// 重新读
///
public static void ResetPlc()
{
PlcClient.Reset();
}
///
/// 读取换电任务状态
///
///
public static int ReadSwapStatus()
{
if (PlcClient != null)
{
return PlcToHostData.StatusPowerChangeProcess.Value;
}
return (-1);
}
///
/// 读取消防任务状态
///
///
public static int ReadFireStatus()
{
if (PlcClient != null)
{
return PlcToHostData.FireFightingTaskStatus.Value;
}
return (-1);
}
///
/// 读取移仓任务状态
///
///
public static int ReadMoveStatus()
{
if (PlcClient != null)
{
return PlcToHostData.MovingCondition.Value;
}
return (-1);
}
///
/// 灯光控制:全部打开
///
///
public static bool AllLightsOn()
{
bool bResult = false;
if (PlcClient != null)
{
HostToPlc writeHostToPlc = new HostToPlc();
writeHostToPlc.LightingControl.Value = 1000;
bResult = PlcClient.WriteValue(writeHostToPlc.LightingControl);
}
return bResult;
}
public static bool ZPushUp()
{
if (PlcClient != null)
{
HostToPlcData.BatteryUnlockedState.Value = 1000;
return PlcClient.WriteValue(HostToPlcData.BatteryUnlockedState);
}
return false;
}
///
/// 获取当前灯光状态
///
///
public static ushort GetNowLightsOn()
{
if (PlcClient != null)
{
return PlcToHostData.LightStatus.Value;
}
return default;
}
///
/// 灯光控制:全部关闭
///
///
public static bool AllLightsOff()
{
bool bResult = false;
if (PlcClient != null)
{
HostToPlc writeHostToPlc = new HostToPlc();
writeHostToPlc.LightingControl.Value = 1010;
bResult = PlcClient.WriteValue(writeHostToPlc.LightingControl);
}
return bResult;
}
}