Make wheelsourceplugin actually read JS events (currently just prints out)
[profile/ivi/automotive-message-broker.git] / plugins / websocketsink / test / servertest / 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             };
63             self.socket.onclose = function()
64             {
65                 PRINT.fail("Server CLOSED");
66             };
67             self.socket.onerror = function(e)
68             {
69                 PRINT.fail("Server ERROR: "+e.data);
70             };
71             self.socket.onmessage = function (e) 
72             {
73                 self.receive(e.data);
74             };
75         }
76         else
77         {
78             PRINT.fail("This browser doesn't appear to support websockets!");
79         }
80     }
81     init();
82 }
83
84 VehicleServer.prototype.receive = function(msg)
85 {
86     var event = JSON.parse(msg);
87     /* accept only methods with transaction ids */
88     if((event == undefined)||(event.transactionid == undefined)||
89        (event.type != "method"))
90     {
91         return;
92     }
93
94     var obj;
95     PRINT.incoming(msg);
96     if(event.name === "getSupportedEventTypes")
97     {
98         var data;
99         if(event.writeable)
100         {
101             data = this.vehicleEventType.getValueEventList(event.data);
102         }
103         else
104         {
105             data = this.vehicleEventType.getSupportedEventList(event.data);
106         }
107         obj = {
108             "type" : "methodReply",
109             "name": event.name,
110             "transactionid" : event.transactionid,
111             "data" : data
112         };
113     }
114     else if(event.name === "get")
115     {
116         var names = this.vehicleEventType.getValueEventList(event.data);
117         if(names.length > 0)
118         {
119             obj = {
120                 "type" : "methodReply",
121                 "name": event.name,
122                 "transactionid" : event.transactionid,
123                 "data" : []
124             };
125             for(i in names)
126             {
127                 var value = this.vehicleEventType.getValue(names[i]);
128                 obj.data.push({"name" : names[i], "value" : value});
129             }
130         }
131         else
132         {
133             obj = {
134                 "type" : "methodReply",
135                 "name": event.name,
136                 "transactionid" : event.transactionid,
137                 "error" : event.data + " is not a valid event"
138             };
139         }
140     }
141     else if(event.name === "set")
142     {
143         if(this.vehicleEventType.isValueEvent(event.data.property))
144         {
145             obj = event;
146             obj.type = "methodReply";
147             this.vehicleEventType.setValue(event.data.property, event.data.value);
148         }
149         else
150         {
151             obj = {
152                 "type" : "methodReply",
153                 "name": event.name,
154                 "transactionid" : event.transactionid,
155                 "error" : event.data.property + " is not a writeable event"
156             };
157         }
158     }
159     else
160     {
161         obj = {
162             "type" : "methodReply",
163             "name": event.name,
164             "transactionid" : event.transactionid,
165             "error" : event.name + " is not a valid method"
166         };
167     }
168     PRINT.outgoing(JSON.stringify(obj));
169     this.socket.send(JSON.stringify(obj));
170 }
171
172 window.addEventListener('load', function () {
173     "use strict";
174     PRINT.init("result");
175     var server = new VehicleServer("ws://localhost:23023/vehicle?server");
176 });