199 lines
5.0 KiB
C#
199 lines
5.0 KiB
C#
using BestHTTP;
|
|
using EasyInject.Attributes;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
/// <summary>
|
|
/// AGV 小车管理
|
|
/// </summary>
|
|
[GameObjectBean]
|
|
public class AgvCarManage : MonoBehaviour,IApplcationShutdown
|
|
{
|
|
/// <summary>
|
|
/// agv小车来源
|
|
/// </summary>
|
|
private List<ICar> source;
|
|
/// <summary>
|
|
/// agv任务列表
|
|
/// </summary>
|
|
[SerializeField]
|
|
private List<AgvTaskInfo> tasks;
|
|
[Autowired]
|
|
NetWorkComponent netWorkComponent;
|
|
|
|
private Coroutine coroutine;
|
|
[SerializeField]
|
|
private bool initialize;
|
|
|
|
public enum CarTaskStatusEnum
|
|
{
|
|
//待机
|
|
STANDBY = 0,
|
|
//取货中
|
|
TAKING = 1,
|
|
//申请取货
|
|
APPLY_TAKE = 2,
|
|
//取货完成
|
|
TAKE_FINISHED = 3,
|
|
//放货中
|
|
PUTTING = 4,
|
|
//申请放货
|
|
APPLY_PUT = 5,
|
|
//放货完成
|
|
PUT_FINISHED = 6,
|
|
//任务异常完成
|
|
ERORR_FINNSHED = 7,
|
|
//异常
|
|
EXCEPTION = 8,
|
|
//作废
|
|
DERRECATE = 9,
|
|
SENT = 10,
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
coroutine = StartCoroutine(LoadAgvTask());
|
|
source = ApplicationBoot.Instance.GetBeans<ICar>();
|
|
}
|
|
|
|
private void OnInit()
|
|
{
|
|
foreach (ICar car in source)
|
|
{
|
|
car?.OnInit();
|
|
}
|
|
|
|
initialize = true;
|
|
}
|
|
private void FixedUpdate()
|
|
{
|
|
if (!initialize)
|
|
{
|
|
return;
|
|
}
|
|
//排序
|
|
//tasks.Sort((a, b) =>
|
|
//{
|
|
// if(a.fromLocation.Contains("-") && b.fromLocation.Contains("-"))
|
|
// {
|
|
// string[] aStr = a.fromLocation.Split("-");
|
|
// string[] bStr = b.fromLocation.Split("-");
|
|
|
|
// if (int.Parse(aStr[0]) > int.Parse(bStr[0]))
|
|
// {
|
|
// return 1;
|
|
// }
|
|
// if (int.Parse(aStr[1]) > int.Parse(bStr[1]))
|
|
// {
|
|
// return 1;
|
|
// }
|
|
// return -1;
|
|
// }
|
|
// return 0;
|
|
//});
|
|
//分配小车去消费任务
|
|
|
|
foreach (AgvTaskInfo data in tasks)
|
|
{
|
|
ICar car = source.Find(v => (v.AllowRows.Contains(data.fromDevice)) && !v.hasTask && v.Initialize);
|
|
if (car != null)
|
|
{
|
|
//下发任务给小车
|
|
car.SendTask(data);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 每秒加载后台AGV任务列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public IEnumerator LoadAgvTask()
|
|
{
|
|
while (true)
|
|
{
|
|
yield return new WaitForSecondsRealtime(1f);
|
|
netWorkComponent.RemoteGetAgvTaskListData(OnTaskList);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 接受响应数据
|
|
/// </summary>
|
|
/// <param name="originalRequest"></param>
|
|
/// <param name="response"></param>
|
|
private void OnTaskList(HTTPRequest originalRequest, HTTPResponse response)
|
|
{
|
|
|
|
if(response == null)
|
|
{
|
|
return;
|
|
}
|
|
if (response.IsSuccess)
|
|
{
|
|
JObject resultData = JObject.Parse(response.DataAsText);
|
|
string json = resultData["object"].ToString();
|
|
if (!string.IsNullOrEmpty(json))
|
|
{
|
|
List<AgvTaskInfo> agvTasks = JsonConvert.DeserializeObject<List<AgvTaskInfo>>(json);
|
|
agvTasks.Sort((a, b) =>
|
|
{
|
|
if (a.fromLocation.Contains("-") && b.fromLocation.Contains("-"))
|
|
{
|
|
string[] aStr = a.fromLocation.Split("-");
|
|
string[] bStr = b.fromLocation.Split("-");
|
|
|
|
if (int.Parse(aStr[0]) > int.Parse(bStr[0]))
|
|
{
|
|
return 1;
|
|
}
|
|
if (int.Parse(aStr[1]) > int.Parse(bStr[1]))
|
|
{
|
|
return 1;
|
|
}
|
|
return -1;
|
|
}
|
|
return 0;
|
|
});
|
|
tasks = agvTasks;
|
|
if (!initialize)
|
|
{
|
|
OnInit();
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 存在当前任务
|
|
/// </summary>
|
|
/// <param name="task"></param>
|
|
/// <returns></returns>
|
|
public bool Contains(AgvTaskInfo task)
|
|
{
|
|
return tasks.Find(v => task.instructionCode == v.instructionCode && task.instructionId == v.instructionId) != null;
|
|
}
|
|
/// <summary>
|
|
/// 根据指令号获取任务
|
|
/// </summary>
|
|
/// <param name="instructionCode"></param>
|
|
/// <returns></returns>
|
|
public AgvTaskInfo GetTaskByCode(long instructionCode)
|
|
{
|
|
return tasks.Find(v => instructionCode == v.instructionCode);
|
|
}
|
|
|
|
public void Shutdown()
|
|
{
|
|
Debug.Log("释放AGV任务协程");
|
|
StopCoroutine(coroutine);
|
|
}
|
|
|
|
|
|
|
|
}
|