  Copyright ( c ) 2020 - 2021 DGIOT Technologies Co. , Ltd. All Rights Reserved .
%%
  Licensed under the Apache License , Version 2.0 ( the " License " ) ;
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     -2.0
%%
%% Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an " AS IS " BASIS ,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(dashboard_worker).
-author("johnliu").
-include("dgiot_topo.hrl").
-include_lib("dgiot/include/logger.hrl").
-behaviour(gen_server).

%% API
-export([start_link/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2,
    handle_info/2, terminate/2, code_change/3, stop/1]).

%%%===================================================================
%%% API
%%%===================================================================

start_link(#{<<"sessionToken">> := SessionToken} = State) ->
    case dgiot_data:lookup({dashboard, SessionToken}) of
        {ok, Pid} when is_pid(Pid) ->
            case is_process_alive(Pid) of
                true ->
                    ok;
                false ->
                    gen_server:start_link(?MODULE, [State], [])
            end;
        _Reason ->
            gen_server:start_link(?MODULE, [State], [])
    end;

start_link(_State) ->
    ok.

stop(#{<<"sessionToken">> := SessionToken}) ->
    case dgiot_data:lookup({dashboard, SessionToken}) of
        {ok, Pid} when is_pid(Pid) ->
            is_process_alive(Pid) andalso gen_server:call(Pid, stop, 5000);
        _Reason ->
            ok
    end.

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
init([#{<<"data">> := Que, <<"dashboardId">> := DashboardId, <<"sessionToken">> := SessionToken}]) ->
    dgiot_data:insert({dashboard, SessionToken}, self()),
    case length(Que) of
        0 ->
            erlang:send_after(3000, self(), stop);
        _ ->
            erlang:send_after(1000, self(), dashboard)
    end,
    {ok, #task{oldque = Que, newque = Que, freq = 1, dashboardId = DashboardId, sessiontoken = SessionToken}};

init([#{<<"dashboardId">> := DashboardId, <<"sessionToken">> := SessionToken}]) ->
    dgiot_data:insert({dashboard, SessionToken}, self()),
    Que = dgiot_topo:get_que(DashboardId),
    case length(Que) of
        0 ->
            erlang:send_after(3000, self(), stop);
        _ ->
            erlang:send_after(1000, self(), topo)
    end,
    {ok, #task{oldque = Que, newque = Que, freq = 1, sessiontoken = SessionToken}};

init(A) ->
    ?LOG(info, "A ~p ", [A]).

handle_call(stop, _From, State) ->
    erlang:garbage_collect(self()),
    {stop, normal, ok, State};

handle_call(_Request, _From, State) ->
    {reply, noreply, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.

handle_info({'EXIT', _From, Reason}, State) ->
    erlang:garbage_collect(self()),
    {stop, Reason, State};

%% 任务结束
handle_info(dashboard, #task{newque = Que} = State) when length(Que) == 0 ->
    erlang:garbage_collect(self()),
    {stop, normal, State};

handle_info(dashboard, State) ->
    {noreply, send_dashboard(State)};

%% 任务结束
handle_info(topo, #task{newque = Que} = State) when length(Que) == 0 ->
    erlang:garbage_collect(self()),
    {stop, normal, State};

handle_info(topo, State) ->
    {noreply, send_topo(State)};

%% 任务结束
handle_info({deliver, _, _Msg}, State) ->
    {noreply, State#task{heart = 0}};

handle_info(_Msg, State) ->
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

send_dashboard(#task{newque = Que} = State) ->
    Task = lists:nth(1, Que),
    dgiot_dashboard:do_task(Task, State),
    NewQue = lists:nthtail(1, Que),
    erlang:send_after(3000, self(), dashboard),
    State#task{newque = NewQue}.

send_topo(#task{newque = Que, sessiontoken = Token} = State) ->
    Task = lists:nth(1, Que),
    dgiot_topo:send_topo(Task, Token),
    NewQue = lists:nthtail(1, Que)  ++ [Task],
    erlang:send_after(3000, self(), topo),
    State#task{newque = NewQue}.


 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     -2.0

 Unless required by applicable law or agreed to in writing, software
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
--------------------------------------------------------------------
 API
 gen_server callbacks
===================================================================
 API
===================================================================
===================================================================
 gen_server callbacks
===================================================================
 任务结束
 任务结束
 任务结束
  Copyright ( c ) 2020 - 2021 DGIOT Technologies Co. , Ltd. All Rights Reserved .
  Licensed under the Apache License , Version 2.0 ( the " License " ) ;
  distributed under the License is distributed on an " AS IS " BASIS ,
-module(dashboard_worker).
-author("johnliu").
-include("dgiot_topo.hrl").
-include_lib("dgiot/include/logger.hrl").
-behaviour(gen_server).

-export([start_link/1]).

-export([init/1, handle_call/3, handle_cast/2,
    handle_info/2, terminate/2, code_change/3, stop/1]).


start_link(#{<<"sessionToken">> := SessionToken} = State) ->
    case dgiot_data:lookup({dashboard, SessionToken}) of
        {ok, Pid} when is_pid(Pid) ->
            case is_process_alive(Pid) of
                true ->
                    ok;
                false ->
                    gen_server:start_link(?MODULE, [State], [])
            end;
        _Reason ->
            gen_server:start_link(?MODULE, [State], [])
    end;

start_link(_State) ->
    ok.

stop(#{<<"sessionToken">> := SessionToken}) ->
    case dgiot_data:lookup({dashboard, SessionToken}) of
        {ok, Pid} when is_pid(Pid) ->
            is_process_alive(Pid) andalso gen_server:call(Pid, stop, 5000);
        _Reason ->
            ok
    end.

init([#{<<"data">> := Que, <<"dashboardId">> := DashboardId, <<"sessionToken">> := SessionToken}]) ->
    dgiot_data:insert({dashboard, SessionToken}, self()),
    case length(Que) of
        0 ->
            erlang:send_after(3000, self(), stop);
        _ ->
            erlang:send_after(1000, self(), dashboard)
    end,
    {ok, #task{oldque = Que, newque = Que, freq = 1, dashboardId = DashboardId, sessiontoken = SessionToken}};

init([#{<<"dashboardId">> := DashboardId, <<"sessionToken">> := SessionToken}]) ->
    dgiot_data:insert({dashboard, SessionToken}, self()),
    Que = dgiot_topo:get_que(DashboardId),
    case length(Que) of
        0 ->
            erlang:send_after(3000, self(), stop);
        _ ->
            erlang:send_after(1000, self(), topo)
    end,
    {ok, #task{oldque = Que, newque = Que, freq = 1, sessiontoken = SessionToken}};

init(A) ->
    ?LOG(info, "A ~p ", [A]).

handle_call(stop, _From, State) ->
    erlang:garbage_collect(self()),
    {stop, normal, ok, State};

handle_call(_Request, _From, State) ->
    {reply, noreply, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.

handle_info({'EXIT', _From, Reason}, State) ->
    erlang:garbage_collect(self()),
    {stop, Reason, State};

handle_info(dashboard, #task{newque = Que} = State) when length(Que) == 0 ->
    erlang:garbage_collect(self()),
    {stop, normal, State};

handle_info(dashboard, State) ->
    {noreply, send_dashboard(State)};

handle_info(topo, #task{newque = Que} = State) when length(Que) == 0 ->
    erlang:garbage_collect(self()),
    {stop, normal, State};

handle_info(topo, State) ->
    {noreply, send_topo(State)};

handle_info({deliver, _, _Msg}, State) ->
    {noreply, State#task{heart = 0}};

handle_info(_Msg, State) ->
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

send_dashboard(#task{newque = Que} = State) ->
    Task = lists:nth(1, Que),
    dgiot_dashboard:do_task(Task, State),
    NewQue = lists:nthtail(1, Que),
    erlang:send_after(3000, self(), dashboard),
    State#task{newque = NewQue}.

send_topo(#task{newque = Que, sessiontoken = Token} = State) ->
    Task = lists:nth(1, Que),
    dgiot_topo:send_topo(Task, Token),
    NewQue = lists:nthtail(1, Que)  ++ [Task],
    erlang:send_after(3000, self(), topo),
    State#task{newque = NewQue}.
