Skip to content

Instantly share code, notes, and snippets.

@shankardevy
Created September 28, 2015 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shankardevy/ed03530f1d3ef299ef1d to your computer and use it in GitHub Desktop.
Save shankardevy/ed03530f1d3ef299ef1d to your computer and use it in GitHub Desktop.
defmodule Snapshot.Server do
@moduledoc """
Provides functions and workers for getting snapshots from the camera
Workers are run in the background getting snapshots from the camera
using the frequency for the given camera.
Functions can be called from other places to get snapshots manually.
"""
use GenServer
alias Snapshot.CamClient
## Client API
@doc """
Start the Snapshot server for a given camera
"""
def start_link(camera, config, opts \\ []) do
GenServer.start_link(__MODULE__, config, opts)
end
@doc """
Get a snapshot from the camera server
"""
def get_snapshot(cam_server) do
GenServer.call(cam_server, :get_camera_snapshot)
end
@doc """
Start a worker for the camera that takes snapshot in frequent interval
as defined in the args passed to the camera server.
"""
def start_worker(cam_server) do
GenServer.call(cam_server, :start_camera_worker)
end
@doc """
Stop a worker for the camera.
"""
def stop_worker(cam_server) do
GenServer.call(cam_server, :stop_camera_worker)
end
@doc """
Get the process id of the camera worker if there is a worker started o
"""
def get_worker_pid(cam_server) do
GenServer.call(cam_server, :get_camera_worker)
end
@doc """
"""
def get_config(cam_server) do
GenServer.call(cam_server, :get_camera_config)
end
@doc """
"""
def update_config(cam_server, config) do
GenServer.call(cam_server, {:update_camera_config, config})
end
## Server Callbacks
@doc """
Initialize the camera server
"""
def init(config) do
state = %{config: config}
{:ok, state}
end
@doc """
Server callback for getting snapshot
"""
def handle_call(:get_camera_snapshot, _from, state) do
{:ok, config} = get_state(:config, state)
result = CamClient.fetch_snapshot(config)
{:reply, result, state}
end
@doc """
Server callback for starting camera worker
"""
def handle_call(:start_camera_worker, _from, state) do
# todo
{:reply, nil, state}
end
@doc """
Server callback for stopping camera worker
"""
def handle_call(:stop_camera_worker, _from, state) do
# todo
{:reply, nil, state}
end
@doc """
Server callback for getting camera worker
"""
def handle_call(:get_camera_worker, _from, state) do
# todo
{:reply, nil, state}
end
@doc """
Server callback for getting camera config
"""
def handle_call(:get_camera_config, _from, state) do
{:reply, get_state(:config, state), state}
end
@doc """
Server callback for updating camera config
"""
def handle_call({:update_camera_config, config}, _from, state) do
# todo
{:reply, nil, state}
end
@doc """
Gets camera config from the server state
"""
defp get_state(:config, state) do
Map.fetch(state, :config)
end
end
@joelbyler
Copy link

@shankardevy, I wonder if you've been keeping up with this, or maybe have a complete example of how this would work in a nerves project? I think the nerves project is looking for someone to add an example of how to interface with a camera for their examples section of their site. I'd be interested in assisting you if you need some help. I think they would just need a very simple example of a program which would take a single snapshot, or perhaps take a snapshot at regular intervals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment