User Tools

Site Tools


http_get_custom

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
http_get_custom [2021/07/13 11:10] emozolyakhttp_get_custom [2023/02/24 15:14] emozolyak
Line 1: Line 1:
 ====== HTTP request implemention using custom protocol ====== ====== HTTP request implemention using custom protocol ======
 The following example sends a HTTP string template to the TCP port upon the register change in the custom protocol.  The following example sends a HTTP string template to the TCP port upon the register change in the custom protocol. 
-Once the response comes, is is copied in the 'responseglobal variable which is returned to the system with the **readRegister** function.+Once the response comes, is is copied in the //response// global variable which is returned to the system with the **readRegister** function.
  
 <code lua> <code lua>
Line 56: Line 56:
     return response     return response
 end  end 
 +
 +</code>
 +
 +===== How to change request parameters  =====
 +
 +When you want to dynamically change your request paramters, the above example could be further extended to this verstion: 
 +
 +<code lua>
 +GET_TEMPLATE = { getPath = "GET /api/registers/"
 +                 reg = 611,  -- can be modified from outside 
 +                 getTail =  " HTTP/1.1" .. "\r\n", 
 +                 acceptHeader = "Accept: application/json".."\r\n", 
 +                --  host = "Host: 192.168.1.110".."\r\n", 
 +                 host = "Host: 127.0.0.1".."\r\n", 
 +                 apiKey = "X-WH-APIKEY: 4025D32205ADC52E8604690FFADFB4600B8D41AA".."\r\n", 
 +                 endLine = "\r\n"
 +                 
 +}
 +
 +function GET_TEMPLATE : make() 
 +    local newRequest =   self.getPath 
 +                      .. self.reg 
 +                      .. self.getTail 
 +                      
 +                      .. self.acceptHeader 
 +                      .. self.host 
 +                      .. self.apiKey 
 +                      .. self.endLine 
 +    
 +    return newRequest
 +end 
 +
 +function createDevices ()
 +    addDevice{name = "CMD",  shift = 0, base = 10, xtraFields = {} };
 +end
 +
 +function readRegister (reg, device, unitId)
 +  return status_ 
 +end
 +
 +function writeRegister (reg, device, unitId, newValue)
 +    
 +    -- check for zero 
 +    if (newValue == 0) then 
 +        status_ = false 
 +        return true              -- no GET will be done 
 +    else 
 +        GET_TEMPLATE.reg = newValue -- change target  for request 
 +    end 
 +    
 +    sendString_(GET_TEMPLATE : make() )
 +    
 +    local response_ = readAll()
 +    
 +    if (#response_ > 0) then 
 +        DEBUG("got something ... " .. response_)
 +        status_ = 1 
 +    else 
 +        status_ = false 
 +    end 
 +    
 +    return true 
 +end
 +
 +------------------ Helpers ---------------
 +
 +-- new sendString version 
 +function sendString_(s)
 +                                                DEBUG("Going to send: " .. s)
 +    local sendBuf = {} 
 +    
 +    for i = 1, #s do 
 +        table.insert(sendBuf, s : byte(i) )
 +    end 
 +    sendBytes(sendBuf)
 +    
 +end
 +
 +-- read everything that is in buffer 
 +function readAll()
 +    local rbuf = {}
 +    repeat 
 +            local b = readString(1)
 +            if b then 
 +                table.insert(rbuf, b)
 +            end 
 +    until (not b)
 +
 +    return table.concat(rbuf)
 +end 
 +
 +------------ Not in use ------------------  
 +function onScanStart ()
 +end
 +
  
 </code> </code>
  
http_get_custom.txt · Last modified: 2023/03/28 10:49 by atolstov

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki