-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexample_profile_read.ex
54 lines (47 loc) · 1.22 KB
/
example_profile_read.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
defmodule Parser do
use Platform.Parsing.Behaviour
require Logger
# Example parser for reading values from device profile
#
# A profile has a "technical name" and each field has its own "technical name".
# These are needed here, NOT the display name!
#
# Name: Example parser for reading profile values of device
# Changelog:
# 2019-09-30 [jb]: Initial implementation
def preloads do
[device: [profile_data: [:profile]]]
end
def parse(<<value::16>>, %{meta: %{frame_port: 42}} = meta) do
factor = get(meta, [:device, :fields, :my_profile, :my_field], 1)
%{
value: value * factor
}
end
def parse(payload, meta) do
Logger.warn(
"Could not parse payload #{inspect(payload)} with frame_port #{
inspect(get_in(meta, [:meta, :frame_port]))
}"
)
[]
end
def tests() do
[
{
# No profile given, default factor =1 will be used.
:parse_hex,
"1337",
%{meta: %{frame_port: 42}},
%{value: 4919}
},
{
# Profile has factor value =2.
:parse_hex,
"1337",
%{meta: %{frame_port: 42}, device: %{fields: %{my_profile: %{my_field: 2}}}},
%{value: 9838}
}
]
end
end