using BestHTTP; using EasyInject.Attributes; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// AGV 小车管理 /// [GameObjectBean] public class AgvCarManage : MonoBehaviour,IApplcationShutdown { /// /// agv小车来源 /// private List source; /// /// agv任务列表 /// [SerializeField] private List 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(); } 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); } } } /// /// 每秒加载后台AGV任务列表 /// /// public IEnumerator LoadAgvTask() { while (true) { yield return new WaitForSecondsRealtime(1f); netWorkComponent.RemoteGetAgvTaskListData(OnTaskList); } } /// /// 接受响应数据 /// /// /// 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 agvTasks = JsonConvert.DeserializeObject>(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(); } } } } /// /// 存在当前任务 /// /// /// public bool Contains(AgvTaskInfo task) { return tasks.Find(v => task.instructionCode == v.instructionCode && task.instructionId == v.instructionId) != null; } /// /// 根据指令号获取任务 /// /// /// public AgvTaskInfo GetTaskByCode(long instructionCode) { return tasks.Find(v => instructionCode == v.instructionCode); } public void Shutdown() { Debug.Log("释放AGV任务协程"); StopCoroutine(coroutine); } }