Class: CIMD::Message

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

Overview

dcs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation_code) ⇒ Message

Returns a new instance of Message.



109
110
111
112
113
114
115
# File 'lib/cimd_structures.rb', line 109

def initialize(operation_code)
  @operation_code = operation_code
  @packet_number = 1
  @parameters = Array.new
  @checksum = 0
  @dcs = Dcs.new(0)
end

Instance Attribute Details

#checksumObject

Returns the value of attribute checksum.



106
107
108
# File 'lib/cimd_structures.rb', line 106

def checksum
  @checksum
end

#dcsObject

Returns the value of attribute dcs.



107
108
109
# File 'lib/cimd_structures.rb', line 107

def dcs
  @dcs
end

#operation_codeObject

Returns the value of attribute operation_code.



103
104
105
# File 'lib/cimd_structures.rb', line 103

def operation_code
  @operation_code
end

#packet_numberObject

Returns the value of attribute packet_number.



105
106
107
# File 'lib/cimd_structures.rb', line 105

def packet_number
  @packet_number
end

#parametersObject

Returns the value of attribute parameters.



104
105
106
# File 'lib/cimd_structures.rb', line 104

def parameters
  @parameters
end

Class Method Details

.parse(data) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/cimd_structures.rb', line 187

def self.parse(data)
  data.slice!(0)
  fields = data.split(TAB)
  header = fields[0].split(PARAM_SEP)
  operation_code = header[0].to_i
  m = Message.new(operation_code)
  m.packet_number = header[1].to_i
  fields.delete_at(0)
  m.checksum = (fields.delete_at(fields.size - 1)).hex.to_i
  fields.each do |p|
      m.parameters << Parameter.parse(p)
  end
  m.dcs.set_value(m.parameter_value(P_DATA_CODING_SCHEME)) if m.has_parameter?(P_DATA_CODING_SCHEME)
  return m
end

Instance Method Details

#add(parameter) ⇒ Object



151
152
153
# File 'lib/cimd_structures.rb', line 151

def add(parameter)
  @parameters.push(parameter)
end

#alphabetObject



117
118
119
# File 'lib/cimd_structures.rb', line 117

def alphabet
  @dcs.alphabet
end

#calc_checksumObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cimd_structures.rb', line 133

def calc_checksum
  checksum = 0
  s = String.new
  s << STX
  s << (sprintf "%02d:%03d",@operation_code,@packet_number)
  s << TAB
  @parameters.each do |p|
    s << p.to_s
    s << TAB
  end

  s.each_byte do |b|
    checksum += b
    checksum &= 0xFF
  end
  return checksum
end

#errorObject



129
130
131
# File 'lib/cimd_structures.rb', line 129

def error
  return has_error? ? "(#{parameter_value(CIMD::P_ERROR_CODE)}) #{parameter_value(CIMD::P_ERROR_TEXT)}" : nil
end

#has_alphabet_ucs2?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/cimd_structures.rb', line 121

def has_alphabet_ucs2?
  @dcs.alphabet == ALPHABET_UCS2
end

#has_error?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/cimd_structures.rb', line 125

def has_error?
  return has_parameter?(P_ERROR_CODE) ? true : false
end

#has_parameter?(code) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
220
221
222
# File 'lib/cimd_structures.rb', line 217

def has_parameter?(code)
  @parameters.each do |p|
    return true if p.code == code
  end
  return false
end

#is_binary?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/cimd_structures.rb', line 183

def is_binary?
  has_parameter?(P_USER_DATA_BINARY)
end

#parameter_value(code) ⇒ Object



224
225
226
227
228
229
# File 'lib/cimd_structures.rb', line 224

def parameter_value(code)
  @parameters.each do |p|
    return p.value if p.code == code
  end
  return nil
end

#parse_binary_dataObject



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/cimd_structures.rb', line 203

def parse_binary_data
  if has_alphabet_ucs2? and @operation_code == OP_DELIVERY_MESSAGE
    data = parameter_value(P_USER_DATA_BINARY)
    i = 0
    s = ""
    while i < data.length do
      s << data[i,2].hex
      i += 2
    end
    p = Parameter.new(P_USER_DATA,Iconv.iconv("UTF-8", "UCS-2BE",s).first)
    add(p)
  end
end

#to_binaryObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/cimd_structures.rb', line 168

def to_binary
  s = String.new
  s << STX
  s << (sprintf "%02d:%03d",@operation_code,@packet_number)
  s << TAB
  #add(Parameter.new(P_DATA_CODING_SCHEME,@dcs.value)) if @operation_code == OP_SUBMIT
  @parameters.each do |p|
    s << p.to_s
    s << TAB
  end
  s << (sprintf "%02x",calc_checksum)
  s << ETX
  return s
end

#to_sObject



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/cimd_structures.rb', line 155

def to_s
  s = sprintf "<STX>%02d:%03d<TAB>",@operation_code,@packet_number
  #add(Parameter.new(P_DATA_CODING_SCHEME,@dcs.value)) @operation_code == OP_SUBMIT
  @parameters.each do |p|
    s << p.to_s
    s << "<TAB>"
  end
  s << (sprintf "%02x",@checksum).upcase
  s << "<ETX> "
  s << CIMD::opcode_description(@operation_code)
  return s
end