4a832756085476e2562abd00b76a733cab6df4e8
[profile/ivi/sdk/web-sample-build.git] / samples / web / Sample / Tizen / Web App / Modello_SDL / project / ffw / VehicleInfoRPC.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 VehicleInfo component.
29  *
30  * Interface to get or set some essential information sent from SDLCore.
31  * VehicleInfo is responsible for sending a data about the condition of the
32  * vehicle between SDLCore and CAN network. Instead CAN network used
33  * VehicleInfoModel.
34  *
35  */
36
37 FFW.VehicleInfo = FFW.RPCObserver.create( {
38
39     /**
40      * access to basic RPC functionality
41      */
42     client: FFW.RPCClient.create( {
43         componentName: "VehicleInfo"
44     } ),
45
46     /**
47      * connect to RPC bus
48      */
49     connect: function() {
50         this.client.connect( this, 600 );
51     },
52
53     /**
54      * disconnect from RPC bus
55      */
56     disconnect: function() {
57         this.client.disconnect();
58     },
59
60     /**
61      * Client is registered - we can send request starting from this point of
62      * time
63      */
64     onRPCRegistered: function() {
65         Em.Logger.log( "FFW.VehicleInfo.onRPCRegistered" );
66         this._super();
67     },
68
69     /**
70      * Client is unregistered - no more requests
71      */
72     onRPCUnregistered: function() {
73         Em.Logger.log( "FFW.VehicleInfo.onRPCUnregistered" );
74         this._super();
75     },
76
77     /**
78      * Client disconnected.
79      */
80     onRPCDisconnected: function() {
81
82     },
83
84     /**
85      * when result is received from RPC component this function is called It is
86      * the propriate place to check results of reuqest execution Please use
87      * previously store reuqestID to determine to which request repsonse belongs
88      * to
89      */
90     onRPCResult: function( response ) {
91         Em.Logger.log( "FFW.VehicleInfo.onRPCResult" );
92         this._super();
93     },
94
95     /**
96      * handle RPC erros here
97      */
98     onRPCError: function( error ) {
99         Em.Logger.log( "FFW.VehicleInfo.onRPCError" );
100         this._super();
101     },
102
103     /**
104      * handle RPC notifications here
105      */
106     onRPCNotification: function( notification ) {
107         Em.Logger.log( "FFW.VehicleInfo.onRPCNotification" );
108         this._super();
109     },
110
111     /**
112      * handle RPC requests here
113      *
114      * @type {Object} request
115      */
116     onRPCRequest: function( request ) {
117         Em.Logger.log( "FFW.VehicleInfo.onRPCRequest" );
118         this._super();
119
120         if( request.method == "VehicleInfo.GetVehicleData" ){
121
122             var vehicleData = SDL.SDLVehicleInfoModel.getVehicleData( request.params ), resultCode;
123
124             if( vehicleData ){
125                 resultCode = "SUCCESS";
126             }else{
127                 resultCode = "GENERIC_ERROR";
128             }
129
130             // send repsonse
131             var JSONMessage = {
132                 "jsonrpc": "2.0",
133                 "id": request.id,
134                 "result": {
135                     "resultCode": resultCode, // type (enum) from SDL protocol
136                     "method": "VehicleInfo.GetVehicleDataResponse"
137                 // request.params.dataType: vehicleData
138                 }
139             };
140             JSONMessage.result[SDL.SDLVehicleInfoModel.vehicleData[request.params.dataType].type] = vehicleData;
141             this.client.send( JSONMessage );
142         }
143
144         if( request.method == "VehicleInfo.ReadDID" ){
145
146             SDL.SDLVehicleInfoModel.vehicleInfoReadDID( request.params, request.id );
147
148         }
149
150         if( request.method == "VehicleInfo.GetDTCs" ){
151
152             SDL.SDLVehicleInfoModel.vehicleInfoGetDTCs( request.params, request.id );
153
154         }
155
156         if( request.method == "VehicleInfo.GetVehicleType" ){
157
158             SDL.SDLVehicleInfoModel.getVehicleType( request.id );
159
160         }
161     },
162
163     /**
164      * Notifies if data was changed
165      *
166      * @type {Object} params
167      */
168     OnVehicleData: function( params ) {
169         Em.Logger.log( "FFW.VehicleInfo.OnVehicleData" );
170
171         // send repsonse
172         var JSONMessage = {
173             "jsonrpc": "2.0",
174             "method": "VehicleInfo.OnVehicleData",
175             "params": params
176         };
177         this.client.send( JSONMessage );
178     },
179
180     /**
181      * GetVehicleType Response
182      *
183      * @type {string} vehicleType
184      * @type {int} id
185      */
186     GetVehicleTypeResponse: function( vehicleType, id ) {
187         Em.Logger.log( "FFW.VehicleInfo.GetVehicleType" );
188
189         var JSONMessage = {
190             "jsonrpc": "2.0",
191             "id": id,
192             "result": {
193                 "resultCode": "SUCCESS", // type (enum) from SDL protocol
194                 "method": "VehicleInfo.GetVehicleTypeResponse",
195                 "vehicleType": vehicleType
196             }
197         };
198
199         this.client.send( JSONMessage );
200     },
201
202     /**
203      * ReadDID Response
204      *
205      * @type {array} dataResult
206      * @type {array} data
207      * @type {string} info
208      * @type {string} result
209      * @type {int} id
210      */
211     vehicleInfoReadDIDResponse: function( dataResult, data, info, result, id ) {
212         Em.Logger.log( "FFW.VehicleInfo.ReadDID" );
213
214         var JSONMessage;
215         // send repsonse
216         if( result != 'ENCRYPTED' ){
217             JSONMessage = {
218                 "jsonrpc": "2.0",
219                 "id": id,
220                 "result": {
221                     "resultCode": result, // type (enum) from SDL protocol
222                     "method": "VehicleInfo.ReadDIDResponse",
223                     "info": info,
224                     "dataResult": dataResult,
225                     "data": data
226                 }
227             };
228         }else{
229             JSONMessage = {
230                 "jsonrpc": "2.0",
231                 "id": id,
232                 "result": {
233                     "resultCode": result, // type (enum) from SDL protocol
234                     "method": "VehicleInfo.ReadDIDResponse",
235                     "info": info
236                 }
237             };
238         }
239         this.client.send( JSONMessage );
240     },
241
242     /**
243      * GetDTCs Response
244      *
245      * @type {array} data
246      * @type {string} info
247      * @type {string} result
248      * @type {int} id
249      */
250     vehicleInfoGetDTCsResponse: function( data, info, result, id ) {
251         Em.Logger.log( "FFW.VehicleInfo.GetDTCs" );
252
253         var JSONMessage;
254         // send repsonse
255         if( result != 'ENCRYPTED' ){
256             JSONMessage = {
257                 "jsonrpc": "2.0",
258                 "id": id,
259                 "result": {
260                     "resultCode": result, // type (enum) from SDL protocol
261                     "method": "VehicleInfo.GetDTCsResponse",
262                     "info": info,
263                     "dtcList": data
264                 }
265             };
266         }else{
267             JSONMessage = {
268                 "jsonrpc": "2.0",
269                 "id": id,
270                 "result": {
271                     "resultCode": result, // type (enum) from SDL protocol
272                     "method": "VehicleInfo.GetDTCsResponse",
273                     "info": info
274                 }
275             };
276         }
277         this.client.send( JSONMessage );
278     }
279 } )