Logic Machine Forum
Lua webrequest library (FW 2022 and later) - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Lua webrequest library (FW 2022 and later) (/showthread.php?tid=4425)



Lua webrequest library (FW 2022 and later) - admin - 05.12.2022

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)



RE: Lua webrequest library (FW 2022 and later) - kike - 23.12.2022

Hello Admin, Is It possible to create a sysconf backup using this webrequest library?

Thanks!!


RE: Lua webrequest library (FW 2022 and later) - admin - 23.12.2022

System config cannot be accessed via webrequest at the moment.


RE: Lua webrequest library (FW 2022 and later) - FatMax - 21.09.2023

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?


RE: Lua webrequest library (FW 2022 and later) - admin - 22.09.2023

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.


RE: Lua webrequest library (FW 2022 and later) - Erwin van der Zwart - 20.10.2023

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


RE: Lua webrequest library (FW 2022 and later) - admin - 20.10.2023

Set the 4th parameter of trends.fetch() to true and you will get timestamps.


RE: Lua webrequest library (FW 2022 and later) - lestole - 05.02.2024

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)
?>



RE: Lua webrequest library (FW 2022 and later) - admin - 05.02.2024

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.


RE: Lua webrequest library (FW 2022 and later) - lestole - 06.02.2024

(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