Make wheelsourceplugin actually read JS events (currently just prints out)
[profile/ivi/automotive-message-broker.git] / plugins / websocketsink / test / api.js
1 /*
2  * Copyright (c) 2012, Intel Corporation.
3  *
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
7  *
8  */
9
10 /* ------------------------ vehicle interface ----------------------------- */
11
12 function Vehicle(socketUrl, sCB, eCB, calltimeout)
13 {
14     var self = this;
15     this.iSuccessCB = sCB;
16     this.iErrorCB = eCB;
17     this.retries = 5;
18     this.connected = false;
19     this.transactionid = 0;
20     this.methodCalls = [];
21     this.methodIdx = 0;
22     this.timeouttime = (calltimeout == undefined)?5000:calltimeout;
23
24     for(var i = 0; i < 100; i++)
25     {
26         this.methodCalls[i] = null;
27     }
28
29     this.VehicleMethodCall = function(id, name, successCB, errorCB)
30     {
31         var me = this;
32         this.successCB = successCB;
33         this.errorCB = errorCB;
34         this.transactionid = id;
35         this.name = name;
36         this.done = false;
37         this.start = function()
38         {
39             me.timeout = setTimeout(function(){
40                 if(me.errorCB != undefined)
41                 {
42                     me.errorCB("\""+me.name+"\" method timed out after "+self.timeouttime+"ms");
43                 }
44                 me.finish();
45             }, self.timeouttime);
46         }
47         this.finish = function()
48         {
49             if(me.timeout != undefined)
50             {
51                 clearTimeout(me.timeout);
52             }
53             me.done = true;
54         }
55     }
56
57     function init() {
58         if ("WebSocket" in window)
59         {
60             self.socket = new WebSocket(socketUrl);
61             self.socket.onopen = function()
62             {
63                 self.connected = true;
64                 this.send("client");
65                 self.iSuccessCB((self.retries < 5)?"(RECONNECTED)":"");
66                 self.retries = 5;
67             };
68             self.socket.onclose = function()
69             {
70                 self.connected = false;
71                 self.iErrorCB("socket closed "+((self.retries > 0)?"retrying in 5 seconds ...":""));
72                 if(self.retries > 0)
73                 {
74                     setTimeout(function(){
75                         self.retries--;
76                         init();
77                     }, 5000);
78                 }
79             };
80             self.socket.onerror = function(e)
81             {
82                 self.iErrorCB(e.data);
83             };
84             self.socket.onmessage = function (e) 
85             {
86                 self.receive(e.data);
87             };
88         }
89         else
90         {
91             console.log("This browser doesn't appear to support websockets!");
92         }
93     }
94     init();
95 }
96
97 Vehicle.prototype.send = function(obj, successCB, errorCB)
98 {
99     obj.transactionid = this.transactionid++;
100     if(!this.connected)
101     {
102         if(errorCB != undefined)
103         {
104             errorCB("\""+obj.name+"\" method failed because socket is closed");
105         }
106         return;
107     }
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();
114 }
115
116
117 Vehicle.prototype.getSupportedEventTypes = function(type, writeable, successCB, errorCB)
118 {
119     var obj = {
120         "type" : "method",
121         "name" : "getSupportedEventTypes",
122         "writeable" : writeable,
123         "transactionid" : 0,
124         "data" : type
125     };
126     this.send(obj, successCB, errorCB);
127 }
128
129 Vehicle.prototype.get = function(type, successCB, errorCB)
130 {
131     var obj = {
132         "type" : "method",
133         "name": "get",
134         "transactionid" : 0,
135         "data" : type
136     };
137     this.send(obj, successCB, errorCB);
138 }
139
140 Vehicle.prototype.set = function(type, value, successCB, errorCB)
141 {
142     var obj = {
143         "type" : "method",
144         "name": "set",
145         "transactionid" : 0,
146         "data" : {"property" : type, "value" : value}
147     };
148     this.send(obj, successCB, errorCB);
149 }
150
151 Vehicle.prototype.receive = function(msg)
152 {
153     var self = this;
154     var event;
155     try {
156         event = JSON.parse(msg);
157     }
158     catch(e) {
159         self.iErrorCB("GARBAGE MESSAGE: "+msg);
160         return;
161     }
162
163     if((event == undefined)||(event.type == undefined)||
164        (event.name == undefined)||(event.transactionid == undefined))
165     {
166         self.iErrorCB("BADLY FORMED MESSAGE: "+msg);
167         return;
168     }
169     else
170     {
171         if(event.type === "methodReply")
172         {
173             var calls = this.methodCalls;
174             for(var i = 0; i < calls.length; i++)
175             {
176                 var call = calls[i];
177                 if(call&&(!call.done)&&(call.transactionid === event.transactionid))
178                 {
179                     call.finish();
180                     if(event.error != undefined)
181                     {
182                         call.errorCB(event.error);
183                     }
184                     else
185                     {
186                         call.successCB(event.data);
187                     }
188                     return;
189                 }
190             }
191         }
192     }
193 }