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();
///
/// 数据有效时间。单位秒
///
static int DataTimeSeconds = 20;
///
/// 数据刷新时间
///
public static DateTime DataValidityTime = DateTime.Now;
public static void PlcInit()
{
if (PlcAfricaClient == null)
{
PlcAfricaClient = AppInfo.Container.Resolve();
}
Task.Run(() => PlcAfricaClient.Connect());
}
public static void Reset()
{
if (PlcAfricaClient == null)
{
PlcAfricaClient.Reset();
}
}
///
/// 0 无动作
/// 1 急停指令
/// 2 自动换电启动指令
/// 3 自动调仓启动指令
/// 4 暂停指令
/// 5 继续指令
/// 6 停止指令
/// 7 结束指令
/// 11 测量轴启动
/// 12 回原点
/// 101 此行及以下未实装,维持《上位机PLC通讯内容V1.5》内容
///
///
public static bool StartSwap()
{
if (PlcAfricaClient == null)
{
PlcAfricaMsg.OperationInstruction.Value = 2;
PlcAfricaClient!.WriteValue(PlcAfricaMsg.OperationInstruction);
Reset();
return PlcAfricaMsg.StationCommandFeedback.Value == 2;
}
return false;
}
///
/// 操作指令
///
///
///
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;
}
///
/// 读8000 自动任务状态
///
///
///
public static ushort AutoActionStatus()
{
return PlcAfricaMsg.ActionCode.Value ;
}
///
/// 机器人状态 7001
///
///
///
public static bool RobotStatus(ushort cmd)
{
return PlcAfricaMsg.PowerChangeModeRobot.Value == cmd;
}
///
/// 机器人状态 就绪
///
///
///
public static bool RobotReady()
{
return PlcAfricaMsg.PowerChangeModeRobot.Value == 1;
}
///
/// 机器人状态 工作完成
///
///
///
public static bool RobotWorkFinish()
{
return PlcAfricaMsg.PowerChangeModeRobot.Value == 3;
}
///
/// 下发车辆信息 0无车辆,1车辆解锁,2车辆上锁
///
///
///
public static bool CarStatus(ushort cmd)
{
if (PlcAfricaClient == null)
{
PlcAfricaMsg.TboxUnlock.Value = cmd;
return PlcAfricaClient!.WriteValue(PlcAfricaMsg.TboxUnlock);
}
return false;
}
///
///下发取放仓位
///
///
/// 下发换电
///
///
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;
}
}
///
/// 红绿灯 0-不亮,1-亮红灯,2-绿灯,3-绿灯闪
///
///
public static bool LightControl(ushort cmd)
{
if (PlcAfricaClient == null)
{
PlcAfricaMsg.TrafficLightFunction.Value = cmd;
return PlcAfricaClient!.WriteValue(PlcAfricaMsg.TrafficLightFunction);
}
return false;
}
}
}