2020-03-08 15:05:20 +13:00
|
|
|
defmodule AugieWeb.PowerSensorLive do
|
|
|
|
use Phoenix.LiveView
|
|
|
|
alias Augie.Sensor.Power
|
|
|
|
|
|
|
|
@moduledoc """
|
|
|
|
A LiveView which displays the data from the Power sensor.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@sample_count 60
|
|
|
|
|
|
|
|
def render(assigns) do
|
|
|
|
~L"""
|
|
|
|
<div class="card">
|
|
|
|
<div class="card-divider">
|
|
|
|
<h4>Power Sensor</h4>
|
|
|
|
</div>
|
|
|
|
<%= if @data_ready do %>
|
|
|
|
<div class="card-section">
|
|
|
|
|
|
|
|
<div class="grid-x">
|
|
|
|
<div class="cell auto"><strong>Bus Voltage</strong></div>
|
|
|
|
<div class="cell auto text-right"><%= Float.round(@avg_bus_voltage, 2) %>V</div>
|
|
|
|
</div>
|
|
|
|
<div class="grid-x">
|
|
|
|
<div class="cell"><%= live_component(@socket, AugieWeb.SparklineComponent, data: @bus_voltage, min: 0, sample_count: 60) %></div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="grid-x">
|
|
|
|
<div class="cell auto"><strong>Shunt Voltage</strong></div>
|
|
|
|
<div class="cell auto text-right"><%= Float.round(@avg_shunt_voltage, 2) %>V</div>
|
|
|
|
</div>
|
|
|
|
<div class="grid-x">
|
|
|
|
<div class="cell"><%= live_component(@socket, AugieWeb.SparklineComponent, data: @shunt_voltage, min: 0, sample_count: 60) %></div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="grid-x">
|
|
|
|
<div class="cell auto"><strong>Current</strong></div>
|
2020-04-03 17:33:50 +13:00
|
|
|
<div class="cell auto text-right"><%= Float.round(@avg_current, 2) %>mA</div>
|
2020-03-08 15:05:20 +13:00
|
|
|
</div>
|
|
|
|
<div class="grid-x">
|
|
|
|
<div class="cell"><%= live_component(@socket, AugieWeb.SparklineComponent, data: @current, min: 0, sample_count: 60) %></div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="grid-x">
|
|
|
|
<div class="cell auto"><strong>Power</strong></div>
|
2020-04-03 17:33:50 +13:00
|
|
|
<div class="cell auto text-right"><%= Float.round(@avg_power, 2) %>mW</div>
|
2020-03-08 15:05:20 +13:00
|
|
|
</div>
|
|
|
|
<div class="grid-x">
|
|
|
|
<div class="cell"><%= live_component(@socket, AugieWeb.SparklineComponent, data: @power, min: 0, sample_count: 60) %></div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<% else %>
|
|
|
|
<div class="card-section">
|
|
|
|
No data available.
|
|
|
|
</div>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
def mount(_params, _context, socket) do
|
2020-04-11 17:24:59 +12:00
|
|
|
if connected?(socket), do: CommunityTheatre.subscribe(Power, 1)
|
2020-03-08 15:05:20 +13:00
|
|
|
|
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign(
|
|
|
|
data_ready: false,
|
|
|
|
bus_voltage: CircularBuffer.new(@sample_count),
|
|
|
|
shunt_voltage: CircularBuffer.new(@sample_count),
|
|
|
|
current: CircularBuffer.new(@sample_count),
|
|
|
|
power: CircularBuffer.new(@sample_count),
|
|
|
|
avg_bus_voltage: 0,
|
|
|
|
avg_shunt_voltage: 0,
|
|
|
|
avg_current: 0,
|
|
|
|
avg_power: 0
|
|
|
|
)
|
|
|
|
|
|
|
|
{:ok, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_info(
|
2020-04-11 17:24:59 +12:00
|
|
|
{CommunityTheatre, %{topic: Power, payload: sample}},
|
2020-03-08 15:05:20 +13:00
|
|
|
%{
|
|
|
|
assigns: %{
|
|
|
|
bus_voltage: bus_voltage,
|
|
|
|
shunt_voltage: shunt_voltage,
|
|
|
|
current: current,
|
|
|
|
power: power
|
|
|
|
}
|
|
|
|
} = socket
|
|
|
|
) do
|
|
|
|
bus_voltage = bus_voltage |> CircularBuffer.insert(abs(sample.bus_voltage))
|
|
|
|
shunt_voltage = shunt_voltage |> CircularBuffer.insert(abs(sample.shunt_voltage))
|
|
|
|
current = current |> CircularBuffer.insert(abs(sample.current))
|
|
|
|
power = power |> CircularBuffer.insert(abs(sample.power))
|
|
|
|
|
|
|
|
avg_bus_voltage = avg(bus_voltage)
|
|
|
|
avg_shunt_voltage = avg(shunt_voltage)
|
|
|
|
avg_current = avg(current)
|
|
|
|
avg_power = avg(power)
|
|
|
|
|
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign(
|
|
|
|
data_ready: true,
|
|
|
|
bus_voltage: bus_voltage,
|
|
|
|
shunt_voltage: shunt_voltage,
|
|
|
|
current: current,
|
|
|
|
power: power,
|
|
|
|
avg_bus_voltage: avg_bus_voltage,
|
|
|
|
avg_shunt_voltage: avg_shunt_voltage,
|
|
|
|
avg_current: avg_current,
|
|
|
|
avg_power: avg_power
|
|
|
|
)
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp avg(buffer) do
|
|
|
|
count = Enum.count(buffer)
|
|
|
|
sum = Enum.sum(buffer)
|
|
|
|
sum / count
|
|
|
|
end
|
|
|
|
end
|