Class: EISCP::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/eiscp/message.rb

Overview

The EISCP::Message class is used to handle commands and responses.

Messages can be parsed directly from raw data or created with values:

receiver = Receiver.new

command = EISCP::Message.new('PWR', 'QSTN')
response = EISCP::Parser.parse(receiver.send_recv(command))

Constant Summary collapse

MAGIC =

ISCP “magic” indicates the start of an eISCP message.

'ISCP'
HEADER_SIZE =

eISCP header size, fixed length.

16
ISCP_VERSION =

ISCP protocol version.

1
RESERVED =

Reserved for future protocol updates.

"\x00\x00\x00"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command: nil, value: nil, terminator: "\r\n", unit_type: '1', start: '!') ⇒ Message

Create an ISCP message

Parameters:

  • command (String) (defaults to: nil)

    three-character length ISCP command

  • value (String) (defaults to: nil)

    variable length ISCP command value

  • unit_type_character (String)

    override default unit type character, optional

  • start_character (String)

    override default start character, optional



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/eiscp/message.rb', line 56

def initialize(command: nil, value: nil, terminator: "\r\n", unit_type: '1', start: '!')
  unless Dictionary.known_command?(command)
    # STDERR.puts "Unknown command #{command}"
  end

  raise 'No value specified.' if value.nil?

  @command = command
  @value = value
  @terminator = terminator
  @unit_type = unit_type
  @start = start
  @header = { magic: MAGIC,
              header_size: HEADER_SIZE,
              data_size: to_iscp.length,
              version: ISCP_VERSION,
              reserved: RESERVED }
  begin
    get_human_readable_attrs
  rescue StandardError
    # STDERR.puts"Couldn't get all human readable attrs"
  end
end

Instance Attribute Details

#commandObject (readonly)

ISCP Command



32
33
34
# File 'lib/eiscp/message.rb', line 32

def command
  @command
end

#command_descriptionObject (readonly)

Command description



36
37
38
# File 'lib/eiscp/message.rb', line 36

def command_description
  @command_description
end

#command_nameObject (readonly)

Human readable command name



34
35
36
# File 'lib/eiscp/message.rb', line 34

def command_name
  @command_name
end

#headerObject

EISCP header



17
18
19
# File 'lib/eiscp/message.rb', line 17

def header
  @header
end

#parsedObject (readonly)

Differentiates parsed messages from command messages



46
47
48
# File 'lib/eiscp/message.rb', line 46

def parsed
  @parsed
end

#startObject (readonly)

ISCP Start character, usually “!”



28
29
30
# File 'lib/eiscp/message.rb', line 28

def start
  @start
end

#terminatorObject (readonly)

Terminator character for eISCP packets



49
50
51
# File 'lib/eiscp/message.rb', line 49

def terminator
  @terminator
end

#unit_typeObject (readonly)

ISCP Unit Type character, usually “1”



30
31
32
# File 'lib/eiscp/message.rb', line 30

def unit_type
  @unit_type
end

#valueObject (readonly)

ISCP Command Value



38
39
40
# File 'lib/eiscp/message.rb', line 38

def value
  @value
end

#value_descriptionObject (readonly)

Value description



42
43
44
# File 'lib/eiscp/message.rb', line 42

def value_description
  @value_description
end

#value_nameObject (readonly)

Human readable value name



40
41
42
# File 'lib/eiscp/message.rb', line 40

def value_name
  @value_name
end

#zoneObject (readonly)

ISCP Zone



44
45
46
# File 'lib/eiscp/message.rb', line 44

def zone
  @zone
end

Instance Method Details

#==(other) ⇒ Object

Check if two messages are equivalent comparing their ISCP messages.



82
83
84
# File 'lib/eiscp/message.rb', line 82

def ==(other)
  to_iscp == other.to_iscp
end

#to_eiscpObject

Return EISCP Message string



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/eiscp/message.rb', line 94

def to_eiscp
  [
    @header[:magic],
    @header[:header_size].to_i,
    @header[:data_size].to_i,
    @header[:version].to_i,
    @header[:reserved],
    to_iscp.to_s,
    @terminator
  ].pack('A4NNCa3A*A*')
end

#to_iscpObject

Return ISCP Message string



88
89
90
# File 'lib/eiscp/message.rb', line 88

def to_iscp
  (@start + @unit_type + @command + @value).to_s
end

#to_sObject

Return human readable description.



108
109
110
# File 'lib/eiscp/message.rb', line 108

def to_s
  "#{@zone} - #{@command_name}:#{@value_name}"
end