diff --git a/apps/iot/src/iot_issue.erl b/apps/iot/src/iot_issue.erl index 7091473..08b1c07 100644 --- a/apps/iot/src/iot_issue.erl +++ b/apps/iot/src/iot_issue.erl @@ -14,6 +14,7 @@ %% API -export([start_link/1]). +-export([get_pid/1]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). @@ -21,16 +22,20 @@ -define(SERVER, ?MODULE). -record(state, { - issue :: #issue{} + issue :: #issue{}, + timer_ref }). %%%=================================================================== %%% API %%%=================================================================== + +get_pid(IssueId) when is_integer(IssueId) -> + whereis(get_name(IssueId)). + get_name(IssueId) when is_integer(IssueId) -> list_to_atom("iot_issue:" ++ integer_to_list(IssueId)). - %% @doc Spawns the server and registers the local name (unique) -spec(start_link(Issue :: #issue{}) -> {ok, Pid :: pid()} | ignore | {error, Reason :: term()}). @@ -47,9 +52,18 @@ start_link(Issue = #issue{issue_id = IssueId}) -> -spec(init(Args :: term()) -> {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} | {stop, Reason :: term()} | ignore). -init([Issue]) -> - lager:debug("iot_issue started!!"), - {ok, #state{issue = Issue}}. +init([Issue = #issue{timeout = Timeout0, hosts = Hosts}]) -> + lager:debug("iot_issue started!!: ~p", [Issue]), + %% 启动任务定时器, 默认最长为1个小时 + Timeout = if Timeout0 > 0 -> Timeout0; true -> 3600 end, + TimerRef = erlang:start_timer(Timeout * 1000, self(), issue_task_timeout), + + %% TODO 下发数据到主机 + lists:map(fun(Host) -> + iot_emqtt_client:publish(Host, x, 1) + end, Hosts), + + {ok, #state{issue = Issue, timer_ref = TimerRef}}. %% @private %% @doc Handling call messages