| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- import QtQuick 2.8
- import QtQuick.Window 2.2
- import QtQuick.Controls 2.1
- import QtQuick.Layouts 1.1
- import MqttClient 1.0
- Rectangle {
- visible: true
- width: 1280
- height: 800
- id: page_mqttclient
- color: "#498ff8"
- radius: 8
- property var tempSubscription: 0
- MqttClient {
- id: client_mqtt
- hostname: hostnameField.text
- port: portField.text
- }
- ListModel {
- id: messageModel
- }
- function addMessage(payload)
- {
- messageModel.insert(0, {"payload" : payload})
- if (messageModel.count >= 100)
- messageModel.remove(99)
- }
- GridLayout {
- anchors.fill: parent
- anchors.margins: 10
- columns: 2
- Label {
- text: "Hostname:"
- enabled: client_mqtt.state === MqttClient.Disconnected
- }
- TextField {
- id: hostnameField
- Layout.fillWidth: true
- text: "120.27.222.26"
- placeholderText: "<Enter host running MQTT broker>"
- enabled: client_mqtt.state === MqttClient.Disconnected
- }
- Label {
- text: "Port:"
- enabled: client_mqtt.state === MqttClient.Disconnected
- }
- TextField {
- id: portField
- Layout.fillWidth: true
- text: "1883"
- placeholderText: "<Port>"
- inputMethodHints: Qt.ImhDigitsOnly
- enabled: client_mqtt.state === MqttClient.Disconnected
- }
- Button {
- id: connectButton
- Layout.columnSpan: 2
- Layout.fillWidth: true
- text: client_mqtt.state === MqttClient.Connected ? "Disconnect" : "Connect"
- onClicked: {
- if (client_mqtt.state === MqttClient.Connected) {
- client_mqtt.disconnectFromHost()
- messageModel.clear()
- tempSubscription.destroy()
- tempSubscription = 0
- } else
- client_mqtt.connectToHost()
- }
- }
- RowLayout {
- enabled: client_mqtt.state === MqttClient.Connected
- Layout.columnSpan: 2
- Layout.fillWidth: true
- Label {
- text: "Topic:"
- }
- TextField {
- id: subField
- placeholderText: "<Subscription topic>"
- Layout.fillWidth: true
- enabled: tempSubscription === 0
- }
- Button {
- id: subButton
- text: "Subscribe"
- visible: tempSubscription === 0
- onClicked: {
- if (subField.text.length === 0) {
- console.log("No topic specified to subscribe to.")
- return
- }
- tempSubscription = client_mqtt.subscribe(subField.text)
- tempSubscription.messageReceived.connect(addMessage)
- }
- }
- }
- ListView {
- id: messageView
- model: messageModel
- height: 300
- width: 200
- Layout.columnSpan: 2
- Layout.fillHeight: true
- Layout.fillWidth: true
- clip: true
- delegate: Rectangle {
- width: messageView.width
- height: 30
- color: index % 2 ? "#DDDDDD" : "#888888"
- radius: 5
- Text {
- text: payload
- anchors.centerIn: parent
- }
- }
- }
- Label {
- function stateToString(value) {
- if (value === 0)
- return "Disconnected"
- else if (value === 1)
- return "Connecting"
- else if (value === 2)
- return "Connected"
- else
- return "Unknown"
- }
- Layout.columnSpan: 2
- Layout.fillWidth: true
- color: "#333333"
- text: "Status:" + stateToString(client_mqtt.state) + "(" + client_mqtt.state + ")"
- enabled: client_mqtt.state === MqttClient.Connected
- }
- }
- }
|