Class: Response

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(central, function, number, parameters) ⇒ Response

Returns a new instance of Response.



5
6
7
# File 'lib/response.rb', line 5

def initialize central, function, number, parameters
  @central, @function, @number, @parameters = central, function, number, parameters
end

Instance Attribute Details

#centralObject (readonly)

Returns the value of attribute central.



3
4
5
# File 'lib/response.rb', line 3

def central
  @central
end

#functionObject (readonly)

Returns the value of attribute function.



3
4
5
# File 'lib/response.rb', line 3

def function
  @function
end

#numberObject (readonly)

Returns the value of attribute number.



3
4
5
# File 'lib/response.rb', line 3

def number
  @number
end

#parametersObject (readonly)

Returns the value of attribute parameters.



3
4
5
# File 'lib/response.rb', line 3

def parameters
  @parameters
end

Class Method Details

.parse(data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/response.rb', line 9

def self.parse data
  begin
    data = data.unpack("C*")
    unless data.first == 10
      startindex = data.index(2) 
      raise "Start byte not found in Response: #{data.inspect}" unless startindex
      length = data[startindex+1]
      calculatedChecksum = data[startindex..startindex+length-1].inject{|sum,x| sum + x } % 256
      checksum = data[startindex+length]
      raise "Checksum mismatch. Expecting #{checksum}, but calculated #{calculatedChecksum}" unless checksum == calculatedChecksum
      raise "Not an response." unless data[startindex+2] == Teletask::Command::EVENTREPORT 
      central = data[startindex+3]
      function = data[startindex+4]
      number = data[startindex+5..startindex+6].pack("c*").unpack("n").first
      parameters =data[startindex+8]
      return Response.new  central, function, number, parameters
    else
      return nil
    end
  rescue Exception => ex
    puts ex
    nil
  end
end