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.

Create Modbus client via script
#9
(18.02.2017, 00:03)Erwin van der Zwart Wrote: Hi,

Yes this is possible, this script simulates the form you normal fill in manually, basicly any user form can be simulated and automated (;

Try this script:

Code:
-- Batch adding modbus devices - Created by Erwin van der Zwart - Schneider Electric Netherlands -----------
-- For spaceLYnk FW 1.2.1 or higher and homeLYnk FW 1.5.1 or higher ----------------------------------------
--------------------------------------------- Start Parameters ---------------------------------------------

-- Set username and password for access to SL/HL
username = 'admin'
password = 'admin'

---------------------------------------------- End Parameters ----------------------------------------------
---------------------------------- DON'T CHANGE ANYTHING UNDER THIS LINE -----------------------------------
-- Load modules
require('json')
require('socket.url')
require('socket.http')

-- Get current HL/SL ip address
data = io.readproc('if-json')
data = json.decode(data)
ip = data.eth0.inetaddr

-- Create url for modbus device creation
url = 'http://' .. username .. ':' .. password .. '@' .. ip .. '/scada-main/general/plugin?plugin=modbus&request=device-save'

-- Set counters for creation log
number_of_devices = 0
number_failed = 0

-- Function to send request to create modbus device
function url_send(device_proto, device_name, device_profile, device_ip, device_port, device_slave, device_pollinterval, device_id)
 local device = {
     proto = device_proto,
     name = device_name,
   profile = device_profile,
     ip = device_ip,
     port = device_port,
   slave = device_slave,
   pollinterval = device_pollinterval,
   id = device_id,
    }
 data = json.encode(device)
    form_data = 'data=' .. socket.url.escape(data)
    socket.http.TIMEOUT = 15  
    local res, code, response_header = socket.http.request(url, form_data)
 return res, code, response_header
end

-- Create devices
for i = 1, 10, 1 do
 -- used fields to send: protocol,name,profile,ip,port,slave,pollinterval,id
 result, code, response_header = url_send("tcp", "Device " .. i, "iEM-iEM3155", "192.168.10." .. i, "502", i, "5", "")
 -- Calculate results for creation devices log
 if result == '{"success":true}' then
   number_of_devices = number_of_devices + 1
 else
   number_failed = number_failed + 1
 end
end

-- Create result log
if number_failed == 0 then
 log ("Created " .. number_of_devices .. " devices succesfully")
else
 log ("Created " .. number_of_devices .. " devices succesfully and creation of " .. number_failed .. " devices failed")
end

-- Disable script when done automaticly
script.disable(_SCRIPTNAME)

Erwin;

This functionality (which is utterly brilliant by the way) seems to be broken i FW 2.1.1. I´ve tried on all FW up to 2.1.0 and it works as a charm. I suspect there's some parameters missing. When I log the response i get a 404. Do you have any idea what might have changed (I don't like undocumented changes!!!) in FW 2.1.1?
Reply


Messages In This Thread
Create Modbus client via script - by FatMax - 17.02.2017, 11:31
RE: Create Modbus client via script - by AEK - 28.09.2017, 16:04
RE: Create Modbus client via script - by AEK - 29.09.2017, 07:56
RE: Create Modbus client via script - by FatMax - 25.05.2018, 12:14
RE: Create Modbus client via script - by AEK - 02.08.2018, 10:55

Forum Jump: