NooLiteClient.qml 1.2 KB

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