05.05.2016, 22:43
(This post was last modified: 05.05.2016, 22:52 by Erwin van der Zwart.)
Hi Pawel,
I created a small HTTP API (as .lp) for you to save and get data from storage from javascript:
1) Create a LUA script and run once to create the storage.lp file that will act as HTTP API:
dst = '/www/user/storage.lp'
io.writefile(dst, [[<?
require('apps')
require('socket.url')
action = socket.url.unescape( getvar('action') or 'none' )
storagename = socket.url.unescape( getvar('storagename') or 'none')
format = socket.url.unescape( getvar('format') or 'string' )
storagevalue = socket.url.unescape( getvar('value') or '{}')
if action == 'storageset' then
if storagename ~= 'none' then
if format == 'json' then
storagevalue = json.decode(storagevalue)
end
storage.set(storagename, storagevalue)
print('value saved to storage as ' .. format)
end
elseif action == 'storageget' then
if storagename ~= 'none' then
storagevalue = storage.get(storagename)
storagevalue = json.encode(storagevalue)
print(storagevalue)
end
end
?>]])
script.disable(_SCRIPTNAME)
2) Put this in your custom javascript:
$(function() {
// Save json data to storage with name 'mystoragename1'
var jsondata = {"name":"Erwin van der Zwart","company":"Schneider Electric","country":"The Netherlands"}
jsondata = JSON.stringify(jsondata);
$.get( "/user/storage.lp?action=storageset&storagename=mystoragename1&format=json&value=" + jsondata, function( data ) {
res = data.trim();
console.log(res);
});
// Get json data from storage with name 'mystoragename1'
$.get( "/user/storage.lp?action=storageget&storagename=mystoragename1", function( data ) {
res = data.trim();
res = JSON.parse(res);
console.log(res);
});
// Save string data to storage with name 'mystoragename2'
var stringdata = "Erwin van der Zwart Schneider Electric The Netherlands"
$.get( "/user/storage.lp?action=storageset&storagename=mystoragename2&format=string&value=" + stringdata, function( data ) {
res = data.trim();
console.log(res);
});
// Get string data from storage with name 'mystoragename2'
$.get( "/user/storage.lp?action=storageget&storagename=mystoragename2", function( data ) {
res = data.trim();
console.log(res);
});
});
By changing the action / storagename / format / value in the URL you can set and get any value from storage with this script.
BR,
Erwin
I created a small HTTP API (as .lp) for you to save and get data from storage from javascript:
1) Create a LUA script and run once to create the storage.lp file that will act as HTTP API:
dst = '/www/user/storage.lp'
io.writefile(dst, [[<?
require('apps')
require('socket.url')
action = socket.url.unescape( getvar('action') or 'none' )
storagename = socket.url.unescape( getvar('storagename') or 'none')
format = socket.url.unescape( getvar('format') or 'string' )
storagevalue = socket.url.unescape( getvar('value') or '{}')
if action == 'storageset' then
if storagename ~= 'none' then
if format == 'json' then
storagevalue = json.decode(storagevalue)
end
storage.set(storagename, storagevalue)
print('value saved to storage as ' .. format)
end
elseif action == 'storageget' then
if storagename ~= 'none' then
storagevalue = storage.get(storagename)
storagevalue = json.encode(storagevalue)
print(storagevalue)
end
end
?>]])
script.disable(_SCRIPTNAME)
2) Put this in your custom javascript:
$(function() {
// Save json data to storage with name 'mystoragename1'
var jsondata = {"name":"Erwin van der Zwart","company":"Schneider Electric","country":"The Netherlands"}
jsondata = JSON.stringify(jsondata);
$.get( "/user/storage.lp?action=storageset&storagename=mystoragename1&format=json&value=" + jsondata, function( data ) {
res = data.trim();
console.log(res);
});
// Get json data from storage with name 'mystoragename1'
$.get( "/user/storage.lp?action=storageget&storagename=mystoragename1", function( data ) {
res = data.trim();
res = JSON.parse(res);
console.log(res);
});
// Save string data to storage with name 'mystoragename2'
var stringdata = "Erwin van der Zwart Schneider Electric The Netherlands"
$.get( "/user/storage.lp?action=storageset&storagename=mystoragename2&format=string&value=" + stringdata, function( data ) {
res = data.trim();
console.log(res);
});
// Get string data from storage with name 'mystoragename2'
$.get( "/user/storage.lp?action=storageget&storagename=mystoragename2", function( data ) {
res = data.trim();
console.log(res);
});
});
By changing the action / storagename / format / value in the URL you can set and get any value from storage with this script.
BR,
Erwin