Class: TeletaskApi::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.



9
10
11
# File 'lib/response.rb', line 9

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.



7
8
9
# File 'lib/response.rb', line 7

def central
  @central
end

#functionObject (readonly)

Returns the value of attribute function.



7
8
9
# File 'lib/response.rb', line 7

def function
  @function
end

#numberObject (readonly)

Returns the value of attribute number.



7
8
9
# File 'lib/response.rb', line 7

def number
  @number
end

#parametersObject (readonly)

Returns the value of attribute parameters.



7
8
9
# File 'lib/response.rb', line 7

def parameters
  @parameters
end

Class Method Details

.parse(data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/response.rb', line 13

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] == Command::EVENTREPORT 
			central = data[startindex+3]
			function = data[startindex+4]
			number = data[startindex+5..startindex+6].pack("c*").unpack("n").first
			case function
			when Function::SENSOR
				parameters =data[startindex+8..startindex+9]
			else
				parameters =data[startindex+8]
			end
			return Response.new  central, function, number, parameters
		#end
	rescue Exception => ex
		puts ex
		nil
	end
	return nil
end

Instance Method Details

#to_hashObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/response.rb', line 42

def to_hash
	hash = Hash.new
	hash[:function] = function
	hash[:function_name] = Function.name function
	hash[:number] = number
	hash[:parameters] = parameters
	case function
	when Function::SENSOR
		hash[:temperature] = Converter.short_to_temperature parameters
	when Function::RELAY
		hash[:state] = parameters
		hash[:state_name] = Setting.name parameters
	end
	hash
end