neiam
/lambent_ex
Lambent Aether 5
public · Issues · Pulls 1 · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
machine persistence flow setup, still need to handle the deletions correctly
67ff785 · gmorell · 2023-01-02 23:14
Files changed
modified
lib/lambent_ex/machine.ex
+2
−0
@@ -84,6 +84,7 @@ defmodule LambentEx.Machine do
| 84 | 84 | single: opts[:single], |
| 85 | 85 | started: DateTime.utc_now, |
| 86 | 86 | persist: opts[:persist], |
| 87 | + full_opts: opts, | |
| 87 | 88 | }} |
| 88 | 89 | end |
| 89 | 90 |
@@ -136,6 +137,7 @@ defmodule LambentEx.Machine do
| 136 | 137 | id: id(state), |
| 137 | 138 | name: state[:name], |
| 138 | 139 | step: state[:step] |> stripstep, |
| 140 | + opts: state[:full_opts] |> Keyword.update(:persist, true, fn _ -> state[:persist] end), | |
| 139 | 141 | speed: state[:speed], |
| 140 | 142 | cnt: state[:cnt], |
| 141 | 143 | bgt: state[:bright_curr], |
modified
lib/lambent_ex/machine/steps/scapes.ex
+1
−6
@@ -39,15 +39,10 @@ defmodule LambentEx.Machine.Steps.Scape do
| 39 | 39 | end |
| 40 | 40 | |
| 41 | 41 | defp read(state) do |
| 42 | −# if state.id == 0 do | |
| 43 | −# IO.inspect(state.status) | |
| 44 | −# IO.inspect([state.hues |> Enum.at(state.status), state.s, state.v]) | |
| 45 | −# end | |
| 46 | 42 | Color.hsv2rgb([state.hues |> Enum.at(state.status), state.s, state.v]) |
| 47 | 43 | end |
| 48 | 44 | |
| 49 | 45 | defp do_status_step(current, target, size \\ 1) do |
| 50 | −# IO.inspect({current, target}) | |
| 51 | 46 | if (current - target) |> abs > size do |
| 52 | 47 | cond do |
| 53 | 48 | current > target -> current - size |
@@ -80,7 +75,7 @@ defmodule LambentEx.Machine.Steps.Scape do
| 80 | 75 | |
| 81 | 76 | |
| 82 | 77 | def generate_hue_range(low, high) when low > high, |
| 83 | − do: Enum.to_list(low..255) ++ Enum.to_list(0..high) |> IO.inspect() | |
| 78 | + do: Enum.to_list(low..255) ++ Enum.to_list(0..high) | |
| 84 | 79 | def generate_hue_range(low, high), do: low..high |> Enum.to_list() |
| 85 | 80 | |
| 86 | 81 | end |
added
lib/lambent_ex/meta.ex
+87
−0
@@ -0,0 +1,87 @@
| 1 | +defmodule LambentEx.Meta do | |
| 2 | + use GenServer | |
| 3 | + | |
| 4 | + @table :lex_metadata | |
| 5 | + @pubsub_name LambentEx.PubSub | |
| 6 | + | |
| 7 | + def init(arg) do | |
| 8 | + File.cd("meta") | |
| 9 | + {:ok, table} = :dets.open_file(@table, type: :set) | |
| 10 | + File.cd("..") | |
| 11 | + | |
| 12 | + Phoenix.PubSub.subscribe(@pubsub_name, "machines_idx") | |
| 13 | + Process.send_after(self(), :machine_flush, 5000) | |
| 14 | + ml = MapSet.new() | |
| 15 | + | |
| 16 | + {:ok, %{table: table, machines: ml}} | |
| 17 | + end | |
| 18 | + | |
| 19 | + def start_link(arg) do | |
| 20 | + GenServer.start_link(__MODULE__, arg, name: __MODULE__) | |
| 21 | + end | |
| 22 | + | |
| 23 | + # devices | |
| 24 | + def get_name(mac) do | |
| 25 | + case :dets.lookup(@table, "name-#{mac}") do | |
| 26 | + [] -> | |
| 27 | + nil | |
| 28 | + | |
| 29 | + [{_key, value}] -> | |
| 30 | + value | |
| 31 | + end | |
| 32 | + end | |
| 33 | + | |
| 34 | + def get_ord(mac) do | |
| 35 | + case :dets.lookup(@table, "ord-#{mac}") do | |
| 36 | + [] -> | |
| 37 | + nil | |
| 38 | + | |
| 39 | + [{_key, value}] -> | |
| 40 | + value | |
| 41 | + end | |
| 42 | + end | |
| 43 | + | |
| 44 | + def put_name(key, value) do | |
| 45 | + :dets.insert(@table, {"name-#{key}", value}) | |
| 46 | + end | |
| 47 | + | |
| 48 | + def put_ord(key, value) do | |
| 49 | + :dets.insert(@table, {"ord-#{key}", value}) | |
| 50 | + end | |
| 51 | + | |
| 52 | + # machines | |
| 53 | + def get_saved_machines do | |
| 54 | + case :dets.lookup(@table, "machs") do | |
| 55 | + [] -> [] | |
| 56 | + [{_key, value}] -> | |
| 57 | + value | |
| 58 | + end | |
| 59 | + end | |
| 60 | + | |
| 61 | + def machines_clear() do | |
| 62 | + :dets.delete(@table, "machs") | |
| 63 | + end | |
| 64 | + | |
| 65 | + def handle_info(:machine_flush, state) do | |
| 66 | + Process.send_after(self(), :machine_flush, 5000) | |
| 67 | + | |
| 68 | + if state.machines |> MapSet.size() == 0 do | |
| 69 | + {:noreply, state} | |
| 70 | + else | |
| 71 | + :dets.insert(@table, {"machs", state.machines}) | |
| 72 | + {:noreply, state} | |
| 73 | + end | |
| 74 | + end | |
| 75 | + | |
| 76 | + def handle_info({:machines_pub, machine}, state) do | |
| 77 | + case machine[:persist] do | |
| 78 | + true -> | |
| 79 | + {:noreply, %{state | machines: MapSet.put(state.machines, machine)}} | |
| 80 | + | |
| 81 | + false -> | |
| 82 | +# IO.inspect(machine) | |
| 83 | + {:noreply, | |
| 84 | + %{state | machines: MapSet.delete(state.machines, machine |> Map.put(:persist, true) |> Map.put(:opts, machine[:opts] |> Keyword.update(:persist, true, fn _ -> true end)))}} | |
| 85 | + end | |
| 86 | + end | |
| 87 | +end |
deleted
lib/lambent_ex/scan/meta.ex
+0
−45
@@ -1,45 +0,0 @@
| 1 | −defmodule LambentEx.Meta do | |
| 2 | − use GenServer | |
| 3 | − | |
| 4 | − @table :lex_metadata | |
| 5 | − | |
| 6 | − def init(arg) do | |
| 7 | − File.cd("meta") | |
| 8 | − {:ok, table} = :dets.open_file(@table, [type: :set]) | |
| 9 | − File.cd("..") | |
| 10 | − | |
| 11 | − {:ok, %{table: table}} | |
| 12 | − end | |
| 13 | − | |
| 14 | − def start_link(arg) do | |
| 15 | − GenServer.start_link(__MODULE__, arg, name: __MODULE__) | |
| 16 | − end | |
| 17 | − | |
| 18 | − def get_name(mac) do | |
| 19 | − case :dets.lookup(@table, "name-#{mac}") do | |
| 20 | − [] -> | |
| 21 | − nil | |
| 22 | − | |
| 23 | − [{_key, value}] -> | |
| 24 | − value | |
| 25 | − end | |
| 26 | − end | |
| 27 | − | |
| 28 | − def get_ord(mac) do | |
| 29 | − case :dets.lookup(@table, "ord-#{mac}") do | |
| 30 | − [] -> | |
| 31 | − nil | |
| 32 | − | |
| 33 | − [{_key, value}] -> | |
| 34 | − value | |
| 35 | − end | |
| 36 | − end | |
| 37 | − | |
| 38 | − def put_name(key, value) do | |
| 39 | − :dets.insert(@table, {"name-#{key}", value}) | |
| 40 | − end | |
| 41 | − | |
| 42 | − def put_ord(key, value) do | |
| 43 | − :dets.insert(@table, {"ord-#{key}", value}) | |
| 44 | − end | |
| 45 | −end |
modified
lib/lambent_ex/supervision/machine.ex
+27
−6
@@ -8,7 +8,15 @@ defmodule LambentEx.MachineSupervisor do
| 8 | 8 | def start_child(step, opts, name, options, count \\ 300) do |
| 9 | 9 | spec = {LambentEx.Machine, [step: step, step_opts: opts, count: count, name: name] ++ options} |
| 10 | 10 | IO.inspect(spec) |
| 11 | − DynamicSupervisor.start_child(__MODULE__, spec) |> IO.inspect | |
| 11 | + # |> IO.inspect | |
| 12 | + DynamicSupervisor.start_child(__MODULE__, spec) | |
| 13 | + end | |
| 14 | + | |
| 15 | + def start_child(opts) do | |
| 16 | + spec = {LambentEx.Machine, opts} | |
| 17 | + IO.inspect(spec) | |
| 18 | + # | |
| 19 | + DynamicSupervisor.start_child(__MODULE__, spec) | |
| 12 | 20 | end |
| 13 | 21 | |
| 14 | 22 | def abort_child(pid) do |
@@ -18,12 +26,25 @@ defmodule LambentEx.MachineSupervisor do
| 18 | 26 | |
| 19 | 27 | @impl true |
| 20 | 28 | def init(init_arg) do |
| 21 | − DynamicSupervisor.init( | |
| 22 | − strategy: :one_for_one, | |
| 23 | − extra_arguments: [init_arg] | |
| 24 | − ) | |
| 29 | + r = | |
| 30 | + DynamicSupervisor.init( | |
| 31 | + strategy: :one_for_one, | |
| 32 | + extra_arguments: [init_arg] | |
| 33 | + ) | |
| 34 | + | |
| 35 | + Task.start(fn -> | |
| 36 | + LambentEx.Meta.get_saved_machines() | |
| 37 | + |> Enum.map(fn m -> | |
| 38 | + IO.inspect(m[:opts]) | |
| 39 | + spec = {LambentEx.Machine, m[:opts]} | |
| 40 | + # | |
| 41 | + DynamicSupervisor.start_child(__MODULE__, spec) | |
| 42 | + end) | |
| 43 | + end) | |
| 44 | + | |
| 45 | + r | |
| 25 | 46 | end |
| 26 | 47 | end |
| 27 | 48 | |
| 28 | 49 | # LambentEx.Machine.Steps.Chase, step_opts: %{}, name: :default, count: 300, |
| 29 | −# | |
| 50 | +# |
Parents: eb39c23