feat(deye): ignore invalid counter increments
All checks were successful
Build / build (push) Successful in 3m10s
All checks were successful
Build / build (push) Successful in 3m10s
This commit is contained in:
parent
42edf668a4
commit
c5e58816db
1 changed files with 28 additions and 29 deletions
|
@ -12,7 +12,7 @@ defmodule WeewxProxy.Modbus.Deye do
|
||||||
@impl true
|
@impl true
|
||||||
def init(_opts) do
|
def init(_opts) do
|
||||||
_ = Logger.info("Initializing handler")
|
_ = Logger.info("Initializing handler")
|
||||||
{:ok, nil}
|
{:ok, 0}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
@ -59,6 +59,10 @@ defmodule WeewxProxy.Modbus.Deye do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_message(["bitshake", "tele", "smartmeter", topic], publish, state) do
|
def handle_message(["bitshake", "tele", "smartmeter", topic], publish, state) do
|
||||||
|
current_time = Time.utc_now()
|
||||||
|
current_hour = current_time.hour
|
||||||
|
new_state = if current_hour == 0, do: 0, else: state
|
||||||
|
|
||||||
if topic == "SENSOR" do
|
if topic == "SENSOR" do
|
||||||
{:ok, meter_data} = Jason.decode(publish)
|
{:ok, meter_data} = Jason.decode(publish)
|
||||||
|
|
||||||
|
@ -77,9 +81,9 @@ defmodule WeewxProxy.Modbus.Deye do
|
||||||
:ok = Publisher.publish("weewx/ingest_si", mqtt_data)
|
:ok = Publisher.publish("weewx/ingest_si", mqtt_data)
|
||||||
:ok = Publisher.publish_value_map("hadata/bitShake", mqtt_data)
|
:ok = Publisher.publish_value_map("hadata/bitShake", mqtt_data)
|
||||||
|
|
||||||
{:ok, state}
|
{:ok, new_state}
|
||||||
else
|
else
|
||||||
{:ok, state}
|
{:ok, new_state}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -111,41 +115,36 @@ defmodule WeewxProxy.Modbus.Deye do
|
||||||
@spec handle_reading(String.t(), float() | nil, {:day | :active, float()}) :: {:day | :active, float()} | nil
|
@spec handle_reading(String.t(), float() | nil, {:day | :active, float()}) :: {:day | :active, float()} | nil
|
||||||
defp handle_reading(_topic, nil, state), do: state
|
defp handle_reading(_topic, nil, state), do: state
|
||||||
|
|
||||||
defp handle_reading("deye/day_energy", reading, {:active, active}) do
|
defp handle_reading("deye/day_energy", reading, old_reading) do
|
||||||
|
if reading >= old_reading do
|
||||||
timestamp = DateTime.utc_now() |> DateTime.to_unix()
|
timestamp = DateTime.utc_now() |> DateTime.to_unix()
|
||||||
|
|
||||||
data = %{
|
data = %{
|
||||||
dateTime: timestamp,
|
dateTime: timestamp,
|
||||||
solarEnergyDay: reading * 1000.0,
|
solarEnergyDay: reading * 1000.0
|
||||||
solarEnergyActive: active
|
|
||||||
}
|
}
|
||||||
|
|
||||||
:ok = Publisher.publish("weewx/ingest_si", data)
|
:ok = Publisher.publish("weewx/ingest_si", data)
|
||||||
:ok = Publisher.publish_value_map("hadata/deye", data)
|
:ok = Publisher.publish_value_map("hadata/deye", data)
|
||||||
|
|
||||||
nil
|
reading
|
||||||
|
else
|
||||||
|
_ = Logger.warning("Reading unexpectedly decreased from #{old_reading} to #{reading}")
|
||||||
|
old_reading
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp handle_reading("deye/day_energy", reading, _other) do
|
defp handle_reading("deye/ac/active_power", reading, state) do
|
||||||
{:day, reading}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp handle_reading("deye/ac/active_power", reading, {:day, day}) do
|
|
||||||
timestamp = DateTime.utc_now() |> DateTime.to_unix()
|
timestamp = DateTime.utc_now() |> DateTime.to_unix()
|
||||||
|
|
||||||
data = %{
|
data = %{
|
||||||
dateTime: timestamp,
|
dateTime: timestamp,
|
||||||
solarEnergyDay: day * 1000.0,
|
|
||||||
solarEnergyActive: reading
|
solarEnergyActive: reading
|
||||||
}
|
}
|
||||||
|
|
||||||
:ok = Publisher.publish("weewx/ingest_si", data)
|
:ok = Publisher.publish("weewx/ingest_si", data)
|
||||||
:ok = Publisher.publish_value_map("hadata/deye", data)
|
:ok = Publisher.publish_value_map("hadata/deye", data)
|
||||||
|
|
||||||
nil
|
state
|
||||||
end
|
|
||||||
|
|
||||||
defp handle_reading("deye/ac/active_power", reading, _other) do
|
|
||||||
{:active, reading}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue