User Tools

Site Tools


access_from_lua_to_webhmi_itself

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
access_from_lua_to_webhmi_itself [2023/03/02 10:34] – created emozolyakaccess_from_lua_to_webhmi_itself [2023/03/02 10:56] (current) – [Getting meter data example] emozolyak
Line 1: Line 1:
 ====== Access from WebHMI itself ====== ====== Access from WebHMI itself ======
 +
 +To access the WebHMI DB data via API, you have to generate API key in the setup menu and use it in your requests. 
 +You can use //localhost// address as a part of the resource address. 
 +
 +{{ :api:setup-menu-api-key.png?direct |}}
 +
 +{{ :api:api-key-example.png?direct&800 |}}
 +
 +===== Getting meter data example =====
  
 <code lua> <code lua>
-function getMeterData(meterId, startTime, endTime) +function getMeterData(startTime, endTime) 
          
     local c = cURL.easy_init()     local c = cURL.easy_init()
     c : setopt{ url = 'http://127.0.0.1/api/meters-log/',     c : setopt{ url = 'http://127.0.0.1/api/meters-log/',
               timeout = 15,                timeout = 15, 
-              httpheader = {'X-WH-APIKEY: 4025D32205ADC52E8604690FFADFB4600B8D41AA',+              httpheader = { 'X-WH-APIKEY: 4025D32205ADC52E8604690FFADFB4600B8D41AA',
                              'X-WH-START: ' .. startTime,                               'X-WH-START: ' .. startTime, 
                              'X-WH-END: ' ..   endTime,                               'X-WH-END: ' ..   endTime, 
-                            --  'X-WH-SLICES: ' ..   30,  +                            
-                            --  'X-WH-REGISTERS: ' ..   meterId,  +
-                             +
                              'Accept: application/json',                              'Accept: application/json',
                              'Content-Type: application/json'                              'Content-Type: application/json'
Line 34: Line 41:
 end end
 </code> </code>
 +
 +===== Getting graph data example  =====
 +<code lua>
 +function getGraphData(regId, startTime, endTime) 
 +    
 +    local c = cURL.easy_init()
 +    c : setopt{ url = 'http://127.0.0.1/api/graph-data/',
 +              timeout = 15, 
 +               httpheader = {'X-WH-APIKEY: 9833A5C553C5E05D944C28615EB252A6CB2E9322',
 +                             'X-WH-START: ' .. startTime, 
 +                             'X-WH-END: ' ..   endTime, 
 +                             'X-WH-SLICES: ' ..   30, 
 +                             'X-WH-REGISTERS: ' ..   regId, 
 +                             
 +                             'Accept: application/json',
 +                             'Content-Type: application/json'
 +                            }
 +         }
 + 
 +    local res = ""
 +    c:perform{writefunction = function(str)
 +            res = res .. str
 +        end}
 +    c : close()
 + 
 +    if (res == '') or (res == '%[%]')then 
 +        ERROR('No data in response!'
 +        return false 
 +    else 
 +        return res 
 +    end
 +    
 +end
 +</code>
 +
  
access_from_lua_to_webhmi_itself.1677753296.txt.gz · Last modified: 2023/03/02 10:34 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki