Make wheelsourceplugin actually read JS events (currently just prints out)
[profile/ivi/automotive-message-broker.git] / plugins / websocketsink / test / server.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 /* --------------------------- utility code ------------------------------- */
11
12 var PRINT = {
13     logElement : null,
14     init : function(log_id) {
15         this.logElement = document.getElementById(log_id);
16     },
17
18     scrollToBottom : function() {
19         this.logElement.scrollTop = this.logElement.scrollHeight;
20     },
21
22     incoming : function(msg) {
23         this.logElement.innerHTML += "<div style='color: blue'> REQUEST: " + msg + "</div>";
24         this.scrollToBottom();
25     },
26
27     outgoing : function(msg) {
28         this.logElement.innerHTML += "<div style='color: purple'> RESPONSE: " + msg + "</div>";
29         this.scrollToBottom();
30     },
31
32     pass : function(msg) {
33         this.logElement.innerHTML += "<div style='color: green'> SUCCESS: " + msg + "</div>";
34         this.scrollToBottom();
35     },
36
37     fail : function(msg) {
38         this.logElement.innerHTML += "<div style='color: red'> FAIL: " + msg + "</div>";
39         this.scrollToBottom();
40     },
41
42     log : function(msg) {
43         this.logElement.innerHTML += "<div class='LogClass'> " + msg + "</div>";
44         this.scrollToBottom();
45     },
46 }
47
48 /* ----------------------------- test code --------------------------------- */
49
50 function VehicleServer(socketUrl)
51 {
52     var self = this;
53     this.vehicleEventType = new VehicleEventType();
54  
55     function init() {
56         if ("WebSocket" in window)
57         {
58             self.socket = new WebSocket(socketUrl);
59             self.socket.onopen = function()
60             {
61                 PRINT.pass("Server READY");
62                 this.send("server");
63             };
64             self.socket.onclose = function()
65             {
66                 PRINT.fail("Server CLOSED");
67             };
68             self.socket.onerror = function(e)
69             {
70                 PRINT.fail("Server ERROR: "+e.data);
71             };
72             self.socket.onmessage = function (e) 
73             {
74                 self.receive(e.data);
75             };
76         }
77         else
78         {
79             PRINT.fail("This browser doesn't appear to support websockets!");
80         }
81     }
82     init();
83 }
84
85 VehicleServer.prototype.receive = function(msg)
86 {
87     var event = JSON.parse(msg);
88     /* accept only methods with transaction ids */
89     if((event == undefined)||(event.transactionid == undefined)||
90        (event.type != "method"))
91     {
92         return;
93     }
94
95     var obj;
96     PRINT.incoming(msg);
97     if(event.name === "getSupportedEventTypes")
98     {
99         var data;
100         if(event.writeable)
101         {
102             data = this.vehicleEventType.getValueEventList(event.data);
103         }
104         else
105         {
106             data = this.vehicleEventType.getSupportedEventList(event.data);
107         }
108         obj = {
109             "type" : "methodReply",
110             "name": event.name,
111             "transactionid" : event.transactionid,
112             "data" : data
113         };
114     }
115     else if(event.name === "get")
116     {
117         var names = this.vehicleEventType.getValueEventList(event.data);
118         if(names.length > 0)
119         {
120             obj = {
121                 "type" : "methodReply",
122                 "name": event.name,
123                 "transactionid" : event.transactionid,
124                 "data" : []
125             };
126             for(i in names)
127             {
128                 var value = this.vehicleEventType.getValue(names[i]);
129                 obj.data.push({"name" : names[i], "value" : value});
130             }
131         }
132         else
133         {
134             obj = {
135                 "type" : "methodReply",
136                 "name": event.name,
137                 "transactionid" : event.transactionid,
138                 "error" : event.data + " is not a valid event"
139             };
140         }
141     }
142     else if(event.name === "set")
143     {
144         if(this.vehicleEventType.isValueEvent(event.data.property))
145         {
146             obj = event;
147             obj.type = "methodReply";
148             this.vehicleEventType.setValue(event.data.property, event.data.value);
149         }
150         else
151         {
152             obj = {
153                 "type" : "methodReply",
154                 "name": event.name,
155                 "transactionid" : event.transactionid,
156                 "error" : event.data.property + " is not a writeable event"
157             };
158         }
159     }
160     else
161     {
162         obj = {
163             "type" : "methodReply",
164             "name": event.name,
165             "transactionid" : event.transactionid,
166             "error" : event.name + " is not a valid method"
167         };
168     }
169     PRINT.outgoing(JSON.stringify(obj));
170     this.socket.send(JSON.stringify(obj));
171 }
172
173 window.addEventListener('load', function () {
174     "use strict";
175     PRINT.init("result");
176     var server = new VehicleServer("ws://localhost:23000/vehicle");
177 });