Various functions to do network communication
Allows sending mails
pilight.network.mail
()¶Creates a new mail object
getUserdata
()¶Returns a persistent userdata table for the lifetime of the mail object.
getFrom
()¶Returns the sender.
getHost
()¶Returns the mail server host.
getMessage
()¶Returns the mail message.
getPassword
()¶Returns the mail server password.
getPort
()¶Returns the mail server port.
getSSL
()¶Returns the mail server ssl.
getStatus
()¶After the mail was sent this function can be used in the callback to check if the mail was sent successfully to the server.
getTo
()¶Returns the recipient.
getUser
()¶Returns the mail server user.
setCallback
(string callback)¶The name of the callback being triggered by the mail library. The mail object will be passed as the only parameter of this callback function.
setUserdata
(userdata table)¶Set a new persistent userdata table for the lifetime of the mail object. The userdata table cannot be of another type as returned from the getData functions.
setFrom
()¶Sets the sender.
setHost
()¶Sets the mail server host.
setMessage
()¶Sets the mail message.
setPassword
()¶Sets the mail server password.
setPort
()¶Sets the mail server port.
setSSL
()¶Sets the mail server ssl.
setTo
()¶Sets the recipient.
setUser
()¶Sets the mail server user.
send
()¶Send the mail
local M = {}
function M.callback(mail)
print(mail.getStatus());
end
function M.run()
local mailobj = pilight.network.mail();
mailobj.setSubject("foo");
mailobj.setFrom("order@pilight.org");
mailobj.setTo("info@pilight.nl");
mailobj.setMessage("bar");
mailobj.setHost("127.0.0.1");
mailobj.setPort(25);
mailobj.setUser("pilight");
mailobj.setPassword("test");
mailobj.setSSL(0);
mailobj.setCallback("callback");
mailobj.send();
return 1;
end
return M;
Do HTTP GET or POST requests
pilight.network.http
()¶Creates a new http object
getUserdata
()Returns a persistent userdata table for the lifetime of the mail object.
getMimetype
()¶Returns the mimetype of the received data. This means it does not returns the mimetype set by the setMimetype() function.
getData
()¶Returns the data received from the url. This means it does not returns the data set by the setData() function.
getUrl
()¶Returns the url.
getCode
()¶After the HTTP request was sent this function can be used in the callback to check if the return HTTP code.
getSize
()¶After the HTTP request was sent this function can be used in the callback to get the size of the received content.
setCallback
(string callback)The name of the callback being triggered by the http library. The http object will be passed as the only parameter of this callback function.
setUserdata
(userdata table)Set a new persistent userdata table for the lifetime of the http object. The userdata table cannot be of another type as returned from the getUserdata functions.
setUrl
(string url)¶Sets the request URL. Examples:
setMimetype
(string mimetype)¶Sets the mimetype of the data being posted.
setData
(string data)¶Sets the data to be posted.
get
()¶Send a GET request to the URL.
post
()¶Send a POST request to the URL with the data set.
local M = {}
function M.callback(http)
print(http.getCode());
print(http.getMimetype());
print(http.getSize());
print(http.getData());
end
function M.run()
local httpobj = pilight.network.http();
httpobj.setUrl("http://127.0.0.1:10080/")
httpobj.setCallback("callback");
httpobj.get();
return 1;
end
return M;
Send and/or receive COAP messages
pilight.network.coap
()¶Creates a new coap object
getUserdata
()Returns a persistent userdata table for the lifetime of the mail object.
setCallback
(string callback)The name of the callback set for this coap object.
listen
(userdata table)¶Listens to coap messages
setCallback
(string callback)The name of the callback being triggered by the coap library. The parameters of the callback function are the coap object, received data object, ip and port of the sender.
setUserdata
(userdata table)Set a new persistent userdata table for the lifetime of the http object. The userdata table cannot be of another type as returned from the getUserdata functions.
send
(userdata table)Sends a coap message
Note
COAP data specifications
The data the lua COAP interface parses has to be set in a low-level way. That means you have to construct a valid coap object yourself. The COAP responses are represented in the same low-level way. The COAP interface follows the same specification for it’s data as the protocol description.
The allowed COAP data fields are
ver
t
token
payload
code
msgid
options
num
val
The options field is in itself an array with num
and val
keys.
local M = {}
function M.discover(coap, data, ip, port)
return;
end
function M.run()
local coap = pilight.network.coap();
local send = {};
send['ver'] = 1;
send['t'] = 1;
send['code'] = 0001;
send['msgid'] = 0001;
send['options'] = {};
send['options'][0] = {};
send['options'][0]['val'] = 'cit';
send['options'][0]['num'] = 11;
send['options'][1] = {};
send['options'][1]['val'] = 'd';
send['options'][1]['num'] = 11;
coap.setCallback("discover");
coap.send(send);
coap.listen();
return 1;
end
return M;