MQTTTest.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Collections.Generic;
  2. using Blue;
  3. using UnityEngine;
  4. using uPLibrary.Networking.M2Mqtt.Messages;
  5. public class MQTTTest : AbstractController
  6. {
  7. private List<string> topics = new List<string>()
  8. {
  9. "client/manage",
  10. "client/mqttx_d4c106a3",
  11. "client/mqttx_b4c02ddc",
  12. "client/mqttx_ab869d21"
  13. };
  14. private string[] topicSub = new string[4]
  15. {
  16. "client/manage",
  17. "client/mqttx_d4c106a3",
  18. "client/mqttx_b4c02ddc",
  19. "client/mqttx_ab869d21"
  20. };
  21. private IMQTTService mQTTService;
  22. private void Start()
  23. {
  24. mQTTService = this.GetService<IMQTTService>();
  25. mQTTService.brokerAddress = "api-fat1.ghz-tech.com";
  26. mQTTService.brokerPort = 1883;
  27. mQTTService.mqttUserName = "u@unity3";
  28. }
  29. void Update()
  30. {
  31. mQTTService.Update();
  32. if(Input.GetKeyDown(KeyCode.A))
  33. {
  34. mQTTService.SubscribeTopics(topicSub, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE , MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE , MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
  35. }
  36. else if(Input.GetKeyDown(KeyCode.S))
  37. {
  38. mQTTService.UnsubscribeTopics(topicSub);
  39. }
  40. if(Input.GetKeyDown(KeyCode.D))
  41. {
  42. mQTTService.Connect();
  43. }
  44. if(Input.GetKeyDown(KeyCode.Q))
  45. {
  46. this.SendCommand(new MQTTCommand(topics[0],"KeyCode.Q"));
  47. }
  48. else if(Input.GetKeyDown(KeyCode.W))
  49. {
  50. this.SendCommand(new MQTTCommand(topics[1],"KeyCode.W"));
  51. }
  52. else if(Input.GetKeyDown(KeyCode.E))
  53. {
  54. this.SendCommand(new MQTTCommand(topics[2],"KeyCode.E"));
  55. }
  56. else if(Input.GetKeyDown(KeyCode.R))
  57. {
  58. this.SendCommand(new MQTTCommand(topics[3],"KeyCode.R"));
  59. }
  60. }
  61. private void OnApplicationQuit()
  62. {
  63. mQTTService.OnApplicationQuit();
  64. }
  65. }