上报设备状态
This commit is contained in:
parent
2465fa5bab
commit
73cab5ae09
@ -95,6 +95,7 @@ init0(#{<<"device_uuid">> := DeviceUUID, <<"status">> := Status, <<"authorize_st
|
|||||||
{ok, ?STATE_ACTIVATED, #state{device_uuid = DeviceUUID, status = Status}};
|
{ok, ?STATE_ACTIVATED, #state{device_uuid = DeviceUUID, status = Status}};
|
||||||
false ->
|
false ->
|
||||||
{ok, _} = device_bo:change_status(DeviceUUID, ?DEVICE_OFFLINE),
|
{ok, _} = device_bo:change_status(DeviceUUID, ?DEVICE_OFFLINE),
|
||||||
|
report_event(DeviceUUID, ?DEVICE_OFFLINE),
|
||||||
lager:debug("[iot_device] started device: ~p, state_name: ~p, status: ~p", [DeviceUUID, ?STATE_DENIED, ?DEVICE_OFFLINE]),
|
lager:debug("[iot_device] started device: ~p, state_name: ~p, status: ~p", [DeviceUUID, ?STATE_DENIED, ?DEVICE_OFFLINE]),
|
||||||
{ok, ?STATE_DENIED, #state{device_uuid = DeviceUUID, status = ?DEVICE_OFFLINE}}
|
{ok, ?STATE_DENIED, #state{device_uuid = DeviceUUID, status = ?DEVICE_OFFLINE}}
|
||||||
end.
|
end.
|
||||||
@ -120,6 +121,7 @@ handle_event(cast, {change_status, ?DEVICE_ONLINE}, ?STATE_ACTIVATED, State = #s
|
|||||||
%% 改变数据库的状态, 其他情况下执行次数都很少
|
%% 改变数据库的状态, 其他情况下执行次数都很少
|
||||||
handle_event(cast, {change_status, NewStatus}, _, State = #state{device_uuid = DeviceUUID}) ->
|
handle_event(cast, {change_status, NewStatus}, _, State = #state{device_uuid = DeviceUUID}) ->
|
||||||
{ok, _} = device_bo:change_status(DeviceUUID, NewStatus),
|
{ok, _} = device_bo:change_status(DeviceUUID, NewStatus),
|
||||||
|
report_event(DeviceUUID, NewStatus),
|
||||||
{keep_state, State#state{status = NewStatus}};
|
{keep_state, State#state{status = NewStatus}};
|
||||||
|
|
||||||
%% 重新加载数据库数据
|
%% 重新加载数据库数据
|
||||||
@ -174,3 +176,18 @@ code_change(_OldVsn, StateName, State = #state{}, _Extra) ->
|
|||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
%%% Internal functions
|
%%% Internal functions
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
|
|
||||||
|
-spec report_event(DeviceUUID :: binary(), NewStatus :: integer()) -> no_return().
|
||||||
|
report_event(DeviceUUID, NewStatus) when is_binary(DeviceUUID), is_integer(NewStatus) ->
|
||||||
|
%% 设备的状态信息上报给中电
|
||||||
|
Timestamp = iot_util:timestamp_of_seconds(),
|
||||||
|
FieldsList = [#{
|
||||||
|
<<"key">> => <<"device_status">>,
|
||||||
|
<<"value">> => NewStatus,
|
||||||
|
<<"unit">> => <<"">>,
|
||||||
|
<<"type">> => <<"SOE">>,
|
||||||
|
<<"name">> => <<"设备状态"/utf8>>,
|
||||||
|
<<"timestamp">> => Timestamp
|
||||||
|
}],
|
||||||
|
iot_router:route(DeviceUUID, FieldsList, Timestamp),
|
||||||
|
lager:debug("[iot_host] device_uuid: ~p, route fields: ~p", [DeviceUUID, FieldsList]).
|
||||||
@ -412,19 +412,6 @@ handle_cast({handle, {event, Event0}}, State = #state{uuid = UUID, aes = AES, ha
|
|||||||
lager:debug("[iot_host] uuid: ~p, get event: ~p", [UUID, EventText]),
|
lager:debug("[iot_host] uuid: ~p, get event: ~p", [UUID, EventText]),
|
||||||
case catch jiffy:decode(EventText, [return_maps]) of
|
case catch jiffy:decode(EventText, [return_maps]) of
|
||||||
#{<<"event_type">> := ?EVENT_DEVICE, <<"params">> := #{<<"device_uuid">> := DeviceUUID, <<"status">> := Status}} ->
|
#{<<"event_type">> := ?EVENT_DEVICE, <<"params">> := #{<<"device_uuid">> := DeviceUUID, <<"status">> := Status}} ->
|
||||||
%% 设备的状态信息上报给中电
|
|
||||||
Timestamp = iot_util:timestamp_of_seconds(),
|
|
||||||
FieldsList = [#{
|
|
||||||
<<"key">> => <<"device_status">>,
|
|
||||||
<<"value">> => Status,
|
|
||||||
<<"unit">> => <<"">>,
|
|
||||||
<<"type">> => <<"SOE">>,
|
|
||||||
<<"name">> => <<"设备状态"/utf8>>,
|
|
||||||
<<"timestamp">> => Timestamp
|
|
||||||
}],
|
|
||||||
iot_router:route(DeviceUUID, FieldsList, Timestamp),
|
|
||||||
lager:debug("[iot_host] uuid: ~p, route fields: ~p", [UUID, FieldsList]),
|
|
||||||
|
|
||||||
DevicePid = iot_device:get_pid(DeviceUUID),
|
DevicePid = iot_device:get_pid(DeviceUUID),
|
||||||
iot_device:change_status(DevicePid, Status);
|
iot_device:change_status(DevicePid, Status);
|
||||||
Event when is_map(Event) ->
|
Event when is_map(Event) ->
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user