I'm been playing around with a skin to display a sequence of satellite images of weather in the Western Pacific which you can find here . This links to this feed which provides info and images for any active Tropical Storms in the Western Pacific.
I've got the above working nicely but have now become a little greedy. There are five images available from here but I want to increase this to 10-12 images for each day. This means that I need to rename all the downloaded images (5.jpg = 6.jpg , 4.jpg = 5.jpg , etc ) every hour (update=3600) and also update #NumMaps# by 1 every hour.
I'm sure this can be done with a LUA script, but I'm f'd if I can see how to do it.
PROPERTIES =
{
}
function Initialize()
CurrPath = SKIN:GetVariable('CURRENTPATH')
end -- function Initialize
function Update()
os.rename(CurrPath.."OldName.txt", CurrPath.."NewName.txt" )
return "Something"
end -- function Update
That is how you rename / move files in Lua. Easy enough. The trick is going to be the "logic" you are going to have to come up with to know when to rename things and how you keep from stepping all over yourself. Should be fun.
PROPERTIES =
function Initialize()
CurrPath = SKIN:GetVariable('CURRENTPATH')
end -- function Initialize
function Update()
os.rename(CurrPath.."OldName.txt", CurrPath.."NewName.txt" )
return "Something"
end -- function Update
Ok.. that bit I get.. But what do I do to only call the LUA script once every hour after the images have been updated?
I would make that rename a function in lua, then call it with a FinishAction= on the WebParser measure getting the images, using !CommandMeasure. Then only use the "renamed" versions in meters and such. That way the new ones coming in won't step on the old ones.
Let me know if that isn't clear, I can probably knock out a little example.
jsmorley wrote:I would make that rename a function in lua, then call it with a FinishAction= on the WebParser measure getting the images, using !CommandMeasure. Then only use the "renamed" versions in meters and such. That way the new ones coming in won't step on the old ones.
I hear you, but the problem is that the images do not always get downloaded in sequence. So that makes a mess of whenever I try to use FinishAction= (which I tried before and got stuffed) e.g Image 03 gets downloaded before Image 02)
The bottom line is that I do not want to rename files using the LUA script until one hour (update=3600) after the previous updates of measures and meters in the main skin. (oops - posted this while you were replying.... let me see how the pancake flattens.
Mordasius wrote: The bottom line is that I do not want to rename files using the LUA script until one hour (update=3600) after the previous updates of measures and meters in the main skin.
Well then, off the top of my head I would explore having an Update() in the measure that looks at variables in the lua called CurrentHour and OldHour or something.
function Initialize()
CurrentHour = 0
OldHour = 0
end -->Initialize
function Update()
if CurrentHour ~= OldHour then
RenameMyFiles()
return "Done"
end -->Update
function RenameMyFiles()
os.rename(blah, blah)
os.rename(blah, blah)
OldHour = CurrentHour
end -->RenameMyFiles
Then maybe a Calc measure in the skin that calls the script once an hour, based on Time measures:
jsmorley wrote:I think something like that might get you there.
Thanks, that was pretty much all that was needed.
I just had to tuck in an os.remove before the os.rename because LUA won't rename a file to an existing filename. The other little hiccup was that LUA doesn't have an os.copy type of command so I had to download a spare copy of 5.jpg to replace the one that gets renamed to 6.jpg. I tried to 'force' the measure to download the file with [!UpdateMeasure mMapImageFive] in the skin file but that didn't appear to work. So this is what I ended up with:
PROPERTIES = {}
function Initialize()
iNumMaps = 0
iCurrentHour = 0
iPreviousHour = 0
sCurrentPath = SKIN:GetVariable('CURRENTPATH')
end -->Initialize
function Update()
iNumMaps = SKIN:GetVariable('NumMaps')
if iCurrentHour ~= iPreviousHour
then
RenameImageFiles()
end
return 'Script Done'
end -->Update
function RenameImageFiles()
for i = iNumMaps, 5, -1 do
os.remove( sCurrentPath.."DownloadFile\\"..(i+1)..".jpg" )
os.rename( sCurrentPath.."DownloadFile\\"..i..".jpg", sCurrentPath.."DownloadFile\\"..(i+1)..".jpg" )
end
os.rename( sCurrentPath.."DownloadFile\\0.jpg", sCurrentPath.."DownloadFile\\5.jpg" )
SKIN:Bang('!SetVariable "NumMaps" "'..(iNumMaps+1)..'"')
iPreviousHour = iCurrentHour
end -->RenameImageFiles
I'm still looking for a way to synchronise the downloading and renaming of files and think it will probably be best to do both in the LUA script. Can this be done by simply sending an !EnableMeasure bang to the measures downloading files with WebParser?