Class: Mixergy::Client

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

Constant Summary collapse

API_ROOT =
"https://www.mixergy.io/api/v2"

Instance Method Summary collapse

Constructor Details

#initializeClient

Create a new Mixergy API client.



15
16
17
18
19
20
21
22
23
# File 'lib/mixergy/client.rb', line 15

def initialize
  @connection = Faraday.new(
    url: API_ROOT
  ) do |faraday|
    faraday.request :json      # Automatically encode request bodies as JSON
    faraday.response :json     # Automatically parse response bodies as JSON
    faraday.response :raise_error  # Raise exceptions on HTTP errors
  end
end

Instance Method Details

#default_tank_idString?

Returns the default tank ID from config, or the first tank’s ID.

Returns:

  • (String, nil)

    the default tank ID



69
70
71
72
73
74
# File 'lib/mixergy/client.rb', line 69

def default_tank_id
  @default_tank_id ||= begin
    load_config
    @config[:default_tank_id] || tanks.first&.id
  end
end

#load_configMixergy::Config

Loads configuration from ~/.mixergy and sets the API token if present.

Returns:



27
28
29
30
31
32
33
34
35
# File 'lib/mixergy/client.rb', line 27

def load_config
  @config ||= begin
    config = Mixergy::Config.new
    config.load
    # FIXME: find a better place to put this
    @connection.headers["Authorization"] = "Bearer #{config[:token]}"
    config
  end
end

#login(username, password) ⇒ String

Authenticates with the Mixergy API and stores the token in the connection.

Parameters:

  • username (String)

    the username

  • password (String)

    the password

Returns:

  • (String)

    the authentication token

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mixergy/client.rb', line 42

def (username, password)
  resp = @connection.post(
    "account/login",
    {username: username, password: password}
  )
  data = resp.body

  if data["token"]
    @connection.headers["Authorization"] = "Bearer #{data["token"]}"
    data["token"]
  else
    raise Mixergy::Error, "Login failed (status: #{resp.status}, body: #{resp.body.inspect})"
  end
end

#set_charge(percent, tank = nil) ⇒ Boolean

Sets the target charge for a tank via the control endpoint.

Parameters:

  • percent (Integer)

    the target charge percentage

  • tank (Tank, String, nil) (defaults to: nil)

    the Tank object or Tank GUID (optional)

Returns:

  • (Boolean)

    true if successful, false otherwise



90
91
92
93
94
95
# File 'lib/mixergy/client.rb', line 90

def set_charge(percent, tank = nil)
  tank_id = tank.id if tank.is_a?(Tank)
  tank_id = default_tank_id if tank_id.nil?
  resp = @connection.put("tanks/#{tank_id}/control", {charge: percent})
  resp.success?
end

#set_target_temperature(temp, tank = nil) ⇒ Boolean

Sets the target (maximum) temperature for a tank via the settings endpoint.

Parameters:

  • temp (Numeric)

    The desired target temperature in Celsius

  • tank (Tank, nil) (defaults to: nil)

    The tank object (optional)

Returns:

  • (Boolean)

    true if successful, false otherwise



114
115
116
117
118
119
# File 'lib/mixergy/client.rb', line 114

def set_target_temperature(temp, tank = nil)
  tank_id = tank.id if tank.is_a?(Tank)
  tank_id = default_tank_id if tank_id.nil?
  resp = @connection.put("tanks/#{tank_id}/settings", {max_temp: temp})
  resp.success?
end

#status(tank = nil) ⇒ Status

Fetches the latest status/measurement for a tank.

Parameters:

  • tank (Tank, nil) (defaults to: nil)

    the tank object (optional)

Returns:

  • (Status)

    the status object for the tank



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

def status(tank = nil)
  tank_id = tank.id if tank.is_a?(Tank)
  tank_id = default_tank_id if tank_id.nil?
  resp = @connection.get("tanks/#{tank_id}/measurements/latest")
  Status.new(resp.body)
end

#tanksArray<Tank>

Fetches all tanks associated with the account.

Returns:

  • (Array<Tank>)

    list of Tank objects



59
60
61
62
63
64
65
# File 'lib/mixergy/client.rb', line 59

def tanks
  resp = @connection.get("tanks")
  tank_list = resp.body.dig("_embedded", "tankList") || []
  tank_list.map do |tank_data|
    Tank.new(tank_data)
  end
end

#target_temperature(tank = nil) ⇒ Integer

Get the target (maximum) temperature for a tank via the settings endpoint.

Parameters:

  • tank (Tank, nil) (defaults to: nil)

    The tank object (optional)

Returns:

  • (Integer)

    The target temperature in Celsius



100
101
102
103
104
105
106
107
108
# File 'lib/mixergy/client.rb', line 100

def target_temperature(tank = nil)
  tank_id = tank.id if tank.is_a?(Tank)
  tank_id = default_tank_id if tank_id.nil?
  resp = @connection.get("tanks/#{tank_id}/settings")
  body = resp.body
  # If the response is a String (due to wrong content type), parse it as JSON
  body = JSON.parse(body) if body.is_a?(String) && body.start_with?("{")
  body["max_temp"].to_i
end