following post provides FTP sample as well as management around day time execution of the script
Description of the scenario:
- get FTP file.
Filename is current day dependant. Filename on the FTP server will be for instance “910046_15112013_00010.xml”. So script must be able to know current date and do a FTP get of the file with current date. This is assuming in this scenario FTP server stores files with same filename format, but date part of the name will be changing every day. - store content of ftp get locally
- push this same content back using FTP push
- execute this procedure only once a day at a given time of the day. For isntance every day at 23h30.
Note that you will need to enable SNTP (or GPS) on your device so that the time module is able to synchronize time. In order to enable SNTP go to AceManager (192.168.13.31:9191, loggin user & pwd 12345) and in menu Services>SNTP set “enable”.
See in the script how the data is passed from the get to the push using the f = assert() method, f being passed as argument for file content in the ftp push.
local ftp = require’socket.ftp’
local sched = require’sched’
local timer = require’timer’
local function task ()
-- print("Timestamp", os.time())
-- print("Date:", os.date())
-- print("filename for the get:", os.date("910046_%d%m%Y_000010.xml"))
-- ex: 910046_12112013_000010.xml
-- example with static file name
-- Assert will stop the task in case or error. A proper error handling would be necessary in a real application
-- local f = assert(ftp.get("ftp://nmp:pass@192.168.13.100/test_ftp_lua.txt"))
-- print("Received a file length:", #f)
-- example with dynamic file name management
-- concantenate ftp push method along with day-dependant file name into a string called "url"
local url = "ftp://nmp:pass@192.168.13.100/" .. os.date("910046_%d%m%Y_000010.xml")
-- Assert will stop the task in case or error. A proper error handling would be necessary in a real application
local f = assert(ftp.get(url))
print("Received a file length:", #f)
-- Assert will stop the task in case or error. A proper error handling would be necessary in a real application
assert(ftp.put("ftp://nmp:pass@192.168.13.100/put_du_fichier_courant", f))
print("File pushed successfully")
end
– Schedule the task
– see detail of cron time onto:
– fr.wikipedia.org/wiki/Crontab
– execute script every minutes for test purpose
timer.new("* * * * *", task)
– execute script once per day at 23h30:
– timer.new(“30 23 * * *”, task)
sched.loop()