Class: Sensi::ThermostatConnection
- Inherits:
-
Object
- Object
- Sensi::ThermostatConnection
- Includes:
- Sensi
- Defined in:
- lib/sensi/thermostat_connection.rb
Constant Summary
Constants included from Sensi
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
-
#first_poll_response ⇒ Object
Returns the value of attribute first_poll_response.
-
#groups_token ⇒ Object
Returns the value of attribute groups_token.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#message_id ⇒ Object
Returns the value of attribute message_id.
-
#response ⇒ Object
Returns the value of attribute response.
-
#thermostat_info ⇒ Object
Returns the value of attribute thermostat_info.
Instance Method Summary collapse
- #connect ⇒ Object
- #get_info ⇒ Object
-
#initialize(account) ⇒ ThermostatConnection
constructor
A new instance of ThermostatConnection.
-
#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.
- #poll ⇒ Object
- #set(device_id, setting, *args) ⇒ Object
- #start_polling ⇒ Object
- #stop_polling ⇒ Object
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) @account = account @headers = {'Accept' => 'application/json; version=1, */*; q=0.01', 'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8', 'Cookie' => @account. } end |
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
16 17 18 |
# File 'lib/sensi/thermostat_connection.rb', line 16 def account @account end |
#first_poll_response ⇒ Object
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_token ⇒ Object
Returns the value of attribute groups_token.
16 17 18 |
# File 'lib/sensi/thermostat_connection.rb', line 16 def groups_token @groups_token end |
#headers ⇒ Object
Returns the value of attribute headers.
16 17 18 |
# File 'lib/sensi/thermostat_connection.rb', line 16 def headers @headers end |
#message_id ⇒ Object
Returns the value of attribute message_id.
16 17 18 |
# File 'lib/sensi/thermostat_connection.rb', line 16 def @message_id end |
#response ⇒ Object
Returns the value of attribute response.
16 17 18 |
# File 'lib/sensi/thermostat_connection.rb', line 16 def response @response end |
#thermostat_info ⇒ Object
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
#connect ⇒ Object
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.} ) @message_id = JSON.parse(response.body)['C'] response end |
#get_info ⇒ Object
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 |
#poll ⇒ Object
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.} ) poll_response = Sensi::PollResponse.new(JSON.parse(response.body)) @message_id = poll_response. # 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.} ) #response response.code == 200 end |
#start_polling ⇒ Object
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.} ) json = JSON.parse(@first_poll_response.body) poll_response = Sensi::PollResponse.new(json) @message_id = poll_response. @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_polling ⇒ Object
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.} ) end |