462 lines
19 KiB
C#
462 lines
19 KiB
C#
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<MqttManager>
|
|
{
|
|
/// <summary>
|
|
/// 所有设备的请求头
|
|
/// </summary>
|
|
public List<Allequipment> devices = new List<Allequipment>();
|
|
/// <summary>
|
|
/// 换电池装置
|
|
/// </summary>
|
|
public List<Qcbd> qcbdequipments = new List<Qcbd>();
|
|
/// <summary>
|
|
/// 查询提升机
|
|
/// </summary>
|
|
public List<Elvequipment> elvequipments = new List<Elvequipment>();
|
|
/// <summary>
|
|
/// 拆码垛机
|
|
/// </summary>
|
|
public List<Dpmequipment> dpmequipments = new List<Dpmequipment>();
|
|
/// <summary>
|
|
/// 输送机设备
|
|
/// </summary>
|
|
public List<Convoyorequipment> convoyorequipments = new List<Convoyorequipment>();
|
|
/// <summary>
|
|
/// 输入机设备
|
|
/// </summary>
|
|
public List<Convoyorequipment> exportations = new List<Convoyorequipment>();
|
|
/// <summary>
|
|
/// 小车设备
|
|
/// </summary>
|
|
public List<Carequipment> carequipments = new List<Carequipment>();
|
|
/// <summary>
|
|
/// 查询式落地机
|
|
/// </summary>
|
|
public List<Feequipment> feequipments = new List<Feequipment>();
|
|
/// <summary>
|
|
/// 查训加去盖信息
|
|
/// </summary>
|
|
public List<Cmequipment> cmequipments = new List<Cmequipment>();
|
|
/// <summary>
|
|
/// 消息队列
|
|
/// </summary>
|
|
public ConcurrentQueue<string> messageQueue = new ConcurrentQueue<string>();
|
|
/// <summary>
|
|
/// 第一层箱子
|
|
/// </summary>
|
|
[Header("存取所有一层箱子")]
|
|
public List<GameObject> firstbox = new List<GameObject>();
|
|
/// <summary>
|
|
/// 第二层箱子
|
|
/// </summary>
|
|
[Header("存取所有二层箱子")]
|
|
public List<GameObject> secondbox = new List<GameObject>();
|
|
/// <summary>
|
|
/// 存取所有箱子点位
|
|
/// </summary>
|
|
public List<string> boxinpots = new List<string>();
|
|
/// <summary>
|
|
/// 每个箱子的信息
|
|
/// </summary>
|
|
[Header("存储所有箱子信息")]
|
|
public List<Boxinformation> boxinformationList = new List<Boxinformation>();
|
|
/// <summary>
|
|
/// 八个小车
|
|
/// </summary>
|
|
[Header("所有小车设备")]
|
|
public List<Carinformation> cars = new List<Carinformation>();
|
|
/// <summary>
|
|
/// 仓库号
|
|
/// </summary>
|
|
public Transform Locationnumber;
|
|
/// <summary>
|
|
/// 仓库的所有点位
|
|
/// </summary>
|
|
[Header("存储所有仓库号")]
|
|
public Transform[] points;
|
|
/// <summary>
|
|
/// 输送机所有设备
|
|
/// </summary>
|
|
[Header("所有出库输送机设备")]
|
|
public List<Conveyordata> consents = new List<Conveyordata>();
|
|
/// <summary>
|
|
/// 提升机所有设备
|
|
/// </summary>
|
|
[Header("所有提升机设备")]
|
|
public List<Hoistmovement> hoists = new List<Hoistmovement>();
|
|
[Header("所有入库输送机设备")]
|
|
public List<Warehousingdata> warehousingdatas = new List<Warehousingdata>();
|
|
[Header("所有码垛机")]
|
|
public List<Palletizertent> pallerizertent = new List<Palletizertent>();
|
|
[Obsolete]
|
|
private void Awake()
|
|
{
|
|
//启动MQTT连接
|
|
Application.ExternalCall("OpenHtmlMQTT");
|
|
|
|
|
|
ProcessQueue().Forget();
|
|
}
|
|
private void Start()
|
|
{
|
|
points = Locationnumber.GetComponentsInChildren<Transform>();
|
|
}
|
|
/// <summary>
|
|
/// 接受MQTT消息
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
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<Allequipment>(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<Qcbd>(str.message);
|
|
qcbdequipments.Add(qcbd);
|
|
|
|
break;
|
|
case "el":
|
|
Elvequipment elvequipment = JsonConvert.DeserializeObject<Elvequipment>(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<Dpmequipment>(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<Convoyorequipment>(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<Carequipment>(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<Feequipment>(str.message);
|
|
feequipments.Add(feequipment);
|
|
break;
|
|
case "cm":
|
|
Cmequipment cmequipment = JsonConvert.DeserializeObject<Cmequipment>(str.message);
|
|
|
|
cmequipments.Add(cmequipment);
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
await UniTask.Delay(10);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("等待数据");
|
|
await UniTask.Delay(1000);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 所有架子的查询点位
|
|
/// </summary>
|
|
/// <param name="pointName"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// 通过箱子id删除对应的箱子和添加对应的箱子
|
|
/// </summary>
|
|
/// <param name="boxid"></param>
|
|
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);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 第一个大的输送机
|
|
/// </summary>
|
|
/// <param name="conver"></param>
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|