Class: SML::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-sml/sml-message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transaction_id, group_no, abort_on_error, body, checksum) ⇒ Message

Returns a new instance of Message.



11
12
13
14
15
16
17
# File 'lib/ruby-sml/sml-message.rb', line 11

def initialize(transaction_id, group_no, abort_on_error, body, checksum)
  @transaction_id = transaction_id
  @group_no = group_no
  @abort_on_error = abort_on_error
  @body = body
  @checksum = checksum
end

Instance Attribute Details

#abort_on_errorObject

Returns the value of attribute abort_on_error.



9
10
11
# File 'lib/ruby-sml/sml-message.rb', line 9

def abort_on_error
  @abort_on_error
end

#bodyObject

Returns the value of attribute body.



9
10
11
# File 'lib/ruby-sml/sml-message.rb', line 9

def body
  @body
end

#checksumObject

Returns the value of attribute checksum.



9
10
11
# File 'lib/ruby-sml/sml-message.rb', line 9

def checksum
  @checksum
end

#group_noObject

Returns the value of attribute group_no.



9
10
11
# File 'lib/ruby-sml/sml-message.rb', line 9

def group_no
  @group_no
end

#transaction_idObject

Returns the value of attribute transaction_id.



9
10
11
# File 'lib/ruby-sml/sml-message.rb', line 9

def transaction_id
  @transaction_id
end

Class Method Details

.construct(array_rep) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby-sml/sml-message.rb', line 19

def self.construct(array_rep)
  return nil if array_rep.nil?
  transaction_id = array_rep.shift
  group_no = array_rep.shift
  array_rep.shift unless group_no.nil?
  abort_on_error_code = array_rep.shift
  array_rep.shift unless abort_on_error_code.nil?
  abort_on_error = nil
  abort_on_error = case abort_on_error_code
                   when 0x00
                     :continue
                   when 0x01
                     :continue_with_next_group
                   when 0x02
                     :finish_group
                   when 0xff
                     :abort
                   end
  body = SML::MessageBody.construct(array_rep.shift)
  return nil if body.nil?
  checksum = array_rep.shift
  array_rep.shift unless checksum.nil?

  return nil unless array_rep.shift == :end_of_message
  return SML::Message.new(transaction_id, group_no, abort_on_error, body, checksum)
end

.pconstruct(o = {}) ⇒ Object



46
47
48
# File 'lib/ruby-sml/sml-message.rb', line 46

def self.pconstruct(o={})
  return SML::Message.new(o[:transaction_id], o[:group_no], o[:abord_on_error], o[:body], o[:checksum])
end

Instance Method Details

#calculate_checksumObject



76
77
78
79
80
81
82
83
# File 'lib/ruby-sml/sml-message.rb', line 76

def calculate_checksum
  encoded = SML::BinaryEncoding.encode_value(self.to_a_internal, :array)
  encoded.slice!(-3,3)
  calculated_checksum = CRC16.crc16(encoded)
  calculated_checksum = (calculated_checksum ^ 0xffff)
  calculated_checksum = ((calculated_checksum & 0xff00) >> 8) | ((calculated_checksum & 0x00ff) << 8)
  @checksum = calculated_checksum   
end

#to_aObject



50
51
52
53
# File 'lib/ruby-sml/sml-message.rb', line 50

def to_a
  calculate_checksum
  return to_a_internal
end

#to_a_internalObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby-sml/sml-message.rb', line 55

def to_a_internal
  abort_on_error_code = case abort_on_error
                        when :continue
                          0x00
                        when :continue_with_next_group
                          0x01
                        when :finish_group
                          0x02
                        when :abort
                          0xff
                        end
  
  result = [] << transaction_id << group_no
  result << :uint8 unless group_no.nil?
  result << abort_on_error_code
  result << :uint8 unless abort_on_error_code.nil?
  result << SML::MessageBody.to_a(body) << checksum
  result << :uint16 unless checksum.nil?
  return result << :end_of_message
end