Class: MovingsignApi::SenderReceiverAddress

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/movingsign_api/commands/internal/sender_receiver_address.rb

Overview

Represents a sender or receiver identifier for a Command

A address can be represented as an integer between 0 and 255, or per the spec as a two character hex string: ‘00’ - ‘FF’.

Valid inputs include:

  • Integer - 0 - 255

  • Symbol - :broadcast or :pc

  • String - ‘00’ - ‘FF’

Some notes:

  • Broadcast Address: (:broadcast, 0, or ‘00’) is a broadcast identifier and shouldn’t be used as a sender

  • PC Address: (:pc, 255, or ‘FF”) is reserved to represent the “PC address” and shouldn’t be used as a recipient

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier) ⇒ SenderReceiverAddress

Returns a new instance of SenderReceiverAddress.

Parameters:

  • identifier (String)

    Identifier hex string, or hex string with wildcard



25
26
27
# File 'lib/movingsign_api/commands/internal/sender_receiver_address.rb', line 25

def initialize(identifier)
  @identifier = identifier
end

Instance Attribute Details

#identifierInteger

Returns the address repsented as an integer.

Returns:

  • (Integer)

    the address repsented as an integer



22
23
24
# File 'lib/movingsign_api/commands/internal/sender_receiver_address.rb', line 22

def identifier
  @identifier
end

Class Method Details

.parse(input) ⇒ SenderReceiverAddress

Parses the specified input value returning an appropriate SenderReceiver instance or raises.

Returns:

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/movingsign_api/commands/internal/sender_receiver_address.rb', line 33

def self.parse(input)
  raise MovingsignApi::InvalidInputError, "nil not allowed" if input.nil?

  value = nil
  if input.kind_of? Fixnum
    if input.between?(0, 255)
      value = ('%02x' % input).upcase
    else
      raise MovingsignApi::InvalidInputError, "Integer #{input} is out of the valid range."
    end
  elsif input.kind_of? Symbol
    if input == :broadcast
      value = '00'
    elsif input == :pc
      value = 'FF'
    else
      raise MovingsignApi::InvalidInputError, "Symbol :#{input} isn't supported"
    end
  elsif input.kind_of? String
    if (value = input.upcase.match /\A[0-9,A-F\?]{2}\z/)
      value = value[0]
    else
      raise MovingsignApi::InvalidInputError, "Parsing string '#{input}' isn't supported"
    end
  else
    raise MovingsignApi::InvalidInputError, "Parsing '#{input.class}' isn't supported"
  end

  self.new(value)
end

Instance Method Details

#broadcast?Boolean

Returns true if this represents the special ‘broadcast’ address

Returns:

  • (Boolean)


65
66
67
# File 'lib/movingsign_api/commands/internal/sender_receiver_address.rb', line 65

def broadcast?
  self.identifier == '00'
end

#pc?Boolean

Returns true if this represents the special ‘pc’ address

Returns:

  • (Boolean)


70
71
72
# File 'lib/movingsign_api/commands/internal/sender_receiver_address.rb', line 70

def pc?
  self.identifier = 'FF'
end

#to_bytesObject



78
79
80
# File 'lib/movingsign_api/commands/internal/sender_receiver_address.rb', line 78

def to_bytes
  string_to_ascii_bytes self.identifier
end

#to_sObject



74
75
76
# File 'lib/movingsign_api/commands/internal/sender_receiver_address.rb', line 74

def to_s
  self.identifier
end