using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Lebai.SDK;
using Lebai.SDK.Dtos;
using Robotc;
using BPASmartClient.Message;
using BPASmartClient.Helper;
using TaskStatus = Lebai.SDK.Dtos.TaskStatus;
using BPASmartClient.Peripheral;
using BPASmartClient.EventBus;
using static BPASmartClient.EventBus.EventBus;
using BPASmartClient.Model;
using BPASmartClient.LebaiRobot;
using BPASmartClient.Model.乐白机器人;
namespace BPASmartClient.Lebai
{
public class LebaiRobot : BasePeripheral
{
///
/// 抓手上传感器输入位索引
///
public int HandSensor { get; set; }
///
/// 输出信号量索引
///
public int OutputSingalValue { get; set; }
protected override void InitStatus()
{
status["Connected"] = false;
status["HandSensor"] = 0;
status["OutputSingalValue"] = 0;
}
public override void Start()
{
LebaiHelper.GetInstance().Connect(communicationPar.IPAddress);
ThreadManage.GetInstance().StartLong(() => { LebaiHelper.GetInstance().Reconnect(communicationPar.IPAddress); Thread.Sleep(5000); }, "机器人重连检测");
ThreadManage.GetInstance().StartLong(() =>
{
IsConnected = LebaiHelper.GetInstance().IsConnected;
status["RobotIsConnected"] = LebaiHelper.GetInstance().IsConnected;
status["RobotOK"] = LebaiHelper.GetInstance().GetValueAsync().Ok;
status["RobotValue"] = LebaiHelper.GetInstance().GetValueAsync().Value;
if (LebaiHelper.GetInstance().robotData != null) status["RobotMode"] =(ELebaiRModel)LebaiHelper.GetInstance().robotData.RobotMode.Mode;
status["RobotValue1"] = LebaiHelper.GetInstance().GetValueAsync(1).Value;
status["GetInput"] = LebaiHelper.GetInstance().GetInput();
status["GetInput2"] = LebaiHelper.GetInstance().GetInput(2);
if (LebaiHelper.GetInstance().robotData != null) status["RobotMode"] = LebaiHelper.GetInstance().robotData.RobotMode.Mode;
LebaiHelper.GetInstance().GetRobotModeStatus();
Thread.Sleep(10);
}, "获取乐白机器人数据");
}
public override void Stop()
{
}
public override void Init()
{
EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
{
});
//获取机器人信号
EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
{
if (@event == null) return;
if (@event is LebaiRobot_GetInputEvent getInput)
{
callBack.Invoke(LebaiHelper.GetInstance().GetInput(getInput.Pin));
}
});
//获取Tcp信号
EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
{
if (@event == null) return;
if (@event is LebaiRobot_GetTCPInputEvent getTCPInput)
{
callBack?.Invoke(LebaiHelper.GetInstance().GetTcpInput(getTCPInput.Pin));
}
});
//机器人输入信号
EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
{
if (@event == null) return;
if (@event is LebaiRobot_SetValueEvent SetValueEvent)
{
callBack?.Invoke(LebaiHelper.GetInstance().SetValue(SetValueEvent.RobotSetValue));
}
});
//控制机器人
EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
{
if (@event == null) return;
if (@event is LebaiRobot_LebaiControlEvent lebaiControlEvent)
{
switch (lebaiControlEvent.LebaiControl)
{
case "机器人启动":
LebaiHelper.GetInstance().StartRobot();
break;
case "启动示教":
LebaiHelper.GetInstance().StartTeachMode();
break;
case "停止示教":
LebaiHelper.GetInstance().EndtTeachMode();
break;
case "机器人急停":
LebaiHelper.GetInstance().EStopRobot();
break;
case "暂停":
LebaiHelper.GetInstance().pauseMode();
break;
case "恢复":
LebaiHelper.GetInstance().resumeMode();
break;
default:
break;
}
}
});
//选择机器人场景
EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
{
if (@event == null) return;
if (@event is LebaiRobot_LebaiSenceEvent lebaiSenceEvent)
{
LebaiHelper.GetInstance().Scene(lebaiSenceEvent.LebaiSence);
}
});
EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
{
if (@event == null) return;
if (@event is LebaiRobot_SetOutPutEvent lebaiOutputEvent)
{
LebaiHelper.GetInstance().SetOutput(lebaiOutputEvent.Value,lebaiOutputEvent.Pin);
}
});
InitStatus();
Start();
MessageLog.GetInstance.Show("乐白机器人初始化完成");
}
public override void WriteData(string address, object value)
{
}
}
}