Skip to content

Commit

Permalink
feat:thing_gps
Browse files Browse the repository at this point in the history
  • Loading branch information
AvantLiu committed Sep 9, 2021
1 parent fbcc3db commit fbb2e5b
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 35 deletions.
5 changes: 1 addition & 4 deletions apps/dgiot_api/src/handler/dgiot_system_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,14 @@ do_request(post_trace, #{<<"action">> := Action, <<"tracetype">> := Tracetype, <
<<"topic">> => Handle,
<<"path">> => <<"tracelog/", Handle/binary, ".txt">>
}),
D = emqx_tracer:start_trace({dgiot_utils:to_atom(Tracetype), Handle}, dgiot_utils:to_atom(Level), get_tracelog(<<Handle/binary, ".txt">>)),
?LOG(info, "D ~p", [D]),
D;
emqx_tracer:start_trace({dgiot_utils:to_atom(Tracetype), Handle}, dgiot_utils:to_atom(Level), get_tracelog(<<Handle/binary, ".txt">>));
<<"stop">> ->
LoglevelId = dgiot_parse:get_loglevelid(Handle, <<"trace">>),
dgiot_parse:del_object(<<"LogLevel">>, LoglevelId),
emqx_tracer:stop_trace({dgiot_utils:to_atom(Tracetype), Handle});
_Other ->
{error, _Other}
end,
?LOG(info, "Rtn ~p~n", [Rtn]),
case Rtn of
ok ->
{200, #{<<"code">> => 200, <<"msg">> => <<"SUCCESS">>}};
Expand Down
2 changes: 1 addition & 1 deletion apps/dgiot_http/etc/dgiot_http.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ aliyun.uploadCallbackUrl= http://127.0.0.1:8081
aliyun.uploadHost= http://dgiotpump.oss-cn-shanghai.aliyuncs.com

#百度地图
baidumap.appkey = fnc5Z92jC7CwfBuz8Dk66E9sXEIYZ6TG
baidumap.appkey = fnc5Z92jC7CwfBGz8Dk66E9sXEIYZ6TG


#友盟消息推送
Expand Down
28 changes: 24 additions & 4 deletions apps/dgiot_http/src/baidu/dgiot_gps.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
out_of_china/2,
get_baidu_gps/2,
get_deng_gps/2,
generate_random_gps/3
generate_random_gps/3,
nmea0183_frame/1
]).

%%// 定义一些常量
Expand Down Expand Up @@ -251,9 +252,9 @@ get_baidu_gps(LonDeg, LatDeg) ->
[dgiot_utils:to_float(Bd_lng, 6), dgiot_utils:to_float(Bd_lat - 0.0002, 6)].

%%http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad
%<<"http://api.map.baidu.com/reverse_geocoding/v3/?ak=fnc5Z92jC7CwfBGz8Dk66E9sXEIYZ6TG&output=json&coordtype=wgs84ll&location=31.555,120.455">>.
%<<"http://api.map.baidu.com/reverse_geocoding/v3/?ak=fnc5Z92jC7CwfBGz8Dk66E9sXEIYZ6TG&output=json&coordtype=wgs84ll&location=30.26626,119.60223">>.
get_baidu_addr(LonDeg, LatDeg) ->
AppKey = dgiot:get_env(baidumap_appkey),
AppKey = dgiot_utils:to_binary(application:get_env(dgiot_http, baidumap_appkey, <<"">>)),
get_baidu_addr(AppKey, "wgs84ll", LonDeg, LatDeg).

get_baidu_addr(AK, Coordtype, Lng, Lat) ->
Expand Down Expand Up @@ -294,4 +295,23 @@ generate_random_gps(Base_log, Base_lat, Radius) ->
%% # 这里是想保留14位小数
%% loga = '%.14f' % longitude
%% lata = '%.14f' % latitude
{Longitude, Latitude}.
{Longitude, Latitude}.

%% <<"$GNRMC,", Data:60/binary, "*", _Checksum:2/binary, _/binary>> = <<"$GNRMC,034918.00,A,3015.97544,N,11936.13370,E,000.0,000.0,180821,OK*06">>
nmea0183_frame(<<"$GNRMC,", Data:60/binary, "*", _Checksum:2/binary, _/binary>>) ->
%% <<_Utc:9/binary, ",", _PositioningState:1/binary, ",", Latitude:10/binary, ",", _Latitudedirection:1/binary, ",", Longitude:11/binary, ",", _Longitudedirection:1/binary, ",", Speed:5/binary, ",", Course:5/binary, ",", Date:6/binary, ",", Tianxian:2/binary>> = Data,
<<_Utc:9/binary, ",", _PositioningState:1/binary, ",", LatDu1:2/binary, LatFen1:8/binary, ",", _Latitudedirection:1/binary, ",", LongDu1:3/binary, LongFen1:8/binary, ",", _Longitudedirection:1/binary, _/binary>> = Data,

LatDu = dgiot_utils:to_float(LatDu1),
LatFen = dgiot_utils:to_float(LatFen1),
Latitude = dgiot_utils:to_float(LatDu + LatFen / 60, 5),

LongDu = dgiot_utils:to_float(LongDu1),
LongFen = dgiot_utils:to_float(LongFen1),
Longitude = dgiot_utils:to_float(LongDu + LongFen / 60, 5),

{dgiot_utils:to_binary(Longitude), dgiot_utils:to_binary(Latitude)};

nmea0183_frame(Buff) ->
?LOG(info, "Buff ~p~n", [Buff]),
<<"">>.
2 changes: 1 addition & 1 deletion apps/dgiot_http/src/wechat/dgiot_wechat.erl
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ get_notification(ProductId1, SessionToken, Order, Limit, Skip, Where) ->
_Oth ->
Par
end
end, #{<<"objectId">> => ObjectId, <<"alertstatus">> => Alertstatus, <<"productname">> => ProductName, <<"devicename">> => DeviceName, <<"process">> => Process, <<"public">> => Public, <<"status">> => Status, <<"createdAt">> => Createdat}, Parser);
end, #{<<"objectId">> => ObjectId, <<"alertstatus">> => Alertstatus, <<"productname">> => ProductName, <<"devicename">> => DeviceName, <<"process">> => Process, <<"public">> => Public, <<"status">> => Status, <<"createdAt">> => Newdate}, Parser);
_Other ->
Acc
end;
Expand Down
15 changes: 1 addition & 14 deletions apps/dgiot_modbus/src/dgiot_modbus_channel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,6 @@
zh => <<"控制器厂商"/utf8>>
}
},
<<"heartbeat">> => #{
order => 5,
type => integer,
required => true,
default => 10,
title => #{
zh => <<"心跳周期"/utf8>>
},
description => #{
zh => <<"心跳周期"/utf8>>
}
},
<<"ico">> => #{
order => 102,
type => string,
Expand All @@ -120,7 +108,6 @@ start(ChannelId, ChannelArgs) ->
%% 通道初始化
init(?TYPE, ChannelId, #{
<<"port">> := Port,
<<"heartbeat">> := Heartbeat,
<<"regtype">> := Type,
<<"regular">> := Regular,
<<"product">> := Products,
Expand All @@ -138,7 +125,7 @@ init(?TYPE, ChannelId, #{
dtutype = Dtutype
},

dgiot_data:insert({ChannelId, heartbeat}, {Heartbeat, Port}),
%% dgiot_data:insert({ChannelId, heartbeat}, {Heartbeat, Port}),
{ok, State, dgiot_modbus_tcp:start(Port, State)};

init(?TYPE, _ChannelId, _Args) ->
Expand Down
5 changes: 5 additions & 0 deletions apps/dgiot_modbus/src/modbus/modbus_rtu.erl
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,11 @@ modbus_encoder(ProductId, SlaveId, Address, Value) ->
%% 低地址 --------------------> 高地址
%% 0x78 | 0x56 | 0x34 | 0x12

format_value(Buff, #{
<<"dataType">> := #{<<"type">> := <<"geopoint">>, <<"gpstype">> := <<"NMEA0183">>}}) ->
{Longitude, Latitude} = dgiot_gps:nmea0183_frame(Buff),
{<<Longitude/binary, "_", Latitude/binary>>, <<"Rest">>};

format_value(Buff, #{
<<"accessMode">> := <<"rw">>,
<<"dataForm">> := DataForm} = X) ->
Expand Down
2 changes: 1 addition & 1 deletion apps/dgiot_parse/etc/dgiot_parse.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ parse.delete_field = ACL,objectId,updatedAt,createdAt
##--------------------------------------------------------------------
## parse config
##--------------------------------------------------------------------
parse.parse_server = http://prod.iotn2n.com:1337
parse.parse_server = http://114.117.171.233:1337
parse.parse_path = /parse/
parse.parse_appid = 1uqZbbdd_JMyQ45YLsUzYezMRPerMa80
parse.parse_master_key = PADbN7p973quWLngikp6XvrDbL97u_vM
Expand Down
9 changes: 9 additions & 0 deletions apps/dgiot_task/src/dgiot_task.erl
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ get_collection(ProductId, Dis, Payload, Ack) ->
Acc2;
_ ->
case X of
#{<<"dataForm">> := #{<<"strategy">> := Strategy},
<<"dataType">> := #{<<"type">> := <<"geopoint">>},
<<"identifier">> := Identifier} when Strategy =/= <<"计算值"/utf8>> ->
case maps:find(Identifier, Payload) of
{ok, Value} ->
Acc2#{Identifier => Value};
_ ->
Acc2#{Identifier => <<"">>}
end;
#{<<"dataForm">> := #{<<"address">> := Address, <<"strategy">> := Strategy, <<"collection">> := Collection},
<<"dataType">> := #{<<"type">> := Type, <<"specs">> := Specs},
<<"identifier">> := Identifier} when Strategy =/= <<"计算值"/utf8>> ->
Expand Down
8 changes: 8 additions & 0 deletions apps/dgiot_tdengine/src/dgiot_tdengine.erl
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ alter_table(DB1, TableName, Context, Channel) ->
case Type of
<<"enum">> ->
<<"ALTER TABLE ", DB1/binary, TableName/binary, " ADD COLUMN ", LowerIdentifier/binary, " INT;">>;
<<"file">> ->
<<"ALTER TABLE ", DB1/binary, TableName/binary, " ADD COLUMN ", LowerIdentifier/binary, " NCHAR(10);">>;
<<"text">> ->
<<"ALTER TABLE ", DB1/binary, TableName/binary, " ADD COLUMN ", LowerIdentifier/binary, " NCHAR(10);">>;
<<"url">> ->
<<"ALTER TABLE ", DB1/binary, TableName/binary, " ADD COLUMN ", LowerIdentifier/binary, " NCHAR(10);">>;
<<"geopoint">> ->
<<"ALTER TABLE ", DB1/binary, TableName/binary, " ADD COLUMN ", LowerIdentifier/binary, " NCHAR(20);">>;
_ ->
<<"ALTER TABLE ", DB1/binary, TableName/binary, " ADD COLUMN ", LowerIdentifier/binary, " ", Type/binary, ";">>
end,
Expand Down
22 changes: 14 additions & 8 deletions apps/dgiot_tdengine/src/dgiot_tdengine_channel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -406,20 +406,21 @@ check_init(ChannelId, ProductIds, Config) ->

check_database(ChannelId, ProductIds, #{<<"database">> := DataBase, <<"keep">> := Keep} = Config) ->
case dgiot_tdengine:create_database(ChannelId, DataBase, Keep) of
{ok, _} ->
?LOG(debug, "Check database ChannelId:~p, ProductIds:~p, Config:~p", [ChannelId, ProductIds, Config]),
create_table(ChannelId, ProductIds, Config);
{error, <<"channel not find">>} ->
ok;
{error, #{<<"code">> := 10, <<"desc">> := <<"authentication failure">>}} ->
dgiot_bridge:send_log(ChannelId, "Check database Error, ChannelId:~p, ProductIds:~p, Reason:authentication failure", [ChannelId, ProductIds]),
timer:sleep(5000),
check_database(ChannelId, ProductIds, Config);
{error, Reason} ->
?LOG(error, "Check database Error, ChannelId:~p, ProductIds:~p, Reason:~p", [ChannelId, ProductIds, Reason]),
dgiot_bridge:send_log(ChannelId, "Check database Error, ChannelId:~p, ProductIds:~p, Reason:~p", [ChannelId, ProductIds, Reason]),
timer:sleep(5000),
check_database(ChannelId, ProductIds, Config);
{ok, _} ->
?LOG(debug, "Check database ChannelId:~p, ProductIds:~p, Config:~p", [ChannelId, ProductIds, Config]),
create_table(ChannelId, ProductIds, Config)
_ ->
%% ?LOG(error, "Check database Error, ChannelId:~p, ProductIds:~p, Reason:~p", [ChannelId, ProductIds, Reason]),
%% dgiot_bridge:send_log(ChannelId, "Check database Error, ChannelId:~p, ProductIds:~p, Reason:~p", [ChannelId, ProductIds, Reason]),
%% timer:sleep(5000),
%% check_database(ChannelId, ProductIds, Config)
ok
end.

create_table(_, [], _) ->
Expand Down Expand Up @@ -502,6 +503,9 @@ get_field(#{<<"identifier">> := Field, <<"dataType">> := #{<<"type">> := <<"stri
get_field(#{<<"identifier">> := Field, <<"dataType">> := #{<<"type">> := <<"text">>} = Spec}) ->
Size = integer_to_binary(min(maps:get(<<"size">>, Spec, 10), 200)),
{Field, #{<<"type">> => <<"NCHAR(", Size/binary, ")">>}};
get_field(#{<<"identifier">> := Field, <<"dataType">> := #{<<"type">> := <<"geopoint">>} = Spec}) ->
Size = integer_to_binary(min(maps:get(<<"size">>, Spec, 20), 200)),
{Field, #{<<"type">> => <<"NCHAR(", Size/binary, ")">>}};
get_field(#{<<"identifier">> := Field, <<"dataType">> := #{<<"type">> := <<"enum">>, <<"specs">> := _Specs}}) ->
%% Size = integer_to_binary(maps:size(Specs)),
{Field, #{<<"type">> => <<"INT">>}};
Expand Down Expand Up @@ -568,6 +572,8 @@ check_field(Data, #{<<"identifier">> := Field, <<"dataType">> := #{<<"type">> :=
Value;
<<"TEXT">> ->
{unicode:characters_to_binary(unicode:characters_to_list((Value))), text};
<<"GEOPOINT">> ->
{unicode:characters_to_binary(unicode:characters_to_list((Value))), text};
%% <<"ENUM">> ->
%%%% Specs = maps:get(<<"specs">>, DataType, #{}),
%%%% ?LOG(info, "Specs ~p", [Specs]),
Expand Down
29 changes: 27 additions & 2 deletions apps/dgiot_tdengine/src/handler/dgiot_tdengine_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ get_appdata(Channel, ProductId, DeviceId, _Args) ->
TableName = ?Table(DeviceId),
case dgiot_tdengine:get_appdata(Channel, TableName, #{<<"db">> => ProductId}) of
{ok, #{<<"results">> := Results}} ->
Chartdata = get_app(ProductId, Results),
Chartdata = get_app(ProductId, Results, DeviceId),
{ok, #{<<"data">> => Chartdata}};
{error, Reason} ->
{400, Reason}
Expand Down Expand Up @@ -328,7 +328,7 @@ get_chart(ProductId, Results, Names, Interval) ->
?LOG(debug, "Child ~p", [Child]),
#{<<"columns">> => Columns, <<"rows">> => Rows, <<"child">> => Child}.

get_app(ProductId, Results) ->
get_app(ProductId, Results, DeviceId) ->
Maps = get_prop(ProductId),
Props = get_props(ProductId),
lists:foldl(fun(R, _Acc) ->
Expand All @@ -354,6 +354,31 @@ get_app(ProductId, Results) ->
Type2 when Type2 == <<"struct">> ->
Ico1 = maps:get(<<"ico">>, Prop, <<"">>),
{V, <<"">>, Ico1, Devicetype1};
Type3 when Type3 == <<"geopoint">> ->
Ico1 = maps:get(<<"ico">>, Prop, <<"">>),
Addr =
case binary:split(V, <<$_>>, [global, trim]) of
[Longitude, Latitude] ->
case dgiot_gps:get_baidu_addr(Longitude, Latitude) of
#{<<"baiduaddr">> := #{<<"formatted_address">> := FormattedAddress}} ->
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
{ok, #{<<"location">> := #{<<"__type">> := <<"GeoPoint">>, <<"longitude">> := Longitude, <<"latitude">> := Latitude}}} ->
pass;
{ok, #{<<"detail">> := Detail}} ->
dgiot_parse:update_object(<<"Device">>, DeviceId, #{
<<"location">> => #{<<"__type">> => <<"GeoPoint">>, <<"longitude">> => Longitude, <<"latitude">> => Latitude},
<<"detail">> => Detail#{<<"address">> => FormattedAddress}});
_ ->
pass
end,
FormattedAddress;
_ ->
<<"[", Longitude/binary, ",", Latitude/binary, "]经纬度解析错误"/utf8>>
end;
_ ->
<<"[", V/binary, "]经纬度解析错误"/utf8>>
end,
{Addr, <<"">>, Ico1, Devicetype1};
_ ->
Unit1 = maps:get(<<"unit">>, Specs, <<"">>),
Ico1 = maps:get(<<"ico">>, Prop, <<"">>),
Expand Down

0 comments on commit fbb2e5b

Please sign in to comment.