From ebe88142310e7026c911f62c00d2598914900163 Mon Sep 17 00:00:00 2001 From: Ake Hedman Date: Thu, 12 May 2016 14:32:09 +0200 Subject: [PATCH] REST demo in place --- index.html | 26 +++++++- rest/index.html | 159 +++++++++++++++++++++++++++--------------------- rest/menu.js | 30 ++++----- testws/menu.js | 2 +- 4 files changed, 127 insertions(+), 90 deletions(-) diff --git a/index.html b/index.html index d7e0fdc5..d8788172 100644 --- a/index.html +++ b/index.html @@ -6,15 +6,32 @@ - + + + + + + + +
@@ -23,6 +40,9 @@


+ +
+

It works!

@@ -95,8 +115,8 @@

The important first test

Demo time

- When the login works as of above you can test the user interface components by - clicking on this link. + OK login works, click the demo button and start exploring +
diff --git a/rest/index.html b/rest/index.html index 90e4c5a7..1db3aac6 100644 --- a/rest/index.html +++ b/rest/index.html @@ -506,50 +506,48 @@ }; /////////////////////////////////////////////////////////////////////// - // do_readVariable + // do_sendMeasurement // - var do_readVariable = function() { + var do_sendMeasurement = function() { - var txtVariableName = window.prompt("Name of variable to read:","test"); + var txtValue = window.prompt("Send CLASS2.MEASUREMENT_FLOAT. Type=6 (temperature) i degrees Celsius:","23.98"); if ( VscpSessionKey.length > 0 ) { $.ajax({ url: VscpServer + '/vscp/rest?vscpsession=' + VscpSessionKey + - '&format=jsonp&op=readvar&variable=' + txtVariableName, + '&format=jsonp&op=measurement&value=' + txtValue + + '&guid=""' + // GUID (use GUID of interface) + '&type=6' + // Temperature + '&level=2' + // Level II + '&sensoridx=2' + // Sensor number 2 + '&unit=1' + // Unit = 1 Degrees Celsius + '&zone=22' + // Zone = 22 + '&subzone=33', // Sub Zone = 33 type : "GET", jsonpCallback: 'handler', cache: true, dataType: 'jsonp', success: function(response) { + // response will be a javascript // array of objects console.log("-----------------------------------------------------------"); - console.log(" do_readVariable"); + console.log(" do_sendMeasurement"); console.log("-----------------------------------------------------------"); console.log("Success = " + response.success ); console.log("Code = " + response.code ); console.log("Message = " + response.message ); console.log("Description = " + response.description ); console.log("Info = " + response.info ); - console.log("Variable name = " + response.varname ); - console.log("Variable value = " + response.varvalue ); - console.log("Variable type = " + response.vartype ); - console.log("Variable type code = " + response.vartypecode ); - console.log("Variable percistence = " + response.varpersistence ); if ( response.success ) { - $("#events").html( response.varname + " = " + response.varvalue + ", type = " + - response.vartype + ", typecode = " + response.vartypecode + - ", peristence = " + response.varpersistence ); - } - else { - $("#events").html( "Unable to read variable" ); - } + $("#events").html( "OK Variable written" ); + } }, error: function( xhr, status, error ) { - console.log( "do_readVariable:" + error + " Status:" + status ); + console.log( "Close:" + error + " Status:" + status ); } }); } @@ -560,19 +558,27 @@ }; /////////////////////////////////////////////////////////////////////// - // do_writeVariable + // do_createVariable // - var do_writeVariable = function() { - - var txtVariableName = window.prompt("Name of variable to write:","test"); - var txtVariableValue = window.prompt("New value:",""); + var do_createVariable = function() { + + var txtVariableName = window.prompt("Name of variable to create:","newvar"); + if ( null == txtVariableName ) return; + var txtVariableValue = window.prompt("Value:","This is a new string"); + if ( null == txtVariableValue ) return; + var txtVariableType = window.prompt("Type:","string"); + if ( null == txtVariableType ) return; + var txtVariablePersistent = window.prompt("Persistent (true/false):","false"); + if ( null == txtVariablePersistent ) return; if ( VscpSessionKey.length > 0 ) { $.ajax({ url: VscpServer + '/vscp/rest?vscpsession=' + VscpSessionKey + - '&format=jsonp&op=writevar&variable=' + txtVariableName + - '&value=' + txtVariableValue, + '&format=jsonp&op=createvar&variable=' + txtVariableName + + '&value=' + txtVariableValue + + '&type=' + txtVariableType + + '&persistent=' + txtVariablePersistent, type : "GET", jsonpCallback: 'handler', cache: true, @@ -581,7 +587,7 @@ // response will be a javascript // array of objects console.log("-----------------------------------------------------------"); - console.log(" do_writeVariable"); + console.log(" do_createVariable"); console.log("-----------------------------------------------------------"); console.log("Success = " + response.success ); console.log("Code = " + response.code ); @@ -606,27 +612,17 @@ }; /////////////////////////////////////////////////////////////////////// - // do_createVariable + // do_readVariable // - var do_createVariable = function() { - - var txtVariableName = window.prompt("Name of variable to create:","newvar"); - if ( null == txtVariableName ) return; - var txtVariableValue = window.prompt("Value:","This is a new string"); - if ( null == txtVariableValue ) return; - var txtVariableType = window.prompt("Type:","string"); - if ( null == txtVariableType ) return; - var txtVariablePersistent = window.prompt("Persistent (true/false):","false"); - if ( null == txtVariablePersistent ) return; + var do_readVariable = function() { + + var txtVariableName = window.prompt("Name of variable to read:","test"); if ( VscpSessionKey.length > 0 ) { $.ajax({ url: VscpServer + '/vscp/rest?vscpsession=' + VscpSessionKey + - '&format=jsonp&op=createvar&variable=' + txtVariableName + - '&value=' + txtVariableValue + - '&type=' + txtVariableType + - '&persistent=' + txtVariablePersistent, + '&format=jsonp&op=readvar&variable=' + txtVariableName, type : "GET", jsonpCallback: 'handler', cache: true, @@ -635,21 +631,31 @@ // response will be a javascript // array of objects console.log("-----------------------------------------------------------"); - console.log(" do_createVariable"); + console.log(" do_readVariable"); console.log("-----------------------------------------------------------"); console.log("Success = " + response.success ); console.log("Code = " + response.code ); console.log("Message = " + response.message ); console.log("Description = " + response.description ); console.log("Info = " + response.info ); + console.log("Variable name = " + response.varname ); + console.log("Variable value = " + response.varvalue ); + console.log("Variable type = " + response.vartype ); + console.log("Variable type code = " + response.vartypecode ); + console.log("Variable percistence = " + response.varpersistence ); if ( response.success ) { - $("#events").html( "OK Variable written" ); - } + $("#events").html( response.varname + " = " + response.varvalue + ", type = " + + response.vartype + ", typecode = " + response.vartypecode + + ", peristence = " + response.varpersistence ); + } + else { + $("#events").html( "Unable to read variable" ); + } }, error: function( xhr, status, error ) { - console.log( "Close:" + error + " Status:" + status ); + console.log( "do_readVariable:" + error + " Status:" + status ); } }); } @@ -660,18 +666,19 @@ }; /////////////////////////////////////////////////////////////////////// - // do_deleteVariable + // do_writeVariable // - var do_deleteVariable = function() { - - var txtVariableName = window.prompt("Name of variable to delete:","test"); - if ( null == txtVariableName ) return; + var do_writeVariable = function() { + + var txtVariableName = window.prompt("Name of variable to write:","test"); + var txtVariableValue = window.prompt("New value:",""); if ( VscpSessionKey.length > 0 ) { $.ajax({ url: VscpServer + '/vscp/rest?vscpsession=' + VscpSessionKey + - '&format=jsonp&op=deletevar&variable=' + txtVariableName, + '&format=jsonp&op=writevar&variable=' + txtVariableName + + '&value=' + txtVariableValue, type : "GET", jsonpCallback: 'handler', cache: true, @@ -680,7 +687,7 @@ // response will be a javascript // array of objects console.log("-----------------------------------------------------------"); - console.log(" do_deleteVariable"); + console.log(" do_writeVariable"); console.log("-----------------------------------------------------------"); console.log("Success = " + response.success ); console.log("Code = " + response.code ); @@ -689,7 +696,7 @@ console.log("Info = " + response.info ); if ( response.success ) { - $("#events").html( "OK Variable deleted" ); + $("#events").html( "OK Variable written" ); } }, @@ -704,35 +711,30 @@ }; + + /////////////////////////////////////////////////////////////////////// - // do_sendMeasurement + // do_deleteVariable // - var do_sendMeasurement = function() { - - var txtValue = window.prompt("Send CLASS2.MEASUREMENT_FLOAT. Type=6 (temperature) i degrees Celsius:","23.98"); + var do_deleteVariable = function() { + + var txtVariableName = window.prompt("Name of variable to delete:","test"); + if ( null == txtVariableName ) return; if ( VscpSessionKey.length > 0 ) { $.ajax({ url: VscpServer + '/vscp/rest?vscpsession=' + VscpSessionKey + - '&format=jsonp&op=measurement&value=' + txtValue + - '&guid=""' + // GUID (use GUID of interface) - '&type=6' + // Temperature - '&level=2' + // Level II - '&sensoridx=2' + // Sensor number 2 - '&unit=1' + // Unit = 1 Degrees Celsius - '&zone=22' + // Zone = 22 - '&subzone=33', // Sub Zone = 33 + '&format=jsonp&op=deletevar&variable=' + txtVariableName, type : "GET", jsonpCallback: 'handler', cache: true, dataType: 'jsonp', success: function(response) { - // response will be a javascript // array of objects console.log("-----------------------------------------------------------"); - console.log(" do_sendMeasurement"); + console.log(" do_deleteVariable"); console.log("-----------------------------------------------------------"); console.log("Success = " + response.success ); console.log("Code = " + response.code ); @@ -741,7 +743,7 @@ console.log("Info = " + response.info ); if ( response.success ) { - $("#events").html( "OK Variable written" ); + $("#events").html( "OK Variable deleted" ); } }, @@ -756,6 +758,8 @@ }; + + /* function getQueryParams(qs) { @@ -797,7 +801,14 @@
-

VSCP REST interface demo code

+

VSCP REST interface demo code

+ +

+ This demo demonstrates the VSCP daemon REST interface functionality. The VSCP daemon REST interface is documented + here and you can find the code for this example at + GitHub. +

+ +

+ Open a session and possible define a server (set Server) before you start to work with the other available menu items. +

+

+ Open the console log window of your browser to see status from this demo as it use log functionality for info about what is done under + the hood. +


Host:

Sessionkey:

diff --git a/rest/menu.js b/rest/menu.js index f2aadc5a..73ad57e9 100644 --- a/rest/menu.js +++ b/rest/menu.js @@ -50,7 +50,7 @@ navBarMenu.content = [ dropDown: [ { title: "Set Server", - url: "testauth.html" + url: "javascript:do_setServer();" }, { title: "Open REST session", @@ -62,39 +62,39 @@ navBarMenu.content = [ }, { title: "Get status", - url: "variable.html" + url: "javascript:do_status();" }, { - title: "Read on event", - url: "node_discovery.html" + title: "Read one event", + url: "javascript:do_readEventOne();" }, { title: "Read all events", - url: "slider.html" + url: "javascript:do_readEventAll();" }, { title: "Send event", - url: "progress.html" + url: "javascript:do_sendEvent();" }, { title: "Set filter", - url: "simpletext.html" + url: "javascript:do_setFilter();" }, { title: "Clear filter", - url: "thermometercelsius.html" + url: "javascript:do_clrFilter();" }, { title: "Clear event queue", - url: "lmsensors.html" + url: "javascript:do_clrQueue();" }, { title: "Send measurement", - url: "lmsensors.html" + url: "javascript:do_sendMeasurement();" }, { title: "Get table", - url: "lmsensors.html" + url: "javascript:alert('Not available yet');" }, ] }, @@ -104,19 +104,19 @@ navBarMenu.content = [ dropDown: [ { title: "Create variable", - url: "epoch_simple.html" + url: "javascript:do_createVariable();" }, { title: "Read variable", - url: "epoch_simple.html" + url: "javascript:do_readVariable();" }, { title: "Write variable", - url: "epoch_simple.html" + url: "javascript:do_writeVariable();" }, { title: "Delete variable", - url: "epoch_simple.html" + url: "javascript:do_deleteVariable();" }, ] }, diff --git a/testws/menu.js b/testws/menu.js index ea2c5dc1..0da0480a 100644 --- a/testws/menu.js +++ b/testws/menu.js @@ -42,7 +42,7 @@ var navBarMenu = navBarMenu || {}; navBarMenu.content = [ { title: "Home", - url: "index.html" + url: "../index.html" }, { title: "Widget & general demos",