Class: RSMP::TLC::TrafficController

Inherits:
Component
  • Object
show all
Defined in:
lib/rsmp/tlc/traffic_controller.rb

Overview

TrafficController is the main component of a TrafficControllerSite. It handles all command and status for the main component, and keeps track of signal plans, detector logics, inputs, etc. which do not have dedicated components.

Constant Summary

Constants inherited from Component

Component::AGGREGATED_STATUS_KEYS

Instance Attribute Summary collapse

Attributes inherited from Component

#aggregated_status, #aggregated_status_bools, #alarms, #c_id, #grouped, #node, #statuses

Instance Method Summary collapse

Methods inherited from Component

#aggregated_status_changed, #clear_aggregated_status, #handle_alarm, #handle_status_response, #handle_status_subscribe, #handle_status_unsubscribe, #handle_status_update, #log, #set_aggregated_status, #set_aggregated_status_bools, #store_status

Methods included from Inspect

#inspect, #inspector

Constructor Details

#initialize(node:, id:, cycle_time: 10, signal_plans:, startup_sequence:, live_output: nil, inputs: {}) ⇒ TrafficController

Returns a new instance of TrafficController.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rsmp/tlc/traffic_controller.rb', line 12

def initialize node:, id:, cycle_time: 10, signal_plans:,
    startup_sequence:, live_output:nil, inputs:{}
  super node: node, id: id, grouped: true
  @signal_groups = []
  @detector_logics = []
  @plans = signal_plans
  @cycle_time = cycle_time
  @num_traffic_situations = 1

  if inputs
    @num_inputs = inputs['total'] || 8
    @input_programming = inputs['programming']
  else
    @num_inputs = 8
    @input_programming = nil
  end

  @startup_sequence = startup_sequence
  @live_output = live_output
  reset
end

Instance Attribute Details

#cycle_counterObject (readonly)

Returns the value of attribute cycle_counter.



8
9
10
# File 'lib/rsmp/tlc/traffic_controller.rb', line 8

def cycle_counter
  @cycle_counter
end

#cycle_timeObject (readonly)

Returns the value of attribute cycle_time.



8
9
10
# File 'lib/rsmp/tlc/traffic_controller.rb', line 8

def cycle_time
  @cycle_time
end

#functional_positionObject (readonly)

Returns the value of attribute functional_position.



8
9
10
# File 'lib/rsmp/tlc/traffic_controller.rb', line 8

def functional_position
  @functional_position
end

#planObject (readonly)

Returns the value of attribute plan.



8
9
10
# File 'lib/rsmp/tlc/traffic_controller.rb', line 8

def plan
  @plan
end

#posObject (readonly)

Returns the value of attribute pos.



8
9
10
# File 'lib/rsmp/tlc/traffic_controller.rb', line 8

def pos
  @pos
end

#startup_sequenceObject (readonly)

Returns the value of attribute startup_sequence.



8
9
10
# File 'lib/rsmp/tlc/traffic_controller.rb', line 8

def startup_sequence
  @startup_sequence
end

#startup_sequence_activeObject (readonly)

Returns the value of attribute startup_sequence_active.



8
9
10
# File 'lib/rsmp/tlc/traffic_controller.rb', line 8

def startup_sequence_active
  @startup_sequence_active
end

#startup_sequence_posObject (readonly)

Returns the value of attribute startup_sequence_pos.



8
9
10
# File 'lib/rsmp/tlc/traffic_controller.rb', line 8

def startup_sequence_pos
  @startup_sequence_pos
end

Instance Method Details

#add_detector_logic(logic) ⇒ Object



101
102
103
# File 'lib/rsmp/tlc/traffic_controller.rb', line 101

def add_detector_logic logic
  @detector_logics << logic
end

#add_signal_group(group) ⇒ Object



97
98
99
# File 'lib/rsmp/tlc/traffic_controller.rb', line 97

def add_signal_group group
  @signal_groups << group
end

#bool_string_to_digit(bool) ⇒ Object



364
365
366
367
368
369
370
371
372
373
# File 'lib/rsmp/tlc/traffic_controller.rb', line 364

def bool_string_to_digit bool
  case bool
    when 'True'
      '1'
    when 'False'
      '0'
    else
      raise RSMP::MessageRejected.new "Invalid boolean '#{bool}', must be 'True' or 'False'"
  end
end

#bool_to_digit(bool) ⇒ Object



375
376
377
# File 'lib/rsmp/tlc/traffic_controller.rb', line 375

def bool_to_digit bool
  bool ?  '1' : '0'
end

#check_functional_position_timeoutObject



124
125
126
127
128
129
130
131
# File 'lib/rsmp/tlc/traffic_controller.rb', line 124

def check_functional_position_timeout
  return unless @functional_position_timeout
  if clock.now >= @functional_position_timeout
    switch_functional_position @previous_functional_position, reverting: true
    @functional_position_timeout = nil
    @previous_functional_position = nil
  end
end

#clockObject



84
85
86
# File 'lib/rsmp/tlc/traffic_controller.rb', line 84

def clock
  node.clock
end

#current_planObject



88
89
90
91
92
93
94
95
# File 'lib/rsmp/tlc/traffic_controller.rb', line 88

def current_plan
  # TODO plan 0 should means use time table
  if @plans
    @plans[ plan ] || @plans.values.first
  else
    nil
  end
end

#dark?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/rsmp/tlc/traffic_controller.rb', line 72

def dark?
  @function_position == 'Dark'
end

#end_startup_sequenceObject



147
148
149
150
151
# File 'lib/rsmp/tlc/traffic_controller.rb', line 147

def end_startup_sequence
  @startup_sequence_active = false
  @startup_sequence_initiated_at = nil
  @startup_sequence_pos = nil
end

#find_plan(plan_nr) ⇒ Object



318
319
320
321
322
# File 'lib/rsmp/tlc/traffic_controller.rb', line 318

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

#format_signal_group_statusObject



210
211
212
# File 'lib/rsmp/tlc/traffic_controller.rb', line 210

def format_signal_group_status
  @signal_groups.map { |group| group.state }.join
end

#get_status(code, name = nil) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/rsmp/tlc/traffic_controller.rb', line 468

def get_status code, name=nil
  case code
  when 'S0001', 'S0002', 'S0003', 'S0004', 'S0005', 'S0006', 'S0007',
       'S0008', 'S0009', 'S0010', 'S0011', 'S0012', 'S0013', 'S0014',
       'S0015', 'S0016', 'S0017', 'S0018', 'S0019', 'S0020', 'S0021',
       'S0022', 'S0023', 'S0024', 'S0026', 'S0027', 'S0028',
       'S0029', 'S0030', 'S0031',
       'S0091', 'S0092', 'S0095', 'S0096', 'S0097',
       'S0205', 'S0206', 'S0207', 'S0208'
    return send("handle_#{code.downcase}", code, name)
  else
    raise InvalidMessage.new "unknown status code #{code}"
  end
end

#handle_command(command_code, arg) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/rsmp/tlc/traffic_controller.rb', line 214

def handle_command command_code, arg
  case command_code
  when 'M0001', 'M0002', 'M0003', 'M0004', 'M0005', 'M0006', 'M0007',
       'M0012', 'M0013', 'M0014', 'M0015', 'M0016', 'M0017', 'M0018',
       'M0019', 'M0020', 'M0021', 'M0022',
       'M0103', 'M0104'

    return send("handle_#{command_code.downcase}", arg)
  else
    raise UnknownCommand.new "Unknown command #{command_code}"
  end
end

#handle_m0001(arg) ⇒ Object



227
228
229
230
# File 'lib/rsmp/tlc/traffic_controller.rb', line 227

def handle_m0001 arg
  @node.verify_security_code 2, arg['securityCode']
  switch_functional_position arg['status'], timeout: arg['timeout'].to_i*60
end

#handle_m0002(arg) ⇒ Object



232
233
234
235
236
237
238
239
# File 'lib/rsmp/tlc/traffic_controller.rb', line 232

def handle_m0002 arg
  @node.verify_security_code 2, arg['securityCode']
  if TrafficControllerSite.from_rsmp_bool(arg['status'])
    switch_plan arg['timeplan']
  else
    switch_plan 0   # TODO use clock/calender
  end
end

#handle_m0003(arg) ⇒ Object



241
242
243
244
# File 'lib/rsmp/tlc/traffic_controller.rb', line 241

def handle_m0003 arg
  @node.verify_security_code 2, arg['securityCode']
  @traffic_situation = arg['traficsituation'].to_i
end

#handle_m0004(arg) ⇒ Object



246
247
248
249
250
251
252
# File 'lib/rsmp/tlc/traffic_controller.rb', line 246

def handle_m0004 arg
  @node.verify_security_code 2, arg['securityCode']
  # don't restart immeediately, since we need to first send command response
  # instead, defer an action, which will be handled by the TLC site
  log "Sheduling restart of TLC", level: :info
  @node.defer :restart
end

#handle_m0005(arg) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
# File 'lib/rsmp/tlc/traffic_controller.rb', line 254

def handle_m0005 arg
  @node.verify_security_code 2, arg['securityCode']
  @emergency_route = (arg['status'] == 'True')
  @emergency_route_number = arg['emergencyroute'].to_i

  if @emergency_route
    log "Switching to emergency route #{@emergency_route_number}", level: :info
  else
    log "Switching off emergency route", level: :info
  end
end

#handle_m0006(arg) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/rsmp/tlc/traffic_controller.rb', line 276

def handle_m0006 arg
  @node.verify_security_code 2, arg['securityCode']
  input = arg['input'].to_i
  unless input>0 && input<=@num_inputs
    raise MessageRejected.new("Input #{idx} is invalid, must be in the range 1-#{@num_inputs}")
  end
  idx = input - 1
  @input_activations[idx] = bool_string_to_digit arg['status']
  recompute_input idx
  activate = @input_activations[idx] == '1'
  if activate
    log "Activating input #{input}", level: :info
  else
    log "Deactivating input #{input}", level: :info
  end

  if @input_programming
    actions = @input_programming[input]
    if actions && actions['raise']
      alarm_code = actions['raise']
      if activate
        log "Activating alarm #{alarm_code}, due to input #{input} programming", level: :info
      else
        log "Deactivating alarm #{alarm_code}, due to input #{input} programming", level: :info
      end
    end
  end
end

#handle_m0007(arg) ⇒ Object



305
306
307
308
# File 'lib/rsmp/tlc/traffic_controller.rb', line 305

def handle_m0007 arg
  @node.verify_security_code 2, arg['securityCode']
  set_fixed_time_control arg['status']
end

#handle_m0012(arg) ⇒ Object



310
311
312
# File 'lib/rsmp/tlc/traffic_controller.rb', line 310

def handle_m0012 arg
  @node.verify_security_code 2, arg['securityCode']
end

#handle_m0013(arg) ⇒ Object



314
315
316
# File 'lib/rsmp/tlc/traffic_controller.rb', line 314

def handle_m0013 arg
  @node.verify_security_code 2, arg['securityCode']
end

#handle_m0014(arg) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
# File 'lib/rsmp/tlc/traffic_controller.rb', line 324

def handle_m0014 arg
  @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) ⇒ Object



336
337
338
# File 'lib/rsmp/tlc/traffic_controller.rb', line 336

def handle_m0015 arg
  @node.verify_security_code 2, arg['securityCode']
end

#handle_m0016(arg) ⇒ Object



340
341
342
# File 'lib/rsmp/tlc/traffic_controller.rb', line 340

def handle_m0016 arg
  @node.verify_security_code 2, arg['securityCode']
end

#handle_m0017(arg) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/rsmp/tlc/traffic_controller.rb', line 344

def handle_m0017 arg
  @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
    if nr<0 || nr>12
      raise InvalidMessage.new "time table id must be between 0 and 12, got #{nr}"
    end
    #p "nr: #{nr}, plan #{plan} at #{hour}:#{min}"
    @day_time_table[nr] = {plan: plan, hour: hour, min:min}
  end
end

#handle_m0018(arg) ⇒ Object



360
361
362
# File 'lib/rsmp/tlc/traffic_controller.rb', line 360

def handle_m0018 arg
  @node.verify_security_code 2, arg['securityCode']
end

#handle_m0019(arg) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/rsmp/tlc/traffic_controller.rb', line 379

def handle_m0019 arg
  @node.verify_security_code 2, arg['securityCode']
  input = arg['input'].to_i
  idx = input - 1
  unless idx>=0 && input<@num_inputs # TODO should NotAck
    log "Can't force input #{idx+1}, only have #{@num_inputs} inputs", level: :warning
    return
  end
  @input_forced[idx] = bool_string_to_digit arg['status']
  if @input_forced[idx]
    @input_forced_values[idx] = bool_string_to_digit arg['inputValue']
  end
  recompute_input idx
  if @input_forced[idx]
    log "Forcing input #{idx+1} to #{@input_forced_values[idx]}, #{@input_results}", level: :info
  else
    log "Releasing input #{idx+1}", level: :info
  end
end

#handle_m0020(arg) ⇒ Object



399
400
401
# File 'lib/rsmp/tlc/traffic_controller.rb', line 399

def handle_m0020 arg
  @node.verify_security_code 2, arg['securityCode']
end

#handle_m0021(arg) ⇒ Object



403
404
405
# File 'lib/rsmp/tlc/traffic_controller.rb', line 403

def handle_m0021 arg
  @node.verify_security_code 2, arg['securityCode']
end

#handle_m0103(arg) ⇒ Object



407
408
409
410
# File 'lib/rsmp/tlc/traffic_controller.rb', line 407

def handle_m0103 arg
  level = {'Level1'=>1,'Level2'=>2}[arg['status']]
  @node.change_security_code level, arg['oldSecurityCode'], arg['newSecurityCode']
end

#handle_m0104(arg) ⇒ Object



412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/rsmp/tlc/traffic_controller.rb', line 412

def handle_m0104 arg
  @node.verify_security_code 1, arg['securityCode']
  time = Time.new(
    arg['year'],
    arg['month'],
    arg['day'],
    arg['hour'],
    arg['minute'],
    arg['second'],
    'UTC'
  )
  clock.set time
  log "Clock set to #{time}, (adjustment is #{clock.adjustment}s)", level: :info
end

#handle_s0001(status_code, status_name = nil) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/rsmp/tlc/traffic_controller.rb', line 483

def handle_s0001 status_code, status_name=nil
  case status_name
  when 'signalgroupstatus'
    TrafficControllerSite.make_status format_signal_group_status
  when 'cyclecounter'
    TrafficControllerSite.make_status @cycle_counter.to_s
  when 'basecyclecounter'
    TrafficControllerSite.make_status @cycle_counter.to_s
  when 'stage'
    TrafficControllerSite.make_status 0.to_s
  end
end

#handle_s0002(status_code, status_name = nil) ⇒ Object



496
497
498
499
500
501
# File 'lib/rsmp/tlc/traffic_controller.rb', line 496

def handle_s0002 status_code, status_name=nil
  case status_name
  when 'detectorlogicstatus'
    TrafficControllerSite.make_status @detector_logics.map { |dl| bool_to_digit(dl.value) }.join
  end
end

#handle_s0003(status_code, status_name = nil) ⇒ Object



503
504
505
506
507
508
509
510
# File 'lib/rsmp/tlc/traffic_controller.rb', line 503

def handle_s0003 status_code, status_name=nil
  case status_name
  when 'inputstatus'
    TrafficControllerSite.make_status @input_results
  when 'extendedinputstatus'
    TrafficControllerSite.make_status 0.to_s
  end
end

#handle_s0004(status_code, status_name = nil) ⇒ Object



512
513
514
515
516
517
518
519
# File 'lib/rsmp/tlc/traffic_controller.rb', line 512

def handle_s0004 status_code, status_name=nil
  case status_name
  when 'outputstatus'
    TrafficControllerSite.make_status 0
  when 'extendedoutputstatus'
    TrafficControllerSite.make_status 0
  end
end

#handle_s0005(status_code, status_name = nil) ⇒ Object



521
522
523
524
525
526
# File 'lib/rsmp/tlc/traffic_controller.rb', line 521

def handle_s0005 status_code, status_name=nil
  case status_name
  when 'status'
    TrafficControllerSite.make_status @is_starting
  end
end

#handle_s0006(status_code, status_name = nil) ⇒ Object



528
529
530
531
532
533
534
535
# File 'lib/rsmp/tlc/traffic_controller.rb', line 528

def handle_s0006 status_code, status_name=nil
  case status_name
  when 'status'
    TrafficControllerSite.make_status @emergency_route
  when 'emergencystage'
    TrafficControllerSite.make_status @emergency_route_number
  end
end

#handle_s0007(status_code, status_name = nil) ⇒ Object



537
538
539
540
541
542
543
544
# File 'lib/rsmp/tlc/traffic_controller.rb', line 537

def handle_s0007 status_code, status_name=nil
  case status_name
  when 'intersection'
    TrafficControllerSite.make_status @intersection
  when 'status'
    TrafficControllerSite.make_status @function_position != 'Dark'
  end
end

#handle_s0008(status_code, status_name = nil) ⇒ Object



546
547
548
549
550
551
552
553
# File 'lib/rsmp/tlc/traffic_controller.rb', line 546

def handle_s0008 status_code, status_name=nil
  case status_name
  when 'intersection'
    TrafficControllerSite.make_status @intersection
  when 'status'
    TrafficControllerSite.make_status @manual_control
  end
end

#handle_s0009(status_code, status_name = nil) ⇒ Object



555
556
557
558
559
560
561
562
# File 'lib/rsmp/tlc/traffic_controller.rb', line 555

def handle_s0009 status_code, status_name=nil
  case status_name
  when 'intersection'
    TrafficControllerSite.make_status @intersection
  when 'status'
    TrafficControllerSite.make_status @fixed_time_control
  end
end

#handle_s0010(status_code, status_name = nil) ⇒ Object



564
565
566
567
568
569
570
571
# File 'lib/rsmp/tlc/traffic_controller.rb', line 564

def handle_s0010 status_code, status_name=nil
  case status_name
  when 'intersection'
    TrafficControllerSite.make_status @intersection
  when 'status'
    TrafficControllerSite.make_status @isolated_control
  end
end

#handle_s0011(status_code, status_name = nil) ⇒ Object



573
574
575
576
577
578
579
580
# File 'lib/rsmp/tlc/traffic_controller.rb', line 573

def handle_s0011 status_code, status_name=nil
  case status_name
  when 'intersection'
    TrafficControllerSite.make_status @intersection
  when 'status'
    TrafficControllerSite.make_status TrafficControllerSite.to_rmsp_bool( @function_position == 'YellowFlash' )
  end
end

#handle_s0012(status_code, status_name = nil) ⇒ Object



582
583
584
585
586
587
588
589
# File 'lib/rsmp/tlc/traffic_controller.rb', line 582

def handle_s0012 status_code, status_name=nil
  case status_name
  when 'intersection'
    TrafficControllerSite.make_status @intersection
  when 'status'
    TrafficControllerSite.make_status @all_red
  end
end

#handle_s0013(status_code, status_name = nil) ⇒ Object



591
592
593
594
595
596
597
598
# File 'lib/rsmp/tlc/traffic_controller.rb', line 591

def handle_s0013 status_code, status_name=nil
  case status_name
  when 'intersection'
    TrafficControllerSite.make_status @intersection
  when 'status'
    TrafficControllerSite.make_status @police_key
  end
end

#handle_s0014(status_code, status_name = nil) ⇒ Object



600
601
602
603
604
605
# File 'lib/rsmp/tlc/traffic_controller.rb', line 600

def handle_s0014 status_code, status_name=nil
  case status_name
  when 'status'
    TrafficControllerSite.make_status @plan
  end
end

#handle_s0015(status_code, status_name = nil) ⇒ Object



607
608
609
610
611
612
# File 'lib/rsmp/tlc/traffic_controller.rb', line 607

def handle_s0015 status_code, status_name=nil
  case status_name
  when 'status'
    TrafficControllerSite.make_status @traffic_situation
  end
end

#handle_s0016(status_code, status_name = nil) ⇒ Object



614
615
616
617
618
619
# File 'lib/rsmp/tlc/traffic_controller.rb', line 614

def handle_s0016 status_code, status_name=nil
  case status_name
  when 'number'
    TrafficControllerSite.make_status @detector_logics.size
  end
end

#handle_s0017(status_code, status_name = nil) ⇒ Object



621
622
623
624
625
626
# File 'lib/rsmp/tlc/traffic_controller.rb', line 621

def handle_s0017 status_code, status_name=nil
  case status_name
  when 'number'
    TrafficControllerSite.make_status @signal_groups.size
  end
end

#handle_s0018(status_code, status_name = nil) ⇒ Object



628
629
630
631
632
633
# File 'lib/rsmp/tlc/traffic_controller.rb', line 628

def handle_s0018 status_code, status_name=nil
  case status_name
  when 'number'
    TrafficControllerSite.make_status @plans.size
  end
end

#handle_s0019(status_code, status_name = nil) ⇒ Object



635
636
637
638
639
640
# File 'lib/rsmp/tlc/traffic_controller.rb', line 635

def handle_s0019 status_code, status_name=nil
  case status_name
  when 'number'
    TrafficControllerSite.make_status @num_traffic_situations
  end
end

#handle_s0020(status_code, status_name = nil) ⇒ Object



642
643
644
645
646
647
648
649
# File 'lib/rsmp/tlc/traffic_controller.rb', line 642

def handle_s0020 status_code, status_name=nil
  case status_name
  when 'intersection'
    TrafficControllerSite.make_status @intersection
  when 'controlmode'
    TrafficControllerSite.make_status @control_mode
  end
end

#handle_s0021(status_code, status_name = nil) ⇒ Object



651
652
653
654
655
656
# File 'lib/rsmp/tlc/traffic_controller.rb', line 651

def handle_s0021 status_code, status_name=nil
  case status_name
  when 'detectorlogics'
    TrafficControllerSite.make_status @detector_logics.map { |logic| bool_to_digit(logic.forced)}.join
  end
end

#handle_s0022(status_code, status_name = nil) ⇒ Object



658
659
660
661
662
663
# File 'lib/rsmp/tlc/traffic_controller.rb', line 658

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

#handle_s0023(status_code, status_name = nil) ⇒ Object



665
666
667
668
669
670
671
672
# File 'lib/rsmp/tlc/traffic_controller.rb', line 665

def handle_s0023 status_code, status_name=nil
  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) ⇒ Object



674
675
676
677
678
679
# File 'lib/rsmp/tlc/traffic_controller.rb', line 674

def handle_s0024 status_code, status_name=nil
  case status_name
  when 'status'
    TrafficControllerSite.make_status '1-0'
  end
end

#handle_s0026(status_code, status_name = nil) ⇒ Object



681
682
683
684
685
686
# File 'lib/rsmp/tlc/traffic_controller.rb', line 681

def handle_s0026 status_code, status_name=nil
  case status_name
  when 'status'
    TrafficControllerSite.make_status '0-00'
  end
end

#handle_s0027(status_code, status_name = nil) ⇒ Object



688
689
690
691
692
693
694
695
696
# File 'lib/rsmp/tlc/traffic_controller.rb', line 688

def handle_s0027 status_code, status_name=nil
  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) ⇒ Object



698
699
700
701
702
703
# File 'lib/rsmp/tlc/traffic_controller.rb', line 698

def handle_s0028 status_code, status_name=nil
  case status_name
  when 'status'
    TrafficControllerSite.make_status '00-00'
  end
end

#handle_s0029(status_code, status_name = nil) ⇒ Object



705
706
707
708
709
710
# File 'lib/rsmp/tlc/traffic_controller.rb', line 705

def handle_s0029 status_code, status_name=nil
  case status_name
  when 'status'
    TrafficControllerSite.make_status @input_forced
  end
end

#handle_s0030(status_code, status_name = nil) ⇒ Object



712
713
714
715
716
717
# File 'lib/rsmp/tlc/traffic_controller.rb', line 712

def handle_s0030 status_code, status_name=nil
  case status_name
  when 'status'
    TrafficControllerSite.make_status ''
  end
end

#handle_s0031(status_code, status_name = nil) ⇒ Object



719
720
721
722
723
724
# File 'lib/rsmp/tlc/traffic_controller.rb', line 719

def handle_s0031 status_code, status_name=nil
  case status_name
  when 'status'
    TrafficControllerSite.make_status ''
  end
end

#handle_s0091(status_code, status_name = nil) ⇒ Object



726
727
728
729
730
731
732
733
# File 'lib/rsmp/tlc/traffic_controller.rb', line 726

def handle_s0091 status_code, status_name=nil
  case status_name
  when 'user'
    TrafficControllerSite.make_status 'nobody'
  when 'status'
    TrafficControllerSite.make_status 'logout'
  end
end

#handle_s0092(status_code, status_name = nil) ⇒ Object



735
736
737
738
739
740
741
742
# File 'lib/rsmp/tlc/traffic_controller.rb', line 735

def handle_s0092 status_code, status_name=nil
  case status_name
  when 'user'
    TrafficControllerSite.make_status 'nobody'
  when 'status'
    TrafficControllerSite.make_status 'logout'
  end
end

#handle_s0095(status_code, status_name = nil) ⇒ Object



744
745
746
747
748
749
# File 'lib/rsmp/tlc/traffic_controller.rb', line 744

def handle_s0095 status_code, status_name=nil
  case status_name
  when 'status'
    TrafficControllerSite.make_status RSMP::VERSION
  end
end

#handle_s0096(status_code, status_name = nil) ⇒ Object



751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
# File 'lib/rsmp/tlc/traffic_controller.rb', line 751

def handle_s0096 status_code, status_name=nil
  now = clock.now
  case status_name
  when 'year'
    TrafficControllerSite.make_status now.year.to_s.rjust(4, "0")
  when 'month'
    TrafficControllerSite.make_status now.month.to_s.rjust(2, "0")
  when 'day'
    TrafficControllerSite.make_status now.day.to_s.rjust(2, "0")
  when 'hour'
    TrafficControllerSite.make_status now.hour.to_s.rjust(2, "0")
  when 'minute'
    TrafficControllerSite.make_status now.min.to_s.rjust(2, "0")
  when 'second'
    TrafficControllerSite.make_status now.sec.to_s.rjust(2, "0")
  end
end

#handle_s0097(status_code, status_name = nil) ⇒ Object



769
770
771
772
773
774
775
776
777
# File 'lib/rsmp/tlc/traffic_controller.rb', line 769

def handle_s0097 status_code, status_name=nil
  case status_name
  when 'checksum'
    TrafficControllerSite.make_status '1'
  when 'timestamp'
    now = clock.to_s
    TrafficControllerSite.make_status now
  end
end

#handle_s0205(status_code, status_name = nil) ⇒ Object



779
780
781
782
783
784
785
786
# File 'lib/rsmp/tlc/traffic_controller.rb', line 779

def handle_s0205 status_code, status_name=nil
  case status_name
  when 'start'
    TrafficControllerSite.make_status clock.to_s
  when 'vehicles'
    TrafficControllerSite.make_status 0
  end
end

#handle_s0206(status_code, status_name = nil) ⇒ Object



788
789
790
791
792
793
794
795
# File 'lib/rsmp/tlc/traffic_controller.rb', line 788

def handle_s0206 status_code, status_name=nil
  case status_name
  when 'start'
    TrafficControllerSite.make_status clock.to_s
  when 'speed'
    TrafficControllerSite.make_status 0
  end
end

#handle_s0207(status_code, status_name = nil) ⇒ Object



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

def handle_s0207 status_code, status_name=nil
  case status_name
  when 'start'
    TrafficControllerSite.make_status clock.to_s
  when 'occupancy'
    TrafficControllerSite.make_status 0
  end
end

#handle_s0208(status_code, status_name = nil) ⇒ Object



806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'lib/rsmp/tlc/traffic_controller.rb', line 806

def handle_s0208 status_code, status_name=nil
  case status_name
  when 'start'
    TrafficControllerSite.make_status clock.to_s
  when 'P'
    TrafficControllerSite.make_status 0
  when 'PS'
    TrafficControllerSite.make_status 0
  when 'L'
    TrafficControllerSite.make_status 0
  when 'LS'
    TrafficControllerSite.make_status 0
  when 'B'
    TrafficControllerSite.make_status 0
  when 'SP'
    TrafficControllerSite.make_status 0
  when 'MC'
    TrafficControllerSite.make_status 0
  when 'C'
    TrafficControllerSite.make_status 0
  when 'F'
    TrafficControllerSite.make_status 0
  end
end

#initiate_startup_sequenceObject



139
140
141
142
143
144
145
# File 'lib/rsmp/tlc/traffic_controller.rb', line 139

def initiate_startup_sequence
  log "Initiating startup sequence", level: :info
  reset_modes
  @startup_sequence_active = true
  @startup_sequence_initiated_at = nil
  @startup_sequence_pos = nil
end

#move_cycle_counterObject



119
120
121
122
# File 'lib/rsmp/tlc/traffic_controller.rb', line 119

def move_cycle_counter
  counter = Time.now.to_i % @cycle_time
  @cycle_counter = counter
end

#move_startup_sequenceObject



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rsmp/tlc/traffic_controller.rb', line 153

def move_startup_sequence
  was = @startup_sequence_pos
  if @startup_sequence_initiated_at == nil
    @startup_sequence_initiated_at = Time.now.to_i + 1
    @startup_sequence_pos = 0
  else
    @startup_sequence_pos = Time.now.to_i - @startup_sequence_initiated_at
  end
  if @startup_sequence_pos >= @startup_sequence.size
    end_startup_sequence
  end
end

#normal_control?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/rsmp/tlc/traffic_controller.rb', line 80

def normal_control?
  @function_position == 'NormalControl'
end

#output_statesObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rsmp/tlc/traffic_controller.rb', line 166

def output_states
  return unless @live_output

  str = @signal_groups.map do |group|
    state = group.state
    s = "#{group.c_id}:#{state}"
    if state =~ /^[1-9]$/
        s.colorize(:green)
    elsif state =~ /^[NOP]$/
      s.colorize(:yellow)
    elsif state =~ /^[ae]$/
      s.colorize(:light_black)
    elsif state =~ /^[f]$/
      s.colorize(:yellow)
    elsif state =~ /^[g]$/
      s.colorize(:red)
    else
      s.colorize(:red)
    end
  end.join ' '

  modes = '.'*9
  modes[0] = 'N' if @function_position == 'NormalControl'
  modes[1] = 'Y' if @function_position == 'YellowFlash'
  modes[2] = 'D' if @function_position == 'Dark'
  modes[3] = 'B' if @booting
  modes[4] = 'S' if @startup_sequence_active
  modes[5] = 'M' if @manual_control
  modes[6] = 'F' if @fixed_time_control
  modes[7] = 'R' if @all_red
  modes[8] = 'I' if @isolated_control
  modes[9] = 'P' if @police_key != 0

  plan = "P#{@plan}"

  # create folders if needed
  FileUtils.mkdir_p File.dirname(@live_output)

  # append a line with the current state to the file
  File.open @live_output, 'w' do |file|
    file.puts "#{modes}  #{plan.rjust(2)}  #{@cycle_counter.to_s.rjust(3)}  #{str}\r"
  end
end

#recompute_input(idx) ⇒ Object



266
267
268
269
270
271
272
273
274
# File 'lib/rsmp/tlc/traffic_controller.rb', line 266

def recompute_input idx
  if @input_forced[idx] == '1'
    @input_results[idx] = @input_forced_values[idx]
  elsif @input_activations[idx]=='1'
    @input_results[idx] = '1'
  else
    @input_results[idx] = bool_to_digit( @inputs[idx]=='1' )
  end
end

#resetObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rsmp/tlc/traffic_controller.rb', line 49

def reset
  reset_modes

  @cycle_counter = 0
  @plan = 1
  @intersection = 0
  @emergency_route = false
  @emergency_route_number = 0
  @traffic_situation = 0

  @inputs = '0'*@num_inputs
  @input_activations = '0'*@num_inputs
  @input_forced = '0'*@num_inputs
  @input_forced_values = '0'*@num_inputs
  @input_results = '0'*@num_inputs

  @day_time_table = {}
  @startup_sequence_active = false
  @startup_sequence_initiated_at = nil
  @startup_sequence_pos = 0
  @time_int = nil
end

#reset_modesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rsmp/tlc/traffic_controller.rb', line 34

def reset_modes
  @function_position = 'NormalControl'
  @previous_functional_position = nil
  @functional_position_timeout = nil

  @booting = false
  @is_starting = false
  @control_mode = 'control'
  @manual_control = false
  @fixed_time_control = false
  @isolated_control = false
  @all_red = false
  @police_key = 0
end

#set_fixed_time_control(status) ⇒ Object



432
433
434
# File 'lib/rsmp/tlc/traffic_controller.rb', line 432

def set_fixed_time_control status
  @fixed_time_control = status
end

#set_input(i, value) ⇒ Object



427
428
429
430
# File 'lib/rsmp/tlc/traffic_controller.rb', line 427

def set_input i, value
  return unless i>=0 && i<@num_inputs
  @inputs[i] = bool_to_digit arg['value']
end

#startup_stateObject



133
134
135
136
137
# File 'lib/rsmp/tlc/traffic_controller.rb', line 133

def startup_state
  return unless @startup_sequence_active
  return unless @startup_sequence_pos
  @startup_sequence[ @startup_sequence_pos ]
end

#switch_functional_position(mode, timeout: nil, reverting: false) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/rsmp/tlc/traffic_controller.rb', line 447

def switch_functional_position mode, timeout: nil, reverting: false
  unless ['NormalControl','YellowFlash','Dark'].include? mode
    raise RSMP::MessageRejected.new "Invalid functional position '#{mode}', must be NormalControl, YellowFlash or Dark'"
  end
  if reverting
    log "Reverting to functional position #{mode} after timeout", level: :info
  elsif timeout && timeout > 0
    log "Switching to functional position #{mode} with timeout #{(timeout/60).round(1)}min", level: :info
    @previous_functional_position = @function_position
    now = clock.now
    @functional_position_timeout = now + timeout
  else
    log "Switching to functional position #{mode}", level: :info
  end 
  if mode == 'NormalControl'
    initiate_startup_sequence if @function_position != 'NormalControl'
  end
  @function_position = mode
  mode
end

#switch_plan(plan) ⇒ Object



436
437
438
439
440
441
442
443
444
445
# File 'lib/rsmp/tlc/traffic_controller.rb', line 436

def switch_plan plan
  plan_nr = plan.to_i
  if plan_nr == 0
    log "Switching to plan selection by time table", level: :info
  else
    plan = find_plan plan_nr
    log "Switching to plan #{plan_nr}", level: :info
  end
  @plan = plan_nr
end

#timer(now) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rsmp/tlc/traffic_controller.rb', line 105

def timer now
  # TODO use monotone timer, to avoid jumps in case the user sets the system time
  time = Time.now.to_i
  return if time == @time_int
  @time_int = time
  move_cycle_counter
  check_functional_position_timeout
  move_startup_sequence if @startup_sequence_active

  @signal_groups.each { |group| group.timer }

  output_states
end

#yellow_flash?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/rsmp/tlc/traffic_controller.rb', line 76

def yellow_flash?
  @function_position == 'YellowFlash'
end