|
| 1 | +/* |
| 2 | +* Copyright (c) 2019, Livio, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* Redistribution and use in source and binary forms, with or without |
| 6 | +* modification, are permitted provided that the following conditions are met: |
| 7 | +* |
| 8 | +* Redistributions of source code must retain the above copyright notice, this |
| 9 | +* list of conditions and the following disclaimer. |
| 10 | +* |
| 11 | +* Redistributions in binary form must reproduce the above copyright notice, |
| 12 | +* this list of conditions and the following |
| 13 | +* disclaimer in the documentation and/or other materials provided with the |
| 14 | +* distribution. |
| 15 | +* |
| 16 | +* Neither the name of the Livio Inc. nor the names of its contributors |
| 17 | +* may be used to endorse or promote products derived from this software |
| 18 | +* without specific prior written permission. |
| 19 | +* |
| 20 | +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 24 | +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | +* POSSIBILITY OF SUCH DAMAGE. |
| 31 | +*/ |
| 32 | + |
| 33 | +const fs = require('fs'); |
| 34 | +const SDL = require('../../../lib/node/dist/index.js'); |
| 35 | +const CONFIG = require('./config.js'); |
| 36 | + |
| 37 | +class AppClient { |
| 38 | + constructor (wsClient) { |
| 39 | + this._appConfig = new SDL.manager.AppConfig() |
| 40 | + .setAppId(CONFIG.appId) |
| 41 | + .setAppName(CONFIG.appName) |
| 42 | + .setIsMediaApp(false) |
| 43 | + .setLanguageDesired(SDL.rpc.enums.Language.EN_US) |
| 44 | + .setHmiDisplayLanguageDesired(SDL.rpc.enums.Language.EN_US) |
| 45 | + .setAppTypes([ |
| 46 | + SDL.rpc.enums.AppHMIType.DEFAULT, |
| 47 | + ]) |
| 48 | + .setTransportConfig( |
| 49 | + new SDL.transport.WebSocketServerConfig( |
| 50 | + wsClient, |
| 51 | + CONFIG.connectionLostTimeout |
| 52 | + ) |
| 53 | + ); |
| 54 | + |
| 55 | + const listener = new SDL.manager.lifecycle.LifecycleListener(); |
| 56 | + listener.setOnProxyConnected((manager) => { |
| 57 | + this._onConnected(); |
| 58 | + }); |
| 59 | + listener.setOnProxyClosed((lifecycleManager, info, reason) => {}); |
| 60 | + listener.setOnServiceStarted((serviceType, sessionId, correlationId) => {}); |
| 61 | + listener.setOnServiceEnded((serviceType) => {}); |
| 62 | + listener.setOnError((lifecycleManager, info) => {}); |
| 63 | + |
| 64 | + this._manager = new SDL.manager.lifecycle.LifecycleManager(this._appConfig, listener); |
| 65 | + |
| 66 | + const hmiFullListener = new SDL.rpc.RpcListener().setOnRpcMessage(this._onHmiStatusListener.bind(this)); |
| 67 | + |
| 68 | + this._manager.addRpcListener(SDL.rpc.enums.FunctionID.OnHMIStatus, hmiFullListener); |
| 69 | + } |
| 70 | + |
| 71 | + start () { |
| 72 | + this._manager.start(); |
| 73 | + } |
| 74 | + |
| 75 | + async _onConnected () { |
| 76 | + // app starts in the NONE state |
| 77 | + const fileBinary = await _fetchImageUnit8Array('./test_icon_1.png'); |
| 78 | + const fileName = `${this._appConfig.getAppId()}_icon.gif`; |
| 79 | + |
| 80 | + const putFile = new SDL.rpc.messages.PutFile() |
| 81 | + .setFileName(fileName) |
| 82 | + .setFileType('GRAPHIC_PNG') |
| 83 | + .setPersistentFile(true) |
| 84 | + .setFileData(fileBinary); |
| 85 | + |
| 86 | + await this._asyncSendRpc(putFile); |
| 87 | + |
| 88 | + const setAppIcon = new SDL.rpc.messages.SetAppIcon() |
| 89 | + .setFileName(fileName); |
| 90 | + |
| 91 | + await this._asyncSendRpc(setAppIcon); |
| 92 | + } |
| 93 | + |
| 94 | + async _onHmiStatusListener (onHmiStatus) { |
| 95 | + const hmiLevel = onHmiStatus.getHmiLevel(); |
| 96 | + |
| 97 | + // wait for the FULL state for more functionality |
| 98 | + if (hmiLevel === SDL.rpc.enums.HMILevel.HMI_FULL) { |
| 99 | + const show = new SDL.rpc.messages.Show(); |
| 100 | + show.setMainField1('Hello') |
| 101 | + .setMainField2('こんにちは') |
| 102 | + .setMainField3('你好'); |
| 103 | + |
| 104 | + await this._asyncSendRpc(show); |
| 105 | + |
| 106 | + await this._sleep(2000); |
| 107 | + |
| 108 | + const count = 3; |
| 109 | + for (let i = 0; i < count; i++) { |
| 110 | + const showCountdown = new SDL.rpc.messages.Show(); |
| 111 | + showCountdown.setMainField1(`Exiting in ${(count - i).toString()}`) |
| 112 | + .setMainField2('') |
| 113 | + .setMainField3(''); |
| 114 | + |
| 115 | + this._asyncSendRpc(showCountdown); // don't wait for a response |
| 116 | + |
| 117 | + await this._sleep(); |
| 118 | + } |
| 119 | + |
| 120 | + // tear down the app |
| 121 | + await this._asyncSendRpc(new SDL.rpc.messages.UnregisterAppInterface()); |
| 122 | + |
| 123 | + this._manager.stop(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + async _sleep (timeout = 1000) { |
| 128 | + return new Promise((resolve) => { |
| 129 | + setTimeout(resolve, timeout); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + // TODO: this should go into some manager class |
| 134 | + // abstracts out the work of sending the RPC and attaching listeners to wait for a response |
| 135 | + _asyncSendRpc (request, timeout = 5000) { |
| 136 | + return new Promise((resolve, reject) => { |
| 137 | + const functionId = SDL.rpc.enums.FunctionID.valueForKey(request.getFunctionName()); // this is the number value |
| 138 | + let correlationIdRequest; |
| 139 | + let listener; |
| 140 | + |
| 141 | + listener = new SDL.rpc.RpcListener().setOnRpcMessage(rpcMessage => { |
| 142 | + const correlationIdResponse = rpcMessage.getCorrelationId(); |
| 143 | + // ensure the correlation ids match |
| 144 | + if (correlationIdRequest === correlationIdResponse) { |
| 145 | + // remove the listener once the correct response is received |
| 146 | + this._manager.removeRpcListener(functionId, listener); |
| 147 | + resolve(rpcMessage); |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + this._manager.addRpcListener(functionId, listener); |
| 152 | + this._manager.sendRpcMessage(request); // the request will get a correlation id after this method |
| 153 | + |
| 154 | + correlationIdRequest = request.getCorrelationId(); |
| 155 | + |
| 156 | + setTimeout(() => { |
| 157 | + reject(new Error(`Response timeout for ${request.getFunctionName()}`)); |
| 158 | + }, timeout); // timeout after so long of not getting a response |
| 159 | + }); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +async function _fetchImageUnit8Array (path) { |
| 164 | + const aryBuffer = fs.readFileSync(path, null); |
| 165 | + return new Uint8Array(aryBuffer); |
| 166 | +} |
| 167 | + |
| 168 | +module.exports = AppClient; |
0 commit comments