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 /* ------------------------ vehicle interface ----------------------------- */
12 function Vehicle(socketUrl, sCB, eCB, calltimeout)
15 this.iSuccessCB = sCB;
18 this.connected = false;
19 this.transactionid = 0;
20 this.methodCalls = [];
22 this.timeouttime = (calltimeout == undefined)?5000:calltimeout;
24 for(var i = 0; i < 100; i++)
26 this.methodCalls[i] = null;
29 this.VehicleMethodCall = function(id, name, successCB, errorCB)
32 this.successCB = successCB;
33 this.errorCB = errorCB;
34 this.transactionid = id;
37 this.start = function()
39 me.timeout = setTimeout(function(){
40 if(me.errorCB != undefined)
42 me.errorCB("\""+me.name+"\" method timed out after "+self.timeouttime+"ms");
47 this.finish = function()
49 if(me.timeout != undefined)
51 clearTimeout(me.timeout);
58 if ("WebSocket" in window)
60 self.socket = new WebSocket(socketUrl);
61 self.socket.onopen = function()
63 self.connected = true;
65 self.iSuccessCB((self.retries < 5)?"(RECONNECTED)":"");
68 self.socket.onclose = function()
70 self.connected = false;
71 self.iErrorCB("socket closed "+((self.retries > 0)?"retrying in 5 seconds ...":""));
74 setTimeout(function(){
80 self.socket.onerror = function(e)
82 self.iErrorCB(e.data);
84 self.socket.onmessage = function (e)
91 console.log("This browser doesn't appear to support websockets!");
97 Vehicle.prototype.send = function(obj, successCB, errorCB)
99 obj.transactionid = this.transactionid++;
102 if(errorCB != undefined)
104 errorCB("\""+obj.name+"\" method failed because socket is closed");
108 var i = this.methodIdx;
109 this.methodIdx = (this.methodIdx + 1)%100;
110 this.methodCalls[i] = new this.VehicleMethodCall(obj.transactionid,
111 obj.name, successCB, errorCB);
112 this.socket.send(JSON.stringify(obj));
113 this.methodCalls[i].start();
117 Vehicle.prototype.getSupportedEventTypes = function(type, writeable, successCB, errorCB)
121 "name" : "getSupportedEventTypes",
122 "writeable" : writeable,
126 this.send(obj, successCB, errorCB);
129 Vehicle.prototype.get = function(type, successCB, errorCB)
137 this.send(obj, successCB, errorCB);
140 Vehicle.prototype.set = function(type, value, successCB, errorCB)
146 "data" : {"property" : type, "value" : value}
148 this.send(obj, successCB, errorCB);
151 Vehicle.prototype.receive = function(msg)
156 event = JSON.parse(msg);
159 self.iErrorCB("GARBAGE MESSAGE: "+msg);
163 if((event == undefined)||(event.type == undefined)||
164 (event.name == undefined)||(event.transactionid == undefined))
166 self.iErrorCB("BADLY FORMED MESSAGE: "+msg);
171 if(event.type === "methodReply")
173 var calls = this.methodCalls;
174 for(var i = 0; i < calls.length; i++)
177 if(call&&(!call.done)&&(call.transactionid === event.transactionid))
180 if(event.error != undefined)
182 call.errorCB(event.error);
186 call.successCB(event.data);