|
|
@@ -9,6 +9,7 @@
|
|
|
-include_lib("emqx/include/logger.hrl").
|
|
|
-include_lib("hocon/include/hoconsc.hrl").
|
|
|
-include_lib("snabbkaffe/include/snabbkaffe.hrl").
|
|
|
+-include_lib("emqx_resource/include/emqx_resource.hrl").
|
|
|
|
|
|
%% `emqx_resource' API
|
|
|
-export([
|
|
|
@@ -66,6 +67,7 @@
|
|
|
-type manager_id() :: binary().
|
|
|
|
|
|
-define(CONNECTOR_TYPE, iotdb).
|
|
|
+-define(IOTDB_PING_PATH, <<"ping">>).
|
|
|
|
|
|
-import(hoconsc, [mk/2, enum/1, ref/2]).
|
|
|
|
|
|
@@ -237,9 +239,27 @@ on_stop(InstanceId, State) ->
|
|
|
Res.
|
|
|
|
|
|
-spec on_get_status(manager_id(), state()) ->
|
|
|
- {connected, state()} | {disconnected, state(), term()}.
|
|
|
-on_get_status(InstanceId, State) ->
|
|
|
- emqx_bridge_http_connector:on_get_status(InstanceId, State).
|
|
|
+ connected | connecting | {disconnected, state(), term()}.
|
|
|
+on_get_status(InstanceId, #{base_path := BasePath} = State) ->
|
|
|
+ Func = fun(Worker, Timeout) ->
|
|
|
+ Request = {?IOTDB_PING_PATH, [], undefined},
|
|
|
+ NRequest = emqx_bridge_http_connector:formalize_request(get, BasePath, Request),
|
|
|
+ Result0 = ehttpc:request(Worker, get, NRequest, Timeout),
|
|
|
+ case emqx_bridge_http_connector:transform_result(Result0) of
|
|
|
+ {ok, 200, _, Body} ->
|
|
|
+ case emqx_utils_json:decode(Body) of
|
|
|
+ #{<<"code">> := 200} ->
|
|
|
+ ok;
|
|
|
+ Json ->
|
|
|
+ {error, {unexpected_status, Json}}
|
|
|
+ end;
|
|
|
+ {error, _} = Error ->
|
|
|
+ Error;
|
|
|
+ Result ->
|
|
|
+ {error, {unexpected_ping_result, Result}}
|
|
|
+ end
|
|
|
+ end,
|
|
|
+ emqx_bridge_http_connector:on_get_status(InstanceId, State, Func).
|
|
|
|
|
|
-spec on_query(manager_id(), {send_message, map()}, state()) ->
|
|
|
{ok, pos_integer(), [term()], term()}
|
|
|
@@ -352,12 +372,12 @@ on_remove_channel(InstanceId, #{channels := Channels} = OldState0, ChannelId) ->
|
|
|
on_get_channels(InstanceId) ->
|
|
|
emqx_bridge_v2:get_channels_for_connector(InstanceId).
|
|
|
|
|
|
-on_get_channel_status(_InstanceId, ChannelId, #{channels := Channels}) ->
|
|
|
- case maps:is_key(ChannelId, Channels) of
|
|
|
- true ->
|
|
|
- connected;
|
|
|
+on_get_channel_status(InstanceId, _ChannelId, State) ->
|
|
|
+ case on_get_status(InstanceId, State) of
|
|
|
+ ?status_connected ->
|
|
|
+ ?status_connected;
|
|
|
_ ->
|
|
|
- {error, not_exists}
|
|
|
+ ?status_disconnected
|
|
|
end.
|
|
|
|
|
|
%%--------------------------------------------------------------------
|