103 lines
3.6 KiB
C#
103 lines
3.6 KiB
C#
using Best.MQTT;
|
|
using Best.MQTT.Packets.Builders;
|
|
using System;
|
|
using WI;
|
|
|
|
namespace SHINT.MQTT
|
|
{
|
|
public class MQTTManager : MonoBehaviour, ISingle
|
|
{
|
|
//BrokerSetting setting;
|
|
MQTTClient client;
|
|
public override void AfterAwake()
|
|
{
|
|
//BufferPool.IsDoubleReleaseCheckEnabled = false;
|
|
client = new MQTTClientBuilder()
|
|
.WithOptions(new ConnectionOptionsBuilder().WithWebSocket("sh-int.flexing.ai", 8088).WithTLS().Build())
|
|
.WithEventHandler(OnConnected)
|
|
.WithEventHandler(OnStateChange)
|
|
.WithEventHandler(OnError)
|
|
.CreateClient();
|
|
|
|
client.BeginConnect(ConnectPacketBuilderCallback);
|
|
}
|
|
|
|
public event Action<MQTTClient> onConnected;
|
|
|
|
//string[] topics = new string[]
|
|
//{
|
|
// "injection_production_status/1",
|
|
// "injection_production_status/2",
|
|
// "injection_production_status/3",
|
|
// "injection_production_status/4",
|
|
// "injection_production_status/5",
|
|
// "injection_production_status/6",
|
|
// "injection_production_status/7",
|
|
// "injection_production_status/8",
|
|
// "injection_production_status/9",
|
|
// "injection_production_status/10",
|
|
// "injection_production_status/11",
|
|
// "injection_production_status/12",
|
|
// "assembly_production_status/1",
|
|
// "assembly_production_status/2",
|
|
// "assembly_production_status/3",
|
|
//};
|
|
private void OnConnected(MQTTClient client)
|
|
{
|
|
//foreach(var t in topics)
|
|
//{
|
|
// using (new PacketBufferHelper(client))
|
|
// {
|
|
// //client.AddTopicAlias(f.info.topic);
|
|
// client.CreateSubscriptionBuilder(t)
|
|
// .WithMessageCallback(OnMessage)
|
|
// .WithMaximumQoS(QoSLevels.ExactlyOnceDelivery)
|
|
// .BeginSubscribe();
|
|
// }
|
|
//}
|
|
onConnected?.Invoke(client);
|
|
}
|
|
|
|
//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);
|
|
// var data = JsonConvert.DeserializeObject<List<TopicContainer>>(payload);
|
|
//}
|
|
|
|
void OnDestroy()
|
|
{
|
|
client?.CreateDisconnectPacketBuilder()
|
|
.WithReasonCode(DisconnectReasonCodes.NormalDisconnection)
|
|
//.WithReasonString("Bye")
|
|
.BeginDisconnect();
|
|
}
|
|
private ConnectPacketBuilder ConnectPacketBuilderCallback(MQTTClient client, ConnectPacketBuilder builder)
|
|
{
|
|
return builder;
|
|
}
|
|
|
|
private void OnError(MQTTClient client, string error)
|
|
{
|
|
//Debug.Log($"OnError! :{error}");
|
|
//throw new NotImplementedException();
|
|
}
|
|
|
|
//private void OnDisconnected(MQTTClient client, DisconnectReasonCodes reasonCode, string reasonMessage)
|
|
//{
|
|
// //throw new NotImplementedException();
|
|
//}
|
|
|
|
private void OnStateChange(MQTTClient client, ClientStates oldState, ClientStates newState)
|
|
{
|
|
//Debug.Log($"OnStateChange! :{oldState} -> {newState}");
|
|
//throw new NotImplementedException();
|
|
}
|
|
}
|
|
[Serializable]
|
|
public class TopicContainer
|
|
{
|
|
public string tagId;
|
|
public string name;
|
|
public string value;
|
|
}
|
|
} |