NooLiteClient.qml 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import QtQuick 2.0
  2. import ru.ded.noolight 1.0
  3. QtObject {
  4. id: root
  5. property string serviceUrl: undefined
  6. signal modelLoad(var data)
  7. signal error(string text)
  8. function _get(url, callback) {
  9. var request = new XMLHttpRequest()
  10. request.open('GET', url, true, Settings.login, Settings.password)
  11. request.withCredentials = true
  12. request.onreadystatechange = function () {
  13. if (request.readyState !== XMLHttpRequest.DONE) {
  14. return
  15. }
  16. if (request.status === 200) {
  17. if (callback !== undefined) {
  18. callback(JSON.parse(request.responseText))
  19. }
  20. return
  21. }
  22. root.error(qsTr("[%1] Request error: %2").
  23. arg(request.status).
  24. arg(request.statusText))
  25. }
  26. request.send()
  27. }
  28. function loadModel() {
  29. _get(root.serviceUrl + '/static/channels.js', root.modelLoad)
  30. }
  31. function sendCommand(command, channelId) {
  32. _get(root.serviceUrl + '/noolite/%1/%2'.arg(command).arg(channelId), function (data) {
  33. if (data.error) {
  34. root.error(qsTr("Server error: %1").arg(data.error))
  35. return
  36. }
  37. console.log(data.command, data.channel)
  38. })
  39. }
  40. }