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 /* --------------------------- utility code ------------------------------- */
14 init : function(log_id) {
15 this.logElement = document.getElementById(log_id);
18 scrollToBottom : function() {
19 this.logElement.scrollTop = this.logElement.scrollHeight;
22 incoming : function(msg) {
23 this.logElement.innerHTML += "<div style='color: blue'> REQUEST: " + msg + "</div>";
24 this.scrollToBottom();
27 outgoing : function(msg) {
28 this.logElement.innerHTML += "<div style='color: purple'> RESPONSE: " + msg + "</div>";
29 this.scrollToBottom();
32 pass : function(msg) {
33 this.logElement.innerHTML += "<div style='color: green'> SUCCESS: " + msg + "</div>";
34 this.scrollToBottom();
37 fail : function(msg) {
38 this.logElement.innerHTML += "<div style='color: red'> FAIL: " + msg + "</div>";
39 this.scrollToBottom();
43 this.logElement.innerHTML += "<div class='LogClass'> " + msg + "</div>";
44 this.scrollToBottom();
48 /* ----------------------------- test code --------------------------------- */
50 function VehicleServer(socketUrl)
53 this.vehicleEventType = new VehicleEventType();
56 if ("WebSocket" in window)
58 self.socket = new WebSocket(socketUrl);
59 self.socket.onopen = function()
61 PRINT.pass("Server READY");
63 self.socket.onclose = function()
65 PRINT.fail("Server CLOSED");
67 self.socket.onerror = function(e)
69 PRINT.fail("Server ERROR: "+e.data);
71 self.socket.onmessage = function (e)
78 PRINT.fail("This browser doesn't appear to support websockets!");
84 VehicleServer.prototype.receive = function(msg)
86 var event = JSON.parse(msg);
87 /* accept only methods with transaction ids */
88 if((event == undefined)||(event.transactionid == undefined)||
89 (event.type != "method"))
96 if(event.name === "getSupportedEventTypes")
101 data = this.vehicleEventType.getValueEventList(event.data);
105 data = this.vehicleEventType.getSupportedEventList(event.data);
108 "type" : "methodReply",
110 "transactionid" : event.transactionid,
114 else if(event.name === "get")
116 var names = this.vehicleEventType.getValueEventList(event.data);
120 "type" : "methodReply",
122 "transactionid" : event.transactionid,
127 var value = this.vehicleEventType.getValue(names[i]);
128 obj.data.push({"name" : names[i], "value" : value});
134 "type" : "methodReply",
136 "transactionid" : event.transactionid,
137 "error" : event.data + " is not a valid event"
141 else if(event.name === "set")
143 if(this.vehicleEventType.isValueEvent(event.data.property))
146 obj.type = "methodReply";
147 this.vehicleEventType.setValue(event.data.property, event.data.value);
152 "type" : "methodReply",
154 "transactionid" : event.transactionid,
155 "error" : event.data.property + " is not a writeable event"
162 "type" : "methodReply",
164 "transactionid" : event.transactionid,
165 "error" : event.name + " is not a valid method"
168 PRINT.outgoing(JSON.stringify(obj));
169 this.socket.send(JSON.stringify(obj));
172 window.addEventListener('load', function () {
174 PRINT.init("result");
175 var server = new VehicleServer("ws://localhost:23023/vehicle?server");