Class: Easee::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/easee/client.rb

Constant Summary collapse

BASE_URL =
"https://api.easee.cloud".freeze
TOKENS_CACHE_KEY =
"easee.auth.tokens".freeze

Instance Method Summary collapse

Constructor Details

#initialize(user_name:, password:, token_cache: ActiveSupport::Cache::MemoryStore.new, encryptor: NullEncryptor.new) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/easee/client.rb', line 11

def initialize(
  user_name:,
  password:,
  token_cache: ActiveSupport::Cache::MemoryStore.new,
  encryptor: NullEncryptor.new
)
  @user_name = user_name
  @password = password
  @token_cache = token_cache
  @encryptor = encryptor
end

Instance Method Details

#chargersObject



41
42
43
44
45
# File 'lib/easee/client.rb', line 41

def chargers
  get("/api/chargers").then do |response|
    response.body.map { |data| Charger.new(data) }
  end
end

#configuration(charger_id) ⇒ Object



69
70
71
72
# File 'lib/easee/client.rb', line 69

def configuration(charger_id)
  get("/api/chargers/#{charger_id}/config")
    .then { |response| Configuration.new(response.body) }
end

#inspectObject



80
81
82
83
84
# File 'lib/easee/client.rb', line 80

def inspect
  <<~INSPECT
    #<#{self.class.name} @user_name="[FILTERED]", @password="[FILTERED]", @token_cache=#{@token_cache.inspect}, @encryptor=#{@encryptor.inspect}>
  INSPECT
end

#loginObject



24
25
26
27
28
# File 'lib/easee/client.rb', line 24

def 
  with_error_handling do
    request_access_token
  end
end

#pair(charger_id:, pin_code:) ⇒ Object



36
37
38
# File 'lib/easee/client.rb', line 36

def pair(charger_id:, pin_code:)
  post("/api/chargers/#{charger_id}/pair", query: { pinCode: pin_code })
end

#pause_charging(charger_id) ⇒ Object



54
55
56
# File 'lib/easee/client.rb', line 54

def pause_charging(charger_id)
  post("/api/chargers/#{charger_id}/commands/pause_charging")
end

#poll_lifetime_energy(charger_id) ⇒ Object



64
65
66
# File 'lib/easee/client.rb', line 64

def poll_lifetime_energy(charger_id)
  post("/api/chargers/#{charger_id}/commands/poll_lifetimeenergy")
end

#resume_charging(charger_id) ⇒ Object



59
60
61
# File 'lib/easee/client.rb', line 59

def resume_charging(charger_id)
  post("/api/chargers/#{charger_id}/commands/resume_charging")
end

#site(charger_id) ⇒ Object



75
76
77
78
# File 'lib/easee/client.rb', line 75

def site(charger_id)
  get("/api/chargers/#{charger_id}/site")
    .then { |response| Site.new(response.body) }
end

#state(charger_id) ⇒ Object



48
49
50
51
# File 'lib/easee/client.rb', line 48

def state(charger_id)
  get("/api/chargers/#{charger_id}/state")
    .then { |response| State.new(response.body) }
end

#unpair(charger_id:, pin_code:) ⇒ Object



31
32
33
# File 'lib/easee/client.rb', line 31

def unpair(charger_id:, pin_code:)
  post("/api/chargers/#{charger_id}/unpair", query: { pinCode: pin_code })
end