Class: SensiParty::Sensi

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

Constant Summary collapse

SUCCESS_CODE =
200

Instance Method Summary collapse

Constructor Details

#initializeSensi

Returns a new instance of Sensi.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sensi_party/sensi.rb', line 7

def initialize
  @username = ''
  @password = ''
  @baseUrl = 'https://bus-serv.sensicomfort.com'
  @defaultHeaders = {
    'X-Requested-With' => 'XMLHttpRequest',
    'Accept'=> 'application/json; version=1, */*; q=0.01'
  }
  @cookies = nil
  @connectionToken = nil
  @thermostats = nil
end

Instance Method Details

#authorizeObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sensi_party/sensi.rb', line 26

def authorize
  response = nil
  url =  "#{@baseUrl}/api/authorize"
  data = {
    'UserName': @username,
    'Password': @password
  }
  additionalOpts = {
    headers: @defaultHeaders,
    payload: URI.encode_www_form(data)
  }

  response = self.sendRequest(url, :post, additionalOpts)
  @cookies = response.cookies unless response.code != SUCCESS_CODE
  yield if response.code == SUCCESS_CODE && block_given?
end

#getThermostatsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sensi_party/sensi.rb', line 52

def getThermostats
  response = nil
  url = "#{@baseUrl}/api/thermostats"

  additionalOpts = {
    headers: @defaultHeaders.merge({'Accept-Encoding': 'gzip, deflate, sdch'}),
    cookies: @cookies
  }

  response = self.sendRequest(url, :get, additionalOpts)
  json = JSON.parse(response)
  @thermostats = json
end

#negotiateObject



43
44
45
46
47
48
49
50
# File 'lib/sensi_party/sensi.rb', line 43

def negotiate
  response = nil
  url = "#{@baseUrl}/realtime/negotiate"

  response = self.sendRequest(url, :get)
  json = JSON.parse(response)
  @connectionToken = json['ConnectionToken']
end

#setHeat(temperature) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sensi_party/sensi.rb', line 66

def setHeat(temperature)
  response = nil
  url = "#{@baseUrl}/realtime/send"

  myThermostat = @thermostats.first
  additionalOpts = {
    headers: @defaultHeaders.merge({
      'Accept-Encoding': 'gzip, deflate',
    }),
    params: {
      transport: 'longPolling',
      connectionToken: self.getConnectionToken()
    },
    cookies: @cookies,
    payload: URI.encode_www_form({
      'H': 'thermostat-v1',
      'M': 'SetHeat',
      'A': [myThermostat['ICD'], temperature, 'F'],
      'I': 1
      }),
    timeout: nil
  }

  response = self.sendRequest(url, :post, additionalOpts)
end

#startObject



20
21
22
23
24
# File 'lib/sensi_party/sensi.rb', line 20

def start
  self.authorize do
    self.negotiate
  end
end