CRONOS, MASTER OF ALL TIME from last night, and the basic, broken form modals work for svc creation

ca14cf6 · gmorell · 2023-03-06 16:49

53 files +1147 -622

Files changed

modified config/config.exs
+1 −0
@@ -56,3 +56,4 @@ config :phoenix, :json_library, Jason
56 56 import_config "#{config_env()}.exs"
57 57
58 58 config :hackney, :max_connections, 512
59 +config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase
modified config/prod.exs
+1 −0
@@ -11,6 +11,7 @@ import Config
11 11 # before starting your production server.
12 12 config :lambent_ex, LambentExWeb.Endpoint,
13 13 cache_static_manifest: "priv/static/cache_manifest.json"
14 +
14 15 # check_origin: :conn
15 16 #
16 17 # Do not print debug messages in production
added lib/lambent_ex/cronos.ex
+80 −0
@@ -0,0 +1,80 @@
1 +defmodule LambentEx.Cronos do
2 + use GenServer
3 +
4 + require Logger
5 + @s "⏰🐻"
6 +
7 + def init(arg) do
8 + Logger.info(
9 + "#{@s} Cronos waits to start the first tick until the top of the minute, it's currently #{DateTime.utc_now().second} seconds past."
10 + )
11 +
12 + Process.send_after(self(), :work, :timer.seconds(60 - DateTime.utc_now().second + 1))
13 + {zone, result} = System.cmd("date", ["+%Z"])
14 +
15 + Logger.info(
16 + "#{@s} Cronos uses the system timezone to determine when to tick and when to sleep, we've found this to be '#{zone}'"
17 + )
18 + if result == 0 do
19 + {:ok, %{tz: zone |> String.trim()}}
20 + else
21 + {:ok, %{tz: "UTC"}}
22 + end
23 + end
24 +
25 + def start_link(arg) do
26 + GenServer.start_link(__MODULE__, arg, name: __MODULE__)
27 + end
28 +
29 + def group(cronos, state) do
30 + {:ok, now} = DateTime.now(state[:tz])
31 +
32 + cronos
33 + |> Enum.map(fn {key, value} -> value end)
34 + |> Enum.filter(fn val -> val[:time].minute == now.minute and val[:time].hour == now.hour end)
35 + end
36 +
37 + def handle_info(:work, state) do
38 + Process.send_after(self(), :work, :timer.minutes(1))
39 + curr = LambentEx.Meta.get_cronos() |> group(state)
40 +
41 + for i <- curr do
42 + case i[:action] do
43 + :machine_speed ->
44 + case i[:arg] do
45 + :faster ->
46 + i[:targets] |> Enum.map(&LambentEx.Machine.faster/1)
47 +
48 + :slower ->
49 + i[:targets] |> Enum.map(&LambentEx.Machine.slower/1)
50 +
51 + otherwise ->
52 + i[:targets] |> Enum.map(fn t -> LambentEx.Machine.set_speed(t, otherwise) end)
53 + end
54 +
55 + :machine_bright ->
56 + case i[:arg] do
57 + :dimmer ->
58 + i[:targets] |> Enum.map(&LambentEx.Machine.dimmer/1)
59 +
60 + :brighter ->
61 + i[:targets] |> Enum.map(&LambentEx.Machine.brighter/1)
62 +
63 + otherwise ->
64 + i[:targets] |> Enum.map(fn t -> LambentEx.Machine.set_brightness(t, otherwise) end)
65 + end
66 +
67 + :link_toggle ->
68 + i[:targets] |> Enum.map(&LambentEx.Link.toggle/1)
69 + end
70 + end
71 +
72 + {:noreply, state}
73 + end
74 +end
75 +
76 +### Examples
77 +# LambentEx.Meta.put_cronos("testa", %{time: ~T[23:10:00], action: :machine_speed, arg: :slower, targets: ["red"]})
78 +# LambentEx.Meta.put_cronos("testb", %{time: ~T[23:11:00], action: :machine_speed, arg: :faster, targets: ["red"]})
79 +# LambentEx.Meta.put_cronos("testc", %{time: ~T[23:10:00], action: :machine_bright, arg: :dimmer, targets: ["red"]})
80 +# LambentEx.Meta.put_cronos("testd", %{time: ~T[23:12:00], action: :machine_bright, arg: :brighter, targets: ["red"]})
modified lib/lambent_ex/machine.ex
+9 −1
@@ -137,6 +137,14 @@ defmodule LambentEx.Machine do
137 137 via_tuple("machine-#{id}") |> GenServer.cast(:slower)
138 138 end
139 139
140 + def set_speed(id, val) do
141 + via_tuple("machine-#{id}") |> GenServer.cast({:set_speed, val})
142 + end
143 +
144 + def set_brightness(id, val) do
145 + via_tuple("machine-#{id}") |> GenServer.cast({:set_brightness, val})
146 + end
147 +
140 148 def brighter(id) do
141 149 via_tuple("machine-#{id}") |> GenServer.cast(:brighter)
142 150 end
@@ -179,12 +187,12 @@ defmodule LambentEx.Machine do
179 187 String.contains?(s, "Steps") ->
180 188 [_head, step] = step |> to_string |> String.split(".Steps.")
181 189 step
190 +
182 191 String.contains?(s, "Ext") ->
183 192 [_head, step] = step |> to_string |> String.split(".Ext.")
184 193 step
185 194 end
186 195
187 −
188 196 step
189 197 end
190 198
modified lib/lambent_ex/machine/ext/gsi.ex
+82 −22
@@ -80,7 +80,7 @@ defmodule LambentEx.Machine.Ext.GSI do
80 80 end
81 81
82 82 defp generate_fill(state, {color_low, color_high}, min, max) do
83 −# IO.inspect({color_low, color_high, min, max, state[:count]})
83 + # IO.inspect({color_low, color_high, min, max, state[:count]})
84 84 qty = (min / max * state[:count]) |> trunc
85 85 rem = state[:count] - qty
86 86 colored = color_high |> Stream.cycle() |> Enum.take(qty * 3)
@@ -212,24 +212,43 @@ defmodule LambentEx.Machine.Ext.GSI do
212 212 defp read(:dota_score_d, state), do: state[:gs] |> get_in(["map", "dire_score"]) || 0
213 213 defp read(:dota_paused, state), do: state[:gs] |> get_in(["map", "paused"]) || false
214 214 defp read(:dota_team, state), do: state[:gs] |> get_in(["player", "team_name"])
215 − defp read(:dota_ward_purchase_cooldown, state), do: state[:gs] |> get_in(["map", "ward_purchase_cooldown"])
215 +
216 + defp read(:dota_ward_purchase_cooldown, state),
217 + do: state[:gs] |> get_in(["map", "ward_purchase_cooldown"])
218 +
216 219 # scoring
217 220 defp read(:dota_player_kills, state), do: state[:gs] |> get_in(["player", "kills"]) || 0
218 221 defp read(:dota_player_deaths, state), do: state[:gs] |> get_in(["player", "deaths"]) || 0
219 222 defp read(:dota_player_assists, state), do: state[:gs] |> get_in(["player", "assists"]) || 0
220 223 defp read(:dota_player_denies, state), do: state[:gs] |> get_in(["player", "denies"]) || 0
221 224 defp read(:dota_player_last_hits, state), do: state[:gs] |> get_in(["player", "last_hits"]) || 0
222 − defp read(:dota_player_kill_streak, state), do: state[:gs] |> get_in(["player", "kill_streak"]) || 0
225 +
226 + defp read(:dota_player_kill_streak, state),
227 + do: state[:gs] |> get_in(["player", "kill_streak"]) || 0
228 +
223 229 # gold
224 230 defp read(:dota_player_xpm, state), do: state[:gs] |> get_in(["player", "xpm"]) || 0
225 231 defp read(:dota_player_gpm, state), do: state[:gs] |> get_in(["player", "gpm"]) || 0
226 232 defp read(:dota_player_gold, state), do: state[:gs] |> get_in(["player", "gold"]) || 1
227 − defp read(:dota_player_gold_unreliable, state), do: state[:gs] |> get_in(["player", "gold_unreliable"]) || 1
228 − defp read(:dota_player_gold_reliable, state), do: state[:gs] |> get_in(["player", "gold_reliable"]) || 1
229 − defp read(:dota_player_gold_from_creep_kills, state), do: state[:gs] |> get_in(["player", "gold_from_creep_kills"]) || 0
230 − defp read(:dota_player_gold_from_hero_kills, state), do: state[:gs] |> get_in(["player", "gold_from_hero_kills"]) || 0
231 − defp read(:dota_player_gold_from_income, state), do: state[:gs] |> get_in(["player", "gold_from_income"]) || 0
232 − defp read(:dota_player_gold_from_shared, state), do: state[:gs] |> get_in(["player", "gold_from_shared"]) || 0
233 +
234 + defp read(:dota_player_gold_unreliable, state),
235 + do: state[:gs] |> get_in(["player", "gold_unreliable"]) || 1
236 +
237 + defp read(:dota_player_gold_reliable, state),
238 + do: state[:gs] |> get_in(["player", "gold_reliable"]) || 1
239 +
240 + defp read(:dota_player_gold_from_creep_kills, state),
241 + do: state[:gs] |> get_in(["player", "gold_from_creep_kills"]) || 0
242 +
243 + defp read(:dota_player_gold_from_hero_kills, state),
244 + do: state[:gs] |> get_in(["player", "gold_from_hero_kills"]) || 0
245 +
246 + defp read(:dota_player_gold_from_income, state),
247 + do: state[:gs] |> get_in(["player", "gold_from_income"]) || 0
248 +
249 + defp read(:dota_player_gold_from_shared, state),
250 + do: state[:gs] |> get_in(["player", "gold_from_shared"]) || 0
251 +
233 252 # numbers
234 253 defp read(:dota_mana, state), do: state[:gs] |> get_in(["hero", "mana"]) || 0
235 254 defp read(:dota_mana_max, state), do: state[:gs] |> get_in(["hero", "max_mana"]) || 1
@@ -238,28 +257,47 @@ defmodule LambentEx.Machine.Ext.GSI do
238 257 defp read(:dota_health_max, state), do: state[:gs] |> get_in(["hero", "max_health"]) || 1
239 258 defp read(:dota_health_pct, state), do: state[:gs] |> get_in(["hero", "health_percent"]) || 0
240 259 # statuses
241 − defp read(:dota_hero_smoked, state), do: state[:gs] |> get_in(["hero", "smoked"]) ||false
260 + defp read(:dota_hero_smoked, state), do: state[:gs] |> get_in(["hero", "smoked"]) || false
242 261 defp read(:dota_hero_alive, state), do: state[:gs] |> get_in(["hero", "alive"]) || false
243 262 defp read(:dota_hero_muted, state), do: state[:gs] |> get_in(["hero", "muted"]) || false
244 263 defp read(:dota_hero_break, state), do: state[:gs] |> get_in(["hero", "break"]) || false
245 264 defp read(:dota_hero_hexed, state), do: state[:gs] |> get_in(["hero", "hexed"]) || false
246 265 defp read(:dota_hero_silenced, state), do: state[:gs] |> get_in(["hero", "silenced"]) || false
247 − defp read(:dota_hero_has_debuff, state), do: state[:gs] |> get_in(["hero", "has_debuff"]) || false
266 +
267 + defp read(:dota_hero_has_debuff, state),
268 + do: state[:gs] |> get_in(["hero", "has_debuff"]) || false
269 +
248 270 defp read(:dota_hero_disarmed, state), do: state[:gs] |> get_in(["hero", "disarmed"]) || false
249 271 defp read(:dota_hero_stunned, state), do: state[:gs] |> get_in(["hero", "stunned"]) || false
250 − defp read(:dota_hero_magicimmune, state), do: state[:gs] |> get_in(["hero", "magicimmune"]) || false
272 +
273 + defp read(:dota_hero_magicimmune, state),
274 + do: state[:gs] |> get_in(["hero", "magicimmune"]) || false
275 +
251 276 # hero
252 277 defp read(:dota_hero_xpos, state), do: state[:gs] |> get_in(["map", "xpos"]) || 0
253 278 defp read(:dota_hero_ypos, state), do: state[:gs] |> get_in(["map", "ypos"]) || 0
254 279 defp read(:dota_hero_level, state), do: state[:gs] |> get_in(["hero", "level"]) || 0
255 280 defp read(:dota_hero_xp, state), do: state[:gs] |> get_in(["hero", "xp"]) || 0
256 − defp read(:dota_hero_attributes_level, state), do: state[:gs] |> get_in(["hero", "attributes_level"]) || 0
257 − defp read(:dota_hero_respawn_seconds, state), do: state[:gs] |> get_in(["hero", "respawn_seconds"]) || 0
258 − defp read(:dota_hero_buyback_money, state), do: state[:gs] |> get_in(["hero", "buyback_cost"]) || 0
259 − defp read(:dota_hero_buyback_seconds, state), do: state[:gs] |> get_in(["hero", "buyback_cooldown"]) || 0
281 +
282 + defp read(:dota_hero_attributes_level, state),
283 + do: state[:gs] |> get_in(["hero", "attributes_level"]) || 0
284 +
285 + defp read(:dota_hero_respawn_seconds, state),
286 + do: state[:gs] |> get_in(["hero", "respawn_seconds"]) || 0
287 +
288 + defp read(:dota_hero_buyback_money, state),
289 + do: state[:gs] |> get_in(["hero", "buyback_cost"]) || 0
290 +
291 + defp read(:dota_hero_buyback_seconds, state),
292 + do: state[:gs] |> get_in(["hero", "buyback_cooldown"]) || 0
293 +
260 294 # talents
261 − defp read(:dota_hero_aghs_scepter, state), do: state[:gs] |> get_in(["hero", "aghanims_scepter"]) || false
262 − defp read(:dota_hero_aghs_shard, state), do: state[:gs] |> get_in(["hero", "aghanims_shard"]) || false
295 + defp read(:dota_hero_aghs_scepter, state),
296 + do: state[:gs] |> get_in(["hero", "aghanims_scepter"]) || false
297 +
298 + defp read(:dota_hero_aghs_shard, state),
299 + do: state[:gs] |> get_in(["hero", "aghanims_shard"]) || false
300 +
263 301 defp read(:dota_hero_talent_1, state), do: state[:gs] |> get_in(["hero", "talent_1"]) || false
264 302 defp read(:dota_hero_talent_2, state), do: state[:gs] |> get_in(["hero", "talent_2"]) || false
265 303 defp read(:dota_hero_talent_3, state), do: state[:gs] |> get_in(["hero", "talent_3"]) || false
@@ -277,10 +315,14 @@ defmodule LambentEx.Machine.Ext.GSI do
277 315 defp read(:dota_hero_ability_6, state), do: state[:gs] |> get_in(["hero", "ability_6"]) || %{}
278 316 defp read(:dota_hero_ability_7, state), do: state[:gs] |> get_in(["hero", "ability_7"]) || %{}
279 317 defp read(:dota_hero_ability_8, state), do: state[:gs] |> get_in(["hero", "ability_8"]) || %{}
318 +
280 319 ## these are passed in the result of above eg; read(:dota_hero_ability_active, read(:dota_hero_ability_1, state))
281 320 defp read(:dota_hero_ability_active, state), do: state |> Map.get("ability_active") || false
282 321 defp read(:dota_hero_ability_can_cast, state), do: state |> Map.get("can_cast") || false
283 − defp read(:dota_hero_ability_charge_cooldown, state), do: state |> Map.get("charge_cooldown") || 0
322 +
323 + defp read(:dota_hero_ability_charge_cooldown, state),
324 + do: state |> Map.get("charge_cooldown") || 0
325 +
284 326 defp read(:dota_hero_ability_charges, state), do: state |> Map.get("charges") || 0
285 327 defp read(:dota_hero_ability_cooldown, state), do: state |> Map.get("cooldown") || 0
286 328 defp read(:dota_hero_ability_level, state), do: state |> Map.get("level") || 0
@@ -294,11 +336,29 @@ defmodule LambentEx.Machine.Ext.GSI do
294 336
295 337 defp read(:dota, state) do
296 338 %{
297 − health_fill: generate_fill(state, {@green, @whitish}, read(:dota_health, state), read(:dota_health_max, state)),
339 + health_fill:
340 + generate_fill(
341 + state,
342 + {@green, @whitish},
343 + read(:dota_health, state),
344 + read(:dota_health_max, state)
345 + ),
298 346 health_bright: [],
299 − mana_fill: generate_fill(state, {@blue, @whitish}, read(:dota_mana, state), read(:dota_mana_max, state)),
347 + mana_fill:
348 + generate_fill(
349 + state,
350 + {@blue, @whitish},
351 + read(:dota_mana, state),
352 + read(:dota_mana_max, state)
353 + ),
300 354 mana_bright: [],
301 − money_reliable_fill: generate_fill(state, {@yellow, @whitish}, read(:dota_player_gold_reliable, state), read(:dota_player_gold, state)),
355 + money_reliable_fill:
356 + generate_fill(
357 + state,
358 + {@yellow, @whitish},
359 + read(:dota_player_gold_reliable, state),
360 + read(:dota_player_gold, state)
361 + ),
302 362 money_reiiable_bright: []
303 363 }
304 364 end
modified lib/lambent_ex/machine/library.ex
+4 −2

Click to load diff…

modified lib/lambent_ex/machine/steps/solid.ex
+0 −1

Click to load diff…

modified lib/lambent_ex/meta.ex
+30 −15

Click to load diff…

modified lib/lambent_ex/mqtt.ex
+17 −9

Click to load diff…

modified lib/lambent_ex/scan/8266-7777.ex
+5 −1

Click to load diff…

modified lib/lambent_ex/schema/links.ex
+7 −5

Click to load diff…

modified lib/lambent_ex/supervision/component.ex
+2 −1

Click to load diff…

modified lib/lambent_ex_web/components/layouts/app.html.heex
+130 −106

Click to load diff…

modified lib/lambent_ex_web/components/layouts/root.html.heex
+1 −1

Click to load diff…

modified lib/lambent_ex_web/live/devices/index.ex
+1 −2

Click to load diff…

modified lib/lambent_ex_web/live/devices/index.html.heex
+113 −118

Click to load diff…

modified lib/lambent_ex_web/live/links/bulk_form_component.ex
+23 −26

Click to load diff…

modified lib/lambent_ex_web/live/links/bulk_form_component.html.heex
+0 −2

Click to load diff…

modified lib/lambent_ex_web/live/links/index.ex
+0 −1

Click to load diff…

modified lib/lambent_ex_web/live/links/index.html.heex
+63 −59

Click to load diff…

modified lib/lambent_ex_web/live/machines/index.ex
+10 −4

Click to load diff…

modified lib/lambent_ex_web/live/machines/index.html.heex
+134 −125

Click to load diff…

modified lib/lambent_ex_web/live/machines/library_form_component.ex
+1 −1

Click to load diff…

modified lib/lambent_ex_web/live/machines/library_form_component.html.heex
+54 −40

Click to load diff…

added lib/lambent_ex_web/live/services/cronos_form_component.ex
+43 −0

Click to load diff…

added lib/lambent_ex_web/live/services/cronos_form_component.html.heex
+3 −0

Click to load diff…

added lib/lambent_ex_web/live/services/http_form_component.ex
+43 −0

Click to load diff…

added lib/lambent_ex_web/live/services/http_form_component.html.heex
+3 −0

Click to load diff…

modified lib/lambent_ex_web/live/services/index.ex
+48 −3

Click to load diff…

modified lib/lambent_ex_web/live/services/index.html.heex
+131 −63

Click to load diff…

added lib/lambent_ex_web/live/services/mqtt_form_component.ex
+43 −0

Click to load diff…

added lib/lambent_ex_web/live/services/mqtt_form_component.html.heex
+3 −0

Click to load diff…

modified lib/lambent_ex_web/live/vendored_components.ex
+56 −11

Click to load diff…

modified lib/lambent_ex_web/router.ex
+7 −6

Click to load diff…

modified mix.exs
+1 −0

Click to load diff…

modified mix.lock
+1 −0

Click to load diff…

Parents: 1bb71e2