all the shit from last night didn't go up

c7914ae · gmorell · 2022-12-15 20:44

19 files +130 -39

Files changed

modified lib/lambent_ex/link.ex
+57 −9
@@ -5,10 +5,11 @@ defmodule LambentEx.Link do
5 5 @pubsub_name LambentEx.PubSub
6 6 @pubsub_topic "machine-"
7 7 @pubsub_topic_idx "links_idx"
8 + @pubsub_pfx_on "links_on_"
8 9
9 10 @registry :lambent_links
10 11
11 − def start_link(opts) do
12 + def start_link(_args, opts) do
12 13 GenServer.start_link(__MODULE__, opts, name: via_tuple("link-#{opts[:name]}"))
13 14 end
14 15
@@ -16,6 +17,17 @@ defmodule LambentEx.Link do
16 17 Process.send_after(self(), :publish, 50)
17 18 {:ok, opts} = Keyword.validate(opts, [:source, :target, :name])
18 19 Phoenix.PubSub.subscribe(@pubsub_name, @pubsub_topic <> opts[:source])
20 + Phoenix.PubSub.subscribe(@pubsub_name, @pubsub_pfx_on <> opts[:target])
21 +
22 + state = %{
23 + source: opts[:source],
24 + target: opts[:target],
25 + name: opts[:name],
26 + enabled: true,
27 + started: DateTime.utc_now
28 + }
29 +
30 + neighbor_on(state)
19 31
20 32 # todo
21 33 # publish-pause-siblings
@@ -23,17 +35,22 @@ defmodule LambentEx.Link do
23 35
24 36 {
25 37 :ok,
26 − %{
27 − source: opts[:source],
28 − target: opts[:target],
29 − name: opts[:name],
30 − enabled: true,
31 − }
38 + state
32 39 }
33 40 end
34 41
35 42 defp via_tuple(name), do: {:via, Registry, {@registry, name}}
36 43
44 + # public
45 +
46 + def toggle(id) do
47 + via_tuple("link-#{id}") |> GenServer.cast(:do_toggle)
48 + end
49 +
50 + def quit(id) do
51 + via_tuple("link-#{id}") |> GenServer.cast(:quit)
52 + end
53 +
37 54 defp publish(state) do
38 55 %{
39 56 name: state[:name],
@@ -43,9 +60,14 @@ defmodule LambentEx.Link do
43 60 }
44 61 end
45 62
63 + defp neighbor_on(state) do
64 + Phoenix.PubSub.broadcast(@pubsub_name, @pubsub_pfx_on <> state[:target], {:neighbor_on, state[:name]})
65 + end
66 +
67 + @impl true
46 68 def handle_info({:publish, data}, state) do
47 69 case state[:enabled] do
48 − true -> LambentEx.Scan.ESP8266x7777.submit(state[:target], data |> List.flatten)
70 + true -> LambentEx.Scan.ESP8266x7777.submit(state[:target], data |> List.flatten())
49 71 false -> :ok
50 72 end
51 73
@@ -58,4 +80,30 @@ defmodule LambentEx.Link do
58 80 Phoenix.PubSub.broadcast(@pubsub_name, @pubsub_topic_idx, {:links_pub, publish(state)})
59 81 {:noreply, state}
60 82 end
61 −end
83 +
84 + @impl true
85 + def handle_info({:neighbor_on, id}, state) do
86 + case state[:name] == id do
87 + true -> {:noreply, state}
88 + false -> {:noreply, %{state | enabled: false}}
89 + end
90 + end
91 +
92 + @impl true
93 + def handle_cast(:do_toggle, state) do
94 + case state[:enabled] do
95 + true ->
96 + {:noreply, %{state | enabled: false}}
97 +
98 + false ->
99 + neighbor_on(state)
100 + {:noreply, %{state | enabled: true}}
101 + end
102 + end
103 +
104 + @impl
105 + def handle_cast(:quit, state) do
106 + LambentEx.LinkSupervisor.abort_child(self())
107 + {:noreply, state}
108 + end
109 +end
modified lib/lambent_ex/machine.ex
+2 −1
@@ -73,7 +73,8 @@ defmodule LambentEx.Machine do
73 73 bright_curr: 255,
74 74 bright_delay: 0,
75 75 bright_mvmul: 2,
76 − cnt: cnt
76 + cnt: cnt,
77 + started: DateTime.utc_now
77 78 }}
78 79 end
79 80
modified lib/lambent_ex/schema/lambent_ex/schema/links.ex
+11 −4
@@ -10,9 +10,9 @@ defmodule LambentEx.Schema.Links do
10 10 embedded_schema do
11 11 field :name, :string
12 12 field :source_id, :string
13 − field :source_type, Ecto.Enum, values: [:machine, :anchor]
13 + field :source_type, Ecto.Enum, values: [:machine, :anchor], default: :machine
14 14 field :target_id, :string
15 − field :target_type, Ecto.Enum, values: [:device, :group]
15 + field :target_type, Ecto.Enum, values: [:device, :group], default: :device
16 16 end
17 17
18 18 def change_link(%Links{} = link, attrs \\ %{}) do
@@ -22,9 +22,16 @@ defmodule LambentEx.Schema.Links do
22 22 def update_links(%Links{} = link, attrs) do
23 23 IO.puts("actual write")
24 24
25 − link
25 +
26 + res = link
26 27 |> Links.changeset(attrs)
27 − |> IO.inspect
28 +
29 + IO.inspect([name: res.changes[:name], source: res.changes[:source_id], target: res.changes[:target_id]])
30 +
31 + case res.valid? do
32 + true -> {:ok, LambentEx.LinkSupervisor.start_child(res.changes[:name], res.changes[:source_id], res.changes[:target_id])}
33 + false -> {:error, res}
34 + end
28 35 end
29 36
30 37 def changeset(source, params) do
modified lib/lambent_ex/supervision/link.ex
+2 −2
@@ -6,8 +6,8 @@ defmodule LambentEx.LinkSupervisor do
6 6 end
7 7
8 8 def start_child(name, source, target) do
9 − spec = {LambentEx.Link, name: name, source: source, target: target}
10 − DynamicSupervisor.start_child(__MODULE__, spec)
9 + spec = {LambentEx.Link, [name: name, source: source, target: target]}
10 + DynamicSupervisor.start_child(__MODULE__, spec) |> IO.inspect
11 11 end
12 12
13 13 def abort_child(pid) do
modified lib/lambent_ex_web/components/core_components.ex
+18 −14
@@ -262,6 +262,8 @@ defmodule LambentExWeb.CoreComponents do
262 262 attr :rest, :global, include: ~w(autocomplete disabled form max maxlength min minlength
263 263 pattern placeholder readonly required size step)
264 264 slot :inner_block
265 + slot :icon_pre
266 + slot :icon_post
265 267
266 268 def input(%{field: {f, field}} = assigns) do
267 269 assigns
@@ -339,21 +341,23 @@ defmodule LambentExWeb.CoreComponents do
339 341
340 342 def input(assigns) do
341 343 ~H"""
342 − <div phx-feedback-for={@name}>
344 + <div phx-feedback-for={@name} class="form-control">
343 345 <.label for={@id}><%= @label %></.label>
344 − <input
345 − type={@type}
346 − name={@name}
347 − id={@id || @name}
348 − value={@value}
349 − class={[
350 − input_border(@errors),
351 − "mt-2 block w-full rounded-lg border-zinc-300 py-[7px] px-[11px]",
352 − "text-zinc-900 focus:outline-none focus:ring-4 sm:text-sm sm:leading-6",
353 − "phx-no-feedback:border-zinc-300 phx-no-feedback:focus:border-zinc-400 phx-no-feedback:focus:ring-zinc-800/5"
354 − ]}
355 − {@rest}
356 − />
346 + <div class="input-group">
347 + <%= render_slot(@icon_pre) %>
348 + <input
349 + type={@type}
350 + name={@name}
351 + id={@id || @name}
352 + value={@value}
353 + class={[
354 + input_border(@errors),
355 + "input input-bordered"
356 + ]}
357 + {@rest}
358 + />
359 + <%= render_slot(@icon_post) %>
360 + </div>
357 361 <.error :for={msg <- @errors}><%= msg %></.error>
358 362 </div>
359 363 """
modified lib/lambent_ex_web/live/links/form_component.ex
+8 −1
@@ -2,6 +2,8 @@ defmodule LambentEx.LinksLive.FormComponent do
2 2 use LambentExWeb, :live_component
3 3
4 4 alias LambentEx.Schema.Links
5 + alias LambentEx.Names
6 +
5 7 @impl true
6 8 def update(%{link: link} = assigns, socket) do
7 9 changeset = Links.change_link(link)
@@ -34,7 +36,12 @@ defmodule LambentEx.LinksLive.FormComponent do
34 36 {:noreply, assign(socket, :changeset, changeset)}
35 37 end
36 38 # save_testyyyy(socket, socket.assigns.action, testyyyy_params)
37 − :ok
39 +# :ok
40 + end
41 +
42 + def handle_event("new-name", _params, socket) do
43 + changeset = socket.assigns.changeset |> Ecto.Changeset.change(name: Names.name())
44 + {:noreply, assign(socket, :changeset, changeset)}
38 45 end
39 46
40 47 def for_selectm(array) do
modified lib/lambent_ex_web/live/links/form_component.html.heex
+8 −1
@@ -8,7 +8,14 @@
8 8 phx-change="validate"
9 9 phx-submit="save"
10 10 >
11 − <.input field={{f, :name}} type="text" label="Name" options={1..2} />
11 +
12 + <.input field={{f, :name}} type="text" label="Name" options={1..2}>
13 + <:icon_pre>
14 + <btn class="btn btn-accent" phx-click="new-name" phx-target={@myself}>
15 + <i class="fa-solid fa-arrows-rotate"></i>
16 + </btn>
17 + </:icon_pre>
18 + </.input>
12 19 <.input field={{f, :source_id}} type="select" label="Source" options={@machines |> for_selectm} />
13 20 <.input field={{f, :target_id}} type="select" label="Target" options={@devices |> for_selectd} />
14 21 <:actions>
modified lib/lambent_ex_web/live/links/index.ex
+18 −6
@@ -16,16 +16,15 @@ defmodule LambentExWeb.LinksLive.Index do
16 16
17 17 {:ok,
18 18 socket
19 − |> assign(:links, %{} )
19 + |> assign(:links, %{})
20 20 |> assign(:machines, %{})
21 21 |> assign(:devices, %{})
22 22 |> assign(:link, nil)
23 − |> assign(:nonew, :true)
23 + |> assign(:nonew, true)
24 24 |> assign(:newctrl, [
25 25 %{link: ~p"/cfg/links/new", icon: "plus"},
26 26 %{link: ~p"/cfg/links/bulk", icon: "bucket"}
27 − ])
28 − }
27 + ])}
29 28 end
30 29
31 30 @impl true
@@ -49,7 +48,8 @@ defmodule LambentExWeb.LinksLive.Index do
49 48 end
50 49
51 50 def handle_info({:machines_pub, machine}, socket) do
52 − {:noreply, socket |> assign(:machines, socket.assigns.machines |> Map.put(machine[:name], machine))}
51 + {:noreply,
52 + socket |> assign(:machines, socket.assigns.machines |> Map.put(machine[:name], machine))}
53 53 end
54 54
55 55 def handle_info({:links_pub, link}, socket) do
@@ -57,6 +57,18 @@ defmodule LambentExWeb.LinksLive.Index do
57 57 end
58 58
59 59 def handle_info({:devices_pub, device}, socket) do
60 − {:noreply, socket |> assign(:devices, socket.assigns.devices |> Map.put(device[:mac], device))}
60 + {:noreply,
61 + socket |> assign(:devices, socket.assigns.devices |> Map.put(device[:mac], device))}
62 + end
63 +
64 + def handle_event("toggle", params, socket) do
65 + LambentEx.Link.toggle(params["tgt"])
66 + {:noreply, socket}
67 + end
68 +
69 + def handle_event("quit", params, socket) do
70 + LambentEx.Link.quit(params["tgt"])
71 + links = socket.assigns.links |> Map.delete(params["tgt"])
72 + {:noreply, socket |> assign(:links, links)}
61 73 end
62 74 end
modified lib/lambent_ex_web/live/links/index.html.heex
+7 −2
@@ -13,6 +13,7 @@ on_cancel={JS.navigate(~p"/cfg/links")}>
13 13 return_to={~p"/cfg/links"}
14 14 machines={@machines}
15 15 devices={@devices}
16 + navigate={~p"/cfg/links"}
16 17 />
17 18 </.modal>
18 19 <% end %>
@@ -40,10 +41,14 @@ on_cancel={JS.navigate(~p"/cfg/links")}>
40 41 <td><%= v[:target] %></td>
41 42 <td>
42 43 <%= if v[:enabled] == true do %>
43 − <input type="checkbox" class="toggle" checked />
44 + <input type="checkbox" phx-click="toggle" phx-value-tgt={k} class="toggle" checked />
44 45 <% else %>
45 − <input type="checkbox" class="toggle" />
46 + <input type="checkbox" phx-click="toggle" phx-value-tgt={k} class="toggle" />
46 47 <% end %>
48 + &emsp;
49 + <a class="btn btn-error btn-outline" phx-click="quit" phx-value-tgt={k}>
50 + <i class="fa-solid fa-xmark"></i>
51 + </a>
47 52 </td>
48 53 </tr>
49 54 <% end %>

Parents: 3067339