Basic routing + account creation
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
defmodule NeonVertigoService.MessageProcessor do
|
||||
@ets_auctions NeonVertigoService.Auctions
|
||||
@ets_bids NeonVertigoService.Bids
|
||||
|
||||
def init_tables() do
|
||||
@ets_auctions = :ets.new(@ets_auctions, [:named_table])
|
||||
@ets_bids = :ets.new(@ets_bids, [:named_table])
|
||||
:ok
|
||||
end
|
||||
|
||||
def process_message(["auctions", "create"], payload, _properties) do
|
||||
gid = payload["gid"]
|
||||
data = Map.take(payload, ["author_gid", "min_bet"]) |> Enum.map(fn {k, v} -> {String.to_atom(k), v} end) |> Map.new
|
||||
{min_bet, _} = Decimal.parse(data[:min_bet])
|
||||
data = Map.put(data, :min_bet, min_bet)
|
||||
:ets.insert_new(@ets_auctions, {gid, data})
|
||||
:noreply
|
||||
end
|
||||
|
||||
def process_message(_, _, _) do
|
||||
:noreply
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
defmodule NeonVertigoService.Mqtt.Service do
|
||||
defmodule NeonVertigoService.Mqtt.Server do
|
||||
use GenServer
|
||||
|
||||
def start_link(_) do
|
||||
@@ -6,30 +6,41 @@ defmodule NeonVertigoService.Mqtt.Service do
|
||||
end
|
||||
|
||||
def init(_) do
|
||||
{:ok, {}, {:continue, :init_mqtt}}
|
||||
{:ok, {}, {:continue, :post_init}}
|
||||
end
|
||||
|
||||
# Callbacks
|
||||
def handle_continue(:init_mqtt, {}) do
|
||||
def handle_continue(:post_init, {}) do
|
||||
:ok = NeonVertigoService.MessageProcessor.init_tables()
|
||||
|
||||
mqtt_config = Application.fetch_env!(:neon_vertigo_service, :mqtt_params)
|
||||
{:ok, mqtt_pid} = Supervisor.start_child(
|
||||
NeonVertigoService.Mqtt.Supervisor,
|
||||
%{
|
||||
id: :neon_vertigo_emqtt,
|
||||
start: {:emqtt, :start_link, [[owner: self(), name: :neon_vertigo_service_emqtt] ++ mqtt_config]}
|
||||
start: {:emqtt, :start_link, [[owner: self(), name: :neon_vertigo_service_emqtt, proto_ver: :v5] ++ mqtt_config]}
|
||||
}
|
||||
)
|
||||
{:ok, _mqtt_props} = :emqtt.connect(mqtt_pid)
|
||||
|
||||
:emqtt.subscribe(mqtt_pid, %{}, [{"$share/neon-vertigo-service/test/+", [{:qos, 1}]}])
|
||||
:emqtt.subscribe(mqtt_pid, %{}, [{"$share/neon-vertigo-service/auctions/+", [{:qos, 1}]}])
|
||||
|
||||
{:noreply, {mqtt_pid}}
|
||||
end
|
||||
|
||||
# Handling MQTT messages
|
||||
def handle_info({:publish, msg}, state) do
|
||||
def handle_info({:publish, msg}, {mqtt_pid} = state) do
|
||||
IO.puts("Received message")
|
||||
IO.inspect(msg)
|
||||
%{payload: payload_str, topic: topic_str, properties: properties} = msg
|
||||
topic = String.split(topic_str, "/")
|
||||
payload = JSON.decode!(payload_str)
|
||||
|
||||
case NeonVertigoService.MessageProcessor.process_message(topic, payload, properties) do
|
||||
{:reply, topic, data} -> :emqtt.publish(mqtt_pid, topic, data)
|
||||
:noreply -> nil
|
||||
end
|
||||
|
||||
{:noreply, state}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,7 +10,7 @@ defmodule NeonVertigoService.Mqtt.Supervisor do
|
||||
@impl true
|
||||
def init(_init_arg) do
|
||||
children = [
|
||||
NeonVertigoService.Mqtt.Service
|
||||
NeonVertigoService.Mqtt.Server
|
||||
]
|
||||
|
||||
Supervisor.init(children, strategy: :one_for_all)
|
||||
|
||||
Reference in New Issue
Block a user