X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=staging%2Fsoundmanager.git;a=blobdiff_plain;f=sample%2Fradio_qml%2Fapp%2Fapi%2FBindingSoundManager.qml;fp=sample%2Fradio_qml%2Fapp%2Fapi%2FBindingSoundManager.qml;h=04307625392c8e35930a191c5ad730915caa44f3;hp=0000000000000000000000000000000000000000;hb=2e602801b372b0b68111316b89f567213e3ea378;hpb=f743c3428f41b8d127e538ea8a6505a0c882cdb2 diff --git a/sample/radio_qml/app/api/BindingSoundManager.qml b/sample/radio_qml/app/api/BindingSoundManager.qml new file mode 100644 index 0000000..0430762 --- /dev/null +++ b/sample/radio_qml/app/api/BindingSoundManager.qml @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import QtQuick 2.6 +import QtWebSockets 1.0 + +WebSocket { + id: root + active: true + url: bindingAddressSM + property int sourceID + property int connectionID + + property string apiString: "soundmanager" + property var verbs: [] + property string payloadLength: "9999" + + signal connected() + signal disconnected() + signal paused() + + readonly property var msgid: { + "call": 2, + "retok": 3, + "reterr": 4, + "event": 5 + } + + onTextMessageReceived: { + var json = JSON.parse(message); + console.log("Raw response: " + message) + var request = json[2].request + var response = json[2].response + + switch (json[0]) { + case msgid.call: + break + case msgid.retok: + console.log("response:" + response) + var verb = response.verb + var err = response.error + switch(verb){ + case "connect": + console.log("radio: replied by connect") + if(err == 0){ + connectionID = response.mainConnectionID + console.log("radio: mainConnectionID is " + connectionID) + } + break; + case "registerSource": + console.log("radio: replied by registerSource") + if(err == 0){ + sourceID = response.sourceID + } + default: + break; + } + break + case msgid.event: + var content = JSON.parse(JSON.stringify(json[2])); + var eventName = content.event + switch(eventName){ + case "soundmanager\/asyncSetSourceState": + console.log("radio: soundmanager\/asyncSetSourceState") + console.log("radio: my soundID:" + sourceID + "handle:" + content.data.handle + ",sourceID:" + content.data.sourceID + ",sourceState:" + content.data.sourceState) + if(sourceID == content.data.sourceID){ + console.log("radio: call ackSetSourceState") + sendSocketMessage("ackSetSourceState", {handle:content.data.handle, error:0}) + switch(content.data.sourceState){ + case "on": + connected() + break; + case "off": + disconnected() + break; + case "paused": + paused() + break; + } + } + break; + case "soundmanager\/asyncConnect": + // In reality, device shall be opened in this timing + if(connectionID == content.data.connectionID){ + //radio.open_device() + } + break; + case "soundmanager\/asyncDisconnect": + // In reality, device shall be closed in this timing + if(connectionID == content.data.connectionID){ + // radio.close_device() + } + break; + default: + break; + } + break + case msgid.reterr: + console.debug("Bad return value, binding probably not installed") + break + } + } + + onStatusChanged: { + switch (status) { + case WebSocket.Open: + // Initialize band values now that we're connected to the + // binding + sendSocketMessage("subscribe", { event: "asyncSetSourceState" }) + sendSocketMessage("subscribe", { event: "asyncConnect" }) + sendSocketMessage("subscribe", { event: "asyncDisconnect" }) + sendSocketMessage("registerSource", { appname: "radio" }) + break + case WebSocket.Error: + console.debug("WebSocket error: " + root.errorString) + break + } + } + + function sendSocketMessage(verb, parameter) { + var requestJson = [ msgid.call, payloadLength, apiString + '/' + + verb, parameter ] + console.debug("sendSocketMessage: " + JSON.stringify(requestJson)) + sendTextMessage(JSON.stringify(requestJson)) + } + + function connect() { + sendSocketMessage("connect", {sourceID:sourceID,sinkID:1}) + } + + function disconnect() { + sendSocketMessage("disconnect", {mainConnectionID:connectionID}) + } +}