Add sound manager initial source code
[staging/soundmanager.git] / sample / radio_qml / app / api / BindingSoundManager.qml
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 import QtQuick 2.6
17 import QtWebSockets 1.0
18
19 WebSocket {
20     id: root 
21     active: true
22     url: bindingAddressSM
23     property int  sourceID
24     property int  connectionID
25
26     property string apiString: "soundmanager"
27     property var verbs: []
28     property string payloadLength: "9999"
29
30     signal connected()
31     signal disconnected()
32     signal paused()
33
34     readonly property var msgid: {
35          "call": 2,
36          "retok": 3,
37          "reterr": 4,
38          "event": 5
39     }
40
41     onTextMessageReceived: {
42         var json = JSON.parse(message);
43         console.log("Raw response: " + message)
44         var request = json[2].request
45         var response = json[2].response
46
47         switch (json[0]) {
48         case msgid.call:
49             break
50         case msgid.retok:
51             console.log("response:" + response)
52             var verb = response.verb
53             var err  = response.error
54             switch(verb){
55                 case "connect":
56                     console.log("radio: replied by connect")
57                     if(err == 0){
58                         connectionID = response.mainConnectionID
59                         console.log("radio: mainConnectionID is " + connectionID)
60                     }
61                     break;
62                 case "registerSource":
63                     console.log("radio: replied by registerSource")            
64                     if(err == 0){
65                         sourceID = response.sourceID
66                     }
67                 default:
68                     break;
69             }
70             break
71         case msgid.event:
72             var content = JSON.parse(JSON.stringify(json[2]));
73             var eventName = content.event
74             switch(eventName){
75                 case "soundmanager\/asyncSetSourceState":
76                     console.log("radio: soundmanager\/asyncSetSourceState")
77                     console.log("radio: my soundID:" + sourceID + "handle:" + content.data.handle + ",sourceID:" + content.data.sourceID + ",sourceState:" + content.data.sourceState)
78                     if(sourceID == content.data.sourceID){
79                         console.log("radio: call ackSetSourceState")
80                         sendSocketMessage("ackSetSourceState", {handle:content.data.handle, error:0})
81                         switch(content.data.sourceState){
82                             case "on":
83                                 connected()
84                                 break;
85                             case "off":
86                                 disconnected()
87                                 break;
88                             case "paused":
89                                 paused()
90                                 break;
91                         }
92                     }
93                     break;
94                 case "soundmanager\/asyncConnect":
95                     // In reality, device shall be opened in this timing
96                     if(connectionID == content.data.connectionID){
97                         //radio.open_device()
98                     }
99                     break;
100                 case "soundmanager\/asyncDisconnect":
101                     // In reality, device shall be closed in this timing
102                     if(connectionID == content.data.connectionID){
103                         // radio.close_device()
104                     }
105                     break;
106                 default:
107                     break;
108             }
109             break
110         case msgid.reterr:
111             console.debug("Bad return value, binding probably not installed")
112             break
113         }
114     }
115
116     onStatusChanged: {
117         switch (status) {
118         case WebSocket.Open:
119             // Initialize band values now that we're connected to the
120             // binding
121             sendSocketMessage("subscribe", { event: "asyncSetSourceState" })
122             sendSocketMessage("subscribe", { event: "asyncConnect" })
123             sendSocketMessage("subscribe", { event: "asyncDisconnect" })
124             sendSocketMessage("registerSource", { appname: "radio" })            
125             break
126         case WebSocket.Error:
127             console.debug("WebSocket error: " + root.errorString)
128             break
129         }
130     }
131
132     function sendSocketMessage(verb, parameter) {
133         var requestJson = [ msgid.call, payloadLength, apiString + '/'
134                            + verb, parameter ]
135         console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
136         sendTextMessage(JSON.stringify(requestJson))
137     }
138
139         function connect() {
140                 sendSocketMessage("connect", {sourceID:sourceID,sinkID:1})      
141         }
142
143         function disconnect() {
144                 sendSocketMessage("disconnect", {mainConnectionID:connectionID})
145         }
146 }