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 that you accept these cookies being set.

Lua webrequest library (FW 2022 and later)
#1
webrequest library (experimental) allows making configurator web requests from Lua scripts. This library is meant for advanced users as it's possible to break some parts of the system by making a request with missing or incorrect variables. You can use browser developer tools (F12) to inspect network requests that are sent to the UI to figure out the correct function arguments.

General usage:
Code:
webrequest = require('webrequest')
res, err = webrequest(mod, act, vars)
  • mod (string, required) - requested module name
  • act (string, required) - requested action name
  • vars (table, optional) - request argument variables

Code:
-- create a backup
-- data variable contains backup (ZIP) contents as a binary string
webrequest = require('webrequest')
data = webrequest('general', 'backup')

Code:
-- toggle scheduler ID 1 status (active/inactive)
webrequest = require('webrequest')
vars = {
  data = { id = 1 }
}

webrequest('schedulers', 'status', vars)

Code:
-- get a list of modbus device profiles
webrequest = require('webrequest')
vars = {
  plugin = 'modbus',
  request = 'profile-list'
}

res = webrequest('general', 'plugin', vars)
log(res)
Reply
#2
Hello Admin, Is It possible to create a sysconf backup using this webrequest library?

Thanks!!
Reply
#3
System config cannot be accessed via webrequest at the moment.
Reply
#4
Are these only get requests, sort of? Is it documented beyond this? I would like to create a modbus profile from script and upload it, is that possible?
Reply
#5
For creating profiles use this:
Code:
data = [[
  {
    "manufacturer": "TEST",
    "description": "TEST",
    "mapping": [
      { "name": "C1", "bus_datatype": "bool", "type": "coil", "address": 0, "writable": true }
    ]
  }
]]

vars = {
  plugin = 'modbus',
  request = 'profile-save',
  file = data,
  file_filename = 'test.json',
}

webrequest = require('webrequest')
res, err = webrequest('general', 'plugin', vars)
log(res)

As mentioned before use browser dev tools to figure out correct parameters for the given request.
Reply
#6
Is there a way to do a webrequest to:
http://192.168.0.10/scada-vis/trends/fetch

with a payload like this for example:
{"ids":[2,5,6,8,9,14,10,11,12,13],"dates_curr":{"start":{"year":2023,"day":1,"month":1},"end":{"year":2023,"day":1,"month":3}}}

This way you get csv response including date / timestamp (afaik) because trend.fetch does not include them
Reply
#7
Set the 4th parameter of trends.fetch() to true and you will get timestamps.
Reply
#8
I might have done something wrong, but it looks like the webrequest library does not work in an .lp file?

This script works fine in an event script, but returns an empty reply from server when doing a GET from the .lp file.

The SL is running FW 3.0.0


------ example.lp ------

Code:
<script src="/apps/js/localbus.js.gz"></script>
<script src="/scada/vis/busdecode.js.gz"></script>

<? require('apps') ?>

<?
webrequest = require('webrequest')
vars = {
  plugin = 'modbus',
  request = 'profile-list'
}

res = webrequest('general', 'plugin', vars)
print(res)
?>
Reply
#9
This library was not intended to be used in web server context (.lp script). We'll see if it can be fixed in the next version.
Reply
#10
(05.02.2024, 15:04)admin Wrote: This library was not intended to be used in web server context (.lp script). We'll see if it can be fixed in the next version.

Ok! Thanks Smile
Reply


Forum Jump: