PDA

View Full Version : Fixed sleep/wait/delay in Lua script with NetRemote



samplehead
October 16th, 2004, 07:07 PM
Is the standard LUA 'sleep(msec)' supported by Girder/NetRemote?

Otherwise, what is a recommended way of achieving a fixed timer?

Thanks,
Samplehead

danward79
October 17th, 2004, 01:38 AM
There is a timer facility with one of the plugins available, check Avid's utils

you could also use os.time in lua five on NR, on girder you can use the alarmtimer, or a similar lua function.

VaioUserChris
October 17th, 2004, 12:14 PM
Here's a script to execute a timer using Lua 4.0 in Girder:
-- Here's a lua script to create timers and trigger events at the times we want.
-- This works for events that you want to trigger on this specific day, however
-- it could easily be modified to trigger events only on a defined day.

-- TimerMonitor is a repeating timer. At an interval we specify, it will
-- repeatedly call TimerHandler. The interval is in milliseconds - so 5000 = 5 seconds
function TimerMonitor (interval)
local err
Timer = TIMER_CreateObject ()
err = Timer.Create ("","TimerHandler ()","",1)
err = Timer.Arm (interval)
end

-- TimerHandler function is the second part of the TimerMonitor function
-- This does the actual time check versus our timers. When finds timer match, it
-- triggers the eventstring associated with that timer.
function TimerHandler ()
hour = date ("%H")
minute = date ("%M")
time = hour ..":".. minute
for tempi = 1, timertotal do
if time == timerevent[tempi] then
TriggerEvent (timertriggerevent[tempi],18)
end
end
end

-- Now we assign the timers we want to watch for.
-- FYI, the times in this example use a 24 hour clock, i.e. 17:36 = 5:36pm
timerevent = {"10:30", "11:55", "17:36"}

-- Now we assign the events we want to trigger at the appropriate time
timertriggerevent = {"EVENTSTRING1", "EVENTSTRING2", "EVENTSTRING3"}

-- So for this example, EVENTSTRING1 will be triggered at 10:30 a.m.,
-- EVENTSTRING2 at 11:55 a.m., and EVENTSTRING3 at 5:36 p.m.

-- "timertotal" is the amount of times the script will watch for
timertotal = getn(timerevent)

-- Now we specify how frequently we want the timer to check.
-- In this example it will check every 5000 milliseconds (or every 5 seconds).
wait = 5000

for tempi = 1,timertotal do -- for debugging
print (tempi)
print (timerevent[tempi])
print (timertriggerevent[tempi])
end

-- start the timer
TimerMonitor (wait)
To get rid of this timer, run this script:
-- TimerKill function is used to obliterate the timer. Just call this function
-- when you want to get rid of this timer.
function TimerKill ()

Timer.Cancel ()
Timer = nil
end

Hope that helps...
Chris

danward79
October 17th, 2004, 12:52 PM
Nice bit of script Chris. I think I will be poaching that!

samplehead
October 17th, 2004, 01:32 PM
What about a simple sleep though?

i.e.

do something
wait 2 seconds (e.g. sleep(2000); )
do something else


I found the 'sleep' function but it doesn't appear to be working for me so far.

VaioUserChris
October 17th, 2004, 02:01 PM
Glad it's helpful to you Dan. Although credit is ultimately due to Mark F. I wrote this script based on timer examples he posted here http://www.promixis.com/phpBB2/viewtopic.php?t=5491. So "Thanks Mark!" :D

By the way, I forgot to mention that this requries the WinLUAEx plugin he developed.

Samplehead, I don't think there's a native sleep function in Lua (at least not that I can find) but there is one with the WinLUAEx plugin. The syntax of the function is "TIME_Sleep( MILLISEC )". According to the readme:
TIME_Sleep( int MILLISEC )

Stops execution of this script for the supplied number of milliseconds

NOTE: The range of MILLISEC is 0 <= MILLISEC <= 1000 milliseconds (1000 milliseconds == 1 second).

Keep in mind that no other processing of your script will occur while you use this TIME_Sleep function. If you don't care about that, this is the way to go. Also, you can only call this function for up to one second (i.e. TIME_Sleep(1000)). So to get it to wait two seconds, you'd have to call "TIME_Sleep(1000) two times.

But if you want to allow the script to continue to process I'd build a timer instead. Here's another post on the subject with an example that seems to be closer to what you're trying to achieve:http://www.promixis.com/phpBB2/viewtopic.php?t=6594

Chris

samplehead
October 17th, 2004, 02:56 PM
Chris, that's exactly what I'm after - I'll try it at home tonight. Thanks a million.

Best,
Samplehead

samplehead
October 18th, 2004, 12:34 PM
Hmmm, no luck yet. Is there a specific version of WinLUAex needed?

Mark F
October 18th, 2004, 12:45 PM
Do you get an error message (something about TIME_Sleep being a nil value) or is something else going wrong?

samplehead
October 18th, 2004, 02:11 PM
No error message in the Girder status window. The command following the SLEEP_Time is not executed at all, nor is there any delay processing the next function.

e.g.

function_a
SLEEP_Time(1000);
command_a;

function_b
command b;

function_c
function_a;
function_b;

if I run function_c the only thing that happens command_b executes (immediately at that).

Mark F
October 18th, 2004, 02:19 PM
I don't understand. :( When I run the following code in the Girder script editor window, I get exactly what I expect in the Logger window.



function a&#40;&#41;
TIME_Sleep&#40;1000&#41;
print&#40;"a"&#41;
end

function b&#40;&#41;
print&#40;"b1"&#41;
print&#40;"b2"&#41;
end

function c&#40;&#41;
a&#40;&#41;
b&#40;&#41;
end

c&#40;&#41;


Output:


17&#58;18&#58;22.676 LUA&#58; call main
17&#58;18&#58;22.676 LUA&#58; --> call Lua c global
17&#58;18&#58;22.676 LUA&#58; --> --> call Lua a global
17&#58;18&#58;22.676 LUA&#58; --> --> --> call C TIME_Sleep global
17&#58;18&#58;23.676 LUA&#58; --> --> --> return C TIME_Sleep global
17&#58;18&#58;23.676 PRINT&#58; a
17&#58;18&#58;23.676 LUA&#58; --> --> return Lua a global
17&#58;18&#58;23.676 LUA&#58; --> --> call Lua b global
17&#58;18&#58;23.676 PRINT&#58; b1
17&#58;18&#58;23.676 PRINT&#58; b2
17&#58;18&#58;23.676 LUA&#58; --> --> return Lua b global
17&#58;18&#58;23.676 LUA&#58; --> return Lua c global
17&#58;18&#58;23.676 LUA&#58; return main

VaioUserChris
October 18th, 2004, 02:23 PM
samplehead,

Just to confirm, are you actually calling "TIME_Sleep(1000)" or "SLEEP_Time(1000)"? I ask because your example says "SLEEP_Time(1000)".

The correct command is "TIME_Sleep(1000)"

Chris

samplehead
October 18th, 2004, 03:38 PM
Thanks MArk & Chris. Yes I am using the right syntax (sorry about the confusion). I'll try something similar tonight at home and post the results.

samplehead
October 19th, 2004, 03:22 AM
Ok, that code worked for me when run directly in Girder as well. I don't know where to see a logger window like that though?

I used a dummy command, using the Girder tab, then Variable Manipulation Script from the list, then the Script button. The resulting interface did have an output window, but it didn't have time stamps or all of the lua debug info...

The actual sleep I want to incorporate will be inside a .lua file next to my .ccf file on my pocket pc. Does that change any of the requirements?

Incidentally, are there any rules/conventions for filename between the GML loaded in Girder and the CCF/LUA loaded in NetRemote?

Ah, I'm so new to all of this.... it's the big picture of how all the components/layers fit together that throws me most ;)

Thanks,
Samplehead

Mark F
October 19th, 2004, 03:48 AM
The output I showed was from the Logger plugin window.


The actual sleep I want to incorporate will be inside a .lua file next to my .ccf file on my pocket pc. Does that change any of the requirements?
Yes it does. :) This means you are using the LUA 5 built into NetRemote to run the command. Girder doesn't enter into the picture at all.

Unfortunately, I have no idea how to cause LUA 5 or NetRemote to pause or sleep. Sorry.

VaioUserChris
October 19th, 2004, 07:09 AM
Unfortunately, I'm at a loss as well. I don't have Netremote and thus I don't work with Lua 5 either.

You could have girder do the processing and transmit the timed commands back to netremote, but there's got to be an easier way to do it...

I'd suggest posting your question on the Netremote forum.

Chris

danward79
November 8th, 2004, 11:48 AM
Chris,

I found a problem with the script.

If you have a command executed at a time. it will continually re-execute it for the whole minute, the time matches.

Do you have a fix for that?

VaioUserChris
November 8th, 2004, 12:57 PM
Good catch Dan. Sorry about that.

I'm not at my development computer at the moment so I haven't tested the following script but I think it would work. I've modified the script to store the date that the event was last triggered in a table. When the script looks to see if there is a time match, it also checks the table to see if the event was already triggered for that day. If the event hasn't been triggered that day, it triggers the event and stores the date in the table. If it has already been triggered, it won't trigger the event (at least in theory).

Could you test this and let me know if it works?

Thanks, Chris
-- Here's a lua script to create timers and trigger events at the times we want.
-- This works for events that you want to trigger on this specific day, however
-- it could easily be modified to trigger events only on a defined day.

-- TimerMonitor is a repeating timer. At an interval we specify, it will
-- repeatedly call TimerHandler. The interval is in milliseconds - so 5000 = 5 seconds
function TimerMonitor &#40;interval&#41;
local err
Timer = TIMER_CreateObject &#40;&#41;
err = Timer.Create &#40;"","TimerHandler &#40;&#41;","",1&#41;
err = Timer.Arm &#40;interval&#41;
end

-- TimerHandler function is the second part of the TimerMonitor function
-- This does the actual time check versus our timers. When finds timer match, it
-- triggers the eventstring associated with that timer.
function TimerHandler &#40;&#41;
hour = date &#40;"%H"&#41;
minute = date &#40;"%M"&#41;
day = date &#40;"%Y%m%d"&#41;
time = hour .."&#58;".. minute
for tempi = 1, timertotal do
if time == timerevent&#91;tempi&#93; and runningtimer&#91;tempi&#93; ~= day then
TriggerEvent &#40;timertriggerevent&#91;tempi&#93;,18&#41;
runningtimer&#91;tempi&#93; = day
end
end
end

-- Now we assign the timers we want to watch for.
-- FYI, the times in this example use a 24 hour clock, i.e. 17&#58;36 = 5&#58;36pm
timerevent = &#123;"10&#58;30", "11&#58;55", "17&#58;36"&#125;

-- Now we assign the events we want to trigger at the appropriate time
timertriggerevent = &#123;"EVENTSTRING1", "EVENTSTRING2", "EVENTSTRING3"&#125;

-- The following runningtimer table allows us to make sure we trigger the
-- associated timer only once per day. You don't need to set it to anything,
-- it's only for Lua's internal use.
runningtimer = &#123;&#125;

-- So for this example, EVENTSTRING1 will be triggered at 10&#58;30 a.m.,
-- EVENTSTRING2 at 11&#58;55 a.m., and EVENTSTRING3 at 5&#58;36 p.m.

-- "timertotal" is the amount of times the script will watch for
timertotal = getn&#40;timerevent&#41;

-- Now we specify how frequently we want the timer to check.
-- In this example it will check every 5000 milliseconds &#40;or every 5 seconds&#41;.
wait = 5000

for tempi = 1,timertotal do -- for debugging
print &#40;tempi&#41;
print &#40;timerevent&#91;tempi&#93;&#41;
print &#40;timertriggerevent&#91;tempi&#93;&#41;
end

-- start the timer
TimerMonitor &#40;wait&#41;

VaioUserChris
November 8th, 2004, 05:25 PM
Ok. I've tested it and it works - triggering each event only once per day.

Keep in mind that to dispose of the timer you'll still need to use this script:
-- TimerKill function is used to obliterate the timer. Just call this function
-- when you want to get rid of this timer.
function TimerKill &#40;&#41;

Timer.Cancel &#40;&#41;
Timer = nil
end Let me know if there are any problems.

Thanks, Chris

danward79
November 9th, 2004, 10:48 AM
Thanks Chris,

I will try it and let you know.

danward79
November 9th, 2004, 10:59 AM
Chris,

How about this for an idea! ......

To be able to specify what days the timer executes the event on.

For Example.

I would like one light turning on during the week monday to friday, at 0535hrs and off again at 0625hrs

It could be done with simple numbers 1 for sunday thru to 7 for saturday.

Just a thought

:wink:

VaioUserChris
November 9th, 2004, 11:55 AM
Dan,

That's certainly possible although it makes the script more complicated. Here's a quick stab at it (untested). In the following script you now can specify the day you want the event triggered in the table "timerday".


function TimerMonitor &#40;interval&#41;
local err
Timer = TIMER_CreateObject &#40;&#41;
err = Timer.Create &#40;"","TimerHandler &#40;&#41;","",1&#41;
err = Timer.Arm &#40;interval&#41;
end

function TimerHandler &#40;&#41;
hour = date &#40;"%H"&#41;
minute = date &#40;"%M"&#41;
day = date &#40;"%Y%m%d"&#41;
weekday = date&#40;"%A"&#41;
weekdaynum = tonumber&#40;date &#40;"%w"&#41;&#41;
time = hour .."&#58;".. minute
for tempi = 1, getn&#40;timertime&#41; do
if time == timertime&#91;tempi&#93; and runningtimer&#91;tempi&#93; ~= day then
if timerday &#91;tempi&#93; == weekday or timerday &#91;tempi&#93; == "Everyday" or
timerday &#91;tempi&#93; == "Weekdays" and weekdaynum > 0 and weekdaynum < 6 then
TriggerEvent &#40;timertriggerevent&#91;tempi&#93;,18&#41;
runningtimer&#91;tempi&#93; = day
end
end
end
end

runningtimer = &#123;&#125;
-- Options for timerday&#58;
-- You can enter the day of the week you want the timer to run &#40;i.e. "Monday"
-- or "Thursday"&#41; or "Everyday" if you want the timer triggered every day
-- or "Weekdays" if you only want the timer triggered on Weekdays
timerday = &#123;"Weekdays","Sunday","Everyday"&#125;
timertime = &#123;"10&#58;30", "11&#58;55", "17&#58;36"&#125;
timertriggerevent = &#123;"EVENTSTRING1", "EVENTSTRING2", "EVENTSTRING3"&#125;

-- In this case, EventString1 will be triggered every weekday at 10&#58;30 a.m.
-- EventString2 will be triggered only on Sundays at 11&#58;55 a.m.
-- EventString3 will be triggered every day at 5&#58;36 p.m.

wait = 5000

for tempi = 1,getn&#40;timertime&#41; do -- for debugging
print &#40;tempi&#41;
print &#40;timertime&#91;tempi&#93;&#41;
print &#40;timerday&#91;tempi&#93;&#41;
print &#40;timertriggerevent&#91;tempi&#93;&#41;
end

-- start the timer
TimerMonitor &#40;wait&#41;

danward79
November 9th, 2004, 12:40 PM
Thanks I will try that now...

Oh don't forget weekends!! :wink:

Damn I am getting lazy!

VaioUserChris
November 9th, 2004, 02:22 PM
If I didn't enjoy the challenge I'd curse right now. :D

Weekends included and I also added the ability to use a specific date as well (because I figured you'd go there next :wink: ). Still untested though...


function TimerMonitor &#40;interval&#41;
local err
Timer = TIMER_CreateObject &#40;&#41;
err = Timer.Create &#40;"","TimerHandler &#40;&#41;","",1&#41;
err = Timer.Arm &#40;interval&#41;
end

function TimerHandler &#40;&#41;
day = date &#40;"%Y%m%d"&#41;
today = date&#40;"%x"&#41;
weekday = date&#40;"%A"&#41;
weekdaynum = tonumber&#40;date &#40;"%w"&#41;&#41;
time = date &#40;"%H"&#41; .."&#58;".. date &#40;"%M"&#41;
for tempi = 1, getn&#40;timertime&#41; do
if time == timertime&#91;tempi&#93; and runningtimer&#91;tempi&#93; ~= day then
if timerday &#91;tempi&#93; == weekday or timerday &#91;tempi&#93; == "Everyday"
or timerday &#91;tempi&#93; == today then
TriggerEvent &#40;timertriggerevent&#91;tempi&#93;,18&#41;
runningtimer&#91;tempi&#93; = day
end
if timerday &#91;tempi&#93; == "Weekdays" and weekdaynum > 0 and weekdaynum < 6 then
TriggerEvent &#40;timertriggerevent&#91;tempi&#93;,18&#41;
runningtimer&#91;tempi&#93; = day
end
if timerday &#91;tempi&#93; == "Weekends" and weekdaynum == 0 then
TriggerEvent &#40;timertriggerevent&#91;tempi&#93;,18&#41;
runningtimer&#91;tempi&#93; = day
end
if timerday &#91;tempi&#93; == "Weekends" and weekdaynum == 6 then
TriggerEvent &#40;timertriggerevent&#91;tempi&#93;,18&#41;
runningtimer&#91;tempi&#93; = day
end
end
end
end

runningtimer = &#123;&#125;
-- Options for timerday&#58;
-- You can enter the day of the week you want the timer to run &#40;i.e. "Monday"
-- or "Thursday"&#41; or "Everyday" if you want the timer triggered every day
-- or "Weekdays" if you only want the timer triggered on Weekdays
-- or "Weekends" if you only want the timer triggered on Saturdays and Sundays
-- or the date you want it to run &#40;i.e. "11/06/04"&#41;
timerday = &#123;"Weekdays","Sunday","Everyday", "Weekends", "11/09/04"&#125;
timertime = &#123;"10&#58;30", "11&#58;55", "17&#58;36", "11&#58;56", "13&#58;45"&#125;
timertriggerevent = &#123;"EVENTSTRING1", "EVENTSTRING2", "EVENTSTRING3", "EVENTSTRING4", "EVENTSTRING5"&#125;

-- In this case, EventString1 will be triggered every weekday at 10&#58;30 a.m.
-- EventString2 will be triggered only on Sundays at 11&#58;55 a.m.
-- EventString3 will be triggered every day at 5&#58;36 p.m.
-- EventString4 will be triggered every Saturday or Sunday at 11&#58;56 a.m.
-- EventString5 will be triggered on 11/09/04 at 1&#58;45 p.m.

wait = 5000

for tempi = 1,getn&#40;timertime&#41; do -- for debugging
print &#40;tempi&#41;
print &#40;timertime&#91;tempi&#93;&#41;
print &#40;timerday&#91;tempi&#93;&#41;
print &#40;timertriggerevent&#91;tempi&#93;&#41;
end

-- start the timer
TimerMonitor &#40;wait&#41;There's probably a neater way to handle the string of if/then statements in the middle of the script but I decided to leave you with a challenge. :D

To cancel, use:
function TimerKill &#40;&#41;
Timer.Cancel &#40;&#41;
Timer = nil
endHappy Timing.
Chris

danward79
November 9th, 2004, 09:08 PM
Thanks Chris,

I willtry it tonight.

Jlee
November 10th, 2004, 12:56 AM
Well this thread seems like a good place to ask how to do this: Like some others have mentioned I want a script like this:
Do something
wait n seconds
Do something else

I'm currently doing this with alarm timer but this doesn't allow me to have the number of seconds as a variable. I'd like to be able to adjust the delay by setting a variable in NR and sending it to girder. As Dan well knows I'm no Lua programmer so I need a little help. I did see some code that will do the above but the problem was that it was restricted to 1000 ms otherwise the timer had to be called multiple times. I would like to be able to vary the delay in 500 ms increments. Could this be done with some sort of Loop? e.g. if my variable is 8500 the code would divide that by 500 to get 17 and then initiate a 500 ms delay 17 times.

Come on Dan. This is an easy one for you! :D

Jlee
November 10th, 2004, 01:17 AM
I did try to help myself by checking out the Lua 4 manual to see how to do a simple do/loop statement. This is what it says:
The for statement has two forms, one for numbers and one for tables. The numerical for loop has the following syntax&#58;

stat &#58;&#58;= for name `=' exp1 `,' exp1 &#91;`,' exp1&#93; do block end
A for statement like
for var = e1 ,e2, e3 do block end
is equivalent to the code&#58;
do
local var, _limit, _step = tonumber&#40;e1&#41;, tonumber&#40;e2&#41;, tonumber&#40;e3&#41;
if not &#40;var and _limit and _step&#41; then error&#40;&#41; end
while &#40;_step>0 and var<=_limit&#41; or &#40;_step<=0 and var>=_limit&#41; do
block
var = var+_step
end
end


Like someone is supposed to know what that means! :(

If only it were:

'timer variable &#40;myTimer&#41; currently set to 8500
myDelay = myTimer/500
Do until n = myDelay
'execute 1000ms timer
n = n +1
Loop


No that I can understand!

Mark F
November 10th, 2004, 01:55 AM
Let me make sure I understand this right, you want Girder to stop processing events or anything else for 8.5 seconds? (8500 ms)

Here is the code to stop Girder from processing anything for myTimer ms:


-- timer variable &#40;myTimer&#41; currently set to 8500
local myDelay = 0
while &#40;myDelay < myTimer&#41; do
if &#40;&#40;myTimer-myDelay&#41; > 1000&#41; then
TIME_Sleep&#40;1000&#41;
myDelay = myDelay+1000
else
TIME_Sleep&#40;myTimer-myDelay&#41;
myDelay = myTimer
end
end

Jlee
November 10th, 2004, 02:08 AM
You understand it perfectly Mark. That code will be really useful. Thanks.

Jlee
November 10th, 2004, 04:38 AM
Having successfully hijacked this thread :oops: I have one more question. I'm going to store myTimer in the registry and retrieve it with a GirderOpen event. If myTimer does not exist or has a blank / null value I'd like to use a default value of 8000. Is this code correct? I sort of stole and modified it!
myTimer = RegGetStringValue&#40;"HKEY_LOCAL_MACHINE", "Software\\Girder3\\", "myTimer"&#41;
if myTimer ~= 0 then
myTimer = 8000
end

Mark F
November 10th, 2004, 05:18 AM
Try something more like this:


local r
r,myTimer = RegGetIntValue&#40;"HKEY_LOCAL_MACHINE", "Software\\Girder3\\", "myTimer"&#41;
if r ~= 0 then -- key does not exist
myTimer = 8000
end
-- myTimer contains the value or 8000


myTimer is a number so use the RegGetIntValue and RegSetIntValue functions instead of the ones designed for strings.

Jlee
November 10th, 2004, 05:24 AM
Thanks.

danward79
November 10th, 2004, 09:47 AM
Sorry Lee,

I missed all that had to go to my real job.... and sort there problems out!

:wink:

danward79
November 14th, 2004, 03:57 AM
Hi Chris,

I found a bug! It did not do what I wanted this weekend. Turns out to be a problem with the log of the if statements, does that make sense. basically we needed one more set of brakets round the and.

see below


function TimerMonitor &#40;interval&#41;
local err
Timer = TIMER_CreateObject &#40;&#41;
err = Timer.Create &#40;"","TimerHandler &#40;&#41;","",1&#41;
err = Timer.Arm &#40;interval&#41;
end

function TimerHandler &#40;&#41;
hour = date &#40;"%H"&#41;
minute = date &#40;"%M"&#41;
day = date &#40;"%Y%m%d"&#41;
weekday = date&#40;"%A"&#41;
weekdaynum = tonumber&#40;date &#40;"%w"&#41;&#41;
time = hour .."&#58;".. minute
for tempi = 1, getn&#40;timertime&#41; do
if time == timertime&#91;tempi&#93; and runningtimer&#91;tempi&#93; ~= day then
if timerday &#91;tempi&#93; == weekday or timerday &#91;tempi&#93; == "Everyday" or
&#40;timerday &#91;tempi&#93; == "Weekdays" and weekdaynum > 0 and weekdaynum < 6&#41; then
TriggerEvent &#40;timertriggerevent&#91;tempi&#93;,18&#41;
runningtimer&#91;tempi&#93; = day
end
end
end
end

runningtimer = &#123;&#125;
-- Options for timerday&#58;
-- You can enter the day of the week you want the timer to run &#40;i.e. "Monday"
-- or "Thursday"&#41; or "Everyday" if you want the timer triggered every day
-- or "Weekdays" if you only want the timer triggered on Weekdays
timerday = &#123;"Weekdays","Sunday","Everyday"&#125;
timertime = &#123;"10&#58;30", "11&#58;55", "17&#58;36"&#125;
timertriggerevent = &#123;"EVENTSTRING1", "EVENTSTRING2", "EVENTSTRING3"&#125;

-- In this case, EventString1 will be triggered every weekday at 10&#58;30 a.m.
-- EventString2 will be triggered only on Sundays at 11&#58;55 a.m.
-- EventString3 will be triggered every day at 5&#58;36 p.m.

wait = 5000

for tempi = 1,getn&#40;timertime&#41; do -- for debugging
print &#40;tempi&#41;
print &#40;timertime&#91;tempi&#93;&#41;
print &#40;timerday&#91;tempi&#93;&#41;
print &#40;timertriggerevent&#91;tempi&#93;&#41;
end

-- start the timer
TimerMonitor &#40;wait&#41;

I have still got to test the last script you gave me... Sorry

VaioUserChris
November 14th, 2004, 09:52 AM
Hi Dan,

I didn't know you could put "and" statements in parenthesis like that. That makes sense and is useful to know for future scripts. I actually discovered a similar problem as I was working on the last script I posted so I took a different route, breaking the logic tests up into three seperate If/Then statements. I think I got it fixed with the last script I posted here on Wed Nov 10, 2004 1:22 am.http://www.promixis.com/phpBB2/viewtopic.php?p=104471&highlight=#104471

Sorry for not alerting you to the problem though. I figured that you would use the latest script which I had fixed - but I guess I should have let you know that the earlier one didn't work correctly.:oops:

It's a little difficult to test these scripts (as you've found) because now we have daily, weekend, and a specific day as triggers, so it involves testing it over several days. When I tested it I tried to get around this by replacing the date statements with specific values so I could simulate any day of the week and any date - and the latest script worked for all the tests I performed (although we'll only really know with a little real-world testing).

Hopefully the latest script I wrote works for you. Sorry about missing the event on the earlier script.

Chris

danward79
November 14th, 2004, 10:01 AM
Hi Chris,

Don't worry it is not a problem. I have to do something you know!

Mark F
November 14th, 2004, 11:15 AM
Sorry for jumping in but you can change the time/date on your PC to test "corner cases" as well. I now return you to your normal programming. :)

danward79
December 2nd, 2004, 09:41 AM
Chris / Everyone.

Here is a slightly modified version of the Time based Timer Script.

I have changed it to allow, sending of payloads in the events. I needed it to allow my lights to stay in sync with Netremote.

Anyway, with out further adue....


function TimerMonitor &#40;interval&#41;
local err
Timer = TIMER_CreateObject &#40;&#41;
err = Timer.Create &#40;"","TimerHandler &#40;&#41;","",1&#41;
err = Timer.Arm &#40;interval&#41;
end

function TimerHandler &#40;&#41;
hour = date &#40;"%H"&#41;
minute = date &#40;"%M"&#41;
day = date &#40;"%Y%m%d"&#41;
weekday = date&#40;"%A"&#41;
--print&#40;weekday&#41;
weekdaynum = tonumber&#40;date &#40;"%w"&#41;&#41;
time = hour .."&#58;".. minute
--print&#40;time&#41;
for tempi = 1, getn&#40;timertime&#41; do
--print&#40;runningtimer&#91;tempi&#93;&#41;
if time == timertime&#91;tempi&#93; and runningtimer&#91;tempi&#93; ~= day then
--print&#40;"tempi ",timerday &#91;tempi&#93;&#41;
if timerday &#91;tempi&#93; == weekday or timerday &#91;tempi&#93; == "Everyday" or
&#40;timerday &#91;tempi&#93; == "Weekdays" and weekdaynum > 0 and weekdaynum < 6&#41; then
print&#40;"Event.... "..timertriggerevent&#91;tempi&#93;&#41;
TriggerEvent &#40;timertriggerevent&#91;tempi&#93;, 18, timertriggerpld1&#91;tempi&#93;, timertriggerpld2&#91;tempi&#93;&#41;
runningtimer&#91;tempi&#93; = day
end
end
end
end

runningtimer = &#123;&#125;
-- Options for timerday&#58;
-- You can enter the day of the week you want the timer to run &#40;i.e. "Monday"
-- or "Thursday"&#41; or "Everyday" if you want the timer triggered every day
-- or "Weekdays" if you only want the timer triggered on Weekdays
timerday = &#123;"Everyday", "Thursday"&#125; --"Everyday"
timertime = &#123;"18&#58;33", "18&#58;33"&#125;
timertriggerevent = &#123;"OSD", "OSD"&#125;
timertriggerpld1 = &#123;pld1, "Hi"&#125;
timertriggerpld2 = &#123;"Dan", "Dan"&#125;

-- In this case, EventString1 will be triggered every weekday at 10&#58;30 a.m.
-- EventString2 will be triggered only on Sundays at 11&#58;55 a.m.
-- EventString3 will be triggered every day at 5&#58;36 p.m.

wait = 8000

for tempi = 1,getn&#40;timertime&#41; do -- for debugging
print &#40;tempi&#41;
print &#40;timertime&#91;tempi&#93;&#41;
print &#40;timerday&#91;tempi&#93;&#41;
print &#40;timertriggerevent&#91;tempi&#93;&#41;
print &#40;timertriggerpld1&#91;tempi&#93;&#41;
print &#40;timertriggerpld2&#91;tempi&#93;&#41;
end

-- start the timer
TimerMonitor &#40;wait&#41;

-- TimerKill function is used to obliterate the timer. Just call this function
-- when you want to get rid of this timer.
function TimerKill &#40;&#41;

Timer.Cancel &#40;&#41;
Timer = nil
end

--TimerKill&#40;&#41;

VaioUserChris
December 2nd, 2004, 10:11 PM
Cool Dan.

But I noticed that you aren't using the version with triggers for weekends or a specific date. I took your example and added that in but yet again, I haven't had a chance to test it. I may have some time over the weekend but feel free to give it a shot before then if you have the time:
function TimerMonitor &#40;interval&#41;
local err
Timer = TIMER_CreateObject &#40;&#41;
err = Timer.Create &#40;"","TimerHandler &#40;&#41;","",1&#41;
err = Timer.Arm &#40;interval&#41;
end

function TimerHandler &#40;&#41;
hour = date &#40;"%H"&#41;
minute = date &#40;"%M"&#41;
day = date &#40;"%Y%m%d"&#41;
today = date&#40;"%x"&#41;
weekday = date&#40;"%A"&#41;
--print&#40;weekday&#41;
weekdaynum = tonumber&#40;date &#40;"%w"&#41;&#41;
time = hour .."&#58;".. minute
--print&#40;time&#41;
for tempi = 1, getn&#40;timertime&#41; do
--print&#40;runningtimer&#91;tempi&#93;&#41;
if time == timertime&#91;tempi&#93; and runningtimer&#91;tempi&#93; ~= day then
--print&#40;"tempi ",timerday &#91;tempi&#93;&#41;
if timerday &#91;tempi&#93; == weekday or timerday &#91;tempi&#93; == "Everyday" or
&#40;timerday &#91;tempi&#93; == "Weekdays" and weekdaynum > 0 and weekdaynum < 6&#41; or
&#40;timerday &#91;tempi&#93; == "Weekends" and weekdaynum == 0&#41; or
&#40;timerday &#91;tempi&#93; == "Weekends" and weekdaynum == 6&#41; or
timerday &#91;tempi&#93; == today then
print&#40;"Event.... "..timertriggerevent&#91;tempi&#93;&#41;
TriggerEvent &#40;timertriggerevent&#91;tempi&#93;, 18, timertriggerpld1&#91;tempi&#93;, timertriggerpld2&#91;tempi&#93;&#41;
runningtimer&#91;tempi&#93; = day
end
end
end
end

runningtimer = &#123;&#125;
-- Options for timerday&#58;
-- You can enter the day of the week you want the timer to run &#40;i.e. "Monday"
-- or "Thursday"&#41; or "Everyday" if you want the timer triggered every day
-- or "Weekdays" if you only want the timer triggered on Weekdays
timerday = &#123;"Everyday", "Thursday"&#125; --"Everyday"
timertime = &#123;"18&#58;33", "18&#58;33"&#125;
timertriggerevent = &#123;"OSD", "OSD"&#125;
timertriggerpld1 = &#123;pld1, "Hi"&#125;
timertriggerpld2 = &#123;"Dan", "Dan"&#125;

-- In this case, EventString1 will be triggered every weekday at 10&#58;30 a.m.
-- EventString2 will be triggered only on Sundays at 11&#58;55 a.m.
-- EventString3 will be triggered every day at 5&#58;36 p.m.

wait = 8000

for tempi = 1,getn&#40;timertime&#41; do -- for debugging
print &#40;tempi&#41;
print &#40;timertime&#91;tempi&#93;&#41;
print &#40;timerday&#91;tempi&#93;&#41;
print &#40;timertriggerevent&#91;tempi&#93;&#41;
print &#40;timertriggereventid&#91;tempi&#93;&#41;
print &#40;timertriggerpld1&#91;tempi&#93;&#41;
print &#40;timertriggerpld2&#91;tempi&#93;&#41;
end

-- start the timer
TimerMonitor &#40;wait&#41;

-- TimerKill function is used to obliterate the timer. Just call this function
-- when you want to get rid of this timer.
function TimerKill &#40;&#41;

Timer.Cancel &#40;&#41;
Timer = nil
end

--TimerKill&#40;&#41;

danward79
December 3rd, 2004, 01:40 AM
Hi Chris,

I just noticed I left a line in mine you need to take out aswell


print &#40;timertriggereventid&#91;tempi&#93;&#41;

I initially had event in there too, you could of course add event id as well.


But I noticed that you aren't using the version with triggers for weekends or a specific date.

I never got round to adding that one after I worked on this one... You know what it is like!

robertmyhtpc
April 7th, 2005, 08:36 PM
It was suggested I jump in this string for some direction. I am using the Server Timer to enable and disable 2 Command Groups for the purpose of a schedule. I have lights turn on if a command is received only if the group is enabled by being between two Server Timer events. My concern is that every time I shut down the pc or girder, I will lose my group status. Can you direct me on a script that will run on girder startup that will acitivate a group if the time is between x:xx and x:xx? I hope that was enough of a descripiton.
Thanks
Robert

danward79
April 7th, 2005, 09:00 PM
How about the sripts above?

VaioUserChris
April 7th, 2005, 09:44 PM
Robert,

I suggested you hop over here because if you use Dan's script that was posted above on 12/2, you can trigger an event at a specific time every day. So I figured you could use this to trigger your lighting scripts based on the times you specify.


Basically all I need is 2 scripts I think,

Send me an eventstring if it is between 7pm and 12 pm (19:00-23:59)
and another that will
Send me an eventstring if it is between 12am and 6 am (00:00:05-6:00)

Here's a quick script I tweaked which I think will do what you want...

function TimerMonitor &#40;interval&#41;
local err
Timer = TIMER_CreateObject &#40;&#41;
err = Timer.Create &#40;"","TimerHandler &#40;&#41;","",1&#41;
err = Timer.Arm &#40;interval&#41;
end

function TimerHandler &#40;&#41;
hour = date &#40;"%H"&#41;
day = date &#40;"%Y%m%d"&#41;
if hour >= 19 and hour <= 23 then -- evening event
if evening_timer ~= day then
TriggerEvent &#40;"your_evening_eventstring", 18&#41;
evening_timer = day
end
end
if hour >= 0 and hour <= 6 then -- morning event
if morning_timer ~= day then
TriggerEvent &#40;"your_morning_eventstring", 18&#41;
morning_timer = day
end
end
end

wait = 8000 --&#40;this will run the timer every 8 seconds to see if the time matches&#41;

-- start the timer
TimerMonitor &#40;wait&#41;

-- TimerKill function is used to obliterate the timer. Just call this function
-- when you want to get rid of this timer.
function TimerKill &#40;&#41;

Timer.Cancel &#40;&#41;
Timer = nil
end

--TimerKill&#40;&#41; This script will check the hour every 8 seconds to see if it's between either of the times you specified. If it is, it will trigger an event and set a marker for that date so it triggers the event only once per day.

Try it out and let me know if that works.

Chris