Add sample application[phone]
[staging/soundmanager.git] / sample / phone / 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 connectionID
24     property int sourceID_base // this demo uses only interrupt mode
25     property int sourceID_calling
26
27     property string apiString: "soundmanager"
28     property string payloadLength: "9999"
29
30     signal approved()
31     signal denied()
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             var verb = response.verb
52             var err  = response.error
53             switch(verb){
54                 case "connect":
55                     console.log("phone: replied by connect")
56                     if(err == 0){
57                         connectionID = response.mainConnectionID
58                         console.log("phone: mainConnectionID is " + connectionID)
59                     }
60                     break;
61                 case "registerSource":
62                     console.log("phone: replied by registerSource")            
63                     if(err == 0){
64                         sourceID_calling = response.sourceID
65                     }
66                 default:
67                     break;
68             }
69             break
70         case msgid.event:
71             var content = JSON.parse(JSON.stringify(json[2]));
72             var eventName = content.event
73             switch(eventName){
74                 case "soundmanager\/asyncSetSourceState":
75                     if(sourceID_calling == content.data.sourceID){
76                         console.log("phone: call ackSetSourceState")
77                         sendSocketMessage("ackSetSourceState", {handle:content.data.handle, error:0})
78                         switch(content.data.sourceState){
79                             case "on":
80                                 approved()
81                                 break;
82                             case "off":
83                                 denied()
84                                 break;
85                             case "paused":
86                                 paused()
87                                 break;
88                         }
89                     }
90                     break;
91                 case "soundmanager\/asyncConnect":
92                     // In reality, device shall be opened in this timing
93                     if(connectionID == content.data.connectionID){
94                     }
95                     break;
96                 case "soundmanager\/asyncDisconnect":
97                     // In reality, device shall be closed in this timing
98                     if(connectionID == content.data.connectionID){
99                         connectionID = 0
100                     }
101                     break;
102                 default:
103                     break;
104             }
105             break
106         case msgid.reterr:
107             console.debug("Bad return value, binding probably not installed")
108             break
109         }
110     }
111
112     onStatusChanged: {
113         switch (status) {
114         case WebSocket.Open:
115             // Initialize band values now that we're connected to the
116             // binding
117             sendSocketMessage("subscribe", { event: "asyncSetSourceState" })
118             sendSocketMessage("subscribe", { event: "asyncConnect" })
119             sendSocketMessage("subscribe", { event: "asyncDisconnect" })
120             // registerSource("phone-base","base") // if using base such as push sound
121             registerSource("phone", "interrupt")
122             break
123         case WebSocket.Error:
124             console.debug("WebSocket error: " + root.errorString)
125             break
126         }
127     }
128
129     function sendSocketMessage(verb, parameter) {
130         var requestJson = [ msgid.call, payloadLength, apiString + '/'
131                            + verb, parameter ]
132         console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
133         sendTextMessage(JSON.stringify(requestJson))
134     }
135
136         function connect(mode) {
137         if(mode == "calling"){
138                     //sendSocketMessage("connect", {sourceID:sourceID_calling,sinkID:"DRIVER-ZONE"})    // sink ID should be abstracted , but for now, uses fixed number as trial
139                     sendSocketMessage("connect", {sourceID:sourceID_calling,sinkID:1})  // sink ID should be abstracted , but for now, uses fixed number as trial
140         } else{
141                     //sendSocketMessage("connect", {sourceID:sourceID_base,sinkID:"DRIVER-PASSENGER-ZONE"})            
142                     sendSocketMessage("connect", {sourceID:sourceID_base,sinkID:1})
143         }
144         }
145
146         function disconnect() {
147                 sendSocketMessage("disconnect", {mainConnectionID:connectionID})
148         }
149
150     function registerSource(app, sourceClass) {
151                 //sendSocketMessage("registerSource", {appname :app, sourceClassID:sourceClass})
152                 sendSocketMessage("registerSource", {appname :app})
153         }
154 }