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
24 property int connectionID
26 property string apiString: "soundmanager"
27 property var verbs: []
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 console.log("response:" + response)
52 var verb = response.verb
53 var err = response.error
56 console.log("radio: replied by connect")
58 connectionID = response.mainConnectionID
59 console.log("radio: mainConnectionID is " + connectionID)
62 case "registerSource":
63 console.log("radio: replied by registerSource")
65 sourceID = response.sourceID
72 var content = JSON.parse(JSON.stringify(json[2]));
73 var eventName = content.event
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){
94 case "soundmanager\/asyncConnect":
95 // In reality, device shall be opened in this timing
96 if(connectionID == content.data.connectionID){
100 case "soundmanager\/asyncDisconnect":
101 // In reality, device shall be closed in this timing
102 if(connectionID == content.data.connectionID){
103 // radio.close_device()
111 console.debug("Bad return value, binding probably not installed")
119 // Initialize band values now that we're connected to the
121 sendSocketMessage("subscribe", { event: "asyncSetSourceState" })
122 sendSocketMessage("subscribe", { event: "asyncConnect" })
123 sendSocketMessage("subscribe", { event: "asyncDisconnect" })
124 sendSocketMessage("registerSource", { appname: "radio" })
126 case WebSocket.Error:
127 console.debug("WebSocket error: " + root.errorString)
132 function sendSocketMessage(verb, parameter) {
133 var requestJson = [ msgid.call, payloadLength, apiString + '/'
135 console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
136 sendTextMessage(JSON.stringify(requestJson))
140 sendSocketMessage("connect", {sourceID:sourceID,sinkID:1})
143 function disconnect() {
144 sendSocketMessage("disconnect", {mainConnectionID:connectionID})