2011-03-23 09:16:02 +00:00
|
|
|
%% @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
|
|
|
|
2011-03-04 18:30:58 +00:00
|
|
|
-module(flukso_sensor_xyz).
|
2011-03-23 09:16:02 +00:00
|
|
|
-author('Bart Van Der Meerssche <bart.vandermeerssche@flukso.net>').
|
|
|
|
|
2011-03-04 18:30:58 +00:00
|
|
|
-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").
|
2011-03-23 17:40:28 +00:00
|
|
|
-include("flukso.hrl").
|
2010-03-14 13:47:28 +00:00
|
|
|
|
2010-02-25 17:45:11 +00:00
|
|
|
init([]) ->
|
|
|
|
{ok, undefined}.
|
|
|
|
|
2011-03-04 18:30:58 +00:00
|
|
|
% debugging
|
|
|
|
%init(Config) ->
|
|
|
|
% {{trace, "/tmp"}, Config}.
|
|
|
|
|
2010-02-25 17:45:11 +00:00
|
|
|
allowed_methods(ReqData, State) ->
|
2011-03-04 18:30:58 +00:00
|
|
|
{['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) ->
|
2011-03-26 22:47:42 +00:00
|
|
|
{_Version, ValidVersion} = check_version(wrq:get_req_header("X-Version", ReqData)),
|
2011-03-04 18:30:58 +00:00
|
|
|
{RrdSensor, ValidSensor} = check_sensor(wrq:path_info(sensor, ReqData)),
|
2011-03-26 22:47:42 +00:00
|
|
|
{Device, ValidDevice} = check_device(wrq:get_req_header("X-Device", ReqData)),
|
|
|
|
{Digest, ValidDigest} = check_digest(wrq:get_req_header("X-Digest", ReqData)),
|
2011-03-04 18:30:58 +00:00
|
|
|
|
2011-03-26 22:47:42 +00:00
|
|
|
State = #state{rrdSensor = RrdSensor,
|
|
|
|
device = Device,
|
|
|
|
digest = Digest},
|
2011-03-04 18:30:58 +00:00
|
|
|
|
2011-03-26 22:47:42 +00:00
|
|
|
{case {ValidVersion, ValidSensor, ValidDevice, ValidDigest} of
|
|
|
|
{true, true, true, true} -> false;
|
2011-03-04 18:30:58 +00:00
|
|
|
_ -> true
|
|
|
|
end,
|
|
|
|
ReqData, State}.
|
2010-02-25 17:45:11 +00:00
|
|
|
|
2011-03-04 18:30:58 +00:00
|
|
|
malformed_GET(ReqData, _State) ->
|
2010-04-27 22:08:41 +00:00
|
|
|
{_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)),
|
2010-05-22 11:21:31 +00:00
|
|
|
{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)),
|
2010-04-27 21:47:01 +00:00
|
|
|
{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
|
|
|
|
2010-05-22 11:21:31 +00:00
|
|
|
State = #state{rrdSensor = RrdSensor,
|
|
|
|
rrdStart = RrdStart,
|
|
|
|
rrdEnd = RrdEnd,
|
|
|
|
rrdResolution = RrdResolution,
|
|
|
|
rrdFactor = RrdFactor,
|
|
|
|
token = Token,
|
|
|
|
jsonpCallback = JsonpCallback},
|
2010-03-14 13:47:28 +00:00
|
|
|
|
2010-05-21 14:25:01 +00:00
|
|
|
{case {ValidVersion, ValidSensor, ValidTime, ValidUnit, ValidToken, ValidJsonpCallback} of
|
2010-04-27 22:08:41 +00:00
|
|
|
{true, true, true, true, true, true} -> false;
|
2010-02-25 17:45:11 +00:00
|
|
|
_ -> true
|
|
|
|
end,
|
2010-03-14 13:47:28 +00:00
|
|
|
ReqData, State}.
|
2010-02-25 17:45:11 +00:00
|
|
|
|
2011-03-04 18:30:58 +00:00
|
|
|
is_authorized(ReqData, State) ->
|
|
|
|
case wrq:method(ReqData) of
|
|
|
|
'POST' -> is_auth_POST(ReqData, State);
|
|
|
|
'GET' -> is_auth_GET(ReqData, State)
|
|
|
|
end.
|
|
|
|
|
2011-03-26 22:47:42 +00:00
|
|
|
is_auth_POST(ReqData, #state{device = Device, digest = ClientDigest} = State) ->
|
|
|
|
{data, Result} = mysql:execute(pool, device_key, [Device]),
|
|
|
|
[[Key]] = mysql:get_result_rows(Result),
|
|
|
|
Data = wrq:req_body(ReqData),
|
|
|
|
<<X:160/big-unsigned-integer>> = crypto:sha_mac(Key, Data),
|
|
|
|
ServerDigest = lists:flatten(io_lib:format("~40.16.0b", [X])),
|
|
|
|
|
|
|
|
{case ServerDigest of
|
|
|
|
ClientDigest -> true;
|
|
|
|
_WrongDigest -> "access refused"
|
|
|
|
end,
|
|
|
|
ReqData, State}.
|
2011-03-04 18:30:58 +00:00
|
|
|
|
|
|
|
is_auth_GET(ReqData, #state{rrdSensor = RrdSensor, token = Token} = State) ->
|
2010-03-14 20:31:10 +00:00
|
|
|
{data, Result} = mysql:execute(pool, permissions, [RrdSensor, Token]),
|
|
|
|
|
|
|
|
{case mysql:get_result_rows(Result) of
|
|
|
|
[[62]] -> true;
|
2010-04-23 23:20:47 +00:00
|
|
|
_Permission -> "access refused"
|
2010-03-14 20:31:10 +00:00
|
|
|
end,
|
|
|
|
ReqData, State}.
|
|
|
|
|
2010-02-25 17:45:11 +00:00
|
|
|
content_types_provided(ReqData, State) ->
|
2011-03-04 18:30:58 +00:00
|
|
|
{[{"application/json", to_json}], ReqData, State}.
|
2010-02-25 17:45:11 +00:00
|
|
|
|
2010-05-22 11:21:31 +00:00
|
|
|
to_json(ReqData, #state{rrdSensor = RrdSensor, rrdStart = RrdStart, rrdEnd = RrdEnd, rrdResolution = RrdResolution, rrdFactor = RrdFactor, jsonpCallback = JsonpCallback} = State) ->
|
2010-04-27 12:22:25 +00:00
|
|
|
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,
|
|
|
|
|
2010-05-22 11:21:31 +00:00
|
|
|
%% debugging: io:format("~s~n", [erlrrd:c([[Path, [RrdSensor|".rrd"]], "AVERAGE", ["-s ", RrdStart], ["-e ", RrdEnd], ["-r ", RrdResolution]])]),
|
2010-05-21 14:25:01 +00:00
|
|
|
|
2010-05-22 11:21:31 +00:00
|
|
|
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],
|
2010-03-14 14:12:35 +00:00
|
|
|
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],
|
2010-04-23 23:20:47 +00:00
|
|
|
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
|
|
|
|
2010-03-14 20:31:10 +00:00
|
|
|
{error, _Reason} ->
|
2010-02-25 17:45:11 +00:00
|
|
|
{{halt, 404}, ReqData, State}
|
|
|
|
end.
|
|
|
|
|
2011-03-26 22:47:42 +00:00
|
|
|
% JSON: {"measurements":[[<ts1>,<value1>],...,[<tsn>,<valuen>]]}
|
|
|
|
% Mochijson2: {struct,[{<<"measurements">>,[[<ts11>,<value11>],...,[<ts1m>,<value1m>]]}]}
|
2011-03-04 18:30:58 +00:00
|
|
|
process_post(ReqData, #state{rrdSensor = RrdSensor} = State) ->
|
2011-03-26 22:47:42 +00:00
|
|
|
Path = "var/data/base/",
|
|
|
|
|
|
|
|
{struct, JsonData} = mochijson2:decode(wrq:req_body(ReqData)),
|
|
|
|
Measurements = proplists:get_value(<<"measurements">>, JsonData),
|
|
|
|
RrdData = [[integer_to_list(Time), ":", integer_to_list(Counter), " "] || [Time, Counter] <- Measurements],
|
|
|
|
|
|
|
|
%debugging: io:format("~s~n", [[Path, [RrdSensor|".rrd"], " ", RrdData]]),
|
|
|
|
|
|
|
|
case erlrrd:update([Path, [RrdSensor|".rrd"], " ", RrdData]) of
|
|
|
|
{ok, _RrdResponse} -> RrdResponse = "ok";
|
|
|
|
{error, RrdResponse} -> true
|
|
|
|
end,
|
|
|
|
|
|
|
|
JsonResponse = mochijson2:encode({struct, [{<<"response">>, list_to_binary(RrdResponse)}]}),
|
|
|
|
{true , wrq:set_resp_body(JsonResponse, ReqData), State}.
|