Class: RSMP::Tlc

Inherits:
Site show all
Defined in:
lib/rsmp/tlc.rb

Instance Attribute Summary collapse

Attributes inherited from Site

#logger, #proxies, #rsmp_versions, #site_settings

Attributes included from Components

#components

Attributes inherited from Node

#archive, #clock, #deferred, #error_queue, #logger, #task

Attributes included from Logging

#archive, #logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Site

#aggregated_status_changed, #alarm, #build_proxy, #check_sxl_version, #connect_to_supervisor, #find_supervisor, #handle_site_settings, #reconnect, #run_site_proxy, #site_id, #starting, #stop, #wait_for_supervisor

Methods included from Components

#add_component, #aggregated_status_changed, #find_component, #initialize_components, #setup_components

Methods inherited from Node

#author, #check_required_settings, #defer, #do_start, #exiting, #idle, #notify_error, #process_deferred, #restart, #start, #stop

Methods included from Inspect

#inspect, #inspector

Methods included from Wait

#wait_for

Methods included from Logging

#author, #initialize_logging, #log

Constructor Details

#initialize(options = {}) ⇒ Tlc

Returns a new instance of Tlc.


797
798
799
800
801
802
803
804
805
# File 'lib/rsmp/tlc.rb', line 797

def initialize options={}
  super options
  @sxl = 'traffic_light_controller'
  @security_codes = options[:site_settings]['security_codes']
  @interval = options[:site_settings].dig('intervals','timer') || 1
  unless @main
    raise ConfigurationError.new "TLC must have a main component"
  end
end

Instance Attribute Details

#mainObject

Returns the value of attribute main.


796
797
798
# File 'lib/rsmp/tlc.rb', line 796

def main
  @main
end

Class Method Details

.from_rsmp_bool(str) ⇒ Object


889
890
891
# File 'lib/rsmp/tlc.rb', line 889

def self.from_rsmp_bool str
  str == 'True'
end

.make_status(value, q = 'recent') ⇒ Object


893
894
895
896
897
898
899
900
# File 'lib/rsmp/tlc.rb', line 893

def self.make_status value, q='recent'
  case value
  when true, false
    return to_rmsp_bool(value), q
  else
    return value, q
  end
end

.to_rmsp_bool(bool) ⇒ Object


881
882
883
884
885
886
887
# File 'lib/rsmp/tlc.rb', line 881

def self.to_rmsp_bool bool
  if bool
    'True'
  else
    'False'
  end
end

Instance Method Details

#build_component(id:, type:, settings: {}) ⇒ Object


807
808
809
810
811
812
813
814
815
816
817
818
819
820
# File 'lib/rsmp/tlc.rb', line 807

def build_component id:, type:, settings:{}
  component = case type
  when 'main'
    @main = TrafficController.new node: self, id: id, cycle_time: settings['cycle_time']
  when 'signal_group'
    group = SignalGroup.new node: self, id: id, plan: settings['plan']
    @main.add_signal_group group
    group
  when 'detector_logic'
    logic = DetectorLogic.new node: self, id: id
    @main.add_detector_logic logic
    logic
  end
end

#change_security_code(level, old_code, new_code) ⇒ Object


876
877
878
879
# File 'lib/rsmp/tlc.rb', line 876

def change_security_code level, old_code, new_code
  verify_security_code level, old_code
  @security_codes[level] = new_code
end

#do_deferred(item) ⇒ Object


902
903
904
905
906
907
908
# File 'lib/rsmp/tlc.rb', line 902

def do_deferred item
  case item
  when :restart
    log "Restarting TLC", level: :info
    restart
  end
end

#start_actionObject


822
823
824
825
# File 'lib/rsmp/tlc.rb', line 822

def start_action
  super
  start_timer
end

#start_timerObject


827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
# File 'lib/rsmp/tlc.rb', line 827

def start_timer
  task_name = "tlc timer"
  log "Starting #{task_name} with interval #{@interval} seconds", level: :debug

  @timer = @task.async do |task|
    task.annotate task_name
    next_time = Time.now.to_f
    loop do
      begin
        timer(@clock.now)
      rescue EOFError => e
        log "Connection closed: #{e}", level: :warning
      rescue IOError => e
        log "IOError", level: :warning
      rescue Errno::ECONNRESET
        log "Connection reset by peer", level: :warning
      rescue Errno::EPIPE => e
        log "Broken pipe", level: :warning
      rescue StandardError => e
        notify_error e, level: :internal
      ensure
        # adjust sleep duration to avoid drift. so wake up always happens on the
        # same fractional second.
        # note that Time.now is not monotonic. If the clock is changed,
        # either manaully or via NTP, the sleep interval might jump.
        # an alternative is to use ::Process.clock_gettime(::Process::CLOCK_MONOTONIC),
        # to get the current time. this ensures a constant interval, but
        # if the clock is changed, the wake up would then happen on a different
        # fractional second
        next_time += @interval
        duration = next_time - Time.now.to_f
        task.sleep duration
      end
    end
  end
end

#timer(now) ⇒ Object


864
865
866
867
# File 'lib/rsmp/tlc.rb', line 864

def timer now
  return unless @main
  @main.timer now
end

#verify_security_code(level, code) ⇒ Object

Raises:

  • (ArgumentError)

869
870
871
872
873
874
# File 'lib/rsmp/tlc.rb', line 869

def verify_security_code level, code
  raise ArgumentError.new("Level must be 1-2, got #{level}") unless (1..2).include?(level)
  if @security_codes[level] != code
    raise MessageRejected.new("Wrong security code for level #{level}")
  end
end