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

196 lines
8.3 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)),
{RrdSensor, ValidSensor} = check_sensor(wrq:path_info(sensor, ReqData)),
{Digest, ValidDigest} = check_digest(wrq:get_req_header("X-Digest", ReqData)),
State = #state{rrdSensor = RrdSensor,
digest = Digest},
{case {ValidVersion, ValidSensor, ValidDigest} of
{true, 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{rrdSensor = Sensor, digest = ClientDigest} = State) ->
{data, Result} = mysql:execute(pool, device_key, [Sensor]),
[[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}.
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
"night" -> Path = ?NIGHT_PATH;
_Interval -> Path = ?BASE_PATH
2010-02-25 17:45:11 +00:00
end,
%% debugging: io:format("~s~n", [erlrrd:c([[Path, [RrdSensor|".rrd"]], "AVERAGE", ["-s ", RrdStart], ["-e ", RrdEnd], ["-r ", RrdResolution]])]),
case rrd_fetch(Path, RrdSensor, RrdStart, RrdEnd, 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.
% JSON: {"measurements":[[<TS1>,<VALUE1>],...,[<TSn>,<VALUEn>]]}
% Mochijson2: {struct,[{<<"measurements">>,[[<TS1>,<VALUE1>],...,[<TSn>,<VALUEn>]]}]}
process_post(ReqData, #state{rrdSensor = RrdSensor} = State) ->
{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],
[LastTimestamp, LastValue] = lists:last(Measurements),
{data, Result} = mysql:execute(pool, sensor_props, [RrdSensor]),
[[Uid, _Device, Midnight]] = mysql:get_result_rows(Result),
case rrd_update(?BASE_PATH, RrdSensor, RrdData) of
{ok, _RrdResponse} ->
RrdResponse = "ok",
NewMidnight = update_night(RrdSensor, Uid, Midnight, LastTimestamp, ReqData),
mysql:execute(pool, sensor_update, [unix_time(), NewMidnight, LastValue, RrdSensor]);
{error, RrdResponse} ->
logger(Uid, <<"rrdupdate.base">>, list_to_binary(RrdResponse), ?ERROR, ReqData)
end,
JsonResponse = mochijson2:encode({struct, [{<<"response">>, list_to_binary(RrdResponse)}]}),
{true , wrq:set_resp_body(JsonResponse, ReqData), State}.
update_night(RrdSensor, Uid, Midnight, LastTimestamp, ReqData) when LastTimestamp > Midnight + 6 * ?HOUR ->
LastMidnight = calculate_midnight(unix_time(), Uid),
RrdStart = integer_to_list(LastMidnight + 2 * ?HOUR),
RrdEnd = integer_to_list(LastMidnight + 5 * ?HOUR),
RrdResolution = integer_to_list(?QUARTER),
case rrd_fetch(?BASE_PATH, RrdSensor, RrdStart, RrdEnd, RrdResolution) of
{ok, Response} ->
Filtered = [re:split(X, "[:][ ]", [{return,list}]) || [X] <- Response, string:str(X, ":") == 11],
Datapoints = [list_to_float(Y) || [_X, Y] <- Filtered, string:len(Y) /= 3],
NightAverage = lists:foldl(fun(X, Sum) -> X / 12 + Sum end, 0, Datapoints),
RrdData = [integer_to_list(LastMidnight + 5 * ?HOUR), ":", float_to_list(NightAverage)],
rrd_update(?NIGHT_PATH, RrdSensor, RrdData);
{error, Reason} ->
logger(Uid, <<"rrdupdate.night">>, list_to_binary(Reason), ?ERROR, ReqData)
end,
LastMidnight + ?DAY;
update_night(_RrdSensor, _Uid, Midnight, _LastTimestamp, _ReqData) ->
Midnight.
calculate_midnight(Timestamp, Uid) ->
{data, Result} = mysql:execute(pool, timezone, [Uid]),
[[TimezoneChar8]] = mysql:get_result_rows(Result),
Timezone = list_to_integer(binary_to_list(TimezoneChar8)),
closest_midnight(trunc(Timestamp/?DAY + 1) * ?DAY - Timezone, Timestamp).
closest_midnight(ProposedMidnight, Timestamp) when ProposedMidnight > Timestamp ->
closest_midnight(ProposedMidnight - ?DAY, Timestamp);
closest_midnight(ProposedMidnight, _Timestamp) ->
ProposedMidnight.