diff --git a/Assets/StreamingAssets.meta b/Assets/StreamingAssets.meta new file mode 100644 index 00000000..1b72cd6d --- /dev/null +++ b/Assets/StreamingAssets.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: af819185787d8dd48b7dc699e7d4403a +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/StreamingAssets/MQTTConfig.json b/Assets/StreamingAssets/MQTTConfig.json new file mode 100644 index 00000000..3cf453e9 --- /dev/null +++ b/Assets/StreamingAssets/MQTTConfig.json @@ -0,0 +1,9 @@ +{ + "configs": [ + { + "name": "main", + "host": "220.90.135.42", + "port": 3018 + } + ] +} \ No newline at end of file diff --git a/Assets/StreamingAssets/MQTTConfig.json.meta b/Assets/StreamingAssets/MQTTConfig.json.meta new file mode 100644 index 00000000..4683ddc7 --- /dev/null +++ b/Assets/StreamingAssets/MQTTConfig.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 2c9fb26d616e68a4a97e4668f7190990 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/WorkSpace.meta b/Assets/WorkSpace.meta new file mode 100644 index 00000000..8fd03491 --- /dev/null +++ b/Assets/WorkSpace.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 69728f35d447ee4419c4a601d59a38ea +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/WorkSpace/LH.meta b/Assets/WorkSpace/LH.meta new file mode 100644 index 00000000..7d5bbf00 --- /dev/null +++ b/Assets/WorkSpace/LH.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 853be44bcfbea934c8ef1209baced936 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/WorkSpace/LH/MQTTConfig.cs b/Assets/WorkSpace/LH/MQTTConfig.cs new file mode 100644 index 00000000..35ed63b6 --- /dev/null +++ b/Assets/WorkSpace/LH/MQTTConfig.cs @@ -0,0 +1,17 @@ +using UnityEngine; +using System.Collections.Generic; + +namespace Octopus.Simulator.Networks +{ + public class MQTTConfigList + { + public List configs=new List(); + } + + public class MQTTConfig + { + public string name; + public string host; + public int port; + } +} \ No newline at end of file diff --git a/Assets/WorkSpace/LH/MQTTConfig.cs.meta b/Assets/WorkSpace/LH/MQTTConfig.cs.meta new file mode 100644 index 00000000..0fe804c5 --- /dev/null +++ b/Assets/WorkSpace/LH/MQTTConfig.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: c3704dcef3ce96e41b03657cf842f32e \ No newline at end of file diff --git a/Assets/WorkSpace/LH/MQTTManager.cs b/Assets/WorkSpace/LH/MQTTManager.cs new file mode 100644 index 00000000..ec47a3d8 --- /dev/null +++ b/Assets/WorkSpace/LH/MQTTManager.cs @@ -0,0 +1,124 @@ +using UnityEngine; +using Best.MQTT; +using Best.MQTT.Packets; +using Best.MQTT.Packets.Builders; +using Newtonsoft.Json; +using System.IO; +using System; +using System.Collections.Generic; +using System.Text; +using System.Collections; + +namespace Octopus.Simulator.Networks +{ + public class MQTTManager : MonoBehaviour + { + public static MQTTManager mqttManager; + + public Dictionary clientTable = new Dictionary(); + + [SerializeField] + string MQTTpath = ""; + MQTTClient client; + + public event Action onConnectedEvent; + public event Action onDisconnectedEvent; + public event Action onMessageReceived; + public event Action onErrorEvent; + public event Action onCompletedTimerEvent; + // Start is called once before the first execution of Update after the MonoBehaviour is created + void Awake() + { + mqttManager = this; + SetMqttConfig(); + } + + private void SetMqttConfig() + { + string path = Application.streamingAssetsPath + MQTTpath; + string json; + using (StreamReader reader = new StreamReader(path)) + { + json = reader.ReadToEnd(); + } + MQTTConfigList ConfigList = JsonConvert.DeserializeObject(json); + foreach (var config in ConfigList.configs) + { + string clientName = config.name; + string host = config.host; + int port = config.port; + + Connect(clientName, host,port); + } + } + + public void SubscriptionTopic(MQTTClient client, string topic) + { + client.AddTopicAlias(topic); + + client.CreateSubscriptionBuilder(topic) + .WithMessageCallback(OnMessage) + .WithMaximumQoS(QoSLevels.ExactlyOnceDelivery) + .BeginSubscribe(); + + } + + public void Connect(string clientName, string host,int port) + { + client = new MQTTClientBuilder() + .WithOptions(new ConnectionOptionsBuilder().WithTCP(host, port)) + .WithEventHandler(client=>OnConnected(client,clientName)) + .WithEventHandler(OnStateChange) + .WithEventHandler(OnDisconnected) + .WithEventHandler(OnError) + .CreateClient(); + + client.BeginConnect(ConnectPacketBuilderCallback); + } + + public event Action onConnected; + private void OnConnected(MQTTClient client,string clientName) + { + Debug.Log("connect"); + clientTable.Add(clientName, client); + onConnected?.Invoke(client); + SubscriptionTopic(client, "simulation/435faabb-6183-4690-a81f-027dd96d5827/#"); + } + + private ConnectPacketBuilder ConnectPacketBuilderCallback(MQTTClient client, ConnectPacketBuilder builder) + { + return builder; + } + + private void OnError(MQTTClient client, string error) + { + Debug.Log($"OnError! :{error}"); + } + + private void OnMessage(MQTTClient client, SubscriptionTopic topic, string topicName, ApplicationMessage message) + { + var payload = Encoding.UTF8.GetString(message.Payload.Data, message.Payload.Offset, message.Payload.Count); + Debug.Log(payload); + onMessageReceived?.Invoke(topicName, payload); + } + + private void OnDisconnected(MQTTClient client, DisconnectReasonCodes reasonCode, string reasonMessage) + { + Debug.Log(reasonMessage); + //throw new NotImplementedException(); + } + + private void OnStateChange(MQTTClient client, ClientStates oldState, ClientStates newState) + { + //throw new NotImplementedException(); + } + + void OnDestroy() + { + client?.CreateDisconnectPacketBuilder() + .WithReasonCode(DisconnectReasonCodes.NormalDisconnection) + //.WithReasonString("Bye") + .BeginDisconnect(); + } + } +} diff --git a/Assets/WorkSpace/LH/MQTTManager.cs.meta b/Assets/WorkSpace/LH/MQTTManager.cs.meta new file mode 100644 index 00000000..e4fa9b85 --- /dev/null +++ b/Assets/WorkSpace/LH/MQTTManager.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 6c81b4b1ce2fb4345950d24862ca380c \ No newline at end of file