feat:设备地址解析

This commit is contained in:
h7ml 2022-07-04 16:03:04 +08:00
parent 239bcf921f
commit a32e1c3bf7
3 changed files with 42 additions and 23 deletions

View File

@ -24,7 +24,7 @@
-export([create_device/1, create_device/2, get_sub_device/1, get_sub_device/2, save_subdevice/2, get_subdevice/2]).
-export([parse_cache_Device/1, sync_parse/1, get/2, post/1, post/2, put/1, save/1, save/2, lookup/1, lookup/2, delete/1, delete/2]).
-export([save_profile/1, get_profile/1, get_profile/2, get_online/1, online/1, offline/1, offline_child/1, enable/1, disable/1]).
-export([put_location/3, get_location/1, get_address/1]).
-export([put_location/3, get_location/1, get_address/2]).
-export([get_acl/1, save_log/3, get_url/1, get_appname/1]).
parse_cache_Device(_ClasseName) ->
@ -96,8 +96,8 @@ put_location(DeviceId, Longitude, Latitude) ->
get_location(DeviceId) ->
dgiot_device_cache:get_location(DeviceId).
get_address(DeviceId) ->
dgiot_device_cache:get_address(DeviceId).
get_address(Lon, Lat) ->
dgiot_device_cache:get_address(Lon, Lat).
save_subdevice({ProductId, DevAddr}, {DtuAddr, SlaveId}) ->
dgiot_device_cache:save_subdevice({ProductId, DevAddr}, {DtuAddr, SlaveId}).

View File

@ -108,6 +108,7 @@ init(?TYPE, ChannelId, #{<<"offline">> := OffLine} = Args) ->
},
dgiot_data:insert({device, offline}, OffLine),
dgiot_parse_hook:subscribe(<<"Device">>, get, ChannelId),
dgiot_parse_hook:subscribe(<<"Device/*">>, get, ChannelId),
dgiot_parse_hook:subscribe(<<"Device">>, post, ChannelId),
dgiot_parse_hook:subscribe(<<"Device/*">>, put, ChannelId, [<<"isEnable">>]),
dgiot_parse_hook:subscribe(<<"Device/*">>, delete, ChannelId),
@ -147,7 +148,13 @@ handle_message({sync_parse, Pid, 'after', get, Token, <<"Device">>, #{<<"results
_ ->
<<"OFFLINE">>
end,
Location = #{<<"__type">> => <<"GeoPoint">>, <<"longitude">> => Longitude, <<"latitude">> => Latitude},
Location =
case dgiot_device:lookup(DeviceId) of
{ok, #{<<"longitude">> := Bd_lng, <<"latitude">> := Bd_lat}} ->
#{<<"__type">> => <<"GeoPoint">>, <<"longitude">> => Bd_lng, <<"latitude">> => Bd_lat};
_ ->
#{<<"__type">> => <<"GeoPoint">>, <<"longitude">> => Longitude, <<"latitude">> => Latitude}
end,
{NewResult ++ [Device#{<<"location">> => Location, <<"status">> => NewStatus, <<"isEnable">> => IsEnable, <<"lastOnlineTime">> => Time}], Dev ++ [DeviceId]};
_ ->
{NewResult ++ [Device], Dev}
@ -162,6 +169,25 @@ handle_message({sync_parse, Pid, 'after', get, Token, <<"Device">>, #{<<"results
dgiot_parse_hook:publish(Pid, ResBody#{<<"results">> => NewResults}),
{ok, State};
handle_message({sync_parse, Pid, 'after', get, _Token, <<"Device">>, #{<<"objectId">> := ObjectId} = ResBody}, State) ->
ResBody1 =
case ResBody of
#{<<"location">> := Location1} ->
Location =
case dgiot_device:lookup(ObjectId) of
{ok, #{<<"longitude">> := Bd_lng, <<"latitude">> := Bd_lat}} ->
#{<<"__type">> => <<"GeoPoint">>, <<"longitude">> => Bd_lng, <<"latitude">> => Bd_lat};
_ ->
Location1
end,
ResBody#{<<"location">> => Location};
_ ->
ResBody
end,
dgiot_parse_hook:publish(Pid, ResBody1),
{ok, State};
handle_message({sync_parse, _Pid, 'after', post, Token, <<"Device">>, QueryData}, State) ->
dgiot_device:post(QueryData, Token),
{ok, State};

View File

@ -19,11 +19,9 @@
-include("dgiot_device.hrl").
-include_lib("dgiot/include/logger.hrl").
-include_lib("dgiot_tdengine/include/dgiot_tdengine.hrl").
-export([parse_cache_Device/1, sync_parse/1, post/1, post/2, put/1, save/1, save/2, save_subdevice/2, get_subdevice/2, lookup/1, lookup/2, delete/1, delete/2]).
-export([get_profile/1, get_profile/2, get_online/1, online/1, offline/1, offline_child/1, enable/1, disable/1, save_profile/1]).
-export([location/3, get_location/1, get_address/1]).
-export([location/3, get_location/1, get_address/2]).
%% Device 线线
parse_cache_Device(_ClassName) ->
@ -182,16 +180,16 @@ insert_mnesia(DeviceId, Acl, Status, Now, IsEnable, ProductId, Devaddr, DeviceSe
_ ->
<<"OFFLINE">>
end,
Address =
{Address, Bd_lng, Bd_lat} =
case dgiot_gps:get_baidu_addr(Longitude, Latitude) of
#{<<"baiduaddr">> := #{<<"formatted_address">> := FormattedAddress}} ->
FormattedAddress;
#{<<"baiduaddr">> := #{<<"formatted_address">> := FormattedAddress, <<"location">> := #{<<"lng">> := Bd_lng1, <<"lat">> := Bd_lat1}}} ->
{FormattedAddress, Bd_lng1, Bd_lat1};
_ ->
<<>>
{<<>>, <<>>, <<>>}
end,
dgiot_mqtt:publish(DeviceId, Topic, jsx:encode(#{DeviceId => #{<<"status">> => NewStatus, <<"isEnable">> => IsEnable, <<"lastOnlineTime">> => Now, <<"address">> => Address, <<"longitude">> => Longitude, <<"latitude">> => Latitude}})),
dgiot_mqtt:publish(DeviceId, Topic, jsx:encode(#{DeviceId => #{<<"status">> => NewStatus, <<"isEnable">> => IsEnable, <<"lastOnlineTime">> => Now, <<"address">> => Address, <<"longitude">> => Bd_lng, <<"latitude">> => Bd_lat}, <<"location">> => #{<<"__type">> => <<"GeoPoint">>, <<"longitude">> => Bd_lng, <<"latitude">> => Bd_lat}})),
%% io:format("~s ~p Data = ~ts~n", [?FILE, ?LINE, jsx:encode(#{DeviceId => #{<<"status">> => NewStatus, <<"isEnable">> => IsEnable, <<"lastOnlineTime">> => Now, <<"address">> => Address}})]),
dgiot_mnesia:insert(DeviceId, ['Device', Acl, Status, Now, IsEnable, dgiot_utils:to_atom(ProductId), Devaddr, DeviceSecret, Node, Longitude, Latitude]).
dgiot_mnesia:insert(DeviceId, ['Device', Acl, Status, Now, IsEnable, dgiot_utils:to_atom(ProductId), Devaddr, DeviceSecret, Node, Bd_lng, Bd_lat]).
%% profile配置
save_profile(#{<<"objectId">> := DeviceId, <<"profile">> := Profile, <<"product">> := #{<<"objectId">> := ProductId}}) ->
@ -272,16 +270,11 @@ get_location(DeviceId) ->
#{}
end.
get_address(DeviceId) ->
case lookup(DeviceId) of
{ok, #{<<"longitude">> := LonDeg, <<"latitude">> := LatDeg}} ->
Address = dgiot_gps:get_baidu_addr(LonDeg, LatDeg),
case Address of
#{<<"baiduaddr">> := #{<<"formatted_address">> := Formatted_address}} ->
Formatted_address;
_ ->
<<"">>
end;
get_address(Lon, Lat) ->
Address = dgiot_gps:get_baidu_addr(Lon, Lat),
case Address of
#{<<"baiduaddr">> := #{<<"formatted_address">> := Formatted_address}} ->
Formatted_address;
_ ->
<<"">>
end.