forked from usetrmnl/byos_phoenix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_controller.ex
More file actions
81 lines (70 loc) · 1.95 KB
/
api_controller.ex
File metadata and controls
81 lines (70 loc) · 1.95 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
defmodule TrmnlWeb.APIController do
use TrmnlWeb, :controller
require Logger
alias Trmnl.Inventory
# GET /api/display
def display(conn, _params) do
with [api_key | _] <- get_req_header(conn, "access-token"),
device = %Inventory.Device{} <- Inventory.get_device_by_api_key(api_key) do
Inventory.ping(device)
json(conn, display_ok(device))
else
_ -> json(conn, display_error())
end
end
# GET /api/setup
def setup(conn, _params) do
with [mac_address | _] <- get_req_header(conn, "id"),
device = %Inventory.Device{} <- Inventory.get_device_by_mac_address(mac_address) do
Inventory.ping(device)
json(conn, setup_ok(device))
else
_ -> json(conn, setup_error())
end
end
# POST /api/log
def log(conn, %{"logs" => log}) do
Logger.debug("/api/log: #{inspect(log)}")
send_resp(conn, 200, "")
end
# --- JSON responses ---
defp display_ok(device) do
# The device uses the filename to determine if the screen should be updated, so base the filename on the timestamp
filename = (device.screen_generated_at |> DateTime.to_unix() |> Integer.to_string()) <> ".bmp"
%{
status: 0,
image_url: System.get_env("TRMNL_URL") <> device.latest_screen,
filename: filename,
refresh_rate: device.refresh_interval,
reset_firmware: false,
update_firmware: false,
firmware_url: nil,
special_function: "sleep"
}
end
defp display_error do
%{
status: 500,
error: "Device not found",
reset_firmware: true
}
end
defp setup_ok(device) do
%{
status: 200,
api_key: device.api_key,
friendly_id: device.friendly_id,
image_url: url(~p"/images/setup/setup-logo.bmp"),
message: "Welcome to TRMNL BYOS"
}
end
defp setup_error do
%{
status: 404,
api_key: nil,
friendly_id: nil,
image_url: nil,
message: "MAC address not registered"
}
end
end