From 0d81c5d6aee9946479dfffd1ee40691aad66eb04 Mon Sep 17 00:00:00 2001 From: Todd Brandt Date: Wed, 22 Aug 2012 04:03:24 -0700 Subject: [PATCH] added set functionality, the basics are in place for hooking up the backend service --- plugins/websocketsink/test/api.js | 115 +++++++++++++++++++++-------------- plugins/websocketsink/test/events.js | 11 ++++ plugins/websocketsink/test/server.js | 29 ++++++++- plugins/websocketsink/test/style.css | 10 ++- plugins/websocketsink/test/test.js | 28 ++++++++- 5 files changed, 142 insertions(+), 51 deletions(-) diff --git a/plugins/websocketsink/test/api.js b/plugins/websocketsink/test/api.js index 71e085d..b4e2a8e 100644 --- a/plugins/websocketsink/test/api.js +++ b/plugins/websocketsink/test/api.js @@ -9,51 +9,48 @@ /* ------------------------ vehicle interface ----------------------------- */ -function Vehicle(socketUrl, sCB, eCB, tout) +function Vehicle(socketUrl, sCB, eCB, calltimeout) { var self = this; - this.successCB = sCB; - this.errorCB = eCB; - this.transactionid = 0; - this.methodCalls = []; - this.timeouttime = (tout == undefined)?2000:tout; + this.iSuccessCB = sCB; + this.iErrorCB = eCB; this.retries = 5; this.connected = false; + this.transactionid = 0; + this.methodCalls = []; + this.methodIdx = 0; + this.timeouttime = (calltimeout == undefined)?5000:calltimeout; - this.VehicleMethodCall = function(n, sCB, eCB) + for(var i = 0; i < 100; i++) { - var me = this; - this.name = n; - this.successCB = sCB; - this.errorCB = eCB; - this.transactionid = self.transactionid++; + this.methodCalls[i] = null; + } - this.send = function(msg) + this.VehicleMethodCall = function(id, name, successCB, errorCB) + { + var me = this; + this.successCB = successCB; + this.errorCB = errorCB; + this.transactionid = id; + this.name = name; + this.done = false; + this.start = function() { - 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) + if(me.errorCB != undefined) { - errCB("\""+me.name+"\" method timed out after "+self.timeouttime+"ms"); + me.errorCB("\""+me.name+"\" method timed out after "+self.timeouttime+"ms"); } - self.methodCalls.pop(this); + me.finish(); }, self.timeouttime); - self.socket.send(msg); } - this.done = function() + this.finish = function() { if(me.timeout != undefined) { clearTimeout(me.timeout); } + me.done = true; } } @@ -65,13 +62,13 @@ function Vehicle(socketUrl, sCB, eCB, tout) { self.connected = true; this.send("client"); - self.successCB((self.retries < 5)?"(RECONNECTED)":""); + self.iSuccessCB((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 ...":"")); + self.iErrorCB("socket closed "+((self.retries > 0)?"retrying in 5 seconds ...":"")); if(self.retries > 0) { setTimeout(function(){ @@ -82,7 +79,7 @@ function Vehicle(socketUrl, sCB, eCB, tout) }; self.socket.onerror = function(e) { - self.errorCB(e.data); + self.iErrorCB(e.data); }; self.socket.onmessage = function (e) { @@ -97,30 +94,58 @@ function Vehicle(socketUrl, sCB, eCB, tout) init(); } +Vehicle.prototype.send = function(obj, successCB, errorCB) +{ + obj.transactionid = this.transactionid++; + if(!this.connected) + { + if(errorCB != undefined) + { + errorCB("\""+obj.name+"\" method failed because socket is closed"); + } + return; + } + var i = this.methodIdx; + this.methodIdx = (this.methodIdx + 1)%100; + this.methodCalls[i] = new this.VehicleMethodCall(obj.transactionid, + obj.name, successCB, errorCB); + this.socket.send(JSON.stringify(obj)); + this.methodCalls[i].start(); +} + + 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, + "name" : "getSupportedEventTypes", + "writeable" : writeable, + "transactionid" : 0, "data" : type }; - call.send(JSON.stringify(obj)); + this.send(obj, successCB, errorCB); } 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, + "transactionid" : 0, "data" : type }; - call.send(JSON.stringify(obj)); + this.send(obj, successCB, errorCB); +} + +Vehicle.prototype.set = function(type, value, successCB, errorCB) +{ + var obj = { + "type" : "method", + "name": "set", + "transactionid" : 0, + "data" : {"property" : type, "value" : value} + }; + this.send(obj, successCB, errorCB); } Vehicle.prototype.receive = function(msg) @@ -131,14 +156,14 @@ Vehicle.prototype.receive = function(msg) event = JSON.parse(msg); } catch(e) { - self.errCB("GARBAGE MESSAGE: "+msg); + self.iErrorCB("GARBAGE MESSAGE: "+msg); return; } if((event == undefined)||(event.type == undefined)|| (event.name == undefined)||(event.transactionid == undefined)) { - self.errCB("BADLY FORMED MESSAGE: "+msg); + self.iErrorCB("BADLY FORMED MESSAGE: "+msg); return; } else @@ -146,13 +171,12 @@ Vehicle.prototype.receive = function(msg) if(event.type === "methodReply") { var calls = this.methodCalls; - for(i in calls) + for(var i = 0; i < calls.length; i++) { var call = calls[i]; - if((call.transactionid == event.transactionid)&& - (call.name === event.name)) + if(call&&(!call.done)&&(call.transactionid === event.transactionid)) { - call.done(); + call.finish(); if(event.error != undefined) { call.errorCB(event.error); @@ -161,7 +185,6 @@ Vehicle.prototype.receive = function(msg) { call.successCB(event.data); } - this.methodCalls.pop(call); return; } } diff --git a/plugins/websocketsink/test/events.js b/plugins/websocketsink/test/events.js index 422d0ce..d3efca0 100644 --- a/plugins/websocketsink/test/events.js +++ b/plugins/websocketsink/test/events.js @@ -268,7 +268,18 @@ VehicleEventType.prototype.getValueEventList = function(val) return list; } +VehicleEventType.prototype.isValueEvent = function(val) +{ + var list = this.getValueEventList(val); + return(list.length === 1); +} + VehicleEventType.prototype.getValue = function(prop) { return this.value[prop]; } + +VehicleEventType.prototype.setValue = function(prop, newval) +{ + this.value[prop] = newval; +} diff --git a/plugins/websocketsink/test/server.js b/plugins/websocketsink/test/server.js index 2d4fc44..54b5ee4 100644 --- a/plugins/websocketsink/test/server.js +++ b/plugins/websocketsink/test/server.js @@ -96,11 +96,20 @@ VehicleServer.prototype.receive = function(msg) PRINT.incoming(msg); if(event.name === "getSupportedEventTypes") { + var data; + if(event.writeable) + { + data = this.vehicleEventType.getValueEventList(event.data); + } + else + { + data = this.vehicleEventType.getSupportedEventList(event.data); + } obj = { "type" : "methodReply", "name": event.name, "transactionid" : event.transactionid, - "data" : this.vehicleEventType.getSupportedEventList(event.data) + "data" : data }; } else if(event.name === "get") @@ -130,6 +139,24 @@ VehicleServer.prototype.receive = function(msg) }; } } + else if(event.name === "set") + { + if(this.vehicleEventType.isValueEvent(event.data.property)) + { + obj = event; + obj.type = "methodReply"; + this.vehicleEventType.setValue(event.data.property, event.data.value); + } + else + { + obj = { + "type" : "methodReply", + "name": event.name, + "transactionid" : event.transactionid, + "error" : event.data.property + " is not a writeable event" + }; + } + } else { obj = { diff --git a/plugins/websocketsink/test/style.css b/plugins/websocketsink/test/style.css index 4302ade..49f83e4 100644 --- a/plugins/websocketsink/test/style.css +++ b/plugins/websocketsink/test/style.css @@ -53,7 +53,7 @@ .proptest .buttons { position: relative; float: right; - width: 240px; + width: 290px; } .testbutton { @@ -93,6 +93,14 @@ content: 'get'; } +.testbutton.set { + width: 40px; +} + +.testbutton.set:after { + content: 'set'; +} + .testbutton:active { color: #666; background: -webkit-gradient(linear, left top, left bottom, from(#000), to(#444)); diff --git a/plugins/websocketsink/test/test.js b/plugins/websocketsink/test/test.js index f041e93..db4f8e2 100644 --- a/plugins/websocketsink/test/test.js +++ b/plugins/websocketsink/test/test.js @@ -95,6 +95,26 @@ function getValue(event) ); } +function setValue(event) +{ + var types = window.vehicle.set(event, 23, + function(data) { + PRINT.clear(); + if(data && data.property && data.value) + { + PRINT.pass(data.property+" value changes to "+data.value); + } + else + { + PRINT.fail("bad response for set "+event); + } + }, + function(msg) { + PRINT.fail(((event === "")?"all events":event)+":
"+msg); + } + ); +} + function start(msg) { if(window.vehicle && window.vehicle.getSupportedEventTypes) @@ -112,13 +132,15 @@ function start(msg) var part1 = '
'; var part2 = '