-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from WebWideMatrix/basic-battery-support
Basic battery support
- Loading branch information
Showing
21 changed files
with
631 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
defmodule BldgServer.Batteries do | ||
@moduledoc """ | ||
The Batteries context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias BldgServer.Repo | ||
|
||
alias BldgServer.Batteries.Battery | ||
|
||
@doc """ | ||
Returns the list of batteries. | ||
## Examples | ||
iex> list_batteries() | ||
[%Battery{}, ...] | ||
""" | ||
def list_batteries do | ||
Repo.all(Battery) | ||
end | ||
|
||
|
||
@doc """ | ||
Returns the list of attached batteries in a given floor. | ||
## Examples | ||
iex> get_batteries_in_floor(flr) | ||
[%Battery{}, ...] | ||
""" | ||
def get_batteries_in_floor(flr) do | ||
q = from b in Battery, where: b.flr == ^flr and b.is_attached | ||
Repo.all(q) | ||
end | ||
|
||
|
||
|
||
|
||
|
||
@doc """ | ||
Gets a single battery. | ||
Raises `Ecto.NoResultsError` if the Battery does not exist. | ||
## Examples | ||
iex> get_battery!(123) | ||
%Battery{} | ||
iex> get_battery!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_battery!(id), do: Repo.get!(Battery, id) | ||
|
||
|
||
|
||
@doc """ | ||
Gets a single battery by it's container bldg's url. | ||
Raises `Ecto.NoResultsError` if the Battery does not exist. | ||
## Examples | ||
iex> get_battery_by_bldg_url!("g/bldg_name") | ||
%Battery{} | ||
iex> get_battery_by_bldg_url!("g/bldg_name") | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_attached_battery_by_bldg_url!(bldg_url) do | ||
clauses = [is_attached: :true, bldg_url: bldg_url] | ||
Repo.get_by!(Battery, clauses) | ||
end | ||
|
||
@doc """ | ||
Gets a single battery by it's container bldg's address. | ||
Raises `Ecto.NoResultsError` if the Battery does not exist. | ||
## Examples | ||
iex> get_battery_by_bldg_address!("g-b(10,20)") | ||
%Battery{} | ||
iex> get_battery_by_bldg_address!("g-b(30,40)") | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_attached_battery_by_bldg_address!(bldg_address) do | ||
clauses = [is_attached: :true, bldg_address: bldg_address] | ||
Repo.get_by!(Battery, clauses) | ||
end | ||
|
||
|
||
@doc """ | ||
Creates a battery. | ||
## Examples | ||
iex> create_battery(%{field: value}) | ||
{:ok, %Battery{}} | ||
iex> create_battery(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_battery(attrs \\ %{}) do | ||
%Battery{} | ||
|> Battery.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a battery. | ||
## Examples | ||
iex> update_battery(battery, %{field: new_value}) | ||
{:ok, %Battery{}} | ||
iex> update_battery(battery, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_battery(%Battery{} = battery, attrs) do | ||
battery | ||
|> Battery.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a battery. | ||
## Examples | ||
iex> delete_battery(battery) | ||
{:ok, %Battery{}} | ||
iex> delete_battery(battery) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_battery(%Battery{} = battery) do | ||
Repo.delete(battery) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking battery changes. | ||
## Examples | ||
iex> change_battery(battery) | ||
%Ecto.Changeset{source: %Battery{}} | ||
""" | ||
def change_battery(%Battery{} = battery) do | ||
Battery.changeset(battery, %{}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule BldgServer.Batteries.Battery do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "batteries" do | ||
field :battery_type, :string | ||
field :battery_vendor, :string | ||
field :battery_version, :string | ||
field :bldg_url, :string | ||
field :callback_url, :string | ||
field :direct_only, :boolean, default: false | ||
field :flr, :string | ||
field :is_attached, :boolean, default: false | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(battery, attrs) do | ||
battery | ||
|> cast(attrs, [:bldg_url, :flr, :callback_url, :is_attached, :direct_only, :battery_type, :battery_version, :battery_vendor]) | ||
|> validate_required([:bldg_url, :flr, :callback_url]) | ||
|> unique_constraint(:single_attached_battery_in_bldg, name: :single_attached_battery_in_bldg) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
bldg_server/lib/bldg_server_web/battery_chat_dispatcher.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
defmodule BldgServerWeb.BatteryChatDispatcher do | ||
use GenServer | ||
require Logger | ||
alias BldgServer.PubSub | ||
|
||
alias BldgServer.Batteries | ||
|
||
|
||
def start_link(_) do | ||
GenServer.start_link(__MODULE__, name: __MODULE__) | ||
end | ||
|
||
def init(_) do | ||
Phoenix.PubSub.subscribe(PubSub, "chat") | ||
IO.puts("~~~~~~~~~~~ [battery chat dispatcher] subscribed to chat") | ||
{:ok, %{}} | ||
end | ||
|
||
def handle_call(:get, _, state) do | ||
{:reply, state, state} | ||
end | ||
|
||
|
||
def send_message_to_battery(callback_url, msg) do | ||
Logger.info("~~~~~ About to invoke battery callback URL at: #{callback_url}") | ||
header_key = "content-type" | ||
header_val = "application/json" | ||
{_, msg_json} = Jason.encode(msg) | ||
IO.inspect(msg_json) | ||
Finch.build(:post, callback_url, [{header_key, header_val}], msg_json) | ||
|> Finch.request(FinchClient) | ||
|> IO.inspect() | ||
end | ||
|
||
|
||
#def handle_info({sender, message, flr}, state) do | ||
def handle_info(%{event: "new_message", payload: new_message}, state) do | ||
#Logger.info("chat message received at #{flr} from #{sender}: #{message}") | ||
Logger.info("~~~~~~~~~~~ [battery chat dispatcher] chat message received: #{new_message["message"]}") | ||
|
||
# query for all batteries inside that message flr | ||
# & invoke the callback url per each battery, with the message details in the body | ||
|
||
batteries = new_message["say_flr"] | ||
|> Batteries.get_batteries_in_floor() | ||
|
||
IO.inspect(batteries) | ||
|
||
Enum.map(batteries, fn b -> send_message_to_battery(b.callback_url, new_message) end) | ||
|
||
{:noreply, state} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.