lol emojis

pull/212/head
connor rigby 2016-12-09 10:05:12 -08:00
parent 42d25a474e
commit 84bd47cb83
6 changed files with 161 additions and 3 deletions

View File

@ -0,0 +1,134 @@
defmodule Farmbot.EasterEggs do
@moduledoc """
Just some random stuff that Farmbot likes to say and do from time to time.
"""
use GenServer
require Logger
@path "#{:code.priv_dir(:farmbot)}/static/easter_eggs.json"
@type json_map :: map
@type parsed_json :: %{nouns: [parsed_noun], verbs: [String.t]}
@type json_noun :: map # %{"some_key" => "some_value"}
@type parsed_noun :: map # %{some_key: "some_value"}
@doc """
Starts the Easter Eggs server. You can pass in a json map, a path to a
json file, or nothing and it will load the default one.
"""
def start_link({:name, name}),
do: GenServer.start_link(__MODULE__, @path, name: name)
def start_link({:name, name}, {:path, path}),
do: GenServer.start_link(__MODULE__, path, name: name)
def start_link({:name, name}, {:json, json}),
do: GenServer.start_link(__MODULE__, json, name: name)
@spec init(json_map | binary) :: {:ok, parsed_json}
def init(%{"nouns" => _, "verbs" => _} = json),
do: json |> parse_easter_eggs_json
def init(path) when is_binary(path) do
path
|> File.read!
|> Poison.decode!
|> parse_easter_eggs_json
end
@spec parse_easter_eggs_json(json_map) :: {:ok, parsed_json}
defp parse_easter_eggs_json(%{"nouns" => nouns, "verbs" => verbs}) do
g =
%{nouns: parse_nouns(nouns),
verbs: verbs}
{:ok, g}
end
@spec parse_nouns([json_noun]) :: parsed_noun
defp parse_nouns(nouns_json) when is_list(nouns_json) do
Enum.reduce(nouns_json, %{}, fn(noun, acc) ->
noun
|> parse_noun
|> Map.merge(acc)
end)
end
@spec parse_noun(json_noun) :: parsed_noun
defp parse_noun(n) when is_map(n) do
Map.new(n, fn({string_key, v}) ->
{String.to_atom(string_key), v}
end)
end
@doc """
Gets a random Farmbot verb
"""
@spec verb(pid) :: String.t
def verb(pid \\ __MODULE__), do: GenServer.call(pid, :verb)
@doc """
Logs a random "fun" sentence to the Web Interface.
"""
@spec say_random_sentence(pid) :: :ok
def say_random_sentence(pid \\ __MODULE__), do: GenServer.cast(pid, verb)
@doc """
Loads new json into the state.
Example:
iex> json = %{"nouns" => [%{"im_a_var" => "im a string"}],
...> "verbs" => ["says look at me! {{im_a_var}}!"]}
iex> #{__MODULE__}.load_json(json)
iex> #{__MODULE__}.say_random_sentence()
#=> "Farmbot says look at me! im a string!"
"""
@spec load_json(json_map) :: :ok
def load_json(%{"nouns" => _, "verbs" => _} = json_map, pid \\ __MODULE__) do
GenServer.cast(pid, json_map)
end
@doc """
Says a random sentence every ten minutes by default.
"""
@lint false # i dont want to alias Quantum.
@spec start_cron_job(binary) :: :ok
def start_cron_job(schedule \\ "*/10 * * * *") do
job = %Quantum.Job{
schedule: schedule,
task: fn -> Farmbot.EasterEggs.say_random_sentence end}
Quantum.add_job(__MODULE__, job)
end
@doc """
Stops an already started job.
"""
@spec stop_cron_job :: :no_job | map
def stop_cron_job, do: __MODULE__ |> Quantum.find_job() |> do_delete_job
@spec do_delete_job(any) :: :no_job | map
@lint false # i dont want to alias Quantum.
defp do_delete_job(%Quantum.Job{name: j_name}), do: Quantum.delete_job(j_name)
defp do_delete_job(_), do: :no_job
# GEN SERVER CALLBACKS
def handle_cast(sentence, %{nouns: nouns, verbs: verbs})
when is_binary(sentence) do
rendered = Mustache.render sentence, nouns
Logger.debug ">> " <> rendered, type: :fun
{:noreply, %{nouns: nouns, verbs: verbs}}
end
def handle_cast(%{"nouns" => _, "verbs" => _} = json, _state) do
{:ok, state} = parse_easter_eggs_json(json)
{:noreply, state}
end
def handle_cast(_event, state), do: {:noreply, state}
def handle_call(:state, _from, state), do: {:reply, state, state}
def handle_call(:verb, _from, %{nouns: n, verbs: v}) do
rv = v |> Enum.random
{:reply, rv, %{nouns: n, verbs: v}}
end
def handle_call(_event, _from, state), do: {:reply, :unhandled, state}
def handle_info(_event, state), do: {:noreply, state}
end

View File

@ -23,7 +23,9 @@ defmodule Farmbot.BotState.Supervisor do
[initial_config], [restart: :permanent]),
worker(Farmbot.BotState.Hardware, [[]], [restart: :permanent]),
worker(Farmbot.BotState.Authorization, [[]], [restart: :permanent]),
worker(Farmbot.BotState.Network, [[]], [restart: :permanent])
worker(Farmbot.BotState.Network, [[]], [restart: :permanent]),
worker(Farmbot.EasterEggs, [name: Farmbot.EasterEggs],
[restart: :permanent])
]
opts = [strategy: :one_for_one, name: __MODULE__]
supervise(children, opts)
@ -35,6 +37,7 @@ defmodule Farmbot.BotState.Supervisor do
# like position and some configuraion.
sup = Supervisor.start_link(__MODULE__, args)
Logger.add_backend(Farmbot.Logger)
Farmbot.EasterEggs.start_cron_job
sup
end
end

View File

@ -6,10 +6,13 @@ defmodule Farmbot.RPC.Transport.GenMqtt.Client do
use GenMQTT
require Logger
alias RPC.MessageManager
def init(%Token{} = token) do
{:ok, token}
end
def init(maybe_token), do: Token.create(maybe_token)
def start_link(%Token{} = token) do
GenMQTT.start_link(__MODULE__, token, build_opts(token))
end
@ -57,10 +60,12 @@ defmodule Farmbot.RPC.Transport.GenMqtt.Client do
end
@spec frontend_topic(Token.t) :: String.t
defp frontend_topic(%Token{} = token), do: "bot/#{token.unencoded.bot}/from_device"
defp frontend_topic(%Token{} = token),
do: "bot/#{token.unencoded.bot}/from_device"
@spec bot_topic(Token.t) :: String.t
defp bot_topic(%Token{} = token), do: "bot/#{token.unencoded.bot}/from_clients"
defp bot_topic(%Token{} = token),
do: "bot/#{token.unencoded.bot}/from_clients"
@spec build_last_will_message(Token.t) :: binary
defp build_last_will_message(%Token{} = token) do

View File

@ -56,6 +56,7 @@ defmodule Farmbot.Mixfile do
:farmbot_configurator,
:vmq_commons,
:amnesia,
# :porcelain,
:quantum]
end
@ -97,6 +98,7 @@ defmodule Farmbot.Mixfile do
{:socket, github: "meh/elixir-socket"},
{:amnesia, github: "meh/amnesia"},
{:quantum, ">= 1.8.1"},
# {:porcelain, "~> 2.0"},
{:farmbot_auth, github: "Farmbot/farmbot_auth"},
# {:farmbot_auth, path: "../farmbot_auth"},
{:farmbot_configurator, github: "Farmbot/farmbot_configurator"}

View File

@ -55,6 +55,7 @@
"nerves_wpa_supplicant": {:hex, :nerves_wpa_supplicant, "0.2.2", "da829baec94a8afa7da3a6beaaf1725b9ca7f5f58779ba50b73568b7d37d3d98", [:make, :mix], [{:elixir_make, "~> 0.3", [hex: :elixir_make, optional: false]}]},
"plug": {:hex, :plug, "1.2.2", "cfbda521b54c92ab8ddffb173fbaabed8d8fc94bec07cd9bb58a84c1c501b0bd", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]},
"poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []},
"porcelain": {:hex, :porcelain, "2.0.3", "2d77b17d1f21fed875b8c5ecba72a01533db2013bd2e5e62c6d286c029150fdc", [:mix], []},
"providers": {:hex, :providers, "1.6.0", "db0e2f9043ae60c0155205fcd238d68516331d0e5146155e33d1e79dc452964a", [:rebar3], [{:getopt, "0.8.2", [hex: :getopt, optional: false]}]},
"quantum": {:hex, :quantum, "1.8.1", "37c9ad0307cf47bd578507ce1ddda98746199b281e8afe91cbe44c21d56af983", [:mix], [{:calendar, "~> 0.16", [hex: :calendar, optional: false]}]},
"ranch": {:hex, :ranch, "1.2.1", "a6fb992c10f2187b46ffd17ce398ddf8a54f691b81768f9ef5f461ea7e28c762", [:make], []},

View File

@ -0,0 +1,13 @@
{
"nouns" : [
{"fav_song_link": "https://www.youtube.com/watch?v=d4nbfPZa9Ws"},
{"fav_activity": "gardening"}
],
"verbs" : [
"is listening to :musical_score: {{fav_song_link}}",
"is jamming out to {{fav_song_link}} :musical_score:",
"is doing my favorite activity {{fav_activity}}",
"wants to know when it should water it's plants? :sweat_drops: :sweat_drops:",
"is getting kind of bored... :clock830: :zzz:"
]
}