408 lines
14 KiB
C#
408 lines
14 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 HighlightPlus;
|
|
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>();
|
|
[Header("所有机械手臂")]
|
|
public List<Machine> machines = new List<Machine>();
|
|
[Header("机械手臂点位存储")]
|
|
public Transform Roboticarmpointposition;
|
|
public Transform[] Robotpoints;
|
|
[Header("所有电池设备")]
|
|
public List<Batteryanimation> batteryanimations = new List<Batteryanimation>();
|
|
[Header("输送机设备故障高亮")]
|
|
public List<GameObject> Highlights = new List<GameObject>();
|
|
[Header("提升机高亮故障")]
|
|
public List<GameObject> hoisters = new List<GameObject>();
|
|
public void Awake()
|
|
{
|
|
//启动MQTT连接
|
|
Application.ExternalCall("OpenHtmlMQTT");
|
|
//Debug.Log("连接成功MQTT");
|
|
|
|
ProcessQueue().Forget();
|
|
}
|
|
private void Start()
|
|
{
|
|
points = Locationnumber.GetComponentsInChildren<Transform>();
|
|
Robotpoints = Roboticarmpointposition.GetComponentsInChildren<Transform>();
|
|
}
|
|
/// <summary>
|
|
/// 接受MQTT消息
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
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<Allequipment>(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<Qcbd>(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<Elvequipment>(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<Dpmequipment>(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<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] != null)
|
|
{
|
|
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;
|
|
case "fx":
|
|
//机械手臂
|
|
Roboticarm roboticarm = JsonConvert.DeserializeObject<Roboticarm>(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);
|
|
}
|
|
}
|
|
}
|
|
/// <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;
|
|
}
|
|
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;
|
|
}
|
|
/// <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="init"></param>
|
|
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>();
|
|
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>();
|
|
highlightEffect.highlighted = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 提升机高亮
|
|
/// </summary>
|
|
/// <param name="init"></param>
|
|
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>();
|
|
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>();
|
|
highlightEffect.highlighted = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|