增加设备数据查询接口
This commit is contained in:
parent
c8c95b68ce
commit
c8142256e8
@ -63,6 +63,32 @@ handle_request("POST", "/device/activate", _, #{<<"host_id">> := HostId, <<"devi
|
|||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
%% 重新加载对应的主机信息
|
||||||
|
handle_request("POST", "/device/query", _, Params = #{<<"device_uuid">> := DeviceUUID}) when is_binary(DeviceUUID) ->
|
||||||
|
lager:debug("[device_handler] query device uuid: ~p, params: ~p", [DeviceUUID, Params]),
|
||||||
|
case iot_device:get_pid(DeviceUUID) of
|
||||||
|
undefined ->
|
||||||
|
{ok, 200, iot_util:json_error(404, <<"device not found">>)};
|
||||||
|
DevicePid when is_pid(DevicePid) ->
|
||||||
|
Tags = maps:get(<<"tags">>, Params, #{}),
|
||||||
|
StartTs = maps:get(<<"start_ts">>, Params, 0),
|
||||||
|
StopTs = maps:get(<<"stop_ts">>, Params, 0),
|
||||||
|
Limit = maps:get(<<"limit">>, Params, 0),
|
||||||
|
|
||||||
|
case is_map(Tags) andalso is_integer(StartTs) andalso is_integer(StopTs) andalso is_integer(Limit)
|
||||||
|
andalso StartTs >= 0 andalso StopTs >= 0 andalso Limit >= 0 of
|
||||||
|
|
||||||
|
true ->
|
||||||
|
{ok, DeviceDataList} = iot_device:query(DevicePid, Tags, StartTs, StopTs, Limit),
|
||||||
|
DataItems = lists:map(fun(#device_data{tags = Tags, val = Val, timestamp = Timestamp}) ->
|
||||||
|
#{<<"tags">> => Tags, <<"val">> => Val, <<"timestamp">> => Timestamp}
|
||||||
|
end, DeviceDataList),
|
||||||
|
{ok, 200, iot_util:json_data(DataItems)};
|
||||||
|
false ->
|
||||||
|
{ok, 200, iot_util:json_error(404, <<"invalid params">>)}
|
||||||
|
end
|
||||||
|
end;
|
||||||
|
|
||||||
handle_request(_, Path, _, _) ->
|
handle_request(_, Path, _, _) ->
|
||||||
Path1 = list_to_binary(Path),
|
Path1 = list_to_binary(Path),
|
||||||
{ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}.
|
{ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}.
|
||||||
@ -55,8 +55,9 @@ serialize(FieldsList) when is_list(FieldsList) ->
|
|||||||
lists:flatmap(fun serialize0/1, FieldsList).
|
lists:flatmap(fun serialize0/1, FieldsList).
|
||||||
serialize0(Fields = #{<<"key">> := Key}) when is_binary(Key) andalso Key /= <<>> ->
|
serialize0(Fields = #{<<"key">> := Key}) when is_binary(Key) andalso Key /= <<>> ->
|
||||||
Values = maps:remove(<<"key">>, Fields),
|
Values = maps:remove(<<"key">>, Fields),
|
||||||
S = base64:encode(iolist_to_binary(jiffy:encode(#{Key => Values}, [force_utf8]))),
|
%S = base64:encode(iolist_to_binary(jiffy:encode(#{Key => Values}, [force_utf8]))),
|
||||||
[<<"base64:", S/binary>>];
|
%[<<"base64:", S/binary>>];
|
||||||
|
[#{Key => Values}];
|
||||||
serialize0(_) ->
|
serialize0(_) ->
|
||||||
[].
|
[].
|
||||||
|
|
||||||
@ -106,7 +107,8 @@ auth(Pid, Auth) when is_pid(Pid), is_boolean(Auth) ->
|
|||||||
data(Pid, DataList) when is_pid(Pid), is_list(DataList) ->
|
data(Pid, DataList) when is_pid(Pid), is_list(DataList) ->
|
||||||
gen_statem:cast(Pid, {data, DataList}).
|
gen_statem:cast(Pid, {data, DataList}).
|
||||||
|
|
||||||
query(Pid, Tags, StartTs, StopTs, Limit) when is_pid(Pid), is_map(Tags), is_integer(StartTs), is_integer(StopTs), is_integer(Limit) ->
|
-spec query(Pid :: pid(), Tags :: map(), StartTs :: integer(), StopTs :: integer(), Limit :: integer()) -> {ok, [#device_data{}]}.
|
||||||
|
query(Pid, Tags, StartTs, StopTs, Limit) when is_pid(Pid), is_map(Tags), is_integer(StartTs), is_integer(StopTs), is_integer(Limit), StartTs >= 0, StopTs >= 0, Limit >= 0 ->
|
||||||
gen_statem:call(Pid, {query, Tags, StartTs, StopTs, Limit}).
|
gen_statem:call(Pid, {query, Tags, StartTs, StopTs, Limit}).
|
||||||
|
|
||||||
%% @doc Creates a gen_statem process which calls Module:init/1 to
|
%% @doc Creates a gen_statem process which calls Module:init/1 to
|
||||||
@ -226,7 +228,7 @@ handle_event({call, From}, {query, Tags, StartTs, StopTs, Limit}, _StateName, St
|
|||||||
false ->
|
false ->
|
||||||
lists:sublist(L3, 1, Limit)
|
lists:sublist(L3, 1, Limit)
|
||||||
end,
|
end,
|
||||||
{keep_state, State, [{reply, From, L4}]};
|
{keep_state, State, [{reply, From, {ok, L4}}]};
|
||||||
|
|
||||||
%% 处理授权
|
%% 处理授权
|
||||||
handle_event(cast, {auth, Auth}, StateName, State = #state{device_uuid = DeviceUUID}) ->
|
handle_event(cast, {auth, Auth}, StateName, State = #state{device_uuid = DeviceUUID}) ->
|
||||||
|
|||||||
@ -82,9 +82,8 @@ chunks0([Hd | Tail], Size, Num, Target, AccTarget) ->
|
|||||||
chunks0(Tail, Size, Num - 1, [Hd | Target], AccTarget).
|
chunks0(Tail, Size, Num - 1, [Hd | Target], AccTarget).
|
||||||
|
|
||||||
json_data(Data) ->
|
json_data(Data) ->
|
||||||
jiffy:encode(#{
|
Json = jiffy:encode(#{<<"result">> => Data}, [force_utf8]),
|
||||||
<<"result">> => Data
|
iolist_to_binary(Json).
|
||||||
}, [force_utf8]).
|
|
||||||
|
|
||||||
json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) ->
|
json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) ->
|
||||||
jiffy:encode(#{
|
jiffy:encode(#{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user