Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
Add Elixir map web server
Browse files Browse the repository at this point in the history
  • Loading branch information
jj1bdx committed May 27, 2018
1 parent facbeab commit 6911b4c
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 2 deletions.
4 changes: 2 additions & 2 deletions js-libs/LeafLet-1.3.1/example-overlays.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@



<div id="mapid" style="width: 1024px; height: 768px;"></div>
<div id="mapid" style="width: 1280px; height: 768px;"></div>
<script>

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function demo() {
var mymap = L.map('mapid').setView([34.786, 135.44], 13);
var mymap = L.map('mapid').setView([0, 0], 1);
L.gridLayer.googleMutant({type: 'roadmap'}).addTo(mymap);

var layer = L.marker([34.786, 135.44]).addTo(mymap)
Expand Down
2 changes: 2 additions & 0 deletions lib/apresse_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule ApresseWeb do
def start(_type, _args) do
import Supervisor.Spec, warn: false

:aprs_receiver.start()

children = [
# Start the endpoint when the application starts
supervisor(ApresseWeb.Endpoint, []),
Expand Down
66 changes: 66 additions & 0 deletions lib/apresse_web_aprsmap.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
defmodule ApresseWeb.APRSMap do
@moduledoc """
TBD. A standalone plug module to pick APRS data from ETS
and convert them into map data through templates.
"""

# Note for module development:
# No compression
# No caching (since the contents of directory may vary every time)

@behaviour Plug
@allowed_methods ~w(GET HEAD)

import Plug.Conn
alias Plug.Conn

def init(_opts) do
# do nothing here
end

def call(conn = %Conn{method: meth}, _)
when meth in @allowed_methods do
dump_aprs_map(conn)
end

def call(conn, _opts) do
conn
end

defp dump_aprs_map(conn) do
conn
|> put_resp_header("content-type", "text/html")
|> send_resp(200, make_aprs_map())
|> halt
end

require EEx

EEx.function_from_file(:defp, :header_html,
"lib/templates/apresse_web_header.html.eex", [
])

EEx.function_from_file(:defp, :footer_html,
"lib/templates/apresse_web_footer.html.eex", [
])

EEx.function_from_file(:defp, :mapentry_html,
"lib/templates/apresse_web_mapentry.html.eex", [
:source,
:lat,
:long
])

defp make_aprs_map() do
# Plug.conn.send_resp/3 accepts IOlist in the body
[
header_html(),
Enum.map(:ets.tab2list(:aprs_positions),
fn({_, source, lat, long}) -> mapentry_html(source, lat, long) end),
footer_html()
]
end
end
5 changes: 5 additions & 0 deletions lib/templates/apresse_web_footer.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
}
showmap();
</script>
</body>
</html>
21 changes: 21 additions & 0 deletions lib/templates/apresse_web_header.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>apresse APRS map</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Google Maps API -->
<script
src="http://maps.google.com/maps/api/js?sensor=false&libraries=geomtry">
</script>
<link rel="stylesheet" href="static/leaflet.css">
<script src="static/leaflet.js"></script>
<script src='static/Leaflet.GoogleMutant.js'></script>
</head>
<body>
<h1>apresse APRS map</h1>
<div id="mapid" style="width: 1280px; height: 768px;"></div>
<script>
function showmap() {
var mymap = L.map('mapid').setView([0.0, 0.0], 1);
L.gridLayer.googleMutant({type: 'roadmap'}).addTo(mymap);
5 changes: 5 additions & 0 deletions lib/templates/apresse_web_mapentry.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%
popup = :io_lib.format("Source: ~s<br>Lat: ~.4f<br>Long: ~.4f",
[source, lat, long])
%>
var marker = L.marker([<%= lat %>, <%= long %>]).addTo(mymap).bindPopup('<%= popup %>');
3 changes: 3 additions & 0 deletions src/aprs_receiver.erl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ parse_message(
{Lat, Long};
parse_message(_) -> undefined.

put_ets(_Time, undefined, {_Lat, _Long}) -> undefined;
put_ets(_Time, _Source, {undefined, _Long}) -> undefined;
put_ets(_Time, _Source, {_Lat, undefined}) -> undefined;
put_ets(Time, Source, {Lat, Long}) ->
% io:format("~p~n", [{Time, Source, Lat, Long}]),
ets:insert(aprs_positions, {Time, Source, Lat, Long});
Expand Down

0 comments on commit 6911b4c

Please sign in to comment.