http_get_custom
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
http_get_custom [2021/07/13 11:06] – created emozolyak | http_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. | ||
+ | Once the response comes, is is copied in the // | ||
<code lua> | <code lua> | ||
Line 11: | Line 13: | ||
]] | ]] | ||
- | response = " | + | response = " |
function createDevices () | function createDevices () | ||
Line 54: | Line 56: | ||
return response | return response | ||
end | end | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== 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 / | ||
+ | reg = 611, -- can be modified from outside | ||
+ | | ||
+ | | ||
+ | -- host = "Host: 192.168.1.110" | ||
+ | host = "Host: 127.0.0.1" | ||
+ | | ||
+ | | ||
+ | |||
+ | } | ||
+ | |||
+ | function GET_TEMPLATE : make() | ||
+ | local newRequest = | ||
+ | .. self.reg | ||
+ | .. self.getTail | ||
+ | | ||
+ | .. self.acceptHeader | ||
+ | .. self.host | ||
+ | .. self.apiKey | ||
+ | .. self.endLine | ||
+ | | ||
+ | return newRequest | ||
+ | end | ||
+ | |||
+ | function createDevices () | ||
+ | addDevice{name = " | ||
+ | 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 | ||
+ | end | ||
+ | | ||
+ | sendString_(GET_TEMPLATE : make() ) | ||
+ | | ||
+ | local response_ = readAll() | ||
+ | | ||
+ | if (#response_ > 0) then | ||
+ | DEBUG(" | ||
+ | status_ = 1 | ||
+ | else | ||
+ | status_ = false | ||
+ | end | ||
+ | | ||
+ | return true | ||
+ | end | ||
+ | |||
+ | ------------------ Helpers --------------- | ||
+ | |||
+ | -- new sendString version | ||
+ | function sendString_(s) | ||
+ | DEBUG(" | ||
+ | local sendBuf = {} | ||
+ | | ||
+ | for i = 1, #s do | ||
+ | table.insert(sendBuf, | ||
+ | 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, | ||
+ | end | ||
+ | until (not b) | ||
+ | |||
+ | return table.concat(rbuf) | ||
+ | end | ||
+ | |||
+ | ------------ Not in use ------------------ | ||
+ | function onScanStart () | ||
+ | end | ||
+ | |||
</ | </ | ||
http_get_custom.txt · Last modified: 2023/03/28 10:49 by atolstov