mqttclientpage.qml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import QtQuick 2.8
  2. import QtQuick.Window 2.2
  3. import QtQuick.Controls 2.1
  4. import QtQuick.Layouts 1.1
  5. import MqttClient 1.0
  6. Rectangle {
  7. visible: true
  8. width: 1280
  9. height: 800
  10. id: page_mqttclient
  11. color: "#498ff8"
  12. radius: 8
  13. property var tempSubscription: 0
  14. MqttClient {
  15. id: client_mqtt
  16. hostname: hostnameField.text
  17. port: portField.text
  18. }
  19. ListModel {
  20. id: messageModel
  21. }
  22. function addMessage(payload)
  23. {
  24. messageModel.insert(0, {"payload" : payload})
  25. if (messageModel.count >= 100)
  26. messageModel.remove(99)
  27. }
  28. GridLayout {
  29. anchors.fill: parent
  30. anchors.margins: 10
  31. columns: 2
  32. Label {
  33. text: "Hostname:"
  34. enabled: client_mqtt.state === MqttClient.Disconnected
  35. }
  36. TextField {
  37. id: hostnameField
  38. Layout.fillWidth: true
  39. text: "120.27.222.26"
  40. placeholderText: "<Enter host running MQTT broker>"
  41. enabled: client_mqtt.state === MqttClient.Disconnected
  42. }
  43. Label {
  44. text: "Port:"
  45. enabled: client_mqtt.state === MqttClient.Disconnected
  46. }
  47. TextField {
  48. id: portField
  49. Layout.fillWidth: true
  50. text: "1883"
  51. placeholderText: "<Port>"
  52. inputMethodHints: Qt.ImhDigitsOnly
  53. enabled: client_mqtt.state === MqttClient.Disconnected
  54. }
  55. Button {
  56. id: connectButton
  57. Layout.columnSpan: 2
  58. Layout.fillWidth: true
  59. text: client_mqtt.state === MqttClient.Connected ? "Disconnect" : "Connect"
  60. onClicked: {
  61. if (client_mqtt.state === MqttClient.Connected) {
  62. client_mqtt.disconnectFromHost()
  63. messageModel.clear()
  64. tempSubscription.destroy()
  65. tempSubscription = 0
  66. } else
  67. client_mqtt.connectToHost()
  68. }
  69. }
  70. RowLayout {
  71. enabled: client_mqtt.state === MqttClient.Connected
  72. Layout.columnSpan: 2
  73. Layout.fillWidth: true
  74. Label {
  75. text: "Topic:"
  76. }
  77. TextField {
  78. id: subField
  79. placeholderText: "<Subscription topic>"
  80. Layout.fillWidth: true
  81. enabled: tempSubscription === 0
  82. }
  83. Button {
  84. id: subButton
  85. text: "Subscribe"
  86. visible: tempSubscription === 0
  87. onClicked: {
  88. if (subField.text.length === 0) {
  89. console.log("No topic specified to subscribe to.")
  90. return
  91. }
  92. tempSubscription = client_mqtt.subscribe(subField.text)
  93. tempSubscription.messageReceived.connect(addMessage)
  94. }
  95. }
  96. }
  97. ListView {
  98. id: messageView
  99. model: messageModel
  100. height: 300
  101. width: 200
  102. Layout.columnSpan: 2
  103. Layout.fillHeight: true
  104. Layout.fillWidth: true
  105. clip: true
  106. delegate: Rectangle {
  107. width: messageView.width
  108. height: 30
  109. color: index % 2 ? "#DDDDDD" : "#888888"
  110. radius: 5
  111. Text {
  112. text: payload
  113. anchors.centerIn: parent
  114. }
  115. }
  116. }
  117. Label {
  118. function stateToString(value) {
  119. if (value === 0)
  120. return "Disconnected"
  121. else if (value === 1)
  122. return "Connecting"
  123. else if (value === 2)
  124. return "Connected"
  125. else
  126. return "Unknown"
  127. }
  128. Layout.columnSpan: 2
  129. Layout.fillWidth: true
  130. color: "#333333"
  131. text: "Status:" + stateToString(client_mqtt.state) + "(" + client_mqtt.state + ")"
  132. enabled: client_mqtt.state === MqttClient.Connected
  133. }
  134. }
  135. }