249 lines
7.8 KiB
C#
249 lines
7.8 KiB
C#
using HslCommunication;
|
|
using HslCommunication.MQTT;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
|
|
public class MQTTManager : MonoBehaviour
|
|
{
|
|
public static MQTTManager instance;
|
|
/// <summary>
|
|
/// MQTT
|
|
/// </summary>
|
|
MqttClient mqtt_client;
|
|
/// <summary>
|
|
/// 接收到的消息队列
|
|
/// </summary>
|
|
private Queue<string> reciveDatas = new Queue<string>();
|
|
/// <summary>
|
|
/// 待发送消息队列
|
|
/// </summary>
|
|
private Queue<MQTTSendData> willSendDatas = new Queue<MQTTSendData>();
|
|
#region 订阅主题
|
|
//房间/标题
|
|
|
|
/// <summary>
|
|
/// 无人机位置信息传递
|
|
/// </summary>
|
|
public string DronePosition;
|
|
/// <summary>
|
|
/// 无人机攻击目标锁定
|
|
/// </summary>
|
|
public string SingleDronePosition;
|
|
/// <summary>
|
|
/// 驱离无人机
|
|
/// </summary>
|
|
public string WRJExpel;
|
|
/// <summary>
|
|
/// 单个无人机被销毁
|
|
/// </summary>
|
|
public string DroneWasDestroyed;
|
|
/// <summary>
|
|
/// 无线电干扰无人机同步
|
|
/// </summary>
|
|
public string WRJDitch;
|
|
/// <summary>
|
|
/// 无人机被销毁
|
|
/// </summary>
|
|
public string Planedata;
|
|
/// <summary>
|
|
/// 激光发射位置传递
|
|
/// </summary>
|
|
public string Lasing;
|
|
/// <summary>
|
|
/// 微波武器发射位置
|
|
/// </summary>
|
|
public string Micow;
|
|
/// <summary>
|
|
/// 设备被销毁
|
|
/// </summary>
|
|
public string SetToBeDestroyed;
|
|
/// <summary>
|
|
/// 设备被收回
|
|
/// </summary>
|
|
public string SetToBeDestroyedTwo;
|
|
/// <summary>
|
|
/// 设备频段设置
|
|
/// </summary>
|
|
public string BandSetting;
|
|
/// <summary>
|
|
/// 设备探测频段设置
|
|
/// </summary>
|
|
public string SweepFrequencyBand;
|
|
/// <summary>
|
|
/// 重要用户被攻击
|
|
/// </summary>
|
|
public string KeyTarget;
|
|
/// <summary>
|
|
/// 标识符
|
|
/// </summary>
|
|
public string identification;
|
|
#endregion
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
identification = "/" + GlobalFlag.practiceSubjectID;// + "/" + GlobalFlag.currentUser.user_id;
|
|
DronePosition += identification;
|
|
SingleDronePosition += identification;
|
|
WRJExpel += identification;
|
|
DroneWasDestroyed += identification;
|
|
WRJDitch += identification;
|
|
Planedata += identification;
|
|
Lasing += identification;
|
|
Micow += identification;
|
|
SetToBeDestroyed += identification;
|
|
SetToBeDestroyedTwo += identification;
|
|
BandSetting += identification;
|
|
SweepFrequencyBand += identification;
|
|
KeyTarget += identification;
|
|
instance = this;
|
|
StartContent();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (reciveDatas.Count > 0)
|
|
{
|
|
WorkWhthData(reciveDatas.Dequeue());
|
|
}
|
|
|
|
if (willSendDatas.Count > 0)
|
|
{
|
|
SendData(willSendDatas.Dequeue());
|
|
}
|
|
|
|
//if (Input.GetKeyDown(KeyCode.P))
|
|
//{
|
|
// SendData(UAV_DronePosition, "数据");
|
|
//}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 主连接线程
|
|
/// </summary>
|
|
private void StartContent()
|
|
{
|
|
try
|
|
{
|
|
Debug.Log("开始链接蓝牙网关");
|
|
// 授权示例 调用一次即可 call only once
|
|
if (!HslCommunication.Authorization.SetAuthorizationCode("d88d407a-4637-423b-ba06-4ee89b3e01f8"))
|
|
{
|
|
return;
|
|
}
|
|
//MQTTInitData.SaveDefault();
|
|
var data = new MQTTInitData();
|
|
data.LoadConfig();
|
|
var options = data.ToContentOptions();
|
|
//options.Credentials = new MqttCredential("admin", "123456");
|
|
mqtt_client = new MqttClient(options);
|
|
// 接收到数据的时候进行触发
|
|
mqtt_client.OnMqttMessageReceived += OnMQTTMessageReceived;
|
|
// 订阅服务器的主题,在连接成功后就去订阅
|
|
mqtt_client.OnClientConnected += m =>
|
|
{
|
|
Debug.Log("连接成功");
|
|
|
|
OperateResult sub0 = m.SubscribeMessage(DronePosition);//订阅主题
|
|
OperateResult sub1 = m.SubscribeMessage(SingleDronePosition);//订阅主题
|
|
OperateResult sub2 = m.SubscribeMessage(WRJExpel);//订阅主题
|
|
OperateResult sub3 = m.SubscribeMessage(DroneWasDestroyed);//订阅主题
|
|
OperateResult sub4 = m.SubscribeMessage(WRJDitch);//订阅主题
|
|
OperateResult sub5 = m.SubscribeMessage(Planedata);//订阅主题
|
|
OperateResult sub6 = m.SubscribeMessage(Lasing);//订阅主题
|
|
OperateResult sub7 = m.SubscribeMessage(Micow);//订阅主题
|
|
OperateResult sub8 = m.SubscribeMessage(SetToBeDestroyed);//订阅主题
|
|
OperateResult sub9 = m.SubscribeMessage(SetToBeDestroyedTwo);//订阅主题
|
|
OperateResult sub10 = m.SubscribeMessage(BandSetting);//订阅主题
|
|
OperateResult sub11 = m.SubscribeMessage(SweepFrequencyBand);//订阅主题
|
|
OperateResult sub12 = m.SubscribeMessage(KeyTarget);//订阅主题
|
|
if (sub0.IsSuccess && sub1.IsSuccess && sub2.IsSuccess && sub3.IsSuccess && sub4.IsSuccess
|
|
&& sub5.IsSuccess&& sub6.IsSuccess && sub7.IsSuccess && sub8.IsSuccess && sub9.IsSuccess
|
|
&& sub10.IsSuccess && sub11.IsSuccess && sub12.IsSuccess)
|
|
{
|
|
Debug.Log("订阅"+ DronePosition+ SingleDronePosition + WRJExpel + DroneWasDestroyed + WRJDitch + Planedata + Lasing
|
|
+ Micow + SetToBeDestroyed + SetToBeDestroyedTwo + "成功");
|
|
}
|
|
};
|
|
mqtt_client.ConnectServer();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogException(ex);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 接收消息
|
|
/// </summary>
|
|
/// <param name="client"></param>
|
|
/// <param name="topic"></param>
|
|
/// <param name="payload"></param>
|
|
private void OnMQTTMessageReceived(MqttClient client, string topic, byte[] payload)
|
|
{
|
|
var msg = Encoding.UTF8.GetString(payload);
|
|
reciveDatas.Enqueue(msg);
|
|
}
|
|
/// <summary>
|
|
/// 处理接收到的消息
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
void WorkWhthData(string data)
|
|
{
|
|
Debug.Log($"<color=yellow>{$"处理数据{data}"}</color>");
|
|
string[] datas = data.Split('$');
|
|
if (datas[0] != GlobalFlag.currentUser.user_id)
|
|
DeviceManager.Instance.GetSend2roomMsg(data);
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 外部调用的数据发送方法
|
|
/// </summary>
|
|
/// <param name="cannel"></param>
|
|
/// <param name="data"></param>
|
|
public void SendData(string cannel, string data)
|
|
{
|
|
willSendDatas.Enqueue(new MQTTSendData(cannel, GlobalFlag.currentUser.user_id + "$" + data));
|
|
}
|
|
/// <summary>
|
|
/// 发送数据
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
private void SendData(MQTTSendData data)
|
|
{
|
|
try
|
|
{
|
|
OperateResult send = mqtt_client.PublishMessage(new MqttApplicationMessage()
|
|
{
|
|
QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce,
|
|
Topic = data.cannal,
|
|
Payload = Encoding.UTF8.GetBytes(data.data),
|
|
Retain = false
|
|
});
|
|
if (send.IsSuccess)
|
|
{
|
|
Debug.Log($"<color=red>{$"发布成功{data.data}"}</color>");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogException(ex);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
public class MQTTSendData
|
|
{
|
|
public string cannal;
|
|
public string data;
|
|
public MQTTSendData(string cannal, string data)
|
|
{
|
|
this.cannal = cannal;
|
|
this.data = data;
|
|
}
|
|
} |