binding: add initial media binding
[apps/mediaplayer.git] / app / api / LightMediaScanner.qml
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2017 Konsulko Group
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 import QtQuick 2.6
19 import QtWebSockets 1.0
20
21 WebSocket {
22     id: root
23     active: true
24     url: bindingAddress
25
26     property string statusString: "waiting..."
27     property string apiString: "media-manager"
28     property var verbs: []
29     property string payloadLength: "9999"
30
31     readonly property var msgid: {
32         "call": 2,
33         "retok": 3,
34         "reterr": 4,
35         "event": 5
36     }
37
38     onTextMessageReceived: {
39         var json = JSON.parse(message)
40         console.debug("Raw response: " + message)
41         var request = json[2].request
42         var response = json[2].response
43         console.debug("response: " + JSON.stringify(response))
44         switch (json[0]) {
45             case msgid.call:
46             break
47             case msgid.retok:
48             root.statusString = request.status
49             var verb = verbs.shift()
50             if (verb == "media_added") {
51                 console.debug("Media is inserted")
52             } else if (verb == "media_removed") {
53                 console.debug("Media is removed")
54             }
55             break
56             case msgid.reterr:
57             root.statusString = "Bad return value, binding probably not installed"
58             break
59             case msgid.event:
60             break
61         }
62     }
63
64     onStatusChanged: {
65         switch (status) {
66             case WebSocket.Open:
67             console.debug("onStatusChanged: Open")
68             break
69             case WebSocket.Error:
70             root.statusString = "WebSocket error: " + root.errorString
71             break
72         }
73     }
74
75     function sendSocketMessage(verb, parameter) {
76         var requestJson = [ msgid.call, payloadLength, apiString + '/'
77         + verb, parameter ]
78         console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
79         verbs.push(verb)
80         sendTextMessage(JSON.stringify(requestJson))
81     }
82 }