class AmpelExtase::Controller

Public Class Methods

for( serial:, semaphore_url:, warning_jenkins_url: nil, sleep: 10 ) click to toggle source
# File lib/ampel_extase/controller.rb, line 12
def self.for(
  serial:,
  semaphore_url:,
  warning_jenkins_url: nil,
  sleep: 10
)
  ampel_semaphore = AmpelExtase::SemaphoreStateObserver.for_url(semaphore_url)
  urls            = warning_jenkins_url.full?(:split, ?,) || []
  warning_jenkins = AmpelExtase::JenkinsWarningStateObserver.for_urls(*urls)
  lights          = AmpelExtase::LightSwitcher.for(serial: serial)
  new(ampel_semaphore, warning_jenkins, lights, sleep: sleep)
end
new( ampel_semaphore, warning_jenkins, lights, sleep: 10 ) click to toggle source
# File lib/ampel_extase/controller.rb, line 25
def initialize(
  ampel_semaphore,
  warning_jenkins,
  lights,
  sleep: 10
)
  @ampel_semaphore, @warning_jenkins, @lights, @sleep =
    ampel_semaphore, warning_jenkins, lights, sleep
  @expire_duration = 6 * @sleep
  check_lights
end

Public Instance Methods

start() click to toggle source
# File lib/ampel_extase/controller.rb, line 37
def start
  puts "starting controller loop"
  at_exit { stop }
  loop do
    begin
      perform
    rescue => e
      handle_crash e
    end
    sleep_duration
  end
end
stop() click to toggle source
# File lib/ampel_extase/controller.rb, line 50
def stop
  switch_all_lights_off
  self
end

Private Instance Methods

check_lights() click to toggle source
# File lib/ampel_extase/controller.rb, line 143
def check_lights
  puts "checking lights configuration"
  switch_all_lights_on
  sleep 1
  switch_all_lights_off
  sleep 1
  puts "OK"
end
expire_warning() click to toggle source
# File lib/ampel_extase/controller.rb, line 105
def expire_warning
  if @warning_jenkins.expired?(@expire_duration)
    @lights.aux.off
    puts info('WARNING EXPIRED')
  end
end
failure(message) click to toggle source
# File lib/ampel_extase/controller.rb, line 116
def failure(message)
  red message
end
failure_building(message) click to toggle source
# File lib/ampel_extase/controller.rb, line 120
def failure_building(message)
  green on_red message
end
handle_crash(exception) click to toggle source
# File lib/ampel_extase/controller.rb, line 136
def handle_crash(exception)
  warn "Caught: #{exception.class}: #{exception}\n#{exception.backtrace * ?\n}"
  @ampel_semaphore.reset
  @warning_jenkins.reset
  switch_all_lights_off
end
info(message) click to toggle source
# File lib/ampel_extase/controller.rb, line 124
def info(message)
  yellow message
end
perform() click to toggle source
# File lib/ampel_extase/controller.rb, line 62
def perform
  @ampel_semaphore.on_state_change do |state|
    perform_lights_switch state
  end
  @warning_jenkins.on_state_change(@expire_duration) do |state|
    perform_warning state
  end
  expire_warning
end
perform_lights_switch(state) click to toggle source
# File lib/ampel_extase/controller.rb, line 72
def perform_lights_switch(state)
  if state.success?
    @lights.green.on
    @lights.red.off
    puts success('LIGHTS SUCCESS')
  else
    @lights.red.on
    if state.building?
      @lights.green.on
      puts failure_building('LIGHTS FAILURE BUILDING')
    else
      @lights.green.off
      puts failure('LIGHTS FAILURE')
    end
  end
end
perform_warning(state) click to toggle source
# File lib/ampel_extase/controller.rb, line 89
def perform_warning(state)
  case state.last_result
  when 'SUCCESS'
    @lights.aux.off
    puts success('WARNING SUCCESS')
  when 'FAILURE', 'ABORTED'
    if state.building?
      @lights.aux.off
      puts failure_building('WARNING FAILURE BUILDING')
    else
      @lights.aux.on
      puts failure('WARNING FAILURE')
    end
  end
end
sleep_duration() click to toggle source
# File lib/ampel_extase/controller.rb, line 57
def sleep_duration
  puts "sleep for #@sleep seconds"
  sleep @sleep
end
success(message) click to toggle source
# File lib/ampel_extase/controller.rb, line 112
def success(message)
  green message
end
switch_all_lights_off() click to toggle source
# File lib/ampel_extase/controller.rb, line 128
def switch_all_lights_off
  @lights.each(&:off)
end
switch_all_lights_on() click to toggle source
# File lib/ampel_extase/controller.rb, line 132
def switch_all_lights_on
  @lights.each(&:on)
end