using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HBLConsole.Service;
using Lebai.SDK;
using Lebai.SDK.Dtos;
using Robotc;
using HBLConsole.Model;
namespace HBLConsole.Communication
{
public class LebaiHelper
{
public const int SENCE_自嗨 = 10008;
public const int SENCE_接冰淇淋1 = 10004;
public const int SENCE_接冰淇淋2 = 10003;
public const int SENCE_接冰淇淋3 = 10005;
public const int SENCE_咖啡 = 10006;
#region 新增场景
public const int SENCE_取咖啡杯 = 10013;
public const int SENCE_咖啡杯检测 = 10014;
public const int SENCE_再次取杯 = 10017;
public const int SENCE_接咖啡 = 10015;
public const int SENCE_送咖啡 = 10016; //无取走咖啡检测配合场景
public const int SENCE_New送咖啡 = 10021;//有取走咖啡检测配合场景
public const int SENCE_有无咖啡检测 = 10019;
public const int SENCE_再送咖啡 = 10020;
public const int SENCE_复位 = 10018;
#endregion
public const int SENCE_欢迎 = 10002;
private volatile static LebaiHelper _Instance;
public static LebaiHelper GetInstance => _Instance ?? (_Instance = new LebaiHelper());
private LebaiHelper() { }
private LebaiRobotClient client;
private RobotData robotData;
public bool IsIdle { get; set; } = false;
public bool IsConnected { get { return null != robotData; } }
public void Connect(string ip)
{
bool ErrorFlag = false;
while (robotData == null)
{
try
{
client = new LebaiRobotClient(ip);
robotData = client.GetRobotData().Result;
}
catch (Exception ex)
{
if (!ErrorFlag)
{
MessageLog.GetInstance.ShowEx(ex.ToString());
ErrorFlag = true;
}
Thread.Sleep(3000);
}
}
StartRobot();
MessageLog.GetInstance.Show("乐百机器人连接成功!");
}
public void GetRobotModeStatus()
{
if (robotData == null) return;
int mode = robotData.RobotMode.Mode;
IsIdle = mode == 5;
for (int i = 0; i < 14; i++)
{
if (RTrig.GetInstance(((ELebaiRModel)i).ToString()).Start(mode == i))
MessageLog.GetInstance.Show(((ELebaiRModel)i).ToString());
}
}
public void GetSpeed()
{
//client.StartSys().GetAwaiter();
var aa = client.GetActualTcpSpeed().GetAwaiter();
//robotData.RobotMode.Mode
}
///
/// 启动机器人
///
public async void StartRobot()
{
if (robotData != null)
{
await client.StartSys();
await client.Sync();
MessageLog.GetInstance.Show("机器人启动成功");
}
}
///
/// 获取抓手重量
///
///
public double GetClawWdight()
{
if (robotData != null)
{
return client.GetClawWeight().Result.Weight_;
}
return 0;
}
///
/// 获取信号量
///
///
///
public SignalResult GetValueAsync(int index = 0)
{
if (robotData == null) return default(SignalResult);
SignalValue signalValue = new SignalValue();
signalValue.Index = index;
return client?.GetSignal(signalValue).Result;
}
///
/// 设置信号量
///
///
///
///
public SignalResult SetValue(int value, int index = 0)
{
if (robotData == null) return default(SignalResult);
SignalValue signalValue = new SignalValue();
signalValue.Index = index;
signalValue.Value = value;
return client.SetSignal(signalValue).Result;
}
public bool GetInput(int pin=0)
{
if (client == null) return false;
var res = client.GetDIO(new IOPin() { Pin=pin}).Result;
if (res != null)
{
return res.Value == 1 ? true : false;
}
//int a = res.Value;
return false;
}
///
/// 运行指定的场景
///
///
public void Scene(int id)
{
if (robotData == null || client == null) return;
if (!client.GetIsCanRunTask().Result) return;
client?.RunScene(id);
//var r = client.RunSceneUntilDone(id).Result;
//var res = client.GetTask(id).Result;
//var res1 = GetValueAsync().Value;
//while (res1!=id)
//{
// client?.RunScene(id);
// Thread.Sleep(100);
//}
//SetValue(1000);
}
}
}