using HslCommunication; using HslCommunication.MQTT; using System; using System.Collections.Generic; using System.Text; using UnityEngine; using static InterfaceManager; public class MQTTManager : MonoBehaviour { public static MQTTManager instance; /// /// MQTT /// MqttClient mqtt_client; /// /// 接收到的消息队列 /// private Queue reciveDatas = new Queue(); /// /// 待发送消息队列 /// private Queue willSendDatas = new Queue(); #region 订阅主题 //房间/标题 /// /// 无人机位置信息传递 /// public string DronePosition; /// /// 无人机攻击目标锁定 /// public string SingleDronePosition; /// /// 驱离无人机 /// public string WRJExpel; /// /// 单个无人机被销毁 /// public string DroneWasDestroyed; /// /// 无线电干扰无人机同步 /// public string WRJDitch; /// /// 无人机被销毁 /// public string Planedata; /// /// 激光发射位置传递 /// public string Lasing; /// /// 微波武器发射位置 /// public string Micow; /// /// 设备被销毁 /// public string SetToBeDestroyed; /// /// 设备被收回 /// public string SetToBeDestroyedTwo; /// /// 设备频段设置 /// public string BandSetting; /// /// 设备探测频段设置 /// public string SweepFrequencyBand; /// /// 重要用户被攻击 /// public string KeyTarget; /// /// 标识符 /// 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, "数据"); //} } /// /// 主连接线程 /// private void StartContent() { try { Debug.Log("开始链接MQTT服务"); // 授权示例 调用一次即可 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); } } /// /// 接收消息 /// /// /// /// private void OnMQTTMessageReceived(MqttClient client, string topic, byte[] payload) { var msg = Encoding.UTF8.GetString(payload); reciveDatas.Enqueue(msg); } /// /// 处理接收到的消息 /// /// void WorkWhthData(string data) { Debug.Log($"{$"处理数据{data}"}"); string[] datas = data.Split('$'); //if (datas[0] != GlobalFlag.currentUser.user_id) //DeviceManager.Instance.GetSend2roomMsg(datas[1]); } /// /// 外部调用的数据发送方法 /// /// /// public void SendData(string cannel, string data) { willSendDatas.Enqueue(new MQTTSendData(cannel, GlobalFlag.currentUser.user_id + "$" + data)); } /// /// 发送数据 /// /// 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($"{$"发布成功{data.data}"}"); } } 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; } }