added set functionality, the basics are in place for hooking up the backend service
authorTodd Brandt <tebrandt@frontier.com>
Wed, 22 Aug 2012 11:03:24 +0000 (04:03 -0700)
committerTodd Brandt <tebrandt@frontier.com>
Wed, 22 Aug 2012 11:03:24 +0000 (04:03 -0700)
plugins/websocketsink/test/api.js
plugins/websocketsink/test/events.js
plugins/websocketsink/test/server.js
plugins/websocketsink/test/style.css
plugins/websocketsink/test/test.js

index 71e085d..b4e2a8e 100644 (file)
@@ -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;
                 }
             }
index 422d0ce..d3efca0 100644 (file)
@@ -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;
+}
index 2d4fc44..54b5ee4 100644 (file)
@@ -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 = {
index 4302ade..49f83e4 100644 (file)
@@ -53,7 +53,7 @@
 .proptest .buttons {
     position: relative;
     float: right;
-    width: 240px;
+    width: 290px;
 }
 
 .testbutton {
     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));
index f041e93..db4f8e2 100644 (file)
@@ -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)+":<br>"+msg);
+        }
+    );
+}
+
 function start(msg)
 {
     if(window.vehicle && window.vehicle.getSupportedEventTypes)
@@ -112,13 +132,15 @@ function start(msg)
     var part1 = '<div class="proptest">';
     var part2 = '<div class="buttons"><div class="testbutton types" onclick=getTypes("';
     var part3 = '")></div><div class="testbutton get" onclick=getValue("'
-    var part4 = '")></div></div></div>';
+    var part4 = '")></div><div class="testbutton set" onclick=setValue("'
+    var part5 = '")></div></div></div>';
     var events = vehicleEventType.event;
-    var html = part1 + "all events" + part2 + part3 + part4;
+    var html = part1 + "all events" + part2 + part3 + part4 + part5;
     for(i in events)
     {
         html += part1 + events[i] + part2 + events[i] + 
-                part3 + events[i] + part4;
+                part3 + events[i] + part4 + events[i] +
+                part5;
     }
     tester.innerHTML = html;
 }