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.

236 lines
6.2 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 Autofac;
using HybirdFrameworkCore.Autofac;
using Service.PlcAfrica.Msg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HslCommunication;
using HybirdFrameworkCore.Utils;
using Service.Charger.Server;
using Service.Execute.Model;
namespace Service.PlcAfrica.Client
{
public class PlcAfricaMgr
{
public static PlcAfricaClient? PlcAfricaClient;
public static PlcAfricaMsg PlcAfricaMsg = new PlcAfricaMsg();
/// <summary>
/// 数据有效时间。单位秒
/// </summary>
static int DataTimeSeconds = 20;
/// <summary>
/// 数据刷新时间
/// </summary>
public static DateTime DataValidityTime = DateTime.Now;
public static void PlcInit()
{
if (PlcAfricaClient == null)
{
PlcAfricaClient = AppInfo.Container.Resolve<PlcAfricaClient>();
}
Task.Run(() => PlcAfricaClient.Connect());
}
public static void Reset()
{
if (PlcAfricaClient == null)
{
PlcAfricaClient.Reset();
}
}
/// <summary>
/// 0 无动作
/// 1 急停指令
/// 2 自动换电启动指令
/// 3 自动调仓启动指令
/// 4 暂停指令
/// 5 继续指令
/// 6 停止指令
/// 7 结束指令
/// 11 测量轴启动
/// 12 回原点
/// 101 此行及以下未实装维持《上位机PLC通讯内容V1.5》内容
/// </summary>
/// <param name="cmd"></param>
public static bool StartSwap()
{
if (PlcAfricaClient == null)
{
PlcAfricaMsg.OperationInstruction.Value = 2;
PlcAfricaClient!.WriteValue(PlcAfricaMsg.OperationInstruction);
Reset();
return PlcAfricaMsg.StationCommandFeedback.Value == 2;
}
return false;
}
/// <summary>
/// 操作指令
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
public static bool OperationInstruction(ushort cmd)
{
if (PlcAfricaClient == null)
{
PlcAfricaMsg.OperationInstruction.Value = cmd;
PlcAfricaClient!.WriteValue(PlcAfricaMsg.OperationInstruction);
Reset();
return PlcAfricaMsg.StationCommandFeedback.Value == cmd;
}
return false;
}
/// <summary>
/// 读8000 自动任务状态
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
public static ushort AutoActionStatus()
{
return PlcAfricaMsg.ActionCode.Value ;
}
/// <summary>
/// 机器人状态 7001
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
public static bool RobotStatus(ushort cmd)
{
return PlcAfricaMsg.PowerChangeModeRobot.Value == cmd;
}
/// <summary>
/// 机器人状态 就绪
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
public static bool RobotReady()
{
return PlcAfricaMsg.PowerChangeModeRobot.Value == 1;
}
/// <summary>
/// 机器人状态 工作完成
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
public static bool RobotWorkFinish()
{
return PlcAfricaMsg.PowerChangeModeRobot.Value == 3;
}
/// <summary>
/// 下发车辆信息 0无车辆1车辆解锁2车辆上锁
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
public static bool CarStatus(ushort cmd)
{
if (PlcAfricaClient == null)
{
PlcAfricaMsg.TboxUnlock.Value = cmd;
return PlcAfricaClient!.WriteValue(PlcAfricaMsg.TboxUnlock);
}
return false;
}
///
///下发取放仓位
///
/// <summary>
/// 下发换电
/// </summary>
/// <returns></returns>
public static bool DistributeTask(ushort inBinNo, ushort outBinNo)
{
//下发任务前,增加待机状态判断
if (!RobotReady())
{
return false;
}
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 = PlcAfricaClient.Write("x=16;111", encode);
return operateResult.IsSuccess;
}
public static bool IsAutoAndRemote()
{
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
{
return PlcAfricaMsg.PowerChangeMode.Value == 2;
}
else
{
return false;
}
}
/// <summary>
/// 红绿灯 0-不亮1-亮红灯2-绿灯3-绿灯闪
/// </summary>
/// <returns></returns>
public static bool LightControl(ushort cmd)
{
if (PlcAfricaClient == null)
{
PlcAfricaMsg.TrafficLightFunction.Value = cmd;
return PlcAfricaClient!.WriteValue(PlcAfricaMsg.TrafficLightFunction);
}
return false;
}
}
}