Skip to content

Commit

Permalink
Fix timed command expire
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng committed Sep 16, 2024
1 parent f805bbd commit 4efee18
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/openhab/dsl/items/timed_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ def reschedule(time = nil)
# @return [void]
#
def resume
self.resolution = nil
if expired?
logger.warn "Cannot resume a timed command that has expired. Use reschedule instead."
else
self.resolution = nil
end
end
end

Expand Down Expand Up @@ -216,14 +220,12 @@ def timed_command_timer(timed_command_details, duration)
DSL.after(duration) do
timed_command_details.mutex.synchronize do
logger.trace "Timed command expired - #{timed_command_details}"
DSL.rules[timed_command_details.rule_uid].disable
timed_command_details.resolution = :expired
case timed_command_details.on_expire
when Proc
logger.trace "Invoking block #{timed_command_details.on_expire} after timed command for #{name} expired"
timed_command_details.on_expire.call(timed_command_details)
if timed_command_details.resolution.nil?
logger.trace { "Block rescheduled the timer to #{timed_command_details.timer.execution_time}" }
end
when Core::Types::UnDefType
update(timed_command_details.on_expire)
else
Expand All @@ -232,6 +234,9 @@ def timed_command_timer(timed_command_details, duration)
if timed_command_details.resolution
DSL.rules.remove(timed_command_details.rule_uid)
TimedCommand.timed_commands.delete(timed_command_details.item)
else
DSL.rules[timed_command_details.rule_uid].enable
logger.trace { "Block rescheduled the timer to #{timed_command_details.timer.execution_time}" }
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/openhab/dsl/items/timed_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,29 @@ def self.test_it(initial_state, command)
expect(item.state).to eq 7
end

it "cannot be resumed when expired" do
timed_command_rule_uid = nil
resume_called = false

item.command(5, for: 2.seconds) do |timed_command|
timed_command_rule_uid = timed_command.rule_uid
if timed_command.expired?
timed_command.resume
resume_called = true
end
end

expect(resume_called).to be false
expect(OpenHAB::DSL::Items::TimedCommand.timed_commands[item.__getobj__]).not_to be_nil

time_travel_and_execute_timers(3.seconds)

expect(resume_called).to be true
expect(timed_command_rule_uid).not_to be_nil
expect(rules[timed_command_rule_uid]).to be_nil
expect(OpenHAB::DSL::Items::TimedCommand.timed_commands[item.__getobj__]).to be_nil
end

it "can be rescheduled" do
rescheduled = false
finalized = false
Expand Down

0 comments on commit 4efee18

Please sign in to comment.