-/*\r
- * Copyright (c) 2012, Intel Corporation.\r
- *\r
- * This program is licensed under the terms and conditions of the\r
- * Apache License, version 2.0. The full text of the Apache License is at\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- */\r
-\r
-var vehicle;\r
-var PRINT = {\r
- moduleName : "IVI API",\r
- logElement : null,\r
- init : function(log_id) {\r
- this.logElement = document.getElementById(log_id);\r
- },\r
-\r
- setModule : function(moduleName) {\r
- this.moduleName = moduleName;\r
- },\r
-\r
- pass : function(msg) {\r
- this.logElement.innerHTML += "<div class='PassClass'> " + this.moduleName + " : Pass : " + msg + "</div>";\r
- },\r
-\r
- fail : function(msg) {\r
- this.logElement.innerHTML += "<div class='FailClass'> " + this.moduleName + " : Fail : " + msg + "</div>";\r
- },\r
-\r
- log : function(msg) {\r
- this.logElement.innerHTML += "<div class='LogClass'> " + msg + "</div>";\r
- },\r
-}\r
-\r
-var myJSONObject = {\r
- "type" : "method",\r
- "name": "GetProperty",\r
- "Arguments": [\r
- "Velocity"\r
- ],\r
- "transactionid": "0f234002-95b8-48ac-aa06-cb49e372cc1c"\r
-};\r
-\r
-function Vehicle()\r
-{\r
- var self = this;\r
- \r
- function init() {\r
- if ("WebSocket" in window)\r
- {\r
- PRINT.pass("The browser is websocket capable");\r
- \r
- this.socket = new WebSocket("ws://localhost:7681","http-only");\r
- this.socket.onopen = function()\r
- {\r
- PRINT.pass("Connection OPEN");\r
- this.send(JSON.stringify(myJSONObject));\r
- };\r
- this.socket.onmessage = function (e) \r
- {\r
- self.receive(e.data);\r
- };\r
- this.socket.onclose = function(e)\r
- {\r
- PRINT.fail("Connection CLOSED: " + e.reason + " code: " + e.code);\r
- };\r
- this.socket.onerror = function(evt) {\r
- alert(evt);\r
- }\r
- }\r
- else\r
- {\r
- PRINT.fail("This browser doesn't ppear to support websockets!");\r
- }\r
- }\r
- init();\r
-}\r
-\r
-Vehicle.prototype.send = function(msg)\r
-{\r
-\r
-}\r
-\r
-Vehicle.prototype.receive = function(msg)\r
-{\r
- PRINT.log("Message Received: "+msg);\r
- var data = JSON.parse(msg);\r
- console.log(data);\r
-}\r
-\r
-function init() {\r
- PRINT.init("result");\r
- vehicle = new Vehicle();\r
-}\r
+/*
+ * Copyright (c) 2012, Intel Corporation.
+ *
+ * This program is licensed under the terms and conditions of the
+ * Apache License, version 2.0. The full text of the Apache License is at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ */
+
+/* ------------------------ vehicle interface ----------------------------- */
+
+function Vehicle(socketUrl, sCB, eCB, tout)
+{
+ var self = this;
+ this.successCB = sCB;
+ this.errorCB = eCB;
+ this.transactionid = 0;
+ this.methodCalls = [];
+ this.timeouttime = (tout == undefined)?2000:tout;
+ this.retries = 5;
+ this.connected = false;
+
+ this.VehicleMethodCall = function(n, sCB, eCB)
+ {
+ var me = this;
+ this.name = n;
+ this.successCB = sCB;
+ this.errorCB = eCB;
+ this.transactionid = self.transactionid++;
+
+ this.send = function(msg)
+ {
+ var errCB = (me.errorCB == undefined)?self.errorCB:me.errorCB;
+ if(!self.connected)
+ {
+ if(errCB != undefined)
+ {
+ errCB("\""+me.name+"\" method failed because socket is closed");
+ }
+ self.methodCalls.pop(this);
+ }
+ me.timeout = setTimeout(function(){
+ if(errCB != undefined)
+ {
+ errCB("\""+me.name+"\" method timed out after "+self.timeouttime+"ms");
+ }
+ self.methodCalls.pop(this);
+ }, self.timeouttime);
+ self.socket.send(msg);
+ }
+ this.done = function()
+ {
+ if(me.timeout != undefined)
+ {
+ clearTimeout(me.timeout);
+ }
+ }
+ }
+
+ function init() {
+ if ("WebSocket" in window)
+ {
+ self.socket = new WebSocket(socketUrl);
+ self.socket.onopen = function()
+ {
+ self.connected = true;
+ this.send("client");
+ self.successCB((self.retries < 5)?"(RECONNECTED)":"");
+ self.retries = 5;
+ };
+ self.socket.onclose = function()
+ {
+ self.connected = false;
+ self.errorCB("socket closed "+((self.retries > 0)?"retrying in 5 seconds ...":""));
+ if(self.retries > 0)
+ {
+ setTimeout(function(){
+ self.retries--;
+ init();
+ }, 5000);
+ }
+ };
+ self.socket.onerror = function(e)
+ {
+ self.errorCB(e.data);
+ };
+ self.socket.onmessage = function (e)
+ {
+ self.receive(e.data);
+ };
+ }
+ else
+ {
+ console.log("This browser doesn't appear to support websockets!");
+ }
+ }
+ init();
+}
+
+Vehicle.prototype.getSupportedEventTypes = function(type, writeable, successCB, errorCB)
+{
+ var call = new this.VehicleMethodCall("getSupportedEventTypes", successCB, errorCB);
+ this.methodCalls.push(call);
+ var obj = {
+ "type" : "method",
+ "name": "getSupportedEventTypes",
+ "transactionid" : call.transactionid,
+ "data" : type
+ };
+ call.send(JSON.stringify(obj));
+}
+
+Vehicle.prototype.get = function(type, successCB, errorCB)
+{
+ var call = new this.VehicleMethodCall("get", successCB, errorCB);
+ this.methodCalls.push(call);
+ var obj = {
+ "type" : "method",
+ "name": "get",
+ "transactionid" : call.transactionid,
+ "data" : type
+ };
+ call.send(JSON.stringify(obj));
+}
+
+Vehicle.prototype.receive = function(msg)
+{
+ var self = this;
+ var event;
+ try {
+ event = JSON.parse(msg);
+ }
+ catch(e) {
+ self.errCB("GARBAGE MESSAGE: "+msg);
+ return;
+ }
+
+ if((event == undefined)||(event.type == undefined)||
+ (event.name == undefined)||(event.transactionid == undefined))
+ {
+ self.errCB("BADLY FORMED MESSAGE: "+msg);
+ return;
+ }
+ else
+ {
+ if(event.type === "methodReply")
+ {
+ var calls = this.methodCalls;
+ for(i in calls)
+ {
+ var call = calls[i];
+ if((call.transactionid == event.transactionid)&&
+ (call.name === event.name))
+ {
+ call.done();
+ if(event.error != undefined)
+ {
+ call.errorCB(event.error);
+ }
+ else
+ {
+ call.successCB(event.data);
+ }
+ this.methodCalls.pop(call);
+ return;
+ }
+ }
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2012, Intel Corporation.
+ *
+ * This program is licensed under the terms and conditions of the
+ * Apache License, version 2.0. The full text of the Apache License is at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ */
+
+/* ---------------------- vehicle event typedef --------------------------- */
+
+function VehicleEventType()
+{
+ this.event = [
+ "vehicle_info",
+ "vehicle_info_wmi",
+ "vehicle_info_vin",
+ "vehicle_info_vehicle_type",
+ "vehicle_info_door_type",
+ "vehicle_info_door_type_1st_row",
+ "vehicle_info_door_type_2nd_row",
+ "vehicle_info_door_type_3rd_row",
+ "vehicle_info_fuel_type",
+ "vehicle_info_transmission_gear_type",
+ "vehicle_info_wheel_info",
+ "vehicle_info_wheel_info_radius",
+ "vehicle_info_wheel_info_track",
+ "running_status",
+ "running_status_vehicle_power_mode",
+ "running_status_speedometer",
+ "running_status_engine_speed",
+ "running_status_trip_meter",
+ "running_status_trip_meter_1",
+ "running_status_trip_meter_2",
+ "running_status_trip_meter_1_mileage",
+ "running_status_trip_meter_2_mileage",
+ "running_status_trip_meter_1_average_speed",
+ "running_status_trip_meter_2_average_speed",
+ "running_status_trip_meter_1_fuel_consumption",
+ "running_status_trip_meter_2_fuel_consumption",
+ "running_status_transmission_gear_status",
+ "running_status_cruise_control",
+ "running_status_cruise_control_status",
+ "running_status_cruise_control_speed",
+ "running_status_wheel_brake",
+ "running_status_lights_status",
+ "running_status_lights_status_head",
+ "running_status_lights_status_high_beam",
+ "running_status_lights_status_turn_left",
+ "running_status_lights_status_turn_right",
+ "running_status_lights_status_brake",
+ "running_status_lights_status_fog_front",
+ "running_status_lights_status_fog_rear",
+ "running_status_lights_status_hazard",
+ "running_status_lights_status_parking",
+ "running_status_interior_lights_status",
+ "running_status_interior_lights_status_driver",
+ "running_status_interior_lights_status_passenger",
+ "running_status_interior_lights_status_center",
+ "running_status_automatic_headlights",
+ "running_status_dynamic_high_beam",
+ "running_status_horn",
+ "running_status_chime",
+ "running_status_fuel",
+ "running_status_estimated_range",
+ "running_status_engine_oil",
+ "running_status_engine_oil_remaining",
+ "running_status_engine_oil_change",
+ "running_status_engine_oil_temp",
+ "running_status_engine_coolant",
+ "running_status_engine_coolant_level",
+ "running_status_engine_coolant_temp",
+ "running_status_steering_wheel_angle",
+ "maintenance",
+ "maintenance_odometer",
+ "maintenance_transmission_oil",
+ "maintenance_transmission_oil_life_level",
+ "maintenance_transmission_oil_temp",
+ "maintenance_brake_fluid_level",
+ "maintenance_washer_fluid_level",
+ "maintenance_malfunction_indicator_lamp",
+ "maintenance_battery",
+ "maintenance_battery_voltage",
+ "maintenance_battery_current",
+ "maintenance_tire_pressure",
+ "maintenance_tire_pressure_front_left",
+ "maintenance_tire_pressure_front_right",
+ "maintenance_tire_pressure_rear_left",
+ "maintenance_tire_pressure_rear_right",
+ "maintenance_tire_pressure_status",
+ "maintenance_tire_pressure_status_front_left",
+ "maintenance_tire_pressure_status_front_right",
+ "maintenance_tire_pressure_status_rear_left",
+ "maintenance_tire_pressure_status_rear_right",
+ "personalization",
+ "personalization_key_id",
+ "personalization_language",
+ "personalization_measurement_system",
+ "personalization_measurement_system_string",
+ "personalization_measurement_system_string_fuel",
+ "personalization_measurement_system_string_distance",
+ "personalization_measurement_system_string_speed",
+ "personalization_measurement_system_string_consumption",
+ "personalization_mirror",
+ "personalization_mirror_driver",
+ "personalization_mirror_passenger",
+ "personalization_mirror_inside",
+ "personalization_steering_wheel_position",
+ "personalization_steering_wheel_position_slide",
+ "personalization_steering_wheel_position_tilt",
+ "personalization_driving_mode",
+ "personalization_driver_seat_position",
+ "personalization_driver_seat_position_recline_seatback",
+ "personalization_driver_seat_position_slide",
+ "personalization_driver_seat_position_cushion_height",
+ "personalization_driver_seat_position_headrest",
+ "personalization_driver_seat_position_back_cushion",
+ "personalization_driver_seat_position_side_cushion",
+ "personalization_passenger_seat_position",
+ "personalization_passenger_seat_position_recline_seatback",
+ "personalization_passenger_seat_position_slide",
+ "personalization_passenger_seat_position_cushion_height",
+ "personalization_passenger_seat_position_headrest",
+ "personalization_passenger_seat_position_back_cushion",
+ "personalization_passenger_seat_position_side_cushion",
+ "personalization_dashboard_illumination",
+ "personalization_generated_vehicle_sound_mode",
+ "driving_safety",
+ "driving_safety_antilock_braking_system",
+ "driving_safety_traction_control_system",
+ "driving_safety_electronic_stability_control",
+ "driving_safety_vehicle_top_speed_limit",
+ "driving_safety_airbag_status",
+ "driving_safety_airbag_status_driver",
+ "driving_safety_airbag_status_passenger",
+ "driving_safety_airbag_status_side",
+ "driving_safety_door_open_status",
+ "driving_safety_door_open_status_driver",
+ "driving_safety_door_open_status_passenger",
+ "driving_safety_door_open_status_rear_left",
+ "driving_safety_door_open_status_rear_right",
+ "driving_safety_door_open_status_trunk",
+ "driving_safety_door_open_status_fuel_filter_cap",
+ "driving_safety_door_open_status_hood",
+ "driving_safety_door_lock_status",
+ "driving_safety_door_lock_status_driver",
+ "driving_safety_door_lock_status_passenger",
+ "driving_safety_door_lock_status_rear_left",
+ "driving_safety_door_lock_status_rear_right",
+ "driving_safety_child_safety_lock",
+ "driving_safety_occupants_status",
+ "driving_safety_occupants_status_driver",
+ "driving_safety_occupants_status_passenger",
+ "driving_safety_occupants_status_rear_left",
+ "driving_safety_occupants_status_rear_right",
+ "driving_safety_seat_belt",
+ "driving_safety_seat_belt_driver",
+ "driving_safety_seat_belt_passenger",
+ "driving_safety_seat_belt_rear_left",
+ "driving_safety_seat_belt_rear_right",
+ "driving_safety_window_lock",
+ "driving_safety_window_lock_driver",
+ "driving_safety_window_lock_passenger",
+ "driving_safety_window_lock_rear_left",
+ "driving_safety_window_lock_rear_right",
+ "driving_safety_obstacle_distance",
+ "driving_safety_obstacle_distance_sensor_status",
+ "driving_safety_obstacle_distance_front_center",
+ "driving_safety_obstacle_distance_rear_center",
+ "driving_safety_obstacle_distance_front_left",
+ "driving_safety_obstacle_distance_front_right",
+ "driving_safety_obstacle_distance_middle_left",
+ "driving_safety_obstacle_distance_middle_right",
+ "driving_safety_obstacle_distance_rear_left",
+ "driving_safety_obstacle_distance_rear_right",
+ "driving_safety_front_collision_detection",
+ "driving_safety_front_collision_detection_status",
+ "driving_safety_front_collision_detection_distance",
+ "driving_safety_front_collision_detection_time",
+ "vision_system",
+ "vision_system_lane_departure_detection_status",
+ "vision_system_lane_departed",
+ "parking",
+ "parking_security_alert",
+ "parking_parking_brake",
+ "parking_parking_lights",
+ "climate_environment_interior_temp",
+ "climate_environment_exterior_temp",
+ "climate_environment_exterior_brightness",
+ "climate_environment_rain_sensor",
+ "climate_environment_windshield_wiper",
+ "climate_environment_rear_wiper",
+ "climate_environment_hvac_fan",
+ "climate_environment_hvac_fan_direction",
+ "climate_environment_hvac_fan_speed",
+ "climate_environment_hvac_fan_target_temp",
+ "climate_environment_air_conditioning",
+ "climate_environment_air_recirculation",
+ "climate_environment_heater",
+ "climate_environment_defrost",
+ "climate_environment_defrost_windshield",
+ "climate_environment_defrost_rear_window",
+ "climate_environment_defrost_side_mirrors",
+ "climate_environment_steering_wheel_heater",
+ "climate_environment_seat_heater",
+ "climate_environment_seat_cooler",
+ "climate_environment_window",
+ "climate_environment_window_driver",
+ "climate_environment_window_passenger",
+ "climate_environment_window_rear_left",
+ "climate_environment_window_rear_right",
+ "climate_environment_sunroof",
+ "climate_environment_sunroof_openness",
+ "climate_environment_sunroof_tilt",
+ "climate_environment_convertible_roof"
+ ];
+ this.value = [];
+
+ /* set random initial values for all the props */
+ for(i in this.event)
+ {
+ var prop = this.event[i];
+ this.value[prop] = Math.floor(Math.random() * 1000000);
+ }
+}
+
+VehicleEventType.prototype.getSupportedEventList = function(val)
+{
+ /* for undefined just assume everything */
+ if((val == undefined)||(val === ""))
+ return this.event;
+
+ /* grab every event with case insensitive prefix of val */
+ var value = val.toLowerCase();
+ var list = [];
+ for(i in this.event)
+ {
+ var prop = this.event[i].toLowerCase();
+ if(prop.indexOf(value) === 0)
+ {
+ list[list.length] = prop;
+ }
+ }
+
+ /* if the target val isn't alone, remove it, it's a grouping */
+ var idx = list.indexOf(value);
+ if((idx >= 0)&&(list.length > 1))
+ {
+ list.splice(idx, 1);
+ }
+ return list;
+}
+
+VehicleEventType.prototype.getValueEventList = function(val)
+{
+ var i, j, list = this.getSupportedEventList(val);
+ for(i = 0; i < list.length; i++)
+ {
+ for(j = i + 1; j < list.length; j++)
+ {
+ if(list[j].indexOf(list[i]) === 0)
+ {
+ list.splice(i, 1);
+ i--;
+ }
+ }
+ }
+ return list;
+}
+
+VehicleEventType.prototype.getValue = function(prop)
+{
+ return this.value[prop];
+}
--- /dev/null
+/*
+ * Copyright (c) 2012, Intel Corporation.
+ *
+ * This program is licensed under the terms and conditions of the
+ * Apache License, version 2.0. The full text of the Apache License is at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ */
+
+/* --------------------------- utility code ------------------------------- */
+
+var PRINT = {
+ logElement : null,
+ init : function(log_id) {
+ this.logElement = document.getElementById(log_id);
+ },
+
+ scrollToBottom : function() {
+ this.logElement.scrollTop = this.logElement.scrollHeight;
+ },
+
+ incoming : function(msg) {
+ this.logElement.innerHTML += "<div style='color: blue'> REQUEST: " + msg + "</div>";
+ this.scrollToBottom();
+ },
+
+ outgoing : function(msg) {
+ this.logElement.innerHTML += "<div style='color: purple'> RESPONSE: " + msg + "</div>";
+ this.scrollToBottom();
+ },
+
+ pass : function(msg) {
+ this.logElement.innerHTML += "<div style='color: green'> SUCCESS: " + msg + "</div>";
+ this.scrollToBottom();
+ },
+
+ fail : function(msg) {
+ this.logElement.innerHTML += "<div style='color: red'> FAIL: " + msg + "</div>";
+ this.scrollToBottom();
+ },
+
+ log : function(msg) {
+ this.logElement.innerHTML += "<div class='LogClass'> " + msg + "</div>";
+ this.scrollToBottom();
+ },
+}
+
+/* ----------------------------- test code --------------------------------- */
+
+function VehicleServer(socketUrl)
+{
+ var self = this;
+ this.vehicleEventType = new VehicleEventType();
+
+ function init() {
+ if ("WebSocket" in window)
+ {
+ self.socket = new WebSocket(socketUrl);
+ self.socket.onopen = function()
+ {
+ PRINT.pass("Server READY");
+ this.send("server");
+ };
+ self.socket.onclose = function()
+ {
+ PRINT.fail("Server CLOSED");
+ };
+ self.socket.onerror = function(e)
+ {
+ PRINT.fail("Server ERROR: "+e.data);
+ };
+ self.socket.onmessage = function (e)
+ {
+ self.receive(e.data);
+ };
+ }
+ else
+ {
+ PRINT.fail("This browser doesn't appear to support websockets!");
+ }
+ }
+ init();
+}
+
+VehicleServer.prototype.receive = function(msg)
+{
+ var event = JSON.parse(msg);
+ /* accept only methods with transaction ids */
+ if((event == undefined)||(event.transactionid == undefined)||
+ (event.type != "method"))
+ {
+ return;
+ }
+
+ var obj;
+ PRINT.incoming(msg);
+ if(event.name === "getSupportedEventTypes")
+ {
+ obj = {
+ "type" : "methodReply",
+ "name": event.name,
+ "transactionid" : event.transactionid,
+ "data" : this.vehicleEventType.getSupportedEventList(event.data)
+ };
+ }
+ else if(event.name === "get")
+ {
+ var names = this.vehicleEventType.getValueEventList(event.data);
+ if(names.length > 0)
+ {
+ obj = {
+ "type" : "methodReply",
+ "name": event.name,
+ "transactionid" : event.transactionid,
+ "data" : []
+ };
+ for(i in names)
+ {
+ var value = this.vehicleEventType.getValue(names[i]);
+ obj.data.push({"name" : names[i], "value" : value});
+ }
+ }
+ else
+ {
+ obj = {
+ "type" : "methodReply",
+ "name": event.name,
+ "transactionid" : event.transactionid,
+ "error" : event.data + " is not a valid event"
+ };
+ }
+ }
+ else
+ {
+ obj = {
+ "type" : "methodReply",
+ "name": event.name,
+ "transactionid" : event.transactionid,
+ "error" : event.name + " is not a valid method"
+ };
+ }
+ PRINT.outgoing(JSON.stringify(obj));
+ this.socket.send(JSON.stringify(obj));
+}
+
+window.addEventListener('load', function () {
+ "use strict";
+ PRINT.init("result");
+ var server = new VehicleServer("ws://localhost:23000/vehicle");
+});