Current section
2 Versions
Jump to
Current section
2 Versions
Compare versions
4
files changed
+61
additions
-49
deletions
| @@ -0,0 +1 @@ | |
| 1 | + 0.2.0 |
| @@ -5,16 +5,16 @@ | |
| 5 5 | {<<"elixir">>,<<"~> 1.5">>}. |
| 6 6 | {<<"files">>, |
| 7 7 | [<<"lib">>,<<"lib/exred_node_suppress.ex">>,<<"mix.exs">>,<<"README.md">>, |
| 8 | - <<"LICENSE">>]}. |
| 8 | + <<"LICENSE">>,<<"VERSION">>]}. |
| 9 9 | {<<"licenses">>,[<<"MIT">>]}. |
| 10 10 | {<<"links">>, |
| 11 11 | [{<<"Exred">>,<<"http://exred.org">>}, |
| 12 | - {<<"GitHub">>,<<"https://github.com/exredorg/exred_node_suppress">>}]}. |
| 12 | + {<<"GitHub">>,<<"https://github.com/exredorg/exred_node_suppress.git">>}]}. |
| 13 13 | {<<"name">>,<<"exred_node_suppress">>}. |
| 14 14 | {<<"requirements">>, |
| 15 | - [[{<<"app">>,<<"exred_library">>}, |
| 16 | - {<<"name">>,<<"exred_library">>}, |
| 15 | + [[{<<"app">>,<<"exred_nodeprototype">>}, |
| 16 | + {<<"name">>,<<"exred_nodeprototype">>}, |
| 17 17 | {<<"optional">>,false}, |
| 18 18 | {<<"repository">>,<<"hexpm">>}, |
| 19 | - {<<"requirement">>,<<"~> 0.1.11">>}]]}. |
| 20 | - {<<"version">>,<<"0.1.0">>}. |
| 19 | + {<<"requirement">>,<<"~> 0.2">>}]]}. |
| 20 | + {<<"version">>,<<"0.2.0">>}. |
| @@ -1,14 +1,14 @@ | |
| 1 1 | defmodule Exred.Node.Suppress do |
| 2 2 | @moduledoc """ |
| 3 3 | Suppresses or filters incoming messages for configurable time periods. |
| 4 | - |
| 4 | + |
| 5 5 | ###Inputs |
| 6 | - |
| 7 | - `topic :: string` |
| 6 | + |
| 7 | + `topic :: string` |
| 8 8 | if message suppression starts when triggered then a message with '_START' topic triggers the suppression |
| 9 9 | otherwise topic is ignored |
| 10 10 | |
| 11 | - `payload :: term | number` |
| 11 | + `payload :: term | number` |
| 12 12 | if suppressing a band then payload needs to be number (it'll get compared to the less\\_then and greater\\_then limits) |
| 13 13 | """ |
| 14 14 | |
| @@ -18,8 +18,8 @@ defmodule Exred.Node.Suppress do | |
| 18 18 | @config [ |
| 19 19 | name: %{ |
| 20 20 | info: "Visible node name", |
| 21 | - value: @name, |
| 22 | - type: "string", |
| 21 | + value: @name, |
| 22 | + type: "string", |
| 23 23 | attrs: %{max: 20} |
| 24 24 | }, |
| 25 25 | per_topic: %{ |
| @@ -29,7 +29,8 @@ defmodule Exred.Node.Suppress do | |
| 29 29 | attrs: %{options: [true, false]} |
| 30 30 | }, |
| 31 31 | start_when: %{ |
| 32 | - info: "Start suppressing when the flow is deployed or when a message with topic: '_START' arrives", |
| 32 | + info: |
| 33 | + "Start suppressing when the flow is deployed or when a message with topic: '_START' arrives", |
| 33 34 | type: "select", |
| 34 35 | value: "triggered", |
| 35 36 | attrs: %{options: ["deployed", "triggered"]} |
| @@ -44,7 +45,7 @@ defmodule Exred.Node.Suppress do | |
| 44 45 | info: "Time period in milliseconds", |
| 45 46 | type: "number", |
| 46 47 | value: 100, |
| 47 | - attrs: %{min: 1, max: 3600000} |
| 48 | + attrs: %{min: 1, max: 3_600_000} |
| 48 49 | }, |
| 49 50 | what: %{ |
| 50 51 | info: "What to suppress", |
| @@ -64,64 +65,74 @@ defmodule Exred.Node.Suppress do | |
| 64 65 | } |
| 65 66 | ] |
| 66 67 | @ui_attributes [ |
| 67 | - fire_button: false, |
| 68 | + fire_button: false, |
| 68 69 | right_icon: "gavel" |
| 69 70 | ] |
| 70 | - |
| 71 | - use Exred.Library.NodePrototype |
| 71 | + |
| 72 | + use Exred.NodePrototype |
| 72 73 | require Logger |
| 73 74 | |
| 74 75 | @impl true |
| 75 76 | def node_init(state) do |
| 76 | - Logger.warn "CONFIG #{inspect state.config}" |
| 77 | - timer_ref = if state.config.start_when.value == "deployed" do |
| 78 | - case state.config.how_long.value do |
| 79 | - "forever" -> :forever |
| 80 | - "time_period" -> |
| 81 | - {:ok, ref} = :timer.send_after state.config.time_period.value, :_TIMEOUT |
| 82 | - ref |
| 77 | + Logger.warn("CONFIG #{inspect(state.config)}") |
| 78 | + |
| 79 | + timer_ref = |
| 80 | + if state.config.start_when.value == "deployed" do |
| 81 | + case state.config.how_long.value do |
| 82 | + "forever" -> |
| 83 | + :forever |
| 84 | + |
| 85 | + "time_period" -> |
| 86 | + {:ok, ref} = :timer.send_after(state.config.time_period.value, :_TIMEOUT) |
| 87 | + ref |
| 88 | + end |
| 89 | + else |
| 90 | + nil |
| 83 91 | end |
| 84 | - else |
| 85 | - nil |
| 86 | - end |
| 87 | - state|> Map.put(:timer_ref, timer_ref) |
| 92 | + |
| 93 | + state |> Map.put(:timer_ref, timer_ref) |
| 88 94 | end |
| 89 95 | |
| 90 96 | # start a new timer |
| 91 97 | # TODO: cancel the old timer |
| 92 98 | @impl true |
| 93 99 | def handle_msg(%{payload: "_START"}, %{config: %{start_when: %{value: "triggered"}}} = state) do |
| 94 | - timer_ref = case state.config.how_long.value do |
| 95 | - "forever" -> :forever |
| 96 | - "time_period" -> |
| 97 | - {:ok, ref} = :timer.send_after state.config.time_period.value, :_TIMEOUT |
| 100 | + timer_ref = |
| 101 | + case state.config.how_long.value do |
| 102 | + "forever" -> |
| 103 | + :forever |
| 104 | + |
| 105 | + "time_period" -> |
| 106 | + {:ok, ref} = :timer.send_after(state.config.time_period.value, :_TIMEOUT) |
| 98 107 | ref |
| 99 108 | end |
| 109 | + |
| 100 110 | {nil, %{state | timer_ref: timer_ref}} |
| 101 111 | end |
| 102 | - |
| 112 | + |
| 103 113 | # clear timer_ref from state |
| 104 114 | def handle_msg(:_TIMEOUT, state) do |
| 105 115 | {nil, %{state | timer_ref: nil}} |
| 106 116 | end |
| 107 | - |
| 117 | + |
| 108 118 | def handle_msg(msg, %{timer_ref: timer_ref} = state) do |
| 109 119 | case timer_ref do |
| 110 | - |
| 111 120 | # no timer, forward all messages |
| 112 | - nil -> {msg, state} |
| 121 | + nil -> |
| 122 | + {msg, state} |
| 113 123 | |
| 114 124 | # there's a timer running (or it's set to :forever) |
| 115 | - # this means that we need to suppress messages |
| 116 | - _ -> |
| 125 | + # this means that we need to suppress messages |
| 126 | + _ -> |
| 117 127 | case state.config.what.value do |
| 118 128 | # suppress all messages |
| 119 | - "all" -> {nil, state} |
| 120 | - |
| 129 | + "all" -> |
| 130 | + {nil, state} |
| 131 | + |
| 121 132 | # suppress if payload is in a certain band |
| 122 133 | "band" -> |
| 123 | - if msg.payload < state.config.band_less_then.value or |
| 124 | - msg.payload > state.config.band_greater_then.value do |
| 134 | + if msg.payload < state.config.band_less_then.value or |
| 135 | + msg.payload > state.config.band_greater_then.value do |
| 125 136 | {nil, state} |
| 126 137 | else |
| 127 138 | {msg, state} |
| @@ -129,5 +140,4 @@ defmodule Exred.Node.Suppress do | |
| 129 140 | end |
| 130 141 | end |
| 131 142 | end |
| 132 | - |
| 133 143 | end |
| @@ -2,13 +2,14 @@ defmodule Exred.Node.Suppress.Mixfile do | |
| 2 2 | use Mix.Project |
| 3 3 | |
| 4 4 | @description "Suppresses incoming messages for a defined time period" |
| 5 | + @version File.read!("VERSION") |> String.trim() |
| 5 6 | |
| 6 7 | def project do |
| 7 8 | [ |
| 8 9 | app: :exred_node_suppress, |
| 9 | - version: "0.1.0", |
| 10 | + version: @version, |
| 10 11 | elixir: "~> 1.5", |
| 11 | - start_permanent: Mix.env == :prod, |
| 12 | + start_permanent: Mix.env() == :prod, |
| 12 13 | description: @description, |
| 13 14 | package: package(), |
| 14 15 | deps: deps() |
| @@ -23,8 +24,8 @@ defmodule Exred.Node.Suppress.Mixfile do | |
| 23 24 | |
| 24 25 | defp deps do |
| 25 26 | [ |
| 26 | - {:exred_library, "~> 0.1.11"}, |
| 27 | - {:ex_doc, "~> 0.18.0", only: :dev, runtime: false} |
| 27 | + {:exred_nodeprototype, "~> 0.2"}, |
| 28 | + {:ex_doc, "~> 0.19.0", only: :dev, runtime: false} |
| 28 29 | ] |
| 29 30 | end |
| 30 31 | |
| @@ -33,10 +34,10 @@ defmodule Exred.Node.Suppress.Mixfile do | |
| 33 34 | licenses: ["MIT"], |
| 34 35 | maintainers: ["Zsolt Keszthelyi"], |
| 35 36 | links: %{ |
| 36 | - "GitHub" => "https://github.com/exredorg/exred_node_suppress", |
| 37 | + "GitHub" => "https://github.com/exredorg/exred_node_suppress.git", |
| 37 38 | "Exred" => "http://exred.org" |
| 38 39 | }, |
| 39 | - files: ["lib", "mix.exs", "README.md", "LICENSE"] |
| 40 | + files: ["lib", "mix.exs", "README.md", "LICENSE", "VERSION"] |
| 40 41 | } |
| 41 42 | end |
| 42 43 | end |