MQTTTest.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. }
  26. void Update()
  27. {
  28. if(Input.GetKeyDown(KeyCode.A))
  29. {
  30. 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 });
  31. }
  32. else if(Input.GetKeyDown(KeyCode.S))
  33. {
  34. mQTTService.UnsubscribeTopics(topicSub);
  35. }
  36. if(Input.GetKeyDown(KeyCode.D))
  37. {
  38. mQTTService.Connect();
  39. }
  40. if(Input.GetKeyDown(KeyCode.Q))
  41. {
  42. this.SendCommand(new MQTTCommand(topics[0],"KeyCode.Q"));
  43. }
  44. else if(Input.GetKeyDown(KeyCode.W))
  45. {
  46. this.SendCommand(new MQTTCommand(topics[1],"KeyCode.W"));
  47. }
  48. else if(Input.GetKeyDown(KeyCode.E))
  49. {
  50. this.SendCommand(new MQTTCommand(topics[2],"KeyCode.E"));
  51. }
  52. else if(Input.GetKeyDown(KeyCode.R))
  53. {
  54. this.SendCommand(new MQTTCommand(topics[3],"KeyCode.R"));
  55. }
  56. }
  57. }