Module: RSMP::TLC::Modules::Plans

Included in:
TrafficController
Defined in:
lib/rsmp/tlc/modules/plans.rb

Overview

Time plans, traffic situations, and scheduling management Handles time plan selection, dynamic bands, schedules, and cycle times

Instance Method Summary collapse

Instance Method Details

#current_planObject



7
8
9
10
11
12
# File 'lib/rsmp/tlc/modules/plans.rb', line 7

def current_plan
  # TODO: plan 0 should means use time table
  return unless @plans

  @plans[plan] || @plans.values.first
end

#find_plan(plan_nr) ⇒ Object

Raises:



30
31
32
33
34
35
# File 'lib/rsmp/tlc/modules/plans.rb', line 30

def find_plan(plan_nr)
  plan = @plans[plan_nr.to_i]
  raise InvalidMessage, "unknown signal plan #{plan_nr}, known only [#{@plans.keys.join(', ')}]" unless plan

  plan
end

#handle_m0002(arg, _options = {}) ⇒ Object

M0002 - Set current time plan



15
16
17
18
19
20
21
22
# File 'lib/rsmp/tlc/modules/plans.rb', line 15

def handle_m0002(arg, _options = {})
  @node.verify_security_code 2, arg['securityCode']
  if TrafficControllerSite.from_rsmp_bool?(arg['status'])
    switch_plan arg['timeplan'], source: 'forced'
  else
    switch_plan 0, source: 'startup' # TODO: use clock/calender
  end
end

#handle_m0003(arg, _options = {}) ⇒ Object

M0003 - Set traffic situation



25
26
27
28
# File 'lib/rsmp/tlc/modules/plans.rb', line 25

def handle_m0003(arg, _options = {})
  @node.verify_security_code 2, arg['securityCode']
  switch_traffic_situation arg['traficsituation']
end

#handle_m0014(arg, _options = {}) ⇒ Object

M0014 - Set dynamic bands



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rsmp/tlc/modules/plans.rb', line 55

def handle_m0014(arg, _options = {})
  @node.verify_security_code 2, arg['securityCode']
  plan = find_plan arg['plan']
  arg['status'].split(',').each do |item|
    matched = /(\d+)-(\d+)/.match item
    band = matched[1].to_i
    value = matched[2].to_i
    log "Set plan #{arg['plan']} dynamic band #{band} to #{value}", level: :info
    plan.set_band band, value
  end
end

#handle_m0015(arg, _options = {}) ⇒ Object

M0015 - Set offset time



68
69
70
# File 'lib/rsmp/tlc/modules/plans.rb', line 68

def handle_m0015(arg, _options = {})
  @node.verify_security_code 2, arg['securityCode']
end

#handle_m0016(arg, _options = {}) ⇒ Object

M0016 - Set week time table



73
74
75
# File 'lib/rsmp/tlc/modules/plans.rb', line 73

def handle_m0016(arg, _options = {})
  @node.verify_security_code 2, arg['securityCode']
end

#handle_m0017(arg, _options = {}) ⇒ Object

M0017 - Set time tables



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rsmp/tlc/modules/plans.rb', line 78

def handle_m0017(arg, _options = {})
  @node.verify_security_code 2, arg['securityCode']
  arg['status'].split(',').each do |item|
    elems = item.split('-')
    nr = elems[0].to_i
    plan = elems[1].to_i
    hour = elems[2].to_i
    min = elems[3].to_i
    raise InvalidMessage, "time table id must be between 0 and 12, got #{nr}" if nr.negative? || nr > 12

    @day_time_table[nr] = { plan: plan, hour: hour, min: min }
  end
end

#handle_m0018(arg, _options = {}) ⇒ Object

M0018 - Set cycle time



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rsmp/tlc/modules/plans.rb', line 93

def handle_m0018(arg, _options = {})
  @node.verify_security_code 2, arg['securityCode']
  nr = arg['plan'].to_i
  cycle_time = arg['status'].to_i
  plan = @plans[nr]
  raise RSMP::MessageRejected, "Plan '#{nr}' not found" unless plan
  raise RSMP::MessageRejected, 'Cycle time must be greater or equal to zero' if cycle_time.negative?

  log "Set plan #{nr} cycle time to #{cycle_time}", level: :info
  plan.cycle_time = cycle_time
end

#handle_m0023(arg, _options = {}) ⇒ Object

M0023 - Dynamic bands timeout



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rsmp/tlc/modules/plans.rb', line 106

def handle_m0023(arg, _options = {})
  @node.verify_security_code 2, arg['securityCode']
  timeout = arg['status'].to_i
  unless (timeout >= 0) && (timeout <= 65_535)
    raise RSMP::MessageRejected,
          "Timeout must be in the range 0-65535, got #{timeout}"
  end

  if timeout.zero?
    log 'Dynamic bands timeout disabled', level: :info
  else
    log "Dynamic bands timeout set to #{timeout}min", level: :info
  end
  @dynamic_bands_timeout = timeout
end

#handle_s0014(_status_code, status_name = nil, _options = {}) ⇒ Object

S0014 - Current signal program



123
124
125
126
127
128
129
130
# File 'lib/rsmp/tlc/modules/plans.rb', line 123

def handle_s0014(_status_code, status_name = nil, _options = {})
  case status_name
  when 'status'
    TrafficControllerSite.make_status @plan
  when 'source'
    TrafficControllerSite.make_status @plan_source
  end
end

#handle_s0015(_status_code, status_name = nil, _options = {}) ⇒ Object

S0015 - Current traffic situation



133
134
135
136
137
138
139
140
# File 'lib/rsmp/tlc/modules/plans.rb', line 133

def handle_s0015(_status_code, status_name = nil, _options = {})
  case status_name
  when 'status'
    TrafficControllerSite.make_status @traffic_situation
  when 'source'
    TrafficControllerSite.make_status @traffic_situation_source
  end
end

#handle_s0018(_status_code, status_name = nil, _options = {}) ⇒ Object

S0018 - Number of time plans



143
144
145
146
147
148
# File 'lib/rsmp/tlc/modules/plans.rb', line 143

def handle_s0018(_status_code, status_name = nil, _options = {})
  case status_name
  when 'number'
    TrafficControllerSite.make_status @plans.size
  end
end

#handle_s0019(_status_code, status_name = nil, _options = {}) ⇒ Object

S0019 - Number of traffic situations



151
152
153
154
155
156
# File 'lib/rsmp/tlc/modules/plans.rb', line 151

def handle_s0019(_status_code, status_name = nil, _options = {})
  case status_name
  when 'number'
    TrafficControllerSite.make_status @num_traffic_situations
  end
end

#handle_s0022(_status_code, status_name = nil, _options = {}) ⇒ Object

S0022 - List of time plans



159
160
161
162
163
164
# File 'lib/rsmp/tlc/modules/plans.rb', line 159

def handle_s0022(_status_code, status_name = nil, _options = {})
  case status_name
  when 'status'
    TrafficControllerSite.make_status @plans.keys.join(',')
  end
end

#handle_s0023(_status_code, status_name = nil, _options = {}) ⇒ Object

S0023 - Dynamic bands



167
168
169
170
171
172
173
174
# File 'lib/rsmp/tlc/modules/plans.rb', line 167

def handle_s0023(_status_code, status_name = nil, _options = {})
  case status_name
  when 'status'
    dynamic_bands = @plans.map { |_nr, plan| plan.dynamic_bands_string }
    str = dynamic_bands.compact.join(',')
    TrafficControllerSite.make_status str
  end
end

#handle_s0024(_status_code, status_name = nil, _options = {}) ⇒ Object

S0024 - Offset times



177
178
179
180
181
182
# File 'lib/rsmp/tlc/modules/plans.rb', line 177

def handle_s0024(_status_code, status_name = nil, _options = {})
  case status_name
  when 'status'
    TrafficControllerSite.make_status '1-0'
  end
end

#handle_s0026(_status_code, status_name = nil, _options = {}) ⇒ Object

S0026 - Week time table



185
186
187
188
189
190
# File 'lib/rsmp/tlc/modules/plans.rb', line 185

def handle_s0026(_status_code, status_name = nil, _options = {})
  case status_name
  when 'status'
    TrafficControllerSite.make_status '0-00'
  end
end

#handle_s0027(_status_code, status_name = nil, _options = {}) ⇒ Object

S0027 - Time tables



193
194
195
196
197
198
199
200
201
# File 'lib/rsmp/tlc/modules/plans.rb', line 193

def handle_s0027(_status_code, status_name = nil, _options = {})
  case status_name
  when 'status'
    status = @day_time_table.map do |i, item|
      "#{i}-#{item[:plan]}-#{item[:hour]}-#{item[:min]}"
    end.join(',')
    TrafficControllerSite.make_status status
  end
end

#handle_s0028(_status_code, status_name = nil, _options = {}) ⇒ Object

S0028 - Cycle time



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rsmp/tlc/modules/plans.rb', line 204

def handle_s0028(_status_code, status_name = nil, _options = {})
  case status_name
  when 'status'
    times = @plans.map do |_nr, plan|
      "#{format('%02d', plan.number)}-#{format('%02d', plan.cycle_time)}"
    end.join(',')
    TrafficControllerSite.make_status times
  end
rescue StandardError => e
  puts e
end

#switch_plan(plan, source:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rsmp/tlc/modules/plans.rb', line 37

def switch_plan(plan, source:)
  plan_nr = plan.to_i
  if plan_nr.zero?
    log 'Switching to plan selection by time table', level: :info
  else
    find_plan plan_nr
    log "Switching to plan #{plan_nr}", level: :info
  end
  @plan = plan_nr
  @plan_source = source
end

#switch_traffic_situation(situation) ⇒ Object



49
50
51
52
# File 'lib/rsmp/tlc/modules/plans.rb', line 49

def switch_traffic_situation(situation)
  @traffic_situation = situation.to_i
  @traffic_situation_source = 'forced'
end