using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Runtime.Remoting.Messaging; using System.Text; using System.Threading; using System.Threading.Tasks; using Cysharp.Threading.Tasks; using DefaultNamespace; using DG.Tweening; using Newtonsoft.Json; using TMPro; using UnityEngine; using UnityEngine.UIElements; using static InterfaceManager; public class MqttManager : MonoSingleton { /// /// 所有设备的请求头 /// public List devices = new List(); /// /// 换电池装置 /// public List qcbdequipments = new List(); /// /// 查询提升机 /// public List elvequipments = new List(); /// /// 拆码垛机 /// public List dpmequipments = new List(); /// /// 输送机设备 /// public List convoyorequipments = new List(); /// /// 输入机设备 /// public List exportations = new List(); /// /// 小车设备 /// public List carequipments = new List(); /// /// 查询式落地机 /// public List feequipments = new List(); /// /// 查训加去盖信息 /// public List cmequipments = new List(); /// /// 消息队列 /// public ConcurrentQueue messageQueue = new ConcurrentQueue(); /// /// 第一层箱子 /// [Header("存取所有一层箱子")] public List firstbox = new List(); /// /// 第二层箱子 /// [Header("存取所有二层箱子")] public List secondbox = new List(); /// /// 存取所有箱子点位 /// public List boxinpots = new List(); /// /// 每个箱子的信息 /// [Header("存储所有箱子信息")] public List boxinformationList = new List(); /// /// 八个小车 /// [Header("所有小车设备")] public List cars = new List(); /// /// 仓库号 /// public Transform Locationnumber; /// /// 仓库的所有点位 /// [Header("存储所有仓库号")] public Transform[] points; /// /// 输送机所有设备 /// [Header("所有出库输送机设备")] public List consents = new List(); /// /// 提升机所有设备 /// [Header("所有提升机设备")] public List hoists = new List(); [Header("所有入库输送机设备")] public List warehousingdatas = new List(); [Header("所有码垛机")] public List pallerizertent = new List(); [Obsolete] private void Awake() { //启动MQTT连接 Application.ExternalCall("OpenHtmlMQTT"); ProcessQueue().Forget(); } private void Start() { points = Locationnumber.GetComponentsInChildren(); } /// /// 接受MQTT消息 /// /// public void Message(string message) { // Debug.Log($"接收到的MQTT消息---->{message}"); messageQueue.Enqueue(message); // 将消息存入队列 } private async UniTaskVoid ProcessQueue() { while (true) { if (messageQueue.TryDequeue(out string message)) { // 处理消息 Debug.Log($"剩余:{messageQueue.Count}条数据,取数据-----> {message}"); //await UniTask.Delay(10); Allequipment str = JsonConvert.DeserializeObject(message); //Debug.Log("获取到了数据"); //Debug.Log(str.device + " " + str.message); devices.Add(str); string data = str.device.Substring(0, 2); switch (data) { case "qc": Qcbd qcbd = JsonConvert.DeserializeObject(str.message); qcbdequipments.Add(qcbd); break; case "el": Elvequipment elvequipment = JsonConvert.DeserializeObject(str.message); if (hoists.Count > 0) { for (int i = 0; i < hoists.Count; i++) { if (hoists[i].ID == elvequipment.id) { hoists[i].Hoister(elvequipment); } } } elvequipments.Add(elvequipment); break; case "dp": Dpmequipment dpmequipment = JsonConvert.DeserializeObject(str.message); for (int i = 0; i < pallerizertent.Count; i++) { if (pallerizertent[i].ID == dpmequipment.id) { pallerizertent[i].Stacking(dpmequipment); } } dpmequipments.Add(dpmequipment); break; case "co": Convoyorequipment convoyorequipment = JsonConvert.DeserializeObject(str.message); string con = convoyorequipment.Id.Substring(0, 2); if (con == "54") { for (int i = 0; i < consents.Count; i++) { int index = i; if (consents[index].ID == convoyorequipment.Id) { consents[index].ConveyorData(convoyorequipment); } } } else { if (warehousingdatas.Count > 0) { for (int i = 0; i < warehousingdatas.Count; i++) { int index = i; if (warehousingdatas[index].ID == convoyorequipment.Id) { warehousingdatas[index].Equipmentdata(convoyorequipment); } } } } break; case "ca": Carequipment carequipment = JsonConvert.DeserializeObject(str.message); for (int i = 0; i < cars.Count; i++) { int indexI = i; if (cars[indexI].name == str.device) { cars[indexI].Setbox(carequipment); } } break; case "fe": Feequipment feequipment = JsonConvert.DeserializeObject(str.message); feequipments.Add(feequipment); break; case "cm": Cmequipment cmequipment = JsonConvert.DeserializeObject(str.message); cmequipments.Add(cmequipment); break; default: break; } await UniTask.Delay(10); } else { Debug.Log("等待数据"); await UniTask.Delay(1000); } } } /// /// 所有架子的查询点位 /// /// /// public Transform GetTransformByName(string pointName) { if (points.Length > 0) { for (int i = 0; i < points.Length; i++) { if (points[i].name == pointName) { return points[i]; } } } return null; } /// /// 通过箱子id删除对应的箱子和添加对应的箱子 /// /// public void Deletebox(string boxid, Boxinformation boxinformation) { Debug.Log("删除前箱子个数" + boxinformationList.Count); if (boxinformationList.Count > 0) { for (int i = 0; i < boxinformationList.Count; i++) { if (boxinformationList[i].ID == boxid) { Destroy(boxinformationList[i].gameObject); boxinformationList.Remove(boxinformationList[i]); } } } Debug.Log("删除了箱子个数" + boxinformationList.Count); if (boxinformation) { boxinformationList.Add(boxinformation); Debug.Log("添加后箱子个数" + boxinformationList.Count); } } /// /// 第一个大的输送机 /// /// public void Conveyor1(Convoyorequipment conver) { if (conver.TaskNumber != "0") { switch (conver.Id) { case "5419": //二层输送机 if (conver.ForwardRotation == "True") { if (GameObject.Find("13-31-2")) { GameObject point1 = GameObject.Find("13-31-2"); if (point1) { if (point1.gameObject.transform.childCount > 0) { GameObject box1 = point1.gameObject.transform.GetChild(0).gameObject; box1.transform.SetParent(null); if (GameObject.Find("14-31-2")) { GameObject game = GameObject.Find("14-31-2"); float times = Vector3.Distance(box1.gameObject.transform.position, game.transform.position) / 5; box1.transform.DOMove(game.transform.position, times).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("位移成功"); box1.gameObject.transform.SetParent(game.transform); }); } } } } } if (conver.ReverseRotation == "True") { if (GameObject.Find("14-31-2")) { GameObject point1 = GameObject.Find("14-31-2"); if (point1) { if (point1.gameObject.transform.childCount > 0) { GameObject box1 = point1.gameObject.transform.GetChild(0).gameObject; box1.transform.SetParent(null); if (GameObject.Find("13-31-2")) { GameObject game = GameObject.Find("13-31-2"); float times = Vector3.Distance(box1.gameObject.transform.position, game.transform.position) / 5; box1.transform.DOMove(game.transform.position, times).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("位移成功"); box1.gameObject.transform.SetParent(game.transform); }); } } } } } break; case "5420": //一层输送机 if (conver.ForwardRotation == "True") { if (GameObject.Find("14-31-1")) { GameObject point1 = GameObject.Find("14-31-1"); if (point1) { if (point1.gameObject.transform.childCount > 0) { GameObject box1 = point1.gameObject.transform.GetChild(0).gameObject; box1.transform.SetParent(null); if (GameObject.Find("13-31-1")) { GameObject game = GameObject.Find("13-31-1"); float times = Vector3.Distance(box1.gameObject.transform.position, game.transform.position) / 5; box1.transform.DOMove(game.transform.position, times).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("位移成功"); box1.gameObject.transform.SetParent(game.transform); }); } } } } } if (conver.ReverseRotation == "True") { if (GameObject.Find("13-31-1")) { GameObject point1 = GameObject.Find("13-31-1"); if (point1) { if (point1.gameObject.transform.childCount > 0) { GameObject box1 = point1.gameObject.transform.GetChild(0).gameObject; box1.transform.SetParent(null); if (GameObject.Find("14-31-1")) { GameObject game = GameObject.Find("14-31-1"); float times = Vector3.Distance(box1.gameObject.transform.position, game.transform.position) / 5; box1.transform.DOMove(game.transform.position, times).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("位移成功"); box1.gameObject.transform.SetParent(game.transform); }); } } } } } break; case "5421": //提升机内输送机 if (conver.ForwardRotation == "True") { if (GameObject.Find("14-31-2")) { GameObject point1 = GameObject.Find("14-31-2"); if (point1) { if (point1.gameObject.transform.childCount > 0) { GameObject box1 = point1.gameObject.transform.GetChild(0).gameObject; box1.transform.SetParent(null); if (GameObject.Find("14-31-1")) { GameObject game = GameObject.Find("14-31-1"); float times = Vector3.Distance(box1.gameObject.transform.position, game.transform.position) / 5; box1.transform.DOMove(game.transform.position, times).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("位移成功"); box1.gameObject.transform.SetParent(game.transform); }); } } } } } if (conver.ReverseRotation == "True") { if (GameObject.Find("14-31-1")) { GameObject point1 = GameObject.Find("14-31-1"); if (point1) { if (point1.gameObject.transform.childCount > 0) { GameObject box1 = point1.gameObject.transform.GetChild(0).gameObject; box1.transform.SetParent(null); if (GameObject.Find("14-31-2")) { GameObject game = GameObject.Find("14-31-2"); float times = Vector3.Distance(box1.gameObject.transform.position, game.transform.position) / 5; box1.transform.DOMove(game.transform.position, times).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("位移成功"); box1.gameObject.transform.SetParent(game.transform); }); } } } } } break; default: break; } } } }