38 lines
820 B
Elixir
38 lines
820 B
Elixir
defmodule AugieWeb.CameraLive do
|
|
use Phoenix.LiveView
|
|
alias Augie.Sensor.Camera
|
|
|
|
@moduledoc """
|
|
A Liveview which streams images from the raspberry pi camera.
|
|
"""
|
|
|
|
def render(assigns) do
|
|
~L"""
|
|
<div class="card">
|
|
<div class="card-divider">
|
|
<h4>Camera</h4>
|
|
</div>
|
|
<img src="<%= @frame %>">
|
|
</div>
|
|
"""
|
|
end
|
|
|
|
def mount(_params, _context, socket) do
|
|
if connected?(socket), do: CommunityTheatre.subscribe(Camera, 4)
|
|
|
|
jpg = Camera.last_frame(Camera)
|
|
|
|
{:ok, assign(socket, frame: encode_frame(jpg))}
|
|
end
|
|
|
|
def handle_info({CommunityTheatre, %{topic: Camera, payload: jpg}}, socket) do
|
|
{:noreply, assign(socket, frame: encode_frame(jpg))}
|
|
end
|
|
|
|
defp encode_frame(jpg) do
|
|
[
|
|
"data:image/jpeg;base64,",
|
|
Base.encode64(jpg)
|
|
]
|
|
end
|
|
end
|