User Tools

Site Tools


lua:curl_example

Getting some data from WebHMI itself using API

local res = ""    
local c = cURL.easy_init()
c:setopt{ url = 'http://127.0.0.1/api/event-data/' .. event,
          timeout = 15, 
          httpheader = {'X-WH-APIKEY: 2DA38AEB2744DC7FA2C46299299CE4B50FDEBDA8',
                    'X-WH-START: ' .. startTime, 
                    'X-WH-END: ' ..   (startTime + 10 * 60), -- 10 minutes after timestamp 
                    'Accept: application/json',
                    'Content-Type: application/json'
                    }
         }
 
 
    c:perform{writefunction = function(str)
            res = res .. str
        end}
    c:close()
 
    if (res == '') or (res == '%[%]')then 
        ERROR('No data in response!') 
    else 
        INFO('\n' .. res) 
    end

Writing some file onto WebHMI filesystem

The following example create some file with content in the WebHMI filesystem using ftp access. The condition is when runRegId is changed to 0. The script immediately sets the register back to 1 preventing multiple calls and calls curl function to save the file.

function main (userId)
    local runRegId = 840 
    -- check invoke condition 
    if (R(runRegId) ~= 0) then return end 
    W(runRegId, 1)
 
    done = false 
    local fileContent = "my data"
 
    local c = cURL.easy_init()
    c : setopt{ url = 'ftp://admin:webhmi@127.0.0.1/files/stuck.txt',
                upload = true,
                timeout = 2,
                readfunction = 
                function() if (not done) then done = true ; return fileContent end end }
    c : perform(); c : close() 
end
lua/curl_example.txt ยท Last modified: 2024/01/18 12:58 by atolstov

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki