Доделал переключение каналов, небольшой рефакторинг

This commit is contained in:
2018-08-12 10:06:17 +02:00
parent 39aeb66fff
commit b6001e4e81
7 changed files with 77 additions and 37 deletions

View File

@@ -26,6 +26,8 @@ Rectangle {
id: ma
anchors.fill: parent
onClicked: root.clicked()
}
gradient: ma.pressed ? pressedGradient : normalGradient

View File

@@ -4,12 +4,16 @@ import QtQuick.Controls 2.0
Page {
title: qsTr("nooLight")
function showError(text) {
ToolTip.show(text, 1000)
}
LightsModel {
id: lightsModel
serviceUrl: settings.serviceUrl
onError: console.log(text)
onError: showError(text)
}
ListView {
@@ -25,6 +29,10 @@ Page {
title: groupName || ""
lights: channels
onChannelClicked: {
lightsModel.switchChannel(channelId)
}
}
}

View File

@@ -1,30 +0,0 @@
import QtQuick 2.0
QtObject {
id: root
signal reply(var data)
signal error(string text)
function get(url) {
var request = new XMLHttpRequest()
request.open('GET', url)
request.onreadystatechange = function () {
if (request.readyState !== XMLHttpRequest.DONE) {
return
}
if (request.status === 200) {
root.reply(JSON.parse(request.responseText))
return
}
root.error(qsTr("[%1] Request error: %2").
arg(request.status).
arg(request.statusText))
}
request.send()
}
}

View File

@@ -7,6 +7,8 @@ Item {
property string title: ""
property QtObject lights: undefined
signal channelClicked(int channelId)
Column {
width: root.width
@@ -71,6 +73,10 @@ Item {
text: name
}
}
onClicked: {
root.channelClicked(id)
}
}
}
}

View File

@@ -3,21 +3,21 @@ import QtQml.Models 2.1
ListModel {
id: root
readonly property var httpClient: HttpClient {
id: httpClient
readonly property var client: NooLiteClient {
id: nooLiteClient
onError: {
root.error(text)
root.isLoading = false
}
onReply: {
onModelLoad: {
root.populateModel(data)
root.isLoading = false
}
}
property string serviceUrl: undefined
property alias serviceUrl: nooLiteClient.serviceUrl
property bool isLoading: false
signal error(string text)
@@ -25,7 +25,7 @@ ListModel {
onServiceUrlChanged: reload()
function reload() {
root.httpClient.get(root.serviceUrl + '/static/channels.js')
root.client.loadModel()
root.isLoading = true
}
@@ -34,4 +34,8 @@ ListModel {
root.append(group)
})
}
function switchChannel(channelId) {
root.client.switchChannel(channelId)
}
}

50
NooLiteClient.qml Normal file
View File

@@ -0,0 +1,50 @@
import QtQuick 2.0
QtObject {
id: root
property string serviceUrl: undefined
signal modelLoad(var data)
signal error(string text)
function _get(url, callback) {
var request = new XMLHttpRequest()
request.open('GET', url)
request.onreadystatechange = function () {
if (request.readyState !== XMLHttpRequest.DONE) {
return
}
if (request.status === 200) {
if (callback !== undefined) {
callback(JSON.parse(request.responseText))
}
return
}
root.error(qsTr("[%1] Request error: %2").
arg(request.status).
arg(request.statusText))
}
request.send()
}
function loadModel() {
_get(root.serviceUrl + '/static/channels.js', root.modelLoad)
}
function switchChannel(channelId) {
_get(root.serviceUrl + '/noolite/switch/%1'.arg(channelId), function (data) {
if (data.error) {
root.error(qsTr("Server error: %1").arg(data.error))
return
}
console.log(data.command, data.channel)
})
}
}

View File

@@ -11,6 +11,6 @@
<file>GradientButton.qml</file>
<file>lamp.png</file>
<file>Off.png</file>
<file>HttpClient.qml</file>
<file>NooLiteClient.qml</file>
</qresource>
</RCC>