上报设备状态
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}};
|
||||
false ->
|
||||
{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]),
|
||||
{ok, ?STATE_DENIED, #state{device_uuid = DeviceUUID, status = ?DEVICE_OFFLINE}}
|
||||
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}) ->
|
||||
{ok, _} = device_bo:change_status(DeviceUUID, NewStatus),
|
||||
report_event(DeviceUUID, NewStatus),
|
||||
{keep_state, State#state{status = NewStatus}};
|
||||
|
||||
%% 重新加载数据库数据
|
||||
@ -173,4 +175,19 @@ code_change(_OldVsn, StateName, State = #state{}, _Extra) ->
|
||||
|
||||
%%%===================================================================
|
||||
%%% 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]),
|
||||
case catch jiffy:decode(EventText, [return_maps]) of
|
||||
#{<<"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),
|
||||
iot_device:change_status(DevicePid, Status);
|
||||
Event when is_map(Event) ->
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user