Appendix G: Lua Scripts
X-600M Users Manual
print("Device at 192.168.1.15 not found")
else
print("Ping error")
end
httpRequest(url, timeoutMilliseconds)
This function can be used to send an HTTP GET request to another web server. For example, it can
be used to change the relay state on another ControlByWeb device.
This function can only be
called from the 5 Lua Scripts, not Lua expressions
. This function will return the number of bytes
received in the response, a 0 for no response, or a negative number that represents the following
errors:
-1 = Can't run from Lua Expressions
-2 = Host unknown
-3 = Internal Error
-4 = Timeout
-5 = Bad host name length
If the request returns a response, this response will be saved in a response buffer associated with
the Lua script. Each script (1-5) have their own response buffer that can hold responses up to 8K in
length. Responses bigger than 8K will be truncated. These responses can then be parsed using
the functions
responseIndexOf
and
responseSubstr
as explained below. The following example
will turn on the first relay on the ControlByWeb device found at address 192.168.1.15 and store the
state.xml file in the Lua scripts response buffer. While this example uses another ControlByWeb
device, httpRequest can send requests to any web server and device.
httpRequest("http://192.168.1.15/state.xml?relay1State=1", 50)
responseIndexOf(start, searchStr)
When a resource is requested over the network by functions such as httpRequest, any response is
stored in the lua scripts response buffer. This function will search the response buffer beginning at
position
start
, for the string
searchStr
. This allows responses to be parsed for specific information.
For example, to find the first relay state from a state.xml file the following could be used:
position = responseIndexOf(0, "<relay1State>")
str = responseSubstr(p13, p14)
The variable str should be a “1” or “0” depending on the state of the relay. This is done by finding the
position of the string “<relay1state>” in the response and then adding 13 to that to get the start of the
relay1state value, and adding 14 to that to get the end of the relay1state value.
responseSubstr(start, end)
This function will return a substring from the response buffer that begins at the index
start
and ends
at the index
end
. Both start and end are 0 based, meaning that a start value of 0 will return the very
first character from the response buffer. Start must be less than end. If start is greater than end, or
there is an error, this function will return nil. The return value of this function, as well as all others,
should always be checked for errors.
responseGetBytes(start, end)
This function will return a table of bytes from the response buffer that begins at the index
start
and
Page 130
Xytronix Research & Design, Inc.
Содержание X-600M
Страница 1: ...X 600MTitle Page Goes Here...
Страница 6: ...X 600M Users Manual Page 4 Xytronix Research Design Inc...
Страница 99: ...X 600M Users Manual Setup Pages Xytronix Research Design Inc Page 97...
Страница 116: ...Appendix B Installing New Firmware X 600M Users Manual Page 114 Xytronix Research Design Inc...