mirror of
https://github.com/FlightControl-Master/MOOSE.git
synced 2025-10-29 16:58:06 +00:00
SCHEDULER
Clean up
This commit is contained in:
parent
5b9978e390
commit
07edcd7e73
@ -53,6 +53,19 @@ SCHEDULEDISPATCHER = {
|
|||||||
Schedule = nil,
|
Schedule = nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
--- Player data table holding all important parameters of each player.
|
||||||
|
-- @type SCHEDULEDISPATCHER.ScheduleData
|
||||||
|
-- @field #function Function The schedule function to be called.
|
||||||
|
-- @field #table Arguments Schedule function arguments.
|
||||||
|
-- @field #number Start Start time in seconds.
|
||||||
|
-- @field #number Repeat Repeat time intervall in seconds.
|
||||||
|
-- @field #number Randomize Randomization factor [0,1].
|
||||||
|
-- @field #number Stop Stop time in seconds.
|
||||||
|
-- @field #number StartTime Time in seconds when the scheduler is created.
|
||||||
|
-- @field #number ScheduleID Schedule ID.
|
||||||
|
-- @field #function CallHandler Function to be passed to the DCS timer.scheduleFunction().
|
||||||
|
-- @field #boolean ShowTrace If true, show tracing info.
|
||||||
|
|
||||||
--- Create a new schedule dispatcher object.
|
--- Create a new schedule dispatcher object.
|
||||||
-- @param #SCHEDULEDISPATCHER self
|
-- @param #SCHEDULEDISPATCHER self
|
||||||
-- @return #SCHEDULEDISPATCHER self
|
-- @return #SCHEDULEDISPATCHER self
|
||||||
@ -76,7 +89,7 @@ end
|
|||||||
-- @param #number Stop Stop time in seconds.
|
-- @param #number Stop Stop time in seconds.
|
||||||
-- @param #number TraceLevel Trace level [0,3].
|
-- @param #number TraceLevel Trace level [0,3].
|
||||||
-- @param Core.Fsm#FSM Fsm Finite state model.
|
-- @param Core.Fsm#FSM Fsm Finite state model.
|
||||||
-- @return #table Call ID or nil.
|
-- @return #string Call ID or nil.
|
||||||
function SCHEDULEDISPATCHER:AddSchedule( Scheduler, ScheduleFunction, ScheduleArguments, Start, Repeat, Randomize, Stop, TraceLevel, Fsm )
|
function SCHEDULEDISPATCHER:AddSchedule( Scheduler, ScheduleFunction, ScheduleArguments, Start, Repeat, Randomize, Stop, TraceLevel, Fsm )
|
||||||
self:F2( { Scheduler, ScheduleFunction, ScheduleArguments, Start, Repeat, Randomize, Stop, TraceLevel, Fsm } )
|
self:F2( { Scheduler, ScheduleFunction, ScheduleArguments, Start, Repeat, Randomize, Stop, TraceLevel, Fsm } )
|
||||||
|
|
||||||
@ -86,8 +99,9 @@ function SCHEDULEDISPATCHER:AddSchedule( Scheduler, ScheduleFunction, ScheduleAr
|
|||||||
-- Create ID.
|
-- Create ID.
|
||||||
local CallID = self.CallID .. "#" .. ( Scheduler.MasterObject and Scheduler.MasterObject.GetClassNameAndID and Scheduler.MasterObject:GetClassNameAndID() or "" ) or ""
|
local CallID = self.CallID .. "#" .. ( Scheduler.MasterObject and Scheduler.MasterObject.GetClassNameAndID and Scheduler.MasterObject:GetClassNameAndID() or "" ) or ""
|
||||||
|
|
||||||
-- Initialize the ObjectSchedulers array, which is a weakly coupled table.
|
self:T2(string.format("Adding schedule #%d CallID=%s", self.CallID, CallID))
|
||||||
-- If the object used as the key is nil, then the garbage collector will remove the item from the Functions array.
|
|
||||||
|
-- Initialize PersistentSchedulers
|
||||||
self.PersistentSchedulers = self.PersistentSchedulers or {}
|
self.PersistentSchedulers = self.PersistentSchedulers or {}
|
||||||
|
|
||||||
-- Initialize the ObjectSchedulers array, which is a weakly coupled table.
|
-- Initialize the ObjectSchedulers array, which is a weakly coupled table.
|
||||||
@ -104,11 +118,11 @@ function SCHEDULEDISPATCHER:AddSchedule( Scheduler, ScheduleFunction, ScheduleAr
|
|||||||
|
|
||||||
self.Schedule = self.Schedule or setmetatable( {}, { __mode = "k" } )
|
self.Schedule = self.Schedule or setmetatable( {}, { __mode = "k" } )
|
||||||
self.Schedule[Scheduler] = self.Schedule[Scheduler] or {}
|
self.Schedule[Scheduler] = self.Schedule[Scheduler] or {}
|
||||||
self.Schedule[Scheduler][CallID] = {}
|
self.Schedule[Scheduler][CallID] = {} --#SCHEDULEDISPATCHER.ScheduleData
|
||||||
self.Schedule[Scheduler][CallID].Function = ScheduleFunction
|
self.Schedule[Scheduler][CallID].Function = ScheduleFunction
|
||||||
self.Schedule[Scheduler][CallID].Arguments = ScheduleArguments
|
self.Schedule[Scheduler][CallID].Arguments = ScheduleArguments
|
||||||
self.Schedule[Scheduler][CallID].StartTime = timer.getTime() + ( Start or 0 )
|
self.Schedule[Scheduler][CallID].StartTime = timer.getTime() + ( Start or 0 )
|
||||||
self.Schedule[Scheduler][CallID].Start = Start + .1
|
self.Schedule[Scheduler][CallID].Start = Start + 0.1
|
||||||
self.Schedule[Scheduler][CallID].Repeat = Repeat or 0
|
self.Schedule[Scheduler][CallID].Repeat = Repeat or 0
|
||||||
self.Schedule[Scheduler][CallID].Randomize = Randomize or 0
|
self.Schedule[Scheduler][CallID].Randomize = Randomize or 0
|
||||||
self.Schedule[Scheduler][CallID].Stop = Stop
|
self.Schedule[Scheduler][CallID].Stop = Stop
|
||||||
@ -150,6 +164,7 @@ function SCHEDULEDISPATCHER:AddSchedule( Scheduler, ScheduleFunction, ScheduleAr
|
|||||||
|
|
||||||
self:T3( self.Schedule[Scheduler][CallID] )
|
self:T3( self.Schedule[Scheduler][CallID] )
|
||||||
|
|
||||||
|
--- Function passed to the DCS timer.scheduleFunction()
|
||||||
self.Schedule[Scheduler][CallID].CallHandler = function( Params )
|
self.Schedule[Scheduler][CallID].CallHandler = function( Params )
|
||||||
|
|
||||||
local CallID = Params.CallID
|
local CallID = Params.CallID
|
||||||
@ -166,7 +181,8 @@ function SCHEDULEDISPATCHER:AddSchedule( Scheduler, ScheduleFunction, ScheduleAr
|
|||||||
return errmsg
|
return errmsg
|
||||||
end
|
end
|
||||||
|
|
||||||
local Scheduler = self.ObjectSchedulers[CallID]
|
-- Get object or persistant scheduler object.
|
||||||
|
local Scheduler = self.ObjectSchedulers[CallID] --Core.Scheduler#SCHEDULER
|
||||||
if not Scheduler then
|
if not Scheduler then
|
||||||
Scheduler = self.PersistentSchedulers[CallID]
|
Scheduler = self.PersistentSchedulers[CallID]
|
||||||
end
|
end
|
||||||
@ -176,11 +192,15 @@ function SCHEDULEDISPATCHER:AddSchedule( Scheduler, ScheduleFunction, ScheduleAr
|
|||||||
if Scheduler then
|
if Scheduler then
|
||||||
|
|
||||||
local MasterObject = tostring(Scheduler.MasterObject)
|
local MasterObject = tostring(Scheduler.MasterObject)
|
||||||
local Schedule = self.Schedule[Scheduler][CallID]
|
|
||||||
|
-- Schedule object.
|
||||||
|
local Schedule = self.Schedule[Scheduler][CallID] --#SCHEDULEDISPATCHER.ScheduleData
|
||||||
|
|
||||||
--self:T3( { Schedule = Schedule } )
|
--self:T3( { Schedule = Schedule } )
|
||||||
|
|
||||||
local SchedulerObject = Scheduler.MasterObject --Scheduler.SchedulerObject Now is this the Maste or Scheduler object?
|
local SchedulerObject = Scheduler.MasterObject --Scheduler.SchedulerObject Now is this the Maste or Scheduler object?
|
||||||
|
local ShowTrace = Scheduler.ShowTrace
|
||||||
|
|
||||||
local ScheduleFunction = Schedule.Function
|
local ScheduleFunction = Schedule.Function
|
||||||
local ScheduleArguments = Schedule.Arguments or {}
|
local ScheduleArguments = Schedule.Arguments or {}
|
||||||
local Start = Schedule.Start
|
local Start = Schedule.Start
|
||||||
@ -188,7 +208,7 @@ function SCHEDULEDISPATCHER:AddSchedule( Scheduler, ScheduleFunction, ScheduleAr
|
|||||||
local Randomize = Schedule.Randomize or 0
|
local Randomize = Schedule.Randomize or 0
|
||||||
local Stop = Schedule.Stop or 0
|
local Stop = Schedule.Stop or 0
|
||||||
local ScheduleID = Schedule.ScheduleID
|
local ScheduleID = Schedule.ScheduleID
|
||||||
local ShowTrace = Scheduler.ShowTrace
|
|
||||||
|
|
||||||
local Prefix = ( Repeat == 0 ) and "--->" or "+++>"
|
local Prefix = ( Repeat == 0 ) and "--->" or "+++>"
|
||||||
|
|
||||||
@ -215,24 +235,20 @@ function SCHEDULEDISPATCHER:AddSchedule( Scheduler, ScheduleFunction, ScheduleAr
|
|||||||
local CurrentTime = timer.getTime()
|
local CurrentTime = timer.getTime()
|
||||||
local StartTime = Schedule.StartTime
|
local StartTime = Schedule.StartTime
|
||||||
|
|
||||||
self:F3( { Master = MasterObject, CurrentTime = CurrentTime, StartTime = StartTime, Start = Start, Repeat = Repeat, Randomize = Randomize, Stop = Stop } )
|
-- Debug info.
|
||||||
|
self:F3( { CallID=CallID, ScheduleID=ScheduleID, Master = MasterObject, CurrentTime = CurrentTime, StartTime = StartTime, Start = Start, Repeat = Repeat, Randomize = Randomize, Stop = Stop } )
|
||||||
|
|
||||||
|
|
||||||
if Status and (( Result == nil ) or ( Result and Result ~= false ) ) then
|
if Status and (( Result == nil ) or ( Result and Result ~= false ) ) then
|
||||||
|
|
||||||
if Repeat ~= 0 and ( ( Stop == 0 ) or ( Stop ~= 0 and CurrentTime <= StartTime + Stop ) ) then
|
if Repeat ~= 0 and ( ( Stop == 0 ) or ( Stop ~= 0 and CurrentTime <= StartTime + Stop ) ) then
|
||||||
local ScheduleTime =
|
local ScheduleTime = CurrentTime + Repeat + math.random(- ( Randomize * Repeat / 2 ), ( Randomize * Repeat / 2 )) + 0.0001 -- Accuracy
|
||||||
CurrentTime +
|
|
||||||
Repeat +
|
|
||||||
math.random(
|
|
||||||
- ( Randomize * Repeat / 2 ),
|
|
||||||
( Randomize * Repeat / 2 )
|
|
||||||
) +
|
|
||||||
0.0001 -- Accuracy
|
|
||||||
--self:T3( { Repeat = CallID, CurrentTime, ScheduleTime, ScheduleArguments } )
|
--self:T3( { Repeat = CallID, CurrentTime, ScheduleTime, ScheduleArguments } )
|
||||||
return ScheduleTime -- returns the next time the function needs to be called.
|
return ScheduleTime -- returns the next time the function needs to be called.
|
||||||
else
|
else
|
||||||
self:Stop( Scheduler, CallID )
|
self:Stop( Scheduler, CallID )
|
||||||
end
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
self:Stop( Scheduler, CallID )
|
self:Stop( Scheduler, CallID )
|
||||||
end
|
end
|
||||||
@ -265,24 +281,27 @@ end
|
|||||||
-- @param #SCHEDULEDISPATCHER self
|
-- @param #SCHEDULEDISPATCHER self
|
||||||
-- @param Core.Scheduler#SCHEDULER Scheduler Scheduler object.
|
-- @param Core.Scheduler#SCHEDULER Scheduler Scheduler object.
|
||||||
-- @param #table CallID (Optional) Call ID.
|
-- @param #table CallID (Optional) Call ID.
|
||||||
-- @param #table CallID Call ID.
|
|
||||||
-- @param #string Info (Optional) Debug info.
|
-- @param #string Info (Optional) Debug info.
|
||||||
function SCHEDULEDISPATCHER:Start( Scheduler, CallID, Info )
|
function SCHEDULEDISPATCHER:Start( Scheduler, CallID, Info )
|
||||||
self:F2( { Start = CallID, Scheduler = Scheduler } )
|
self:F2( { Start = CallID, Scheduler = Scheduler } )
|
||||||
|
|
||||||
if CallID then
|
if CallID then
|
||||||
|
|
||||||
local Schedule = self.Schedule[Scheduler]
|
local Schedule = self.Schedule[Scheduler][CallID] --#SCHEDULEDISPATCHER.ScheduleData
|
||||||
|
|
||||||
-- Only start when there is no ScheduleID defined!
|
-- Only start when there is no ScheduleID defined!
|
||||||
-- This prevents to "Start" the scheduler twice with the same CallID...
|
-- This prevents to "Start" the scheduler twice with the same CallID...
|
||||||
if not Schedule[CallID].ScheduleID then
|
if not Schedule.ScheduleID then
|
||||||
Schedule[CallID].StartTime = timer.getTime() -- Set the StartTime field to indicate when the scheduler started.
|
|
||||||
Schedule[CallID].ScheduleID = timer.scheduleFunction(
|
-- Current time in seconds.
|
||||||
Schedule[CallID].CallHandler,
|
local Tnow=timer.getTime()
|
||||||
{ CallID = CallID, Info = Info },
|
|
||||||
timer.getTime() + Schedule[CallID].Start
|
Schedule.StartTime = Tnow -- Set the StartTime field to indicate when the scheduler started.
|
||||||
)
|
|
||||||
|
-- Start DCS schedule function https://wiki.hoggitworld.com/view/DCS_func_scheduleFunction
|
||||||
|
Schedule.ScheduleID = timer.scheduleFunction(Schedule.CallHandler, { CallID = CallID, Info = Info }, Tnow + Schedule.Start)
|
||||||
|
|
||||||
|
self:T(string.format("Starting scheduledispatcher Call ID=%s ==> Schedule ID=%s", tostring(CallID), tostring(Schedule.ScheduleID)))
|
||||||
end
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
@ -304,12 +323,20 @@ function SCHEDULEDISPATCHER:Stop( Scheduler, CallID )
|
|||||||
|
|
||||||
if CallID then
|
if CallID then
|
||||||
|
|
||||||
local Schedule = self.Schedule[Scheduler]
|
local Schedule = self.Schedule[Scheduler][CallID] --#SCHEDULEDISPATCHER.ScheduleData
|
||||||
|
|
||||||
-- Only stop when there is a ScheduleID defined for the CallID. So, when the scheduler was stopped before, do nothing.
|
-- Only stop when there is a ScheduleID defined for the CallID. So, when the scheduler was stopped before, do nothing.
|
||||||
if Schedule[CallID].ScheduleID then
|
if Schedule.ScheduleID then
|
||||||
timer.removeFunction( Schedule[CallID].ScheduleID )
|
|
||||||
Schedule[CallID].ScheduleID = nil
|
self:T(string.format("scheduledispatcher stopping scheduler CallID=%s, ScheduleID=%s", tostring(CallID), tostring(Schedule.ScheduleID)))
|
||||||
|
|
||||||
|
-- Remove schedule function https://wiki.hoggitworld.com/view/DCS_func_removeFunction
|
||||||
|
timer.removeFunction(Schedule.ScheduleID)
|
||||||
|
|
||||||
|
Schedule.ScheduleID = nil
|
||||||
|
|
||||||
|
else
|
||||||
|
self:E(string.format("Error no ScheduleID for CallID=%s", tostring(CallID)))
|
||||||
end
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|||||||
@ -221,7 +221,7 @@ function SCHEDULER:New( MasterObject, SchedulerFunction, SchedulerArguments, Sta
|
|||||||
self.ShowTrace = false
|
self.ShowTrace = false
|
||||||
|
|
||||||
if SchedulerFunction then
|
if SchedulerFunction then
|
||||||
ScheduleID = self:Schedule( MasterObject, SchedulerFunction, SchedulerArguments, Start, Repeat, RandomizeFactor, Stop, 4 )
|
ScheduleID = self:Schedule( MasterObject, SchedulerFunction, SchedulerArguments, Start, Repeat, RandomizeFactor, Stop, 3 )
|
||||||
end
|
end
|
||||||
|
|
||||||
return self, ScheduleID
|
return self, ScheduleID
|
||||||
@ -243,13 +243,17 @@ function SCHEDULER:Schedule( MasterObject, SchedulerFunction, SchedulerArguments
|
|||||||
self:F2( { Start, Repeat, RandomizeFactor, Stop } )
|
self:F2( { Start, Repeat, RandomizeFactor, Stop } )
|
||||||
self:T3( { SchedulerArguments } )
|
self:T3( { SchedulerArguments } )
|
||||||
|
|
||||||
|
-- Debug info.
|
||||||
local ObjectName = "-"
|
local ObjectName = "-"
|
||||||
if MasterObject and MasterObject.ClassName and MasterObject.ClassID then
|
if MasterObject and MasterObject.ClassName and MasterObject.ClassID then
|
||||||
ObjectName = MasterObject.ClassName .. MasterObject.ClassID
|
ObjectName = MasterObject.ClassName .. MasterObject.ClassID
|
||||||
end
|
end
|
||||||
self:F3( { "Schedule :", ObjectName, tostring( MasterObject ), Start, Repeat, RandomizeFactor, Stop } )
|
self:F3( { "Schedule :", ObjectName, tostring( MasterObject ), Start, Repeat, RandomizeFactor, Stop } )
|
||||||
|
|
||||||
|
-- Set master object.
|
||||||
self.MasterObject = MasterObject
|
self.MasterObject = MasterObject
|
||||||
|
|
||||||
|
-- Add schedule.
|
||||||
local ScheduleID = _SCHEDULEDISPATCHER:AddSchedule(
|
local ScheduleID = _SCHEDULEDISPATCHER:AddSchedule(
|
||||||
self,
|
self,
|
||||||
SchedulerFunction,
|
SchedulerFunction,
|
||||||
@ -269,32 +273,36 @@ end
|
|||||||
|
|
||||||
--- (Re-)Starts the schedules or a specific schedule if a valid ScheduleID is provided.
|
--- (Re-)Starts the schedules or a specific schedule if a valid ScheduleID is provided.
|
||||||
-- @param #SCHEDULER self
|
-- @param #SCHEDULER self
|
||||||
-- @param #table ScheduleID (Optional) The ScheduleID of the planned (repeating) schedule.
|
-- @param #string ScheduleID (Optional) The ScheduleID of the planned (repeating) schedule.
|
||||||
function SCHEDULER:Start( ScheduleID )
|
function SCHEDULER:Start( ScheduleID )
|
||||||
self:F3( { ScheduleID } )
|
self:F3( { ScheduleID } )
|
||||||
|
self:T(string.format("Starting scheduler ID=%s", tostring(ScheduleID)))
|
||||||
_SCHEDULEDISPATCHER:Start( self, ScheduleID )
|
_SCHEDULEDISPATCHER:Start( self, ScheduleID )
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Stops the schedules or a specific schedule if a valid ScheduleID is provided.
|
--- Stops the schedules or a specific schedule if a valid ScheduleID is provided.
|
||||||
-- @param #SCHEDULER self
|
-- @param #SCHEDULER self
|
||||||
-- @param #table ScheduleID (Optional) The ScheduleID of the planned (repeating) schedule.
|
-- @param #string ScheduleID (Optional) The ScheduleID of the planned (repeating) schedule.
|
||||||
function SCHEDULER:Stop( ScheduleID )
|
function SCHEDULER:Stop( ScheduleID )
|
||||||
self:F3( { ScheduleID } )
|
self:F3( { ScheduleID } )
|
||||||
|
self:T(string.format("Stopping scheduler ID=%s", tostring(ScheduleID)))
|
||||||
_SCHEDULEDISPATCHER:Stop( self, ScheduleID )
|
_SCHEDULEDISPATCHER:Stop( self, ScheduleID )
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Removes a specific schedule if a valid ScheduleID is provided.
|
--- Removes a specific schedule if a valid ScheduleID is provided.
|
||||||
-- @param #SCHEDULER self
|
-- @param #SCHEDULER self
|
||||||
-- @param #number ScheduleID (optional) The ScheduleID of the planned (repeating) schedule.
|
-- @param #string ScheduleID (optional) The ScheduleID of the planned (repeating) schedule.
|
||||||
function SCHEDULER:Remove( ScheduleID )
|
function SCHEDULER:Remove( ScheduleID )
|
||||||
self:F3( { ScheduleID } )
|
self:F3( { ScheduleID } )
|
||||||
_SCHEDULEDISPATCHER:Remove( self, ScheduleID )
|
self:T(string.format("Removing scheduler ID=%s", tostring(ScheduleID)))
|
||||||
|
_SCHEDULEDISPATCHER:RemoveSchedule( self, ScheduleID )
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Clears all pending schedules.
|
--- Clears all pending schedules.
|
||||||
-- @param #SCHEDULER self
|
-- @param #SCHEDULER self
|
||||||
function SCHEDULER:Clear()
|
function SCHEDULER:Clear()
|
||||||
self:F3( )
|
self:F3( )
|
||||||
|
self:T(string.format("Clearing scheduler"))
|
||||||
_SCHEDULEDISPATCHER:Clear( self )
|
_SCHEDULEDISPATCHER:Clear( self )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user