Module: RSMP::SupervisorProxy::Modules::Commands

Included in:
RSMP::SupervisorProxy
Defined in:
lib/rsmp/proxy/supervisor/modules/commands.rb

Overview

Command request handling

Instance Method Summary collapse

Instance Method Details

#build_command_rvs(args) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/rsmp/proxy/supervisor/modules/commands.rb', line 15

def build_command_rvs(args)
  args.map do |item|
    item = item.dup.merge('age' => 'recent')
    item.delete 'cO'
    item
  end
end

#check_required_command_arguments(message) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rsmp/proxy/supervisor/modules/commands.rb', line 51

def check_required_command_arguments(message)
  return unless core_3_3?

  command_codes(message).each do |command_code|
    provided = message.attributes['arg'].select { |item| item['cCI'] == command_code }.map { |item| item['n'] }
    missing = required_command_argument_names(command_code) - provided
    next if missing.empty?

    raise MissingAttribute, "Missing required command argument(s) #{missing.join(', ')} for #{command_code}"
  end
end

#command_catalogue_item(command_code) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/rsmp/proxy/supervisor/modules/commands.rb', line 23

def command_catalogue_item(command_code)
  accepted_sxls.each do |sxl|
    item = command_catalogue_match(sxl, command_code)
    return item if item
  end
  nil
end

#command_catalogue_match(sxl, command_code) ⇒ Object



31
32
33
34
35
36
# File 'lib/rsmp/proxy/supervisor/modules/commands.rb', line 31

def command_catalogue_match(sxl, command_code)
  version = RSMP::Schema.sanitize_version(sxl['version'].to_s)
  catalogue = RSMP::Schema.sxl_catalogue(sxl['name'], version, :commands)
  prefix = RSMP::Schema.sxl_prefix(sxl['name'], version, lenient: true)
  lookup_catalogue_command(catalogue, command_code, prefix)
end

#execute_commands(message, component_id, rvs) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rsmp/proxy/supervisor/modules/commands.rb', line 79

def execute_commands(message, component_id, rvs)
  component = @site.find_component component_id
  commands = simplify_command_requests message.attributes['arg']
  commands.each_pair do |command_code, arg|
    component.handle_command command_code, arg
  rescue UnknownCommand => e
    log e.to_s, message: message, level: :warning
    mark_command_unknown rvs, command_code
  end
  log "Received #{message.type}", message: message, level: :log
rescue UnknownComponent
  log "Received #{message.type} with unknown component id '#{component_id}' and cannot infer type",
      message: message, level: :warning
  mark_commands_undefined rvs
end

#lookup_catalogue_command(catalogue, command_code, prefix) ⇒ Object



38
39
40
41
42
# File 'lib/rsmp/proxy/supervisor/modules/commands.rb', line 38

def lookup_catalogue_command(catalogue, command_code, prefix)
  unprefixed = prefix && command_code.start_with?(prefix) ? command_code[prefix.length..] : command_code
  catalogue[command_code] || catalogue[command_code.to_sym] ||
    catalogue[unprefixed] || catalogue[unprefixed.to_sym]
end

#mark_command_unknown(rvs, command_code) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/rsmp/proxy/supervisor/modules/commands.rb', line 63

def mark_command_unknown(rvs, command_code)
  rvs.each do |item|
    next unless item['cCI'] == command_code

    item['age'] = 'unknown'
    item['v'] = nil
  end
end

#mark_commands_undefined(rvs) ⇒ Object



72
73
74
75
76
77
# File 'lib/rsmp/proxy/supervisor/modules/commands.rb', line 72

def mark_commands_undefined(rvs)
  rvs.each do |item|
    item['age'] = 'undefined'
    item['v'] = nil
  end
end

#process_command_request(message) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rsmp/proxy/supervisor/modules/commands.rb', line 95

def process_command_request(message)
  return reject_multiple_command_codes(message) if core_3_3? && multiple_command_codes?(message)

  check_required_command_arguments message
  component_id = message.attributes['cId']
  rvs = build_command_rvs(message.attributes['arg'])
  execute_commands(message, component_id, rvs)

  response = CommandResponse.new({
                                   'cId' => component_id,
                                   'cTS' => clock.to_s,
                                   'rvs' => rvs
                                 })
  apply_nts_message_attributes response
  acknowledge message
  send_message response
end

#required_command_argument_names(command_code) ⇒ Object



44
45
46
47
48
49
# File 'lib/rsmp/proxy/supervisor/modules/commands.rb', line 44

def required_command_argument_names(command_code)
  item = command_catalogue_item command_code
  return [] unless item

  RSMP::Schema.argument_names(item['required'])
end

#simplify_command_requests(arg) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/rsmp/proxy/supervisor/modules/commands.rb', line 6

def simplify_command_requests(arg)
  sorted = {}
  arg.each do |item|
    sorted[item['cCI']] ||= {}
    sorted[item['cCI']][item['n']] = item['v']
  end
  sorted
end