-
Notifications
You must be signed in to change notification settings - Fork 9
/
DL-CTD10.ELEMENT-IoT.ex
57 lines (46 loc) · 1.76 KB
/
DL-CTD10.ELEMENT-IoT.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
# https://www.decentlab.com/products/pressure-/-liquid-level-temperature-and-electrical-conductivity-sensor-for-lorawan
defmodule Parser do
use Platform.Parsing.Behaviour
## test payloads
# 0207d9000390888081006400000c60
# 0207d900020c60
def fields do
[
%{field: "water_depth", display: "Water depth", unit: "mm"},
%{field: "temperature", display: "Temperature", unit: "°C"},
%{field: "electrical_conductivity", display: "Electrical conductivity", unit: "µS⋅cm⁻¹"},
%{field: "freezing_flag", display: "Freezing flag", unit: ""},
%{field: "battery_voltage", display: "Battery voltage", unit: "V"}
]
end
def parse(<<2, device_id::size(16), flags::binary-size(2), words::binary>>, _meta) do
{_remaining, result} =
{words, %{:device_id => device_id, :protocol_version => 2}}
|> sensor0(flags)
|> sensor1(flags)
result
end
defp where(true, if_true, _if_false), do: if_true
defp where(false, _if_true, if_false), do: if_false
defp sensor0({<<x0::size(16), x1::size(16), x2::size(16), x3::size(16), remaining::binary>>, result},
<<_::size(15), 1::size(1), _::size(0)>>) do
{remaining,
Map.merge(result,
%{
:water_depth => x0 - 32768,
:temperature => (x1 - 32768) / 10,
:electrical_conductivity => x2,
:freezing_flag => x3
})}
end
defp sensor0(result, _flags), do: result
defp sensor1({<<x0::size(16), remaining::binary>>, result},
<<_::size(14), 1::size(1), _::size(1)>>) do
{remaining,
Map.merge(result,
%{
:battery_voltage => x0 / 1000
})}
end
defp sensor1(result, _flags), do: result
end