Class: Sensi::ThermostatConnection

Inherits:
Object
  • Object
show all
Includes:
Sensi
Defined in:
lib/sensi/thermostat_connection.rb

Constant Summary

Constants included from Sensi

API_URI, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account) ⇒ ThermostatConnection

Returns a new instance of ThermostatConnection.



26
27
28
29
30
31
32
# File 'lib/sensi/thermostat_connection.rb', line 26

def initialize()
	@account = 
	@headers = {'Accept' => 'application/json; version=1, */*; q=0.01', 
		'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8', 
		'Cookie' => @account.auth_cookie
	}
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



16
17
18
# File 'lib/sensi/thermostat_connection.rb', line 16

def 
  @account
end

#first_poll_responseObject

Returns the value of attribute first_poll_response.



16
17
18
# File 'lib/sensi/thermostat_connection.rb', line 16

def first_poll_response
  @first_poll_response
end

#groups_tokenObject

Returns the value of attribute groups_token.



16
17
18
# File 'lib/sensi/thermostat_connection.rb', line 16

def groups_token
  @groups_token
end

#headersObject

Returns the value of attribute headers.



16
17
18
# File 'lib/sensi/thermostat_connection.rb', line 16

def headers
  @headers
end

#message_idObject

Returns the value of attribute message_id.



16
17
18
# File 'lib/sensi/thermostat_connection.rb', line 16

def message_id
  @message_id
end

#responseObject

Returns the value of attribute response.



16
17
18
# File 'lib/sensi/thermostat_connection.rb', line 16

def response
  @response
end

#thermostat_infoObject

Returns the value of attribute thermostat_info.



16
17
18
# File 'lib/sensi/thermostat_connection.rb', line 16

def thermostat_info
  @thermostat_info
end

Instance Method Details

#connectObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sensi/thermostat_connection.rb', line 59

def connect
	response = HTTParty.get(
		Sensi::API_URI + '/realtime/connect',
		query: {transport: 'longPolling', 
       connectionToken: @account.token, 
       connectionData: '[{"name": "thermostat-v1"}]', 
       tid: 4, 
       _: (Time.now.to_f*1000).to_i},
		headers: {"Cookie" => @account.auth_cookie}
		)
	@message_id = JSON.parse(response.body)['C']
	response
end

#get_infoObject



34
35
36
37
38
39
40
# File 'lib/sensi/thermostat_connection.rb', line 34

def get_info
	@response = HTTParty.get(
		Sensi::API_URI + '/api/thermostats',
		headers: @account.headers
		)
	@thermostat_info = JSON.parse(@response.body)
end

#initialize_polling(device_id) ⇒ Object

def initialize_polling(device_id) response = HTTParty.post( Sensi::API_URI + ‘/realtime/send’, query: ‘longPolling’, connectionToken: @account.token, body: encode(“‘thermostat-v1’, M: ‘Subscribe’, A:, I: 0}”), headers: => @account.auth_cookie ) response end



83
84
85
# File 'lib/sensi/thermostat_connection.rb', line 83

def initialize_polling(device_id)
  set(device_id, 'Subscribe')
end

#pollObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sensi/thermostat_connection.rb', line 112

def poll
	response = HTTParty.get(
		Sensi::API_URI + '/realtime/poll',
		query: {transport: 'longPolling', 
			connectionToken: @account.token, 
			connectionData: '[{"name": "thermostat-v1"}]', 
			tid: 4, 
			_: (Time.now.to_f*1000).to_i, 
			messageId: @message_id,
			groupsToken: @groups_token},
		headers: {"Cookie" => @account.auth_cookie}
		)

   poll_response = Sensi::PollResponse.new(JSON.parse(response.body))
   @message_id = poll_response.message_id
	# json = JSON.parse(response.body)
	# @message_id = json['C']
	poll_response
end

#set(device_id, setting, *args) ⇒ Object



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

def set(device_id, setting, *args)
  setting_args = [device_id]
  args.each do |arg|
    setting_args.push(arg)
  end

  response = HTTParty.post(
    Sensi::API_URI + '/realtime/send',
    query: {transport: 'longPolling', connectionToken: @account.token},
    body: encode("{H: 'thermostat-v1', M: '#{setting}', A: #{setting_args}, I: 0}"), 
    headers: {"Cookie" => @account.auth_cookie}
    )
  #response
  response.code == 200
end

#start_pollingObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sensi/thermostat_connection.rb', line 87

def start_polling
	@first_poll_response = HTTParty.get(
		Sensi::API_URI + '/realtime/poll',
		query: {transport: 'longPolling', 
       connectionToken: @account.token, 
       connectionData: '[{"name": "thermostat-v1"}]', 
       tid: 4, 
       _: (Time.now.to_f*1000).to_i, 
       messageId: @message_id},
		headers: {"Cookie" => @account.auth_cookie}
		)

   json = JSON.parse(@first_poll_response.body)
   poll_response = Sensi::PollResponse.new(json)
   @message_id = poll_response.message_id
   @groups_token = poll_response.groups_token
	# json = JSON.parse(@first_poll_response.body)
	# @message_id = json['C']
	# @groups_token = json['G']
	# @first_poll_response
   json
 rescue 
   json
end

#stop_pollingObject



132
133
134
135
136
137
138
139
# File 'lib/sensi/thermostat_connection.rb', line 132

def stop_polling
	response = HTTParty.post(
		Sensi::API_URI + '/realtime/abort',
		query: {transport: 'longPolling', connectionToken: @account.token},
		body: '',
		headers: {'Cookie' => @account.auth_cookie}
		)
end