Skip to content

Commit

Permalink
First version of remote control interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Cellane committed May 20, 2024
1 parent 66bc4e0 commit 2f899a2
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 269 deletions.
4 changes: 4 additions & 0 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
@import "tailwindcss/utilities";

/* This file is for your main application CSS */

body {
touch-action: none;
}
1 change: 1 addition & 0 deletions lib/minetti_ui/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule MinettiUi.Application do
{Phoenix.PubSub, name: MinettiUi.PubSub},
# Start a worker by calling: MinettiUi.Worker.start_link(arg)
# {MinettiUi.Worker, arg},
{MinettiUi.FakeState, []},
# Start to serve requests, typically the last entry
MinettiUiWeb.Endpoint
]
Expand Down
102 changes: 102 additions & 0 deletions lib/minetti_ui/fake_state.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
defmodule MinettiUi.FakeState do
use GenServer

@valid_modes [:cool, :heat, :dry, :fan_only, :auto, :off]

# Client
def start_link(_),
do:
GenServer.start_link(
__MODULE__,
%{mode: :off, temperature: 20.0, fan_speed: :auto, on_timer: nil, off_timer: nil},
name: __MODULE__
)

def state, do: GenServer.call(__MODULE__, :state)

def set_mode(mode) when mode in @valid_modes,
do: GenServer.call(__MODULE__, {:set_mode, mode})

def temperature_up, do: GenServer.call(__MODULE__, :temperature_up)
def temperature_down, do: GenServer.call(__MODULE__, :temperature_down)
def cycle_fan_speed, do: GenServer.call(__MODULE__, :cycle_fan_speed)

def swing, do: GenServer.cast(__MODULE__, :swing)
def vertical_direction, do: GenServer.cast(__MODULE__, :vertical_direction)

# Server
@impl GenServer
def init(state), do: {:ok, state}

@impl GenServer
def handle_call(:state, _from, state), do: {:reply, state, state}

def handle_call({:set_mode, mode}, _from, state) when mode in [:dry, :auto] do
state = %{state | mode: mode, fan_speed: :auto}
schedule_apply()
{:reply, state, state}
end

def handle_call({:set_mode, mode}, _from, state) do
state = %{state | mode: mode}
schedule_apply()
{:reply, state, state}
end

def handle_call(_command, _from, %{mode: :off} = state), do: {:reply, state, state}

def handle_call(:temperature_up, _from, %{temperature: temperature} = state)
when temperature >= 30,
do: {:reply, state, state}

def handle_call(:temperature_up, _from, %{temperature: temperature} = state) do
state = %{state | temperature: temperature + 0.5}
schedule_apply()
{:reply, state, state}
end

def handle_call(:temperature_down, _from, %{temperature: temperature} = state)
when temperature <= 16,
do: {:reply, state, state}

def handle_call(:temperature_down, _from, %{temperature: temperature} = state) do
state = %{state | temperature: temperature - 0.5}
schedule_apply()
{:reply, state, state}
end

def handle_call(:cycle_fan_speed, _from, %{mode: mode} = state) when mode in [:dry, :auto],
do: {:reply, state, state}

def handle_call(:cycle_fan_speed, _from, %{fan_speed: fan_speed} = state) do
state = %{state | fan_speed: next_fan_speed(fan_speed)}
schedule_apply()
{:reply, state, state}
end

@impl GenServer
def handle_cast(:swing, state) do
{:noreply, state}
end

def handle_cast(:vertical_direction, state) do
{:noreply, state}
end

@impl GenServer
def handle_info(:apply, state) do
Phoenix.PubSub.broadcast(MinettiUi.PubSub, "state", {:state_changed, state})
{:noreply, state}
end

defp fan_speed_order, do: [:auto, :quiet, :low, :weak, :strong, :super]

defp next_fan_speed(current_speed),
do:
fan_speed_order()
|> Stream.cycle()
|> Stream.drop_while(&(&1 != current_speed))
|> Enum.at(1)

defp schedule_apply, do: send(self(), :apply)
end
26 changes: 0 additions & 26 deletions lib/minetti_ui_web/components/layouts/app.html.heex
Original file line number Diff line number Diff line change
@@ -1,29 +1,3 @@
<header class="px-4 sm:px-6 lg:px-8">
<div class="flex items-center justify-between border-b border-zinc-100 py-3 text-sm">
<div class="flex items-center gap-4">
<a href="/">
<img src={~p"/images/logo.svg"} width="36" />
</a>
<p class="bg-brand/5 text-brand rounded-full px-2 font-medium leading-6">
v<%= Application.spec(:phoenix, :vsn) %>
</p>
</div>
<div class="flex items-center gap-4 font-semibold leading-6 text-zinc-900">
<a href="https://twitter.com/elixirphoenix" class="hover:text-zinc-700">
@elixirphoenix
</a>
<a href="https://github.com/phoenixframework/phoenix" class="hover:text-zinc-700">
GitHub
</a>
<a
href="https://hexdocs.pm/phoenix/overview.html"
class="rounded-lg bg-zinc-100 px-2 py-1 hover:bg-zinc-200/80"
>
Get Started <span aria-hidden="true">&rarr;</span>
</a>
</div>
</div>
</header>
<main class="px-4 py-20 sm:px-6 lg:px-8">
<div class="mx-auto max-w-2xl">
<.flash_group flash={@flash} />
Expand Down
5 changes: 4 additions & 1 deletion lib/minetti_ui_web/components/layouts/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
<html lang="en" class="[scrollbar-gutter:stable]">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<meta name="csrf-token" content={get_csrf_token()} />
<.live_title suffix=" · Phoenix Framework">
<%= assigns[:page_title] || "MinettiUi" %>
Expand Down
9 changes: 0 additions & 9 deletions lib/minetti_ui_web/controllers/page_controller.ex

This file was deleted.

10 changes: 0 additions & 10 deletions lib/minetti_ui_web/controllers/page_html.ex

This file was deleted.

Loading

0 comments on commit 2f899a2

Please sign in to comment.