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");
64 self.socket.onclose = function()
66 PRINT.fail("Server CLOSED");
68 self.socket.onerror = function(e)
70 PRINT.fail("Server ERROR: "+e.data);
72 self.socket.onmessage = function (e)
79 PRINT.fail("This browser doesn't appear to support websockets!");
85 VehicleServer.prototype.receive = function(msg)
87 var event = JSON.parse(msg);
88 /* accept only methods with transaction ids */
89 if((event == undefined)||(event.transactionid == undefined)||
90 (event.type != "method"))
97 if(event.name === "getSupportedEventTypes")
102 data = this.vehicleEventType.getValueEventList(event.data);
106 data = this.vehicleEventType.getSupportedEventList(event.data);
109 "type" : "methodReply",
111 "transactionid" : event.transactionid,
115 else if(event.name === "get")
117 var names = this.vehicleEventType.getValueEventList(event.data);
121 "type" : "methodReply",
123 "transactionid" : event.transactionid,
128 var value = this.vehicleEventType.getValue(names[i]);
129 obj.data.push({"name" : names[i], "value" : value});
135 "type" : "methodReply",
137 "transactionid" : event.transactionid,
138 "error" : event.data + " is not a valid event"
142 else if(event.name === "set")
144 if(this.vehicleEventType.isValueEvent(event.data.property))
147 obj.type = "methodReply";
148 this.vehicleEventType.setValue(event.data.property, event.data.value);
153 "type" : "methodReply",
155 "transactionid" : event.transactionid,
156 "error" : event.data.property + " is not a writeable event"
163 "type" : "methodReply",
165 "transactionid" : event.transactionid,
166 "error" : event.name + " is not a valid method"
169 PRINT.outgoing(JSON.stringify(obj));
170 this.socket.send(JSON.stringify(obj));
173 window.addEventListener('load', function () {
175 PRINT.init("result");
176 var server = new VehicleServer("ws://localhost:23000/vehicle");