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

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 id: ma
anchors.fill: parent anchors.fill: parent
onClicked: root.clicked()
} }
gradient: ma.pressed ? pressedGradient : normalGradient gradient: ma.pressed ? pressedGradient : normalGradient

View File

@@ -4,12 +4,16 @@ import QtQuick.Controls 2.0
Page { Page {
title: qsTr("nooLight") title: qsTr("nooLight")
function showError(text) {
ToolTip.show(text, 1000)
}
LightsModel { LightsModel {
id: lightsModel id: lightsModel
serviceUrl: settings.serviceUrl serviceUrl: settings.serviceUrl
onError: console.log(text) onError: showError(text)
} }
ListView { ListView {
@@ -25,6 +29,10 @@ Page {
title: groupName || "" title: groupName || ""
lights: channels 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 string title: ""
property QtObject lights: undefined property QtObject lights: undefined
signal channelClicked(int channelId)
Column { Column {
width: root.width width: root.width
@@ -71,6 +73,10 @@ Item {
text: name text: name
} }
} }
onClicked: {
root.channelClicked(id)
}
} }
} }
} }

View File

@@ -3,21 +3,21 @@ import QtQml.Models 2.1
ListModel { ListModel {
id: root id: root
readonly property var httpClient: HttpClient { readonly property var client: NooLiteClient {
id: httpClient id: nooLiteClient
onError: { onError: {
root.error(text) root.error(text)
root.isLoading = false root.isLoading = false
} }
onReply: { onModelLoad: {
root.populateModel(data) root.populateModel(data)
root.isLoading = false root.isLoading = false
} }
} }
property string serviceUrl: undefined property alias serviceUrl: nooLiteClient.serviceUrl
property bool isLoading: false property bool isLoading: false
signal error(string text) signal error(string text)
@@ -25,7 +25,7 @@ ListModel {
onServiceUrlChanged: reload() onServiceUrlChanged: reload()
function reload() { function reload() {
root.httpClient.get(root.serviceUrl + '/static/channels.js') root.client.loadModel()
root.isLoading = true root.isLoading = true
} }
@@ -34,4 +34,8 @@ ListModel {
root.append(group) 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>GradientButton.qml</file>
<file>lamp.png</file> <file>lamp.png</file>
<file>Off.png</file> <file>Off.png</file>
<file>HttpClient.qml</file> <file>NooLiteClient.qml</file>
</qresource> </qresource>
</RCC> </RCC>