This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

Application Store documentation and sample app
#15
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
Reply


Messages In This Thread
RE: Application Store documentation and sample app - by Erwin van der Zwart - 05.05.2016, 22:43

Forum Jump: