Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Poll Twitter using ExTwitter and the Streaming API #4

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
defmodule Zeus.ColorParsing do
defmodule Lightning.ColorParsing do
def get_color("Color: #" <> <<raw_hex::bytes-size(6)>> <> _), do: raw_hex

def get_color(_), do: "FFFFFF"

def parse_rgb_hex(rgb_value) do
[r1, r2, g1, g2, b1, b2] = rgb_value |> String.codepoints |> Enum.take(-6)
Expand Down
10 changes: 6 additions & 4 deletions apps/lightning/lib/lightning/control.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ defmodule Lightning.Control do
change_color(0, color, brightness)
end

def change_color(ch, color, brightness) do
IO.puts renderer
renderer.render(ch, {brightness, [color]})
def change_color(channel, color, brightness) do
IO.puts renderer()
IO.puts("Rendering #{inspect([color])} at brightness #{inspect(brightness)} to channel #{inspect(channel)}")
renderer().render(channel, {brightness, [color]})
end

def renderer do
@renderer || Lightning.NeopixelStandin
# @renderer || Lightning.NeopixelStandin
Nerves.Neopixel
end
end
1 change: 1 addition & 0 deletions apps/lightning/lib/lightning/neopixel_standin.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Lightning.NeopixelStandin do
def render(channel, {brightness, color_array}) do
IO.puts("Rendering #{inspect(color_array)} at brightness #{inspect(brightness)} to channel #{inspect(channel)}")
{:ok, ""}
end
end
2 changes: 1 addition & 1 deletion apps/lightning/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Lightning.Mixfile do
elixir: "~> 1.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
deps: deps()]
end

# Configuration for the OTP application
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
defmodule ColorParsingTest do
use ExUnit.Case
doctest Zeus.ColorParsing
doctest Lightning.ColorParsing

describe "parse_rgb_hex" do
test "parses out three values" do
assert "#000000"
|> Zeus.ColorParsing.parse_rgb_hex
|> Lightning.ColorParsing.parse_rgb_hex
|> Tuple.to_list
|> Enum.count
== 3
end

test "converts hex numbers to integers" do
assert "#000000"
|> Zeus.ColorParsing.parse_rgb_hex
== [0, 0, 0]
|> Lightning.ColorParsing.parse_rgb_hex
== {0, 0, 0}
end

test "converts other hex numbers" do
assert "#ffaa11"
|> Zeus.ColorParsing.parse_rgb_hex
== [255, 170, 17]
|> Lightning.ColorParsing.parse_rgb_hex
== {255, 170, 17}
end
end

describe "hex_to_number" do
test "Converts a small hex number" do
assert "00"
|> Zeus.ColorParsing.hex_to_number
|> Lightning.ColorParsing.hex_to_number
== 0
end

test "Converts a large hex number" do
assert "FF"
|> Zeus.ColorParsing.hex_to_number
|> Lightning.ColorParsing.hex_to_number
== 255
end

test "is able to work with lower case numbers" do
assert "aa"
|> Zeus.ColorParsing.hex_to_number
|> Lightning.ColorParsing.hex_to_number
== 170
end
end
Expand Down
24 changes: 24 additions & 0 deletions apps/the_eye/lib/the_eye.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule TheEye do
use Application
alias Lightning.ColorParsing

@kernel_module "brcmfmac"
@interface :wlan0
Expand All @@ -13,7 +14,9 @@ defmodule TheEye do
children = [
worker(Task, [fn -> init_kernel_modules() end], restart: :transient, id: Nerves.Init.KernelModules),
worker(Task, [fn -> init_network() end], restart: :transient, id: Nerves.Init.Network),
worker(Task, [fn -> init_ntpd() end], restart: :transient, id: Nerves.Init.Ntpd),
worker(Nerves.Neopixel, [neopixel_cfg, nil]),
worker(Task, [fn -> IO.puts("Starting Twitter Connection"); get_tweets('#cmm_storm') end], restart: :transient),
]

# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
Expand All @@ -29,4 +32,25 @@ defmodule TheEye do
def init_network() do
Nerves.InterimWiFi.setup(@interface, @wifi_cfg)
end

def get_tweets(query) do
Process.sleep(10000)
IO.puts "get_tweets: #{query}"
ExTwitter.stream_filter(track: query)
|> Stream.map(fn(tweet) -> tweet.text end)
|> Stream.map(&(update_color(&1)))
|> Enum.to_list
end

defp update_color(text) do
IO.puts "update_color: #{text}"
ColorParsing.get_color(text)
|> ColorParsing.parse_rgb_hex
|> Lightning.Control.change_color(150)
end

def init_ntpd do
Process.sleep(8000)
System.cmd("ntpd", ["-q", "-p", "pool.ntp.org"])
end
end
8 changes: 5 additions & 3 deletions apps/the_eye/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ defmodule TheEye.Mixfile do
# Type `mix help compile.app` for more information.
def application do
[mod: {TheEye, []},
applications: [:logger, :nerves_interim_wifi, :nerves_neopixel, :lightning, :zeus]]
applications: [:logger, :nerves_interim_wifi, :nerves_ntp, :nerves_neopixel, :oauther, :extwitter, :lightning, :zeus]]
end

def deps do
[
{:nerves, "~> 0.4.0"},
{:nerves_interim_wifi, "~> 0.1.0"},
{:nerves_ntp, "~> 0.1.1"},
{:nerves_neopixel, "~> 0.3.0"},
{:extwitter, "~> 0.8"},
{:lightning, in_umbrella: true},
{:zeus, in_umbrella: true},
{:zeus, in_umbrella: true}
]
end

Expand All @@ -45,7 +47,7 @@ defmodule TheEye.Mixfile do
def aliases do
["deps.precompile": ["nerves.precompile", "deps.precompile"],
"deps.loadpaths": ["deps.loadpaths", "nerves.loadpaths"],
"burn": ["compile", "firmware", "firmware.burn"]]
"burn": ["clean", "compile", "firmware", "firmware.burn"]]
end

end
20 changes: 20 additions & 0 deletions apps/zapdos/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# The directory Mix will write compiled artifacts to.
/_build

# If you run "mix test --cover", coverage assets end up here.
/cover

# The directory Mix downloads your dependencies sources to.
/deps

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez
19 changes: 19 additions & 0 deletions apps/zapdos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Zapdos

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `zapdos` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:zapdos, "~> 0.1.0"}]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/zapdos](https://hexdocs.pm/zapdos).

32 changes: 32 additions & 0 deletions apps/zapdos/config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.

# You can configure for your application as:
#
# config :zapdos, key: :value
#
# And access this configuration in your application as:
#
# Application.get_env(:zapdos, :key)
#
# Or configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"

config :lightning, renderer: Lightning.NeopixelStandin
24 changes: 24 additions & 0 deletions apps/zapdos/lib/zapdos.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Zapdos do
alias Lightning.ColorParsing

@moduledoc """
Documentation for Zapdos.
"""

def get_tweets(query) do
Process.sleep(10000)
IO.puts "get_tweets: #{query}"
ExTwitter.stream_filter(track: query)
|> Stream.map(fn(tweet) -> tweet.text end)
|> Stream.map(&(update_color(&1)))
|> Enum.to_list
end

defp update_color(text) do
IO.puts "update_color: #{text}"
ColorParsing.get_color(text)
|> ColorParsing.parse_rgb_hex
|> Lightning.Control.change_color(150)
end

end
20 changes: 20 additions & 0 deletions apps/zapdos/lib/zapdos/application.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Zapdos.Application do
# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
@moduledoc false

use Application

def start(_type, _args) do
import Supervisor.Spec, warn: false

# Define workers and child supervisors to be supervised
children = [
]

# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Zapdos.Supervisor]
Supervisor.start_link(children, opts)
end
end
41 changes: 41 additions & 0 deletions apps/zapdos/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule Zapdos.Mixfile do
use Mix.Project

def project do
[app: :zapdos,
version: "0.1.0",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
lockfile: "../../mix.lock",
elixir: "~> 1.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps()]
end

# Configuration for the OTP application
#
# Type "mix help compile.app" for more information
def application do
# Specify extra applications you'll use from Erlang/Elixir
[applications: [:logger, :oauther, :extwitter],
mod: {Zapdos.Application, []}]
end

# Dependencies can be Hex packages:
#
# {:my_dep, "~> 0.3.0"}
#
# Or git/path repositories:
#
# {:my_dep, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
#
# Type "mix help deps" for more examples and options
defp deps do
[
{:extwitter, "~> 0.8"},
{:lightning, in_umbrella: true}
]
end
end
1 change: 1 addition & 0 deletions apps/zapdos/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()
8 changes: 8 additions & 0 deletions apps/zapdos/test/zapdos_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule ZapdosTest do
use ExUnit.Case
doctest Zapdos

test "the truth" do
assert 1 + 1 == 2
end
end
2 changes: 1 addition & 1 deletion apps/zeus/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Zeus.Mixfile do
compilers: [:phoenix, :gettext] ++ Mix.compilers,
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
aliases: aliases,
aliases: aliases(),
deps: deps()]
end

Expand Down
2 changes: 1 addition & 1 deletion apps/zeus/web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Zeus.PageController do
use Zeus.Web, :controller
alias Zeus.ColorParsing
alias Lightning.ColorParsing

def index(conn, _params) do
render conn, "index.html", color: nil, brightness: nil
Expand Down
3 changes: 3 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], []},
"distillery": {:hex, :distillery, "1.1.2", "4cf32ecc70ca7eecca9e52e111edf320bd78011050825863cd8bc7ffee686c5d", [:mix], []},
"elixir_make": {:hex, :elixir_make, "0.3.0", "285147fa943806eee82f6680b7b446b5569bcf3ee8328fa0a7c200ffc44fbaba", [:mix], []},
"extwitter": {:hex, :extwitter, "0.8.2", "1b1b6842c6541ddfa468f1589b54c02a3b278b5ae74d6bed260655394d8cc7be", [:mix], [{:oauther, "~> 1.1", [hex: :oauther, optional: false]}, {:poison, "~> 2.0", [hex: :poison, optional: false]}]},
"fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []},
"gettext": {:hex, :gettext, "0.13.1", "5e0daf4e7636d771c4c71ad5f3f53ba09a9ae5c250e1ab9c42ba9edccc476263", [:mix], []},
"mime": {:hex, :mime, "1.0.1", "05c393850524767d13a53627df71beeebb016205eb43bfbd92d14d24ec7a1b51", [:mix], []},
"nerves": {:hex, :nerves, "0.4.7", "c8d08348e6f717cf140da26dc374bcf2f6f7c2bec646073aeab049e08b588173", [:mix], [{:distillery, "~> 1.0", [hex: :distillery, optional: false]}]},
"nerves_interim_wifi": {:hex, :nerves_interim_wifi, "0.1.1", "88c65ced1440e96b954189ccfc8a55cb4575cb64a8725907c3e8a9900adb2a70", [:make, :mix], [{:elixir_make, "~> 0.3", [hex: :elixir_make, optional: false]}, {:nerves_network_interface, "~> 0.3.1", [hex: :nerves_network_interface, optional: false]}, {:nerves_wpa_supplicant, "~> 0.2.2", [hex: :nerves_wpa_supplicant, optional: false]}]},
"nerves_neopixel": {:hex, :nerves_neopixel, "0.3.0", "afd9c906896f0b39a2bea61cf01afe304d550bc74c789a5e872d64df5e1ae559", [:make, :mix], [{:elixir_make, "~> 0.3.0", [hex: :elixir_make, optional: false]}]},
"nerves_network_interface": {:hex, :nerves_network_interface, "0.3.3", "0a49e00f9f0bdb482e94d8e9885cf0a250ae04cc18d248d83bc184b8813ddda2", [:make, :mix], [{:elixir_make, "~> 0.3", [hex: :elixir_make, optional: false]}]},
"nerves_ntp": {:hex, :nerves_ntp, "0.1.1", "fa1a1f5c325d00fc3d940d8f5deea98964c84e617c7370325989d64accfa698a", [:mix], []},
"nerves_system_br": {:hex, :nerves_system_br, "0.9.4", "5096a9dfec49d4663ccb94c4a4fe45885303fbf31108f7e9400369bdec94b5e7", [:mix], []},
"nerves_system_rpi3": {:hex, :nerves_system_rpi3, "0.10.0", "dc5c05e1caf13027aaa57b4142d128513d9c29acc3284cc984a6770592ed4b34", [:mix], [{:nerves, "~> 0.4.0", [hex: :nerves, optional: false]}, {:nerves_system_br, "~> 0.9.2", [hex: :nerves_system_br, optional: false]}, {:nerves_toolchain_arm_unknown_linux_gnueabihf, "~> 0.9.0", [hex: :nerves_toolchain_arm_unknown_linux_gnueabihf, optional: false]}]},
"nerves_toolchain_arm_unknown_linux_gnueabihf": {:hex, :nerves_toolchain_arm_unknown_linux_gnueabihf, "0.9.0", "5a1bca8c46776ad24c358ab58800ed470f91a3e294ac6eb8ffda0041954781e1", [:mix], [{:nerves, "~> 0.4.0", [hex: :nerves, optional: false]}, {:nerves_toolchain_ctng, "~> 0.8.0", [hex: :nerves_toolchain_ctng, optional: false]}]},
"nerves_toolchain_ctng": {:hex, :nerves_toolchain_ctng, "0.8.0", "6dff7ed51e1711c5f4da3d559bc528a8265e3dd950dda95f4d6832aed9dbe320", [:mix], []},
"nerves_wpa_supplicant": {:hex, :nerves_wpa_supplicant, "0.2.3", "2666b21bf0868f0d3b127930c3bacf59b178a0d015d308ae3985dcb7c1595870", [:make, :mix], [{:elixir_make, "~> 0.3", [hex: :elixir_make, optional: false]}]},
"oauther": {:hex, :oauther, "1.1.0", "c9a56e200507ce64e069f5273143db0cddd7904cd5d1fe46920388a48f22441b", [:mix], []},
"phoenix": {:hex, :phoenix, "1.2.1", "6dc592249ab73c67575769765b66ad164ad25d83defa3492dc6ae269bd2a68ab", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, optional: false]}, {:plug, "~> 1.1", [hex: :plug, optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]},
"phoenix_html": {:hex, :phoenix_html, "2.9.3", "1b5a2122cbf743aa242f54dced8a4f1cc778b8bd304f4b4c0043a6250c58e258", [:mix], [{:plug, "~> 1.0", [hex: :plug, optional: false]}]},
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.0.8", "4333f9c74190f485a74866beff2f9304f069d53f047f5fbb0fb8d1ee4c495f73", [:mix], [{:fs, "~> 0.9.1", [hex: :fs, optional: false]}, {:phoenix, "~> 1.0 or ~> 1.2-rc", [hex: :phoenix, optional: false]}]},
Expand Down