增加设备数据查询接口

This commit is contained in:
anlicheng 2024-06-21 01:31:51 +08:00
parent c8c95b68ce
commit c8142256e8
3 changed files with 34 additions and 7 deletions

View File

@ -63,6 +63,32 @@ handle_request("POST", "/device/activate", _, #{<<"host_id">> := HostId, <<"devi
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, _, _) ->
Path1 = list_to_binary(Path),
{ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}.

View File

@ -55,8 +55,9 @@ serialize(FieldsList) when is_list(FieldsList) ->
lists:flatmap(fun serialize0/1, FieldsList).
serialize0(Fields = #{<<"key">> := Key}) when is_binary(Key) andalso Key /= <<>> ->
Values = maps:remove(<<"key">>, Fields),
S = base64:encode(iolist_to_binary(jiffy:encode(#{Key => Values}, [force_utf8]))),
[<<"base64:", S/binary>>];
%S = base64:encode(iolist_to_binary(jiffy:encode(#{Key => Values}, [force_utf8]))),
%[<<"base64:", S/binary>>];
[#{Key => Values}];
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) ->
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}).
%% @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 ->
lists:sublist(L3, 1, Limit)
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}) ->

View File

@ -82,9 +82,8 @@ chunks0([Hd | Tail], Size, Num, Target, AccTarget) ->
chunks0(Tail, Size, Num - 1, [Hd | Target], AccTarget).
json_data(Data) ->
jiffy:encode(#{
<<"result">> => Data
}, [force_utf8]).
Json = jiffy:encode(#{<<"result">> => Data}, [force_utf8]),
iolist_to_binary(Json).
json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) ->
jiffy:encode(#{