Skip to content

Commit 6b55ea2

Browse files
committed
DUP: Merge introspection triggers inside device triggers
Since introspection triggers are now device triggers, reflect this change in related funcitons. See astarte-platform/astarte_core#76. Signed-off-by: Arnaldo Cesco <[email protected]>
1 parent 2ca7def commit 6b55ea2

File tree

5 files changed

+52
-77
lines changed

5 files changed

+52
-77
lines changed

apps/astarte_data_updater_plant/lib/astarte_data_updater_plant/data_updater/event_type_utils.ex

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,6 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.EventTypeUtils do
4040
end
4141
end
4242

43-
def pretty_change_type(change_type) do
44-
case change_type do
45-
:INCOMING_INTROSPECTION ->
46-
:on_incoming_introspection
47-
48-
:INTERFACE_ADDED ->
49-
:on_interface_added
50-
51-
:INTERFACE_REMOVED ->
52-
:on_interface_removed
53-
54-
:INTERFACE_MINOR_UPDATED ->
55-
:on_interface_minor_updated
56-
end
57-
end
58-
5943
def pretty_device_event_type(device_event_type) do
6044
case device_event_type do
6145
:DEVICE_CONNECTED ->
@@ -69,6 +53,18 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.EventTypeUtils do
6953

7054
:DEVICE_ERROR ->
7155
:on_device_error
56+
57+
:INCOMING_INTROSPECTION ->
58+
:on_incoming_introspection
59+
60+
:INTERFACE_ADDED ->
61+
:on_interface_added
62+
63+
:INTERFACE_REMOVED ->
64+
:on_interface_removed
65+
66+
:INTERFACE_MINOR_UPDATED ->
67+
:on_interface_minor_updated
7268
end
7369
end
7470
end

apps/astarte_data_updater_plant/lib/astarte_data_updater_plant/data_updater/impl.ex

Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Impl do
2626
alias Astarte.DataUpdaterPlant.DataUpdater.State
2727
alias Astarte.Core.Triggers.DataTrigger
2828
alias Astarte.Core.Triggers.SimpleTriggersProtobuf.DataTrigger, as: ProtobufDataTrigger
29+
alias Astarte.Core.Triggers.SimpleTriggersProtobuf.DeviceTrigger, as: ProtobufDeviceTrigger
2930
alias Astarte.Core.Triggers.SimpleTriggersProtobuf.Utils, as: SimpleTriggersProtobufUtils
3031
alias Astarte.DataAccess.Data
3132
alias Astarte.DataAccess.Database
@@ -66,7 +67,6 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Impl do
6667
device_triggers: %{},
6768
data_triggers: %{},
6869
volatile_triggers: [],
69-
introspection_triggers: %{},
7070
interface_exchanged_bytes: %{},
7171
interface_exchanged_msgs: %{},
7272
last_seen_message: 0,
@@ -1078,14 +1078,14 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Impl do
10781078

10791079
any_interface_id = SimpleTriggersProtobufUtils.any_interface_object_id()
10801080

1081-
%{introspection_triggers: introspection_triggers} =
1081+
%{device_triggers: device_triggers} =
10821082
populate_triggers_for_object!(new_state, db_client, any_interface_id, :any_interface)
10831083

10841084
realm = new_state.realm
10851085
device_id_string = Device.encode_device_id(new_state.device_id)
10861086

10871087
on_introspection_targets =
1088-
Map.get(introspection_triggers, {:on_incoming_introspection, :any_interface}, [])
1088+
Map.get(device_triggers, {:on_incoming_introspection, :any_interface}, [])
10891089

10901090
TriggersHandler.incoming_introspection(
10911091
on_introspection_targets,
@@ -1129,8 +1129,9 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Impl do
11291129

11301130
minor = Map.get(db_introspection_minor_map, interface_name)
11311131

1132+
# TODO: move away from :any_interface, I guess
11321133
interface_added_targets =
1133-
Map.get(introspection_triggers, {:on_interface_added, :any_interface}, [])
1134+
Map.get(device_triggers, {:on_interface_added, :any_interface}, [])
11341135

11351136
TriggersHandler.interface_added(
11361137
interface_added_targets,
@@ -1159,8 +1160,9 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Impl do
11591160
:ok
11601161
end
11611162

1163+
# TODO: move away from :any_interface, I guess
11621164
interface_removed_targets =
1163-
Map.get(introspection_triggers, {:on_interface_deleted, :any_interface}, [])
1165+
Map.get(device_triggers, {:on_interface_deleted, :any_interface}, [])
11641166

11651167
TriggersHandler.interface_removed(
11661168
interface_removed_targets,
@@ -1495,9 +1497,6 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Impl do
14951497
{{:error, reason}, state}
14961498
end
14971499

1498-
{:introspection_trigger, _} ->
1499-
{:ok, new_state}
1500-
15011500
{:device_trigger, _} ->
15021501
{:ok, load_trigger(new_state, trigger, target)}
15031502
end
@@ -1611,30 +1610,6 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Impl do
16111610
{:ok, %{state | device_triggers: updated_device_triggers}}
16121611
end
16131612

1614-
defp delete_volatile_trigger(
1615-
state,
1616-
{_obj_id, _obj_type},
1617-
{{:introspection_trigger, proto_buf_introspection_trigger}, trigger_target}
1618-
) do
1619-
introspection_triggers = state.introspection_triggers
1620-
1621-
event_type = EventTypeUtils.pretty_change_type(proto_buf_introspection_trigger.change_type)
1622-
1623-
introspection_trigger_key =
1624-
{event_type, proto_buf_introspection_trigger.match_interface || :any_interface}
1625-
1626-
updated_targets_list =
1627-
Map.get(introspection_triggers, introspection_trigger_key, [])
1628-
|> Enum.reject(fn target ->
1629-
target == trigger_target
1630-
end)
1631-
1632-
updated_introspection_triggers =
1633-
Map.put(introspection_triggers, introspection_trigger_key, updated_targets_list)
1634-
1635-
{:ok, %{state | introspection_triggers: updated_introspection_triggers}}
1636-
end
1637-
16381613
defp reload_groups_on_expiry(state, timestamp, db_client) do
16391614
if state.last_groups_refresh + @groups_lifespan_decimicroseconds <= timestamp do
16401615
{:ok, groups} = Queries.get_device_groups(db_client, state.device_id)
@@ -2161,49 +2136,52 @@ defmodule Astarte.DataUpdaterPlant.DataUpdater.Impl do
21612136
end
21622137

21632138
# TODO: implement on_incoming_introspection, on_interface_minor_updated
2164-
defp load_trigger(
2165-
state,
2166-
{:introspection_trigger, proto_buf_introspection_trigger},
2167-
trigger_target
2168-
) do
2169-
introspection_triggers = state.introspection_triggers
2170-
2171-
event_type = EventTypeUtils.pretty_change_type(proto_buf_introspection_trigger.change_type)
2172-
2173-
introspection_trigger_key =
2174-
{event_type, proto_buf_introspection_trigger.match_interface || :any_interface}
2175-
2176-
existing_trigger_targets = Map.get(introspection_triggers, introspection_trigger_key, [])
2177-
2178-
new_targets = [trigger_target | existing_trigger_targets]
2179-
2180-
next_introspection_triggers =
2181-
Map.put(introspection_triggers, introspection_trigger_key, new_targets)
2182-
2183-
# Register the new target
2184-
:ok = TriggersHandler.register_target(trigger_target)
2185-
2186-
Map.put(state, :introspection_triggers, next_introspection_triggers)
2187-
end
2188-
21892139
# TODO: implement on_empty_cache_received
21902140
defp load_trigger(state, {:device_trigger, proto_buf_device_trigger}, trigger_target) do
21912141
device_triggers = state.device_triggers
21922142

2143+
# device event type is one of
2144+
# :on_device_connected, :on_device_disconnected, :on_device_empty_cache_received, :on_device_error,
2145+
# :on_incoming_introspection, :on_interface_added, :on_interface_removed, :on_interface_minor_updated
21932146
event_type =
21942147
EventTypeUtils.pretty_device_event_type(proto_buf_device_trigger.device_event_type)
21952148

2196-
existing_trigger_targets = Map.get(device_triggers, event_type, [])
2149+
# introspection triggers have a pair as key, device ones do not
2150+
# TODO make a beautiful function
2151+
trigger_key =
2152+
case event_type do
2153+
:on_incoming_introspection ->
2154+
{event_type, introspection_trigger_interface(proto_buf_device_trigger)}
2155+
2156+
:on_interface_added ->
2157+
{event_type, introspection_trigger_interface(proto_buf_device_trigger)}
2158+
2159+
:on_interface_removed ->
2160+
{event_type, introspection_trigger_interface(proto_buf_device_trigger)}
2161+
2162+
:on_interface_minor_updated ->
2163+
{event_type, introspection_trigger_interface(proto_buf_device_trigger)}
2164+
2165+
_ ->
2166+
event_type
2167+
end
2168+
2169+
existing_trigger_targets = Map.get(device_triggers, trigger_key, [])
21972170

21982171
new_targets = [trigger_target | existing_trigger_targets]
21992172

22002173
# Register the new target
22012174
:ok = TriggersHandler.register_target(trigger_target)
22022175

2203-
next_device_triggers = Map.put(device_triggers, event_type, new_targets)
2176+
next_device_triggers = Map.put(device_triggers, trigger_key, new_targets)
22042177
Map.put(state, :device_triggers, next_device_triggers)
22052178
end
22062179

2180+
# TODO
2181+
# defp introspection_tigger_interface(%ProtobufDeviceTrigger{match_interface: interface}),
2182+
# do: interface
2183+
defp introspection_trigger_interface(%ProtobufDeviceTrigger{}), do: :any_interface
2184+
22072185
defp resolve_path(path, interface_descriptor, mappings) do
22082186
case interface_descriptor.aggregation do
22092187
:individual ->

apps/astarte_data_updater_plant/lib/astarte_data_updater_plant/triggers_handler.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ defmodule Astarte.DataUpdaterPlant.TriggersHandler do
130130
end
131131

132132
def incoming_introspection(target, realm, device_id, introspection, timestamp) do
133+
# TODO check that introspection is a string here
133134
%IncomingIntrospectionEvent{introspection: introspection}
134135
|> make_simple_event(
135136
:incoming_introspection_event,

apps/astarte_data_updater_plant/mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ defmodule Astarte.DataUpdaterPlant.Mixfile do
6363

6464
defp astarte_required_modules(_) do
6565
[
66-
{:astarte_core, github: "astarte-platform/astarte_core"},
66+
{:astarte_core, github: "Annopaolo/astarte_core", branch: "introspection-triggers-to-device", override: true},
6767
{:astarte_data_access, github: "astarte-platform/astarte_data_access"},
6868
{:astarte_rpc, github: "astarte-platform/astarte_rpc"}
6969
]

apps/astarte_data_updater_plant/mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%{
22
"amqp": {:hex, :amqp, "2.1.1", "ad8dec713ba885afffffcb81feb619fe7cfcbcabe9377ab65ab7a110bd4f43a0", [:mix], [{:amqp_client, "~> 3.8.0", [hex: :amqp_client, repo: "hexpm", optional: false]}], "hexpm", "b6d926770e4508e30e3e9e476c57b6c8aeda44f7715663bdc38935620ce5be6f"},
33
"amqp_client": {:hex, :amqp_client, "3.8.14", "7569517aefb47e0d1c41bca2f4768dc8a2d88487daf7819fecca0d78943f293c", [:make, :rebar3], [{:rabbit_common, "3.8.14", [hex: :rabbit_common, repo: "hexpm", optional: false]}], "hexpm", "e5ba3ac18abbe34a1d990a6bcac25633dc7061ab8f8d101c7dcff97f49f4c523"},
4-
"astarte_core": {:git, "https://github.com/astarte-platform/astarte_core.git", "8f3f53c392bc886885af830e6dcabcb9a57b1130", []},
4+
"astarte_core": {:git, "https://github.com/Annopaolo/astarte_core.git", "60412a17c3529db49a7b33fe916abc6463742039", [branch: "introspection-triggers-to-device"]},
55
"astarte_data_access": {:git, "https://github.com/astarte-platform/astarte_data_access.git", "bf5f5958420a5c1cd99258b4b6a9c18e6335d4ce", []},
66
"astarte_rpc": {:git, "https://github.com/astarte-platform/astarte_rpc.git", "29d26a272da9c4f67c62b5200a5a7e133655dd31", []},
77
"castore": {:hex, :castore, "0.1.16", "2675f717adc700475345c5512c381ef9273eb5df26bdd3f8c13e2636cf4cc175", [:mix], [], "hexpm", "28ed2c43d83b5c25d35c51bc0abf229ac51359c170cba76171a462ced2e4b651"},

0 commit comments

Comments
 (0)