Skip to content

Instantly share code, notes, and snippets.

@msantos
Created April 13, 2010 00:51
Show Gist options
  • Save msantos/364194 to your computer and use it in GitHub Desktop.
Save msantos/364194 to your computer and use it in GitHub Desktop.
-module(sensor).
-include("light.hrl").
-export([start/0,stop/0]).
% web interface
-export([graph/3]).
start() ->
dets:open_file(?TABLE, [{file, ?STORE}]),
application:start(inets),
inets:start(httpd, [
{modules, [
mod_alias,
mod_auth,
mod_esi,
mod_actions,
mod_cgi,
mod_dir,
mod_get,
mod_head,
mod_log,
mod_disk_log
]},
{port, 8889},
{server_name, "foo"},
{server_root, "log"},
{document_root, "www"},
{directory_index, ["index.html"]},
{error_log, "error.log"},
{security_log, "security.log"},
{transfer_log, "transfer.log"},
{erl_script_alias, {"/web", [sensor]}}
]).
stop() ->
inets:stop().
%%%
%%% web services
%%%
graph(SessionID, _Env, _Input) ->
mod_esi:deliver(SessionID, header(png)),
B = generate_graph(),
mod_esi:deliver(SessionID, binary_to_list(B)).
%%%
%%% helper functions
%%%
generate_graph() ->
L = get_values(),
egd_chart:graph([{"light",L}], [
{bg_rgba, {255,255,255,255}},
{width, 1024},
{height, 800}
]).
header(png) ->
"Content-Type: image/png\r\n\r\n".
get_values() ->
L = lists:sort([
{calendar:datetime_to_gregorian_seconds(calendar:now_to_universal_time(K)), V}
|| [{K, V}] <- dets:match(sensor, '$1') ]),
[ {K - element(1,hd(L)), V} || {K,V} <- L ].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment