This commit is contained in:
anlicheng 2023-08-16 12:37:39 +08:00
parent feebc50793
commit d9b94af0de
2 changed files with 11 additions and 13 deletions

View File

@ -56,6 +56,8 @@ is_activated(Pid) when is_pid(Pid) ->
gen_statem:call(Pid, is_activated).
-spec change_status(Pid :: pid(), NewStatus :: integer()) -> no_return().
change_status(undefined, _NewStatus) ->
ok;
change_status(Pid, NewStatus) when is_pid(Pid), is_integer(NewStatus) ->
gen_statem:cast(Pid, {change_status, NewStatus}).
@ -75,7 +77,9 @@ stop(Pid) when is_pid(Pid) ->
%% initialize. To ensure a synchronized start-up procedure, this
%% function does not return until Module:init/1 has returned.
start_link(Name, DeviceUUID) when is_atom(Name), is_binary(DeviceUUID) ->
gen_statem:start_link({local, Name}, ?MODULE, [DeviceUUID], []).
gen_statem:start_link({local, Name}, ?MODULE, [DeviceUUID], []);
start_link(Name, DeviceInfo) when is_atom(Name), is_map(DeviceInfo) ->
gen_statem:start_link({local, Name}, ?MODULE, [DeviceInfo], []).
%%%===================================================================
%%% gen_statem callbacks

View File

@ -133,8 +133,6 @@ start_link(Name, UUID) when is_atom(Name), is_binary(UUID) ->
%% gen_statem:start_link/[3,4], this function is called by the new
%% process to initialize.
init([UUID]) ->
erlang:process_flag(trap_exit, true),
%% 线;
{ok, _} = host_bo:change_status(UUID, ?HOST_OFFLINE),
@ -373,16 +371,12 @@ handle_event(cast, {handle, {feedback_result, Info0}}, session, State = #state{a
end,
{keep_state, State};
handle_event(cast, {handle, {event, Event0}}, session, State = #state{aes = AES, devices = Devices}) ->
handle_event(cast, {handle, {event, Event0}}, session, State = #state{aes = AES}) ->
EventText = iot_cipher_aes:decrypt(AES, Event0),
case catch jiffy:decode(EventText, [return_maps]) of
#{<<"event_type">> := ?EVENT_DEVICE, <<"params">> := #{<<"device_uuid">> := DeviceUUID, <<"status">> := Status}} ->
case lists:member(DeviceUUID, Devices) of
undefined ->
ok;
Pid ->
iot_device:change_status(Pid, Status)
end;
DevicePid = iot_device:get_pid(DeviceUUID),
iot_device:change_status(DevicePid, Status);
Event when is_map(Event) ->
lager:warning("[iot_host] event: ~p, not supported", [Event]);
Other ->
@ -434,8 +428,8 @@ handle_data(#{<<"device_uuid">> := DeviceUUID, <<"service_name">> := ServiceName
case iot_device:get_pid(DeviceUUID) of
undefined ->
ok;
Pid ->
case iot_device:is_activated(Pid) of
DevicePid when is_pid(DevicePid) ->
case iot_device:is_activated(DevicePid) of
true ->
%%
iot_router:route_uuid(DeviceUUID, FieldsList, Timestamp),
@ -444,7 +438,7 @@ handle_data(#{<<"device_uuid">> := DeviceUUID, <<"service_name">> := ServiceName
NTags = Tags#{<<"uuid">> => UUID, <<"service_name">> => ServiceName, <<"device_uuid">> => DeviceUUID},
influx_client:write_data(DeviceUUID, NTags, FieldsList, Timestamp),
iot_device:change_status(Pid, 1);
iot_device:change_status(DevicePid, 1);
false ->
lager:notice("[iot_host] uuid: ~p, device_uuid: ~p, device is not activated", [UUID, DeviceUUID])
end