Updated GhostCluster web sample from upstream
[profile/ivi/sdk/web-sample-build.git] / samples / web / Sample / Tizen / Web App / ModelloSmartDeviceLink / project / ffw / TTSRPC.js
1 /*
2  * Copyright (c) 2013, Ford Motor Company All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *  · Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  *  · Redistributions in binary form must reproduce the above copyright notice,
9  * this list of conditions and the following disclaimer in the documentation
10  * and/or other materials provided with the distribution.
11  *  · Neither the name of the Ford Motor Company nor the names of its
12  * contributors may be used to endorse or promote products derived from this
13  * software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 /*
28  * Reference implementation of TTS component.
29  *
30  * TTS is responsible for playing sound data sent from SDLCore to notify user
31  * about some events happened.
32  */
33
34 FFW.TTS = FFW.RPCObserver.create( {
35
36     /*
37      * access to basic RPC functionality
38      */
39     client: FFW.RPCClient.create( {
40         componentName: "TTS"
41     } ),
42
43     /*
44      * connect to RPC bus
45      */
46     connect: function() {
47         this.client.connect( this, 300 );
48     },
49
50     /*
51      * disconnect from RPC bus
52      */
53     disconnect: function() {
54         this.client.disconnect();
55     },
56
57     /*
58      * Client is registered - we can send request starting from this point of
59      * time
60      */
61     onRPCRegistered: function() {
62         Em.Logger.log( "FFW.TTS.onRPCRegistered" );
63         this._super();
64     },
65
66     /*
67      * Client is unregistered - no more requests
68      */
69     onRPCUnregistered: function() {
70         Em.Logger.log( "FFW.TTS.onRPCUnregistered" );
71         this._super();
72     },
73
74     /*
75      * Client disconnected.
76      */
77     onRPCDisconnected: function() {
78
79     },
80
81     /*
82      * when result is received from RPC component this function is called It is
83      * the propriate place to check results of reuqest execution Please use
84      * previously store reuqestID to determine to which request repsonse belongs
85      * to
86      */
87     onRPCResult: function( response ) {
88         Em.Logger.log( "FFW.TTS.onRPCResult" );
89         this._super();
90     },
91
92     /*
93      * handle RPC erros here
94      */
95     onRPCError: function( error ) {
96         Em.Logger.log( "FFW.TTS.onRPCError" );
97         this._super();
98     },
99
100     /*
101      * handle RPC notifications here
102      */
103     onRPCNotification: function( notification ) {
104         Em.Logger.log( "FFW.TTS.onRPCNotification" );
105         this._super();
106     },
107
108     /*
109      * handle RPC requests here
110      */
111     onRPCRequest: function( request ) {
112         Em.Logger.log( "FFW.TTS.onRPCRequest" );
113         this._super();
114
115         switch( request.method ){
116             case "TTS.Speak": {
117                 SDL.SDLModel.onPrompt( request.params.ttsChunks.splice( 0, 1 ) );
118
119                 // send repsonse
120                 var JSONMessage = {
121                     "jsonrpc": "2.0",
122                     "id": request.id,
123                     "result": {
124                         "resultCode": "SUCCESS", // type (enum) from SDL
125                         // protocol
126                         "method": "TTS.SpeakResponse"
127                     }
128                 };
129                 this.client.send( JSONMessage );
130
131                 break;
132             }
133             case "TTS.GetCapabilities": {
134
135                 // send repsonse
136                 var JSONMessage = {
137                     "jsonrpc": "2.0",
138                     "id": request.id,
139                     "result": {
140                         "capabilities":
141                             [
142                                 "TEXT"
143                             ],
144
145                         "resultCode": "SUCCESS", // type (enum) from SDL
146                         // protocol
147                         "method": "TTS.GetCapabilitiesResponse"
148                     }
149                 };
150                 this.client.send( JSONMessage );
151
152                 break;
153             }
154             case "TTS.GetSupportedLanguages": {
155
156                 var JSONMessage = {
157                     "jsonrpc": "2.0",
158                     "id": request.id,
159                     "result": {
160                         "resultCode": "SUCCESS", // type (enum) from SDL
161                         // protocol
162                         "method": "TTS.GetSupportedLanguagesResponse",
163                         "languages": SDL.SDLModel.sdlLanguagesList
164                     }
165                 };
166                 this.client.send( JSONMessage );
167
168                 break;
169             }
170             case "TTS.GetLanguage": {
171
172                 var JSONMessage = {
173                     "jsonrpc": "2.0",
174                     "id": request.id,
175                     "result": {
176                         "resultCode": "SUCCESS", // type (enum) from SDL
177                         // protocol
178                         "method": "TTS.GetLanguageResponse",
179                         "language": SDL.SDLModel.hmiTTSVRLanguage
180                     }
181                 };
182                 this.client.send( JSONMessage );
183
184                 break;
185             }
186             case "TTS.ChangeRegistration": {
187
188                 SDL.SDLModel.changeRegistrationTTSVR( request.params.language );
189
190                 // send repsonse
191                 var JSONMessage = {
192                     "jsonrpc": "2.0",
193                     "id": request.id,
194                     "result": {
195                         "resultCode": "SUCCESS", // type (enum) from SDL
196                         // protocol
197                         "method": "TTS.ChangeRegistrationResponse"
198                     }
199                 };
200                 this.client.send( JSONMessage );
201
202                 break;
203             }
204
205             default: {
206                 // statements_def
207                 break;
208             }
209         }
210     },
211
212     /*
213      * Notifies if sdl TTS components language was changed
214      */
215     OnLanguageChange: function( lang ) {
216         Em.Logger.log( "FFW.TTS.OnLanguageChange" );
217
218         // send repsonse
219         var JSONMessage = {
220             "jsonrpc": "2.0",
221             "method": "TTS.OnLanguageChange",
222             "params": {
223                 "language": lang
224             }
225         };
226         this.client.send( JSONMessage );
227     }
228 } )