-
Notifications
You must be signed in to change notification settings - Fork 17
/
dummy.ex
68 lines (58 loc) · 1.44 KB
/
dummy.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
64
65
66
67
68
defmodule Parser do
use Platform.Parsing.Behaviour
require Logger
# Dummy Data parser for ELEMENT IoT.
#
# This parser can be used with a "Dummy Driver" with a expected value range of 1..100
#
# Name: Example parser for adding a GPS location to a reading
# Changelog:
# 2019-09-05 [jb]: Initial implementation supporting reading location.
#
# Add a reading location if this function returns true.
defp add_location?(), do: true
def parse(%{"payload" => value}, _meta) do
%{
type: :dummy,
value: value
}
|> add_location(value)
end
def parse(payload, _meta) do
Logger.warn("Could not parse payload #{inspect(payload)}, expected dummy data.")
[]
end
def fields() do
[
%{
field: "type",
display: "Typ"
},
%{
field: "value",
display: "Value"
}
]
end
defp add_location(reading, value) do
case add_location?() do
true ->
# Box around Hamburg
lat_min = 9.670835
lat_max = 10.471977
lon_min = 53.324266
lon_max = 53.779132
lat = lat_min + (lat_max - lat_min) / 100 * value
lon = lon_min + (lon_max - lon_min) / 100 * value
{reading, [location: {lat, lon}]}
_ ->
{reading, []}
end
end
def tests() do
[
{:parse, %{"payload" => 42}, %{},
{%{type: :dummy, value: 42}, [location: {10.00731464, 53.51530972}]}}
]
end
end