Class: E3DCMqtt::E3DC::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/e3dc_mqtt/e3dc.rb

Overview

High-level client on top of the raw RSCP client. Handles system-info caching, battery enumeration, and domain-typed responses.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rscp_client) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
27
# File 'lib/e3dc_mqtt/e3dc.rb', line 22

def initialize(rscp_client)
  @rscp = rscp_client
  @today = midnight_at(Time.now)
  load_static_info
  load_batteries
end

Instance Attribute Details

#batteriesObject (readonly)

Returns the value of attribute batteries.



13
14
15
# File 'lib/e3dc_mqtt/e3dc.rb', line 13

def batteries
  @batteries
end

#derate_percentObject (readonly)

Returns the value of attribute derate_percent.



13
14
15
# File 'lib/e3dc_mqtt/e3dc.rb', line 13

def derate_percent
  @derate_percent
end

#derate_powerObject (readonly)

Returns the value of attribute derate_power.



13
14
15
# File 'lib/e3dc_mqtt/e3dc.rb', line 13

def derate_power
  @derate_power
end

#external_source_availableObject (readonly)

Returns the value of attribute external_source_available.



13
14
15
# File 'lib/e3dc_mqtt/e3dc.rb', line 13

def external_source_available
  @external_source_available
end

#installed_peak_powerObject (readonly)

Returns the value of attribute installed_peak_power.



13
14
15
# File 'lib/e3dc_mqtt/e3dc.rb', line 13

def installed_peak_power
  @installed_peak_power
end

#mac_addressObject (readonly)

Returns the value of attribute mac_address.



13
14
15
# File 'lib/e3dc_mqtt/e3dc.rb', line 13

def mac_address
  @mac_address
end

#modelObject (readonly)

Returns the value of attribute model.



13
14
15
# File 'lib/e3dc_mqtt/e3dc.rb', line 13

def model
  @model
end

#serial_numberObject (readonly)

Returns the value of attribute serial_number.



13
14
15
# File 'lib/e3dc_mqtt/e3dc.rb', line 13

def serial_number
  @serial_number
end

Class Method Details

.connect(host:, user:, password:, key:, port: 5033, **rest) ⇒ Object



17
18
19
20
# File 'lib/e3dc_mqtt/e3dc.rb', line 17

def self.connect(host:, user:, password:, key:, port: 5033, **rest)
  rscp = RSCP::Client.new(host: host, port: port, user: user, password: password, key: key, **rest)
  new(rscp)
end

Instance Method Details

#batteries_dataObject



108
109
110
# File 'lib/e3dc_mqtt/e3dc.rb', line 108

def batteries_data
  @batteries.map { |bat| battery_data(bat) }
end

#closeObject



29
# File 'lib/e3dc_mqtt/e3dc.rb', line 29

def close = @rscp.close

#daily_statisticsObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/e3dc_mqtt/e3dc.rb', line 96

def daily_statistics
  now = Time.now
  midnight = midnight_at(now)
  if @today != midnight
    stat = history_data(@today, 24 * 60 * 60)
    @today = midnight
    stat
  else
    history_data(midnight, now - midnight)
  end
end

#statusObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/e3dc_mqtt/e3dc.rb', line 33

def status
  vals = query(%i[
    EMS_REQ_POWER_PV
    EMS_REQ_POWER_BAT
    EMS_REQ_POWER_GRID
    EMS_REQ_POWER_HOME
    EMS_REQ_BAT_SOC
    EMS_REQ_AUTARKY
    EMS_REQ_SELF_CONSUMPTION
    EMS_REQ_POWER_WB_ALL
    EMS_REQ_POWER_ADD
  ])

  Status.new(
    timestamp:        Time.now,
    power_pv:         round2(vals[:EMS_POWER_PV]),
    power_battery:    round2(vals[:EMS_POWER_BAT]),
    power_grid:       round2(vals[:EMS_POWER_GRID]),
    power_home:       round2(vals[:EMS_POWER_HOME]),
    battery_soc:      round2(vals[:EMS_BAT_SOC]),
    autarky:          round2(vals[:EMS_AUTARKY]),
    self_consumption: round2(vals[:EMS_SELF_CONSUMPTION]),
    power_wb:         round2(vals[:EMS_POWER_WB_ALL]),
    power_add:        round2(vals[:EMS_POWER_ADD])
  )
end

#system_infoObject



60
61
62
63
64
65
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
91
92
93
94
# File 'lib/e3dc_mqtt/e3dc.rb', line 60

def system_info
  responses = @rscp.send_many([
    RSCP::Message.build(:INFO_REQ_SW_RELEASE),
    RSCP::Message.build(:INFO_REQ_IP_ADDRESS),
    RSCP::Message.build(:EMS_REQ_GET_POWER_SETTINGS),
    RSCP::Message.build(:EMS_REQ_GET_SYS_SPECS)
  ])
  top = by_name(responses)
  power_settings = container_by_name(responses, :EMS_GET_POWER_SETTINGS)
  sys_specs = parse_sys_specs(responses)

  SystemInfo.new(
    timestamp:                       Time.now,
    serial_number:                   @serial_number,
    mac_address:                     @mac_address,
    ip_address:                      top[:INFO_IP_ADDRESS],
    model:                           @model,
    software_release:                top[:INFO_SW_RELEASE],
    installed_peak_power:            @installed_peak_power,
    installed_battery_capacity:      sys_specs["installedBatteryCapacity"],
    max_ac_power:                    sys_specs["maxAcPower"],
    max_battery_charge_power:        sys_specs["maxBatChargePower"],
    max_battery_discharge_power:     sys_specs["maxBatDischargPower"],
    derate_percent:                  @derate_percent,
    derate_power:                    @derate_power,
    max_charge_power:                power_settings[:EMS_MAX_CHARGE_POWER],
    max_discharge_power:             power_settings[:EMS_MAX_DISCHARGE_POWER],
    discharge_start_power:           power_settings[:EMS_DISCHARGE_START_POWER],
    power_limits_used:               power_settings[:EMS_POWER_LIMITS_USED],
    power_save_enabled:              power_settings[:EMS_POWERSAVE_ENABLED],
    weather_forecast_mode:           power_settings[:EMS_WEATHER_FORECAST_MODE],
    weather_regulated_charge_enabled: power_settings[:EMS_WEATHER_REGULATED_CHARGE_ENABLED],
    external_source_available:       @external_source_available
  )
end

#topic_prefixObject



31
# File 'lib/e3dc_mqtt/e3dc.rb', line 31

def topic_prefix = "#{@model}-#{@serial_number}"