2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 import QtWebSockets 1.0
23 property int connectionID
24 property int sourceID_base // this demo uses only interrupt mode
25 property int sourceID_calling
27 property string apiString: "soundmanager"
28 property string payloadLength: "9999"
34 readonly property var msgid: {
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
51 var verb = response.verb
52 var err = response.error
55 console.log("phone: replied by connect")
57 connectionID = response.mainConnectionID
58 console.log("phone: mainConnectionID is " + connectionID)
61 case "registerSource":
62 console.log("phone: replied by registerSource")
64 sourceID_calling = response.sourceID
71 var content = JSON.parse(JSON.stringify(json[2]));
72 var eventName = content.event
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){
91 case "soundmanager\/asyncConnect":
92 // In reality, device shall be opened in this timing
93 if(connectionID == content.data.connectionID){
96 case "soundmanager\/asyncDisconnect":
97 // In reality, device shall be closed in this timing
98 if(connectionID == content.data.connectionID){
107 console.debug("Bad return value, binding probably not installed")
115 // Initialize band values now that we're connected to the
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")
123 case WebSocket.Error:
124 console.debug("WebSocket error: " + root.errorString)
129 function sendSocketMessage(verb, parameter) {
130 var requestJson = [ msgid.call, payloadLength, apiString + '/'
132 console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
133 sendTextMessage(JSON.stringify(requestJson))
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
141 //sendSocketMessage("connect", {sourceID:sourceID_base,sinkID:"DRIVER-PASSENGER-ZONE"})
142 sendSocketMessage("connect", {sourceID:sourceID_base,sinkID:1})
146 function disconnect() {
147 sendSocketMessage("disconnect", {mainConnectionID:connectionID})
150 function registerSource(app, sourceClass) {
151 //sendSocketMessage("registerSource", {appname :app, sourceClassID:sourceClass})
152 sendSocketMessage("registerSource", {appname :app})