diff --git a/src/device/router_device_cache.erl b/src/device/router_device_cache.erl index e2e18e44f..c0280b179 100644 --- a/src/device/router_device_cache.erl +++ b/src/device/router_device_cache.erl @@ -72,28 +72,33 @@ all_devaddrs_tables() -> save(Device) -> DeviceID = router_device:id(Device), true = ets:insert(?ETS, {DeviceID, Device}), - _ = erlang:spawn_monitor(fun() -> - AddrsEtsMap = maps:from_list(all_devaddrs_tables()), - CurrentDevaddrs = router_device:devaddrs(Device), - - %% Build a list of all DevAddrs we can know about. - AllAddrs = lists:usort(maps:keys(AddrsEtsMap) ++ CurrentDevaddrs), - - lists:foreach( - fun(Addr) -> - EtsRef = - case maps:get(Addr, AddrsEtsMap, undefined) of - undefined -> make_devaddr_table(Addr); - Ref -> Ref - end, - case lists:member(Addr, CurrentDevaddrs) of - true -> ets:insert(EtsRef, Device); - false -> ets:delete(EtsRef, DeviceID) - end - end, - AllAddrs - ) - end), + _ = erlang:spawn_opt( + fun() -> + AddrsEtsMap = maps:from_list(all_devaddrs_tables()), + CurrentDevaddrs = router_device:devaddrs(Device), + + %% Build a list of all DevAddrs we can know about. + AllAddrs = lists:usort(maps:keys(AddrsEtsMap) ++ CurrentDevaddrs), + + lists:foreach( + fun(Addr) -> + EtsRef = + case maps:get(Addr, AddrsEtsMap, undefined) of + undefined -> make_devaddr_table(Addr); + Ref -> Ref + end, + case lists:member(Addr, CurrentDevaddrs) of + true -> ets:insert(EtsRef, Device); + false -> ets:delete(EtsRef, DeviceID) + end + end, + AllAddrs + ) + end, + [ + {monitor, [{tag, {'DOWN', device_save}}]} + ] + ), {ok, Device}. -spec delete(binary()) -> ok. diff --git a/src/device/router_device_worker.erl b/src/device/router_device_worker.erl index 1d9de24ef..f581a56b7 100644 --- a/src/device/router_device_worker.erl +++ b/src/device/router_device_worker.erl @@ -1210,6 +1210,12 @@ handle_info( fcnt = FCnt }} end; +handle_info({{'DOWN', device_save}, _Ref, process, _Pid, normal}, State) -> + lager:debug("device saved"), + {noreply, State}; +handle_info({{'DOWN', device_save}, _Ref, process, _Pid, Reason}, State) -> + lager:error("device failed to save ~p", [Reason]), + {noreply, State}; handle_info(_Msg, State) -> lager:warning("rcvd unknown info msg: ~p", [_Msg]), {noreply, State}.