2 * Copyright (c) 2012, Intel Corporation.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
10 /*****************************************************************************
13 * A javascript implementation of the IVI vehicle API that communicates
14 * to the automotive message broker through a websocket
15 * Optional constructor arguments:
16 * sCB: success callback, called when socket is connected, argument is
17 * success message string
18 * eCB: error callback, called on socket close or error, argument is error
20 * url: the URL to use for the websocket, in the form "ws://host:port/script"
21 * protocol: the protocol to use for the websocket, default is "http-only"
23 * [Public Member functions]
24 * Function name: getSupportedEventTypes(type, writeable, successCB, errorCB)
26 * Retrieves a list of vehicle events for the requested type
28 * type: target event or group to query (use empty string for all events)
29 * writeable: if true, return only writeable events, otherwise get all
30 * successCB: success callback, gets called with a string list of names
31 * for all the events and event groups that are children of the
32 * target. e.g. "vehicle_info" returns all events/groups with the
33 * vehicle_info prefix. If the target is an event group, it's
34 * omitted from the returned list
35 * errorCB: error callback, called with error message string
37 * Function name: get(eventlist, successCB, errorCB)
39 * Retrieves a list of event/value pairs for a target list of event names
41 * eventlist[]: list of events to read (use empty string for all events)
42 * successCB: success callback, gets called with the event/value pair list
43 * for all requested events. The list is the in the
44 * form of data[n].name/data[n].value
45 * errorCB: error callback, called with error message string
47 * Function name: set(eventlist, valuelist, successCB, errorCB)
49 * Sets a gourp of event's values (triggers error on read-only events)
51 * eventlist: target events to set
52 * valuelist: target event values
53 * successCB: success callback, gets called with the eventlist
54 * that was successfully set
55 * errorCB: error callback, called with error message string
57 * Function name: subscribe(eventlist, successCB, errorCB)
59 * Subscribe to a list of events so you can listen to value changes, they
60 * can be monitored with document.addEventListener(eventname, callback, false);
61 * The Event object passed to the callback has two parameters, e.name and
62 * e.value. Events are sent to the handler individually.
64 * eventlist: target events to listen to
65 * successCB: success callback, gets called with the eventlist
66 * that was successfully subscribed
67 * errorCB: error callback, called with the eventlist that failed to subscribe
69 * Function name: unsubscribe(eventlist, successCB, errorCB)
71 * Unsubscribe to a list of events to let the server know you're not listening,
72 * they should stop being sent from the server if no other clients are using them,
73 * but will at least stop being triggered in your app.
75 * eventlist: target events to stop listening to
76 * successCB: success callback, gets called with the eventlist
77 * that was successfully unsubscribed
78 * errorCB: error callback, called with the eventlist that failed to unsubscribe
80 ******************************************************************************/
82 function Vehicle(sCB, eCB, url, protocol)
84 /* store a copy of Vehicle this for reference in callbacks */
87 this.iSuccessCB = sCB;
90 /* variables for call management, supports up to 100 simultaneously */
92 this.methodCalls = [];
93 for(var i = 0; i < 100; i++)
95 this.methodCalls[i] = null;
98 /* number of connection retries to attempt if the socket closes */
100 this.connected = false;
102 /* timeout for method calls in milliseconds */
103 this.timeouttime = 5000;
105 /* default values for WebSocket */
106 this.socketUrl = "ws://localhost:23000/vehicle";
107 this.socketProtocol = "http-only";
109 /* override the websocket address if parameters are given */
110 if(url !== undefined) this.socketUrl = url;
111 if(protocol !== undefined) this.socketProtocol = protocol;
113 this.VehicleMethodCall = function(id, name, successCB, errorCB)
116 this.successCB = successCB;
117 this.errorCB = errorCB;
118 this.transactionid = id;
121 this.start = function()
123 me.timeout = setTimeout(function(){
124 if(me.errorCB !== undefined)
126 me.errorCB("\""+me.name+"\" method timed out after "+self.timeouttime+"ms");
129 }, self.timeouttime);
131 this.finish = function()
133 if(me.timeout !== undefined)
135 clearTimeout(me.timeout);
142 if ("WebSocket" in window)
144 if(self.socketProtocol.length > 0)
146 self.socket = new WebSocket(self.socketUrl, self.socketProtocol);
150 self.socket = new WebSocket(self.socketUrl);
152 self.socket.onopen = function()
154 self.connected = true;
155 self.iSuccessCB((self.retries < 5)?"(RECONNECTED)":"");
158 self.socket.onclose = function()
160 self.connected = false;
161 self.iErrorCB("socket closed "+((self.retries > 0)?"retrying in 5 seconds ...":""));
164 setTimeout(function(){
170 self.socket.onerror = function(e)
172 self.iErrorCB(e.data);
174 self.socket.onmessage = function (e)
176 self.receive(e.data);
181 console.log("This browser doesn't appear to support websockets!");
187 Vehicle.prototype.generateTransactionId = function()
190 for(i = 0; i < 8; i++)
192 var num = Math.floor((Math.random()+1)*65536);
193 val[i] = num.toString(16).substring(1);
195 var uuid = val[0]+val[1]+"-"+
196 val[2]+"-"+val[3]+"-"+val[4]+"-"+
197 val[5]+val[6]+val[7];
201 Vehicle.prototype.send = function(obj, successCB, errorCB)
205 if(errorCB !== undefined)
207 errorCB("\""+obj.name+"\" method failed because socket is closed");
211 var i = this.methodIdx;
212 this.methodIdx = (this.methodIdx + 1)%100;
213 this.methodCalls[i] = new this.VehicleMethodCall(obj.transactionid,
214 obj.name, successCB, errorCB);
215 this.socket.send(JSON.stringify(obj));
216 this.methodCalls[i].start();
220 Vehicle.prototype.getSupportedEventTypes = function(type, writeable, successCB, errorCB)
224 "name" : "getSupportedEventTypes",
225 "writeable" : writeable,
226 "transactionid" : this.generateTransactionId(),
229 this.send(obj, successCB, errorCB);
232 Vehicle.prototype.get = function(namelist, successCB, errorCB)
234 if(namelist.length <= 0)
242 "transactionid" : this.generateTransactionId(),
245 this.send(obj, successCB, errorCB);
248 Vehicle.prototype.getHistory = function(namelist, beginDate, endDate, successCB, errorCB)
250 if(namelist.length <= 0)
256 "property" : namelist,
257 "timeBegin" : (beginDate.getTime() / 1000).toString(),
258 "timeEnd" : (endDate.getTime() / 1000).toString(),
259 "sequenceBegin" : "-1",
266 "transactionid" : this.generateTransactionId(),
269 this.send(obj, successCB, errorCB);
272 Vehicle.prototype.set = function(namelist, valuelist, successCB, errorCB)
274 if((namelist.length != valuelist.length)||(namelist.length <= 0))
282 "transactionid" : this.generateTransactionId(),
286 for(var i = 0; i < namelist.length; i++)
288 var val = {"property" : namelist[i], "value" : valuelist[i]};
289 list[list.length] = val;
292 this.send(obj, successCB, errorCB);
295 Vehicle.prototype.subscribe = function(namelist, successCB, errorCB)
300 "transactionid" : this.generateTransactionId(),
303 this.send(obj, successCB, errorCB);
306 Vehicle.prototype.unsubscribe = function(namelist, successCB, errorCB)
310 "name": "unsubscribe",
311 "transactionid" : this.generateTransactionId(),
314 this.send(obj, successCB, errorCB);
317 Vehicle.prototype.sendEvent = function(name, value)
319 var evt = document.createEvent("Event");
320 evt.initEvent(name, true, true);
323 document.dispatchEvent(evt);
327 Vehicle.prototype.receive = function(msg)
332 event = JSON.parse(msg);
335 self.iErrorCB("GARBAGE MESSAGE: "+msg);
339 if((event === undefined)||(event.type === undefined)||
340 (event.name === undefined))
342 self.iErrorCB("BADLY FORMED MESSAGE: "+msg);
347 if(event.type === "methodReply")
349 var calls = this.methodCalls;
350 for(var i = 0; i < calls.length; i++)
353 if(call&&(!call.done)&&(call.transactionid === event.transactionid))
356 if(event.error !== undefined)
358 call.errorCB(event.error);
360 if(event.data !== undefined && call.successCB !== undefined)
362 call.successCB(event.data);
368 else if(event.type === "valuechanged")
370 self.sendEvent(event.name, event.data);