Created
April 11, 2018 03:04
-
-
Save stephenquan/a5ea53fb97005a3c92d962c77339f75f to your computer and use it in GitHub Desktop.
appstudio-datasource-senddata-sample.qml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick 2.7 | |
import QtQuick.Layouts 1.3 | |
import QtQuick.Controls 2.1 | |
import ArcGIS.AppFramework.Devices 1.0 | |
Item { | |
property DeviceListModel deviceListModel: discoveryAgent.devices | |
property Device currentDevice | |
ColumnLayout { | |
anchors.fill: parent | |
anchors.margins: 10 | |
ComboBox { | |
id: comboBox | |
Layout.fillWidth: true | |
model: deviceListModel | |
textRole: "name" | |
onCurrentTextChanged: selectDevice(deviceListModel.get(currentIndex)) | |
} | |
ListView { | |
Layout.fillWidth: true | |
Layout.fillHeight: true | |
model: messagesListModel | |
delegate: Text { text: msg } | |
} | |
RowLayout { | |
Layout.fillWidth: true | |
visible: currentDevice !== null | |
TextField { | |
id: sendTextField | |
Layout.fillWidth: true | |
onAccepted: send() | |
} | |
ComboBox { | |
id: eolComboBox | |
model: [ | |
{ name: "CRLF", value : "\r\n" }, | |
{ name: "LF", value: "\n" }, | |
{ name: "CR", value: "\r" }, | |
] | |
textRole: "name" | |
} | |
} | |
} | |
DeviceDiscoveryAgent { | |
id: discoveryAgent | |
} | |
DataSource { | |
id: dataSource | |
onReceivedDataChanged: log(receivedData.trim()) | |
} | |
ListModel { | |
id: messagesListModel | |
} | |
Connections { | |
target: deviceListModel | |
onCountChanged: if (comboBox.currentIndex == -1 && deviceListModel.count > 0) { comboBox.currentIndex = 0; } | |
} | |
Component.onCompleted: discoveryAgent.start() | |
function selectDevice(device) { | |
if (currentDevice) currentDevice.connected = false; | |
currentDevice = device; | |
messagesListModel.clear(); | |
JSON.stringify(device, undefined, 2).split("\n").forEach(function (txt) { log(txt, 'green'); } ); | |
dataSource.source = currentDevice; | |
currentDevice.connected = true; | |
log("currentDevice.connected: %1".arg(currentDevice.connected)); | |
} | |
function log(txt, color) { | |
console.log(txt); | |
messagesListModel.append( { msg: txt } ); | |
if (messagesListModel.count > 20) { messagesListModel.remove(0); } | |
} | |
function send() { | |
var eol = eolComboBox.model[eolComboBox.currentIndex].value; | |
var text = sendTextField.text + eol; | |
console.log("send: ", JSON.stringify(text)); | |
//currentDevice.writeData = text; | |
dataSource.sendData = text; | |
sendTextField.text = ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment