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.

399 lines
12 KiB

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;
7 months ago
namespace Service.Plc.Client;
7 months ago
public class PlcMgr
7 months ago
{
public static PlcClient? PlcClient;
7 months ago
public static readonly HostToPlc HostToPlcData = new HostToPlc();
public static readonly PlcToHost PlcToHostData = new PlcToHost();
7 months ago
public static PlcFault LastPlcFaultData = new PlcFault();
7 months ago
public static readonly PlcFault PlcFaultData = new PlcFault();
7 months ago
/// <summary>
/// 数据有效时间。单位秒
/// </summary>
static int DataTimeSeconds = 20;
7 months ago
/// <summary>
/// 数据刷新时间
/// </summary>
public static DateTime DataValidityTime = DateTime.Now;
7 months ago
public static void Init()
{
if (PlcClient == null)
{
PlcClient = AppInfo.Container.Resolve<PlcClient>();
}
Task.Run(() => PlcClient.Connect());
}
7 months ago
7 months ago
/// <summary>
/// 写入口灯
/// 红灯1020
/// 绿灯1000
/// </summary>
/// <param name="value"></param>
7 months ago
/// <returns></returns>
public static bool WriteEntranceLamp(ushort value)
7 months ago
{
if (PlcClient != null)
7 months ago
{
HostToPlcData.LightIn.Value = value;
return PlcClient.WriteValue(HostToPlcData.LightIn);
7 months ago
}
7 months ago
return false;
}
7 months ago
public static bool AirBlowerControl(ushort value)
{
if (PlcClient != null)
{
HostToPlcData.ExhaustFanControl.Value = value;
return PlcClient.WriteValue(HostToPlcData.ExhaustFanControl);
}
return false;
}
7 months ago
public static ushort BlowerStatus()
{
if (PlcClient != null)
{
return PlcToHostData.ExhaustFanStatus.Value;
}
return 0;
}
7 months ago
7 months ago
/// <summary>
/// 是否远程模式
/// </summary>
/// <returns></returns>
public static bool IsRemote()
{
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
{
return PlcToHostData.RemoteLocalControlState.Value == 1010;
}
else
{
return false;
}
}
7 months ago
public static bool IsAuto()
{
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
{
return PlcToHostData.ModeControl.Value == 1010;
7 months ago
}
else
{
return false;
}
}
/// <summary>
/// 入口雷达检测
/// </summary>
/// <returns></returns>
public static bool EntranceRadar()
{
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
{
return PlcToHostData.RadarStatesIn.Value == 1000 ? true : false;
}
else
{
return false;
}
}
/// <summary>
/// 出口雷达检测
/// </summary>
/// <returns></returns>
public static bool ExitRadar()
{
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
{
return PlcToHostData.RadarStatesOut.Value == 1000 ? true : false;
}
else
{
return false;
}
}
/// <summary>
/// 写出口灯
/// 红灯1020
/// 绿灯1000
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static bool WriteExistLamp(int data)
{
if (PlcClient != null)
7 months ago
{
HostToPlc writeHostToPlc = new HostToPlc();
writeHostToPlc.LightOut.Value = (ushort)data;
return PlcClient.WriteValue(writeHostToPlc.LightOut);
7 months ago
}
7 months ago
return false;
}
/// <summary>
/// 下发选包
/// </summary>
/// <param name="inBinNo">入仓位,仓位号</param>
/// <param name="outBinNo">出仓位,仓位号</param>
/// <returns></returns>
public static bool DistributeBinNo(string inBinNo, string outBinNo)
7 months ago
{
if (PlcClient != null)
7 months ago
{
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);
7 months ago
return write1 && write2;
}
7 months ago
return false;
}
public static bool ReadTaskStatus(ushort taskNo)
{
if (PlcClient != null)
{
ResetPlc();
return PlcToHostData.TaskType.Value == taskNo;
}
return false;
}
/// <summary>
/// plc上使能
/// </summary>
/// <returns></returns>
public static bool HoldOn()
{
HostToPlc writeHostToPlc = new HostToPlc();
writeHostToPlc.TaskEnablement.Value = 1000;
return PlcClient.WriteValue(writeHostToPlc.TaskEnablement);
}
/// <summary>
/// 下发任务 plc 需要一起下发
/// </summary>
/// <returns></returns>
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数组
7 months ago
OperateResult operateResult = PlcClient.Write("x=16;111", encode);
return operateResult.IsSuccess;
}
7 months ago
/// <summary>
/// 下发启动换电
/// </summary>
/// <returns></returns>
public static bool StartSwapping()
{
if (PlcClient != null)
7 months ago
{
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;
7 months ago
}
7 months ago
return false;
}
7 months ago
static bool heart = false;
private static BinInfoRepository BinInfoRepository = AppInfo.Container.Resolve<BinInfoRepository>();
7 months ago
/// <summary>
/// 发送心跳
/// </summary>
/// <returns></returns>
public static bool SendHear()
{
if (PlcClient != null)
{
7 months ago
heart = !heart;
7 months ago
HostToPlc writeHostToPlc = new HostToPlc();
7 months ago
writeHostToPlc.ProtocolVersion1.Value = 2;
writeHostToPlc.ProtocolVersion2.Value = 0;
writeHostToPlc.ProtocolVersion3.Value = 1;
7 months ago
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;
var lstBinInfo = BinInfoRepository.Query();
writeHostToPlc.ChargingStatus02.Value = (short)(lstBinInfo[1].ChargeStatus == 1 ? 1010 : 1000);
writeHostToPlc.ChargingStatus03.Value = (short)(lstBinInfo[2].ChargeStatus == 1 ? 1010 : 1000);
writeHostToPlc.ChargingStatus04.Value = (short)(lstBinInfo[3].ChargeStatus == 1 ? 1010 : 1000);
writeHostToPlc.ChargingStatus05.Value = (short)(lstBinInfo[4].ChargeStatus == 1 ? 1010 : 1000);
writeHostToPlc.ChargingStatus06.Value = (short)(lstBinInfo[5].ChargeStatus == 1 ? 1010 : 1000);
writeHostToPlc.ChargingStatus07.Value = (short)(lstBinInfo[6].ChargeStatus == 1 ? 1010 : 1000);
writeHostToPlc.ChargingStatus08.Value = (short)(lstBinInfo[7].ChargeStatus == 1 ? 1010 : 1000);
7 months ago
bool write01 = PlcClient.WriteValue(writeHostToPlc.ProtocolVersion1);
bool write02 = PlcClient.WriteValue(writeHostToPlc.ProtocolVersion2);
bool write03 = PlcClient.WriteValue(writeHostToPlc.ProtocolVersion3);
7 months ago
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);
bool writeCharge2 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus02);
bool writeCharge3 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus03);
bool writeCharge4 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus04);
bool writeCharge5 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus05);
bool writeCharge6 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus06);
bool writeCharge7 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus07);
bool writeCharge8 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus08);
return write1 && write2 && write3 && write4 && write5 && write6;
}
return true;
}
7 months ago
/// <summary>
/// 查看拍照状态
/// </summary>
/// <returns></returns>
public static ushort ChannelStatus()
7 months ago
{
if (PlcClient != null)
{
return PlcToHostData.ChannelLocationState.Value;
}
7 months ago
return 0;
}
/// <summary>
/// 重新读
/// </summary>
public static void ResetPlc()
{
PlcClient.Reset();
}
7 months ago
/// <summary>
/// 读plc任务状态
/// </summary>
/// <returns>6未连接</returns>
public static int ReadPlcTaskStatus()
{
if (PlcClient != null)
7 months ago
{
return PlcToHostData.TaskStates.Value;
7 months ago
}
7 months ago
return (-1);
7 months ago
}
/// <summary>
/// 灯光控制:全部打开
/// </summary>
/// <returns></returns>
public static bool AllLightsOn()
{
bool bResult = false;
if (PlcClient != null)
{
HostToPlc writeHostToPlc = new HostToPlc();
writeHostToPlc.LightingControl.Value = 1000;
bResult = PlcClient.WriteValue(writeHostToPlc.LightingControl);
}
7 months ago
return bResult;
}
7 months ago
/// <summary>
/// 获取当前灯光状态
/// </summary>
/// <returns></returns>
public static ushort GetNowLightsOn()
{
if (PlcClient != null)
{
return PlcToHostData.LightStatus.Value;
}
7 months ago
return default;
}
/// <summary>
/// 灯光控制:全部关闭
/// </summary>
/// <returns></returns>
public static bool AllLightsOff()
{
bool bResult = false;
if (PlcClient != null)
{
HostToPlc writeHostToPlc = new HostToPlc();
writeHostToPlc.LightingControl.Value = 1010;
bResult = PlcClient.WriteValue(writeHostToPlc.LightingControl);
}
7 months ago
return bResult;
}
}