flm01/server/api/flukso/src/flukso_sensor_xyz.erl

130 lines
5.1 KiB
Erlang
Raw Normal View History

%% @author Bart Van Der Meerssche <bart.vandermeerssche@flukso.net>
%% @copyright (C) 2009-2011 Bart Van Der Meerssche
%%%
%%% This program is free software: you can redistribute it and/or modify
%%% it under the terms of the GNU General Public License as published by
%%% the Free Software Foundation, either version 3 of the License, or
%%% (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%%% GNU General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License
%%% along with this program. If not, see <http://www.gnu.org/licenses/>.
%%%
%% @doc Flukso API: /sensor/xyz resource specification
2010-02-25 17:45:11 +00:00
-module(flukso_sensor_xyz).
-author('Bart Van Der Meerssche <bart.vandermeerssche@flukso.net>').
-export([init/1,
allowed_methods/2,
malformed_request/2,
is_authorized/2,
content_types_provided/2,
to_json/2,
process_post/2]).
2010-02-25 17:45:11 +00:00
-include_lib("webmachine/include/webmachine.hrl").
-include("flukso.hrl").
2010-02-25 17:45:11 +00:00
init([]) ->
{ok, undefined}.
% debugging
%init(Config) ->
% {{trace, "/tmp"}, Config}.
2010-02-25 17:45:11 +00:00
allowed_methods(ReqData, State) ->
{['POST', 'GET'], ReqData, State}.
malformed_request(ReqData, State) ->
case wrq:method(ReqData) of
'POST' -> malformed_POST(ReqData, State);
'GET' -> malformed_GET(ReqData, State)
end.
malformed_POST(ReqData, _State) ->
{_Version, ValidVersion} = check_version(wrq:get_req_header("X-Version", ReqData), wrq:get_qs_value("version", ReqData)),
{RrdSensor, ValidSensor} = check_sensor(wrq:path_info(sensor, ReqData)),
State = #state{rrdSensor = RrdSensor},
{case {ValidVersion, ValidSensor} of
{true, true} -> false;
_ -> true
end,
ReqData, State}.
2010-02-25 17:45:11 +00:00
malformed_GET(ReqData, _State) ->
{_Version, ValidVersion} = check_version(wrq:get_req_header("X-Version", ReqData), wrq:get_qs_value("version", ReqData)),
2010-04-27 21:23:27 +00:00
{RrdSensor, ValidSensor} = check_sensor(wrq:path_info(sensor, ReqData)),
{RrdStart, RrdEnd, RrdResolution, ValidTime} = check_time(wrq:get_qs_value("interval", ReqData), wrq:get_qs_value("start", ReqData), wrq:get_qs_value("end", ReqData), wrq:get_qs_value("resolution", ReqData)),
{RrdFactor, ValidUnit} = check_unit(wrq:get_qs_value("unit", ReqData)),
2010-04-27 21:23:27 +00:00
{Token, ValidToken} = check_token(wrq:get_req_header("X-Token", ReqData), wrq:get_qs_value("token", ReqData)),
{JsonpCallback, ValidJsonpCallback} = check_jsonp_callback(wrq:get_qs_value("jsonp_callback", ReqData)),
2010-02-25 17:45:11 +00:00
State = #state{rrdSensor = RrdSensor,
rrdStart = RrdStart,
rrdEnd = RrdEnd,
rrdResolution = RrdResolution,
rrdFactor = RrdFactor,
token = Token,
jsonpCallback = JsonpCallback},
{case {ValidVersion, ValidSensor, ValidTime, ValidUnit, ValidToken, ValidJsonpCallback} of
{true, true, true, true, true, true} -> false;
2010-02-25 17:45:11 +00:00
_ -> true
end,
ReqData, State}.
2010-02-25 17:45:11 +00:00
is_authorized(ReqData, State) ->
case wrq:method(ReqData) of
'POST' -> is_auth_POST(ReqData, State);
'GET' -> is_auth_GET(ReqData, State)
end.
is_auth_POST(ReqData, State) ->
{true, ReqData, State}.
is_auth_GET(ReqData, #state{rrdSensor = RrdSensor, token = Token} = State) ->
{data, Result} = mysql:execute(pool, permissions, [RrdSensor, Token]),
{case mysql:get_result_rows(Result) of
[[62]] -> true;
_Permission -> "access refused"
end,
ReqData, State}.
2010-02-25 17:45:11 +00:00
content_types_provided(ReqData, State) ->
{[{"application/json", to_json}], ReqData, State}.
2010-02-25 17:45:11 +00:00
to_json(ReqData, #state{rrdSensor = RrdSensor, rrdStart = RrdStart, rrdEnd = RrdEnd, rrdResolution = RrdResolution, rrdFactor = RrdFactor, jsonpCallback = JsonpCallback} = State) ->
case wrq:get_qs_value("interval", ReqData) of
2010-02-25 17:45:11 +00:00
"night" -> Path = "var/data/night/";
_Interval -> Path = "var/data/base/"
end,
%% debugging: io:format("~s~n", [erlrrd:c([[Path, [RrdSensor|".rrd"]], "AVERAGE", ["-s ", RrdStart], ["-e ", RrdEnd], ["-r ", RrdResolution]])]),
case erlrrd:fetch(erlrrd:c([[Path, [RrdSensor|".rrd"]], "AVERAGE", ["-s ", RrdStart], ["-e ", RrdEnd], ["-r ", RrdResolution]])) of
2010-02-25 17:45:11 +00:00
{ok, Response} ->
Filtered = [re:split(X, "[:][ ]", [{return,list}]) || [X] <- Response, string:str(X, ":") == 11],
Datapoints = [[list_to_integer(X), round(list_to_float(Y) * RrdFactor)] || [X, Y] <- Filtered, string:len(Y) /= 3],
2010-02-25 17:45:11 +00:00
Nans = [[list_to_integer(X), list_to_binary(Y)] || [X, Y] <- Filtered, string:len(Y) == 3],
Final = mochijson2:encode(lists:merge(Datapoints, Nans)),
{case JsonpCallback of
undefined -> Final;
_ -> [JsonpCallback, "(", Final, ");"]
end,
ReqData, State};
2010-02-25 17:45:11 +00:00
{error, _Reason} ->
2010-02-25 17:45:11 +00:00
{{halt, 404}, ReqData, State}
end.
process_post(ReqData, #state{rrdSensor = RrdSensor} = State) ->
{true , ReqData, State}.