-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexample_template.ex
63 lines (55 loc) · 1.39 KB
/
example_template.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
55
56
57
58
59
60
61
62
63
defmodule Parser do
use Platform.Parsing.Behaviour
require Logger
#
# ELEMENT IoT Parser for device EXAMPLE that will provide EXAMPLE data.
#
# Name: Parser Template
# Changelog:
# 2019-05-09 [jb]: Initial implementation according to "Example-Payload-v1.pdf"
#
def parse(<<v1, v2, v3>>, %{meta: %{frame_port: 1}}) do
%{
type: :boot,
version: "#{v1}.#{v2}.#{v3}"
}
end
def parse(<<distance::32>>, %{meta: %{frame_port: 2}}) do
%{
type: :measurement,
distance: distance
}
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
# Define fields with human readable name and a SI unit if available.
def fields() do
[
# The first field should be a numeric value, so it can be used for graphs.
%{
field: "distance",
display: "Distanz",
unit: "cm"
},
%{
field: "type",
display: "Typ"
}
]
end
def tests() do
[
# Test format:
# {:parse_hex, received_payload_as_hex, meta_map, expected_result},
{:parse_hex, "010203", %{meta: %{frame_port: 1}}, %{type: :boot, version: "1.2.3"}},
{:parse_hex, "12345678", %{meta: %{frame_port: 2}},
%{type: :measurement, distance: 305_419_896}}
]
end
end