diff --git a/apps/iot/src/http_handler/device_handler.erl b/apps/iot/src/http_handler/device_handler.erl index 3c4308c..d5dcf30 100644 --- a/apps/iot/src/http_handler/device_handler.erl +++ b/apps/iot/src/http_handler/device_handler.erl @@ -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">>)}. \ No newline at end of file diff --git a/apps/iot/src/iot_device.erl b/apps/iot/src/iot_device.erl index 8c0cafc..08debcf 100644 --- a/apps/iot/src/iot_device.erl +++ b/apps/iot/src/iot_device.erl @@ -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}) -> diff --git a/apps/iot/src/iot_util.erl b/apps/iot/src/iot_util.erl index 026816e..63e7196 100644 --- a/apps/iot/src/iot_util.erl +++ b/apps/iot/src/iot_util.erl @@ -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(#{