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 HighlightPlus; 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(); [Header("所有机械手臂")] public List machines = new List(); [Header("机械手臂点位存储")] public Transform Roboticarmpointposition; public Transform[] Robotpoints; [Header("所有电池设备")] public List batteryanimations = new List(); [Header("输送机设备故障高亮")] public List Highlights = new List(); [Header("提升机高亮故障")] public List hoisters = new List(); public void Awake() { //启动MQTT连接 Application.ExternalCall("OpenHtmlMQTT"); //Debug.Log("连接成功MQTT"); ProcessQueue().Forget(); } private void Start() { points = Locationnumber.GetComponentsInChildren(); Robotpoints = Roboticarmpointposition.GetComponentsInChildren(); } /// /// 接受MQTT消息 /// /// public void Message(string message) { // Debug.Log($"接收到的MQTT消息---->{message}"); messageQueue.Enqueue(message); // 将消息存入队列 } public 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("接收到的数据" + str); //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); if (batteryanimations.Count > 0) { for (int i = 0; i < batteryanimations.Count; i++) { int index = i; if (batteryanimations[index].ID == qcbd.id) { batteryanimations[index].Batterydata(qcbd); } } } //qcbdequipments.Add(qcbd); break; case "el": //提升机 Elvequipment elvequipment = JsonConvert.DeserializeObject(str.message); if (hoists.Count > 0) { for (int i = 0; i < hoists.Count; i++) { int index = i; if (hoists[index].ID == elvequipment.id) { hoists[index].Hoister(elvequipment); } } } //elvequipments.Add(elvequipment); break; case "dp": //码垛机 Dpmequipment dpmequipment = JsonConvert.DeserializeObject(str.message); for (int i = 0; i < pallerizertent.Count; i++) { int index = i; if (pallerizertent[index].ID == dpmequipment.id) { pallerizertent[index].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] != null) { 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; case "fx": //机械手臂 Roboticarm roboticarm = JsonConvert.DeserializeObject(str.message); for (int i = 0; i < machines.Count; i++) { int index = i; if (machines[index].Id == roboticarm.id) { machines[index].Getarm(roboticarm); //Debug.Log("调用对应机械手臂" + roboticarm.id); } } break; default: break; } await UniTask.Delay(100); } 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; } public Transform Mechanicalarm(string name) { if (Robotpoints.Length > 0) { for (int i = 0; i < Robotpoints.Length; i++) { if (Robotpoints[i].name == name) { return Robotpoints[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 Highlight(string init) { if (Highlights.Count > 0) { for (int i = 0; i < Highlights.Count; i++) { if (Highlights[i].name == init) { HighlightEffect highlightEffect = Highlights[i].transform.GetComponent(); highlightEffect.highlighted = true; } } } } public void Highlight3(string init) { if (Highlights.Count > 0) { for (int i = 0; i < Highlights.Count; i++) { if (Highlights[i].name == init) { HighlightEffect highlightEffect = Highlights[i].transform.GetComponent(); highlightEffect.highlighted = false; } } } } /// /// 提升机高亮 /// /// public void Highlight2(string init) { if (hoisters.Count > 0) { for (int i = 0; i < hoisters.Count; i++) { if (hoisters[i].name == init) { HighlightEffect highlightEffect = hoisters[i].transform.GetComponent(); highlightEffect.highlighted = true; } } } } public void Highlight4(string init) { if (hoisters.Count > 0) { for (int i = 0; i < hoisters.Count; i++) { if (hoisters[i].name == init) { HighlightEffect highlightEffect = hoisters[i].transform.GetComponent(); highlightEffect.highlighted = false; } } } } }