Class: Senec::Cloud::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/senec/cloud/connection.rb

Constant Summary collapse

DEFAULT_USER_AGENT =
"ruby-senec/#{Senec::VERSION} (+https://github.com/solectrus/senec)".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username:, password:, user_agent: DEFAULT_USER_AGENT, totp_uri: nil) ⇒ Connection

Returns a new instance of Connection.



20
21
22
23
24
25
# File 'lib/senec/cloud/connection.rb', line 20

def initialize(username:, password:, user_agent: DEFAULT_USER_AGENT, totp_uri: nil)
  @username = username
  @password = password
  @user_agent = user_agent
  @totp_uri = totp_uri
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



27
28
29
# File 'lib/senec/cloud/connection.rb', line 27

def password
  @password
end

#totp_uriObject (readonly)

Returns the value of attribute totp_uri.



27
28
29
# File 'lib/senec/cloud/connection.rb', line 27

def totp_uri
  @totp_uri
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



27
28
29
# File 'lib/senec/cloud/connection.rb', line 27

def user_agent
  @user_agent
end

#usernameObject (readonly)

Returns the value of attribute username.



27
28
29
# File 'lib/senec/cloud/connection.rb', line 27

def username
  @username
end

Instance Method Details

#authenticate!Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/senec/cloud/connection.rb', line 29

def authenticate!
  code_verifier = SecureRandom.alphanumeric(43)
  digest = Digest::SHA256.digest(code_verifier)
  code_challenge = Base64.urlsafe_encode64(digest).delete('=')

  auth_url =
    oauth_client.auth_code.authorize_url(
      redirect_uri: REDIRECT_URI,
      scope: SCOPE,
      code_challenge:,
      code_challenge_method: 'S256',
    )

  # Manual HTTP needed for Keycloak cross-domain form handling
  form_type, form_url = (auth_url)
  redirect_url = submit_credentials(form_type, form_url)
  authorization_code = extract_authorization_code(redirect_url)

  self.oauth_token =
    oauth_client.auth_code.get_token(
      authorization_code,
      redirect_uri: REDIRECT_URI,
      code_verifier:,
    )
end

#authenticated?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/senec/cloud/connection.rb', line 55

def authenticated?
  !!oauth_token
end

#dashboard(system_id) ⇒ Object



67
68
69
# File 'lib/senec/cloud/connection.rb', line 67

def dashboard(system_id)
  get "#{MEASUREMENTS_HOST}/v1/systems/#{system_id}/dashboard"
end

#system_details(system_id) ⇒ Object



63
64
65
# File 'lib/senec/cloud/connection.rb', line 63

def system_details(system_id)
  get "#{SYSTEMS_HOST}/systems/#{system_id}/details"
end

#systemsObject



59
60
61
# File 'lib/senec/cloud/connection.rb', line 59

def systems
  get "#{SYSTEMS_HOST}/v1/systems"
end

#wallbox(system_id, wallbox_id) ⇒ Object



71
72
73
# File 'lib/senec/cloud/connection.rb', line 71

def wallbox(system_id, wallbox_id)
  get "#{WALLBOX_HOST}/v1/systems/#{system_id}/wallboxes/#{wallbox_id}"
end

#wallbox_search(system_id) ⇒ Object



75
76
77
# File 'lib/senec/cloud/connection.rb', line 75

def wallbox_search(system_id)
  post "#{WALLBOX_HOST}/v1/systems/wallboxes/search", { systemIds: [system_id] }
end