feat: http request

This commit is contained in:
dawnwinterLiu 2023-06-25 20:24:55 +08:00
parent 293a7b148e
commit 3694d3a9dd
6 changed files with 57 additions and 21 deletions

View File

@ -166,7 +166,7 @@ do_request(get_devicecard_deviceid, #{<<"deviceid">> := DeviceId} = Args, #{<<"s
end;
%% TDengine : gps轨迹
do_request(get_gps_track_deviceid, #{<<"deviceid">> := DeviceId}, #{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
do_request(get_gps_track_deviceid, #{<<"deviceid">> := DeviceId} = Args, #{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
case DeviceId of
<<"all">> ->
case dgiot_parse:query_object(<<"Device">>, #{}, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
@ -179,7 +179,7 @@ do_request(get_gps_track_deviceid, #{<<"deviceid">> := DeviceId}, #{<<"sessionTo
{ok, Channel} ->
case dgiot_device:lookup(ObjectId) of
{ok, #{<<"productid">> := ProductId}} ->
{ok, #{<<"results">> := Results1}} = dgiot_device_tdengine:get_gps_track(Channel, ProductId, ObjectId),
{ok, #{<<"results">> := Results1}} = dgiot_device_tdengine:get_gps_track(Channel, ProductId, ObjectId, Args),
Results1;
_ ->
[]
@ -197,7 +197,7 @@ do_request(get_gps_track_deviceid, #{<<"deviceid">> := DeviceId}, #{<<"sessionTo
{ok, Channel} ->
case dgiot_device:lookup(DeviceId) of
{ok, #{<<"productid">> := ProductId}} ->
dgiot_device_tdengine:get_gps_track(Channel, ProductId, DeviceId);
dgiot_device_tdengine:get_gps_track(Channel, ProductId, DeviceId, Args);
_ ->
{ok, #{<<"results">> => []}}
end

View File

@ -58,9 +58,33 @@ get_counter({Token, <<"device_poweroff_counter">>}) ->
Count = val(<<"Device_false">>, Key),
{ok, #{<<"lable">> => <<"关机设备"/utf8>>, <<"value">> => Count}};
get_counter({Token, <<"device_peace_counter">>}) ->
Key = get_count(Token),
Count = val(<<"Device_peace">>, Key),
{ok, #{<<"lable">> => <<"平时设备"/utf8>>, <<"value">> => Count}};
get_counter({Token, <<"device_war_counter">>}) ->
Key = get_count(Token),
Count = val(<<"Device_war">>, Key),
{ok, #{<<"lable">> => <<"战时设备"/utf8>>, <<"value">> => Count}};
get_counter({_Token, _}) ->
pass.
get_pie({Token, <<"device_online_offline">>}) ->
Key = get_count(Token),
Device_Online = val(<<"Device_Online">>, Key),
Device_Offline = val(<<"Device_Offline">>, Key),
Payload = #{
<<"columns">> => [<<"名称"/utf8>>, <<"数量"/utf8>>],
<<"rows">> => [
#{<<"名称"/utf8>> => <<"在线数"/utf8>>, <<"数量"/utf8>> => Device_Online},
#{<<"名称"/utf8>> => <<"离线数"/utf8>>, <<"数量"/utf8>> => Device_Offline}
]
},
{ok, Payload};
get_pie({Token, <<"device_poweron_poweroff">>}) ->
Key = get_count(Token),
PowerOnCount = val(<<"Device_true">>, Key),
@ -85,16 +109,16 @@ get_realdata({Token, NodeId}) ->
{error, Error} ->
{error, Error};
{ok, Channel} ->
case dgiot_device:get_productid(DeviceId) of
not_find ->
pass;
ProductId ->
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
{ok, #{<<"objectId">> := DeviceId, <<"product">> := #{<<"objectId">> := ProductId}}} ->
case dgiot_device_card:get_device_card(Channel, ProductId, DeviceId, #{<<"keys">> => [Identifier]}) of
{ok, #{<<"data">> := [#{<<"identifier">> := Identifier} = Data | _]}} ->
{ok, #{<<"lable">> => NodeId, <<"data">> => Data}};
_ ->
pass
end
end;
_ ->
pass
end
end;
_ ->
@ -144,7 +168,7 @@ loop_count(QueryAcls, Key) ->
dgiot_mnesia:search(Fun, #{}).
%%['Device', Acl, Status, Now, IsEnable, dgiot_utils:to_atom(ProductId), Devaddr, DeviceSecret, Node]
add(['Device', _Acls, Status, _, _Time, IsEnable, ProductId | _] = _V, Key) ->
add(['Device', _Acls, Status, State, _Time, IsEnable, ProductId | _] = _V, Key) ->
inc(<<"Device">>, Key),
inc(<<"Device">>, ProductId, Key),
inc("Device_" ++ dgiot_utils:to_list(IsEnable), Key),
@ -156,6 +180,14 @@ add(['Device', _Acls, Status, _, _Time, IsEnable, ProductId | _] = _V, Key) ->
false ->
inc(<<"Device_Offline">>, ProductId, Key),
inc(<<"Device_Offline">>, Key)
end,
case State of
1 ->
inc(<<"Device_war">>, ProductId, Key),
inc(<<"Device_war">>, Key);
_ ->
inc(<<"Device_peace">>, ProductId, Key),
inc(<<"Device_peace">>, Key)
end;
add(_V, _Key) ->

View File

@ -21,7 +21,7 @@
-include_lib("dgiot/include/logger.hrl").
-export([get_device/3, get_device/4, get_device/5]).
-export([get_history_data/4, get_realtime_data/4, get_gps_track/3]).
-export([get_history_data/4, get_realtime_data/4, get_gps_track/4]).
-export([get_history_data2/7]).
%% #{<<"keys">> => <<"last_row(*)">>, <<"limit">> => 1} td最新的一条device
@ -86,8 +86,8 @@ get_history_data(Channel, ProductId, TableName, Query) ->
Function = maps:get(<<"function">>, Query, <<>>),
Keys = maps:get(<<"keys">>, Query, <<"*">>),
Limit = dgiot_tdengine_select:format_limit(Query),
Starttime = maps:get(<<"starttime">>, Query, dgiot_utils:to_binary(dgiot_datetime:now_ms() - 25920000000)),
Endtime = maps:get(<<"endtime">>, Query, dgiot_utils:to_binary(dgiot_datetime:now_ms())),
Starttime = dgiot_utils:to_binary(maps:get(<<"starttime">>, Query, dgiot_datetime:now_ms() - 25920000000)),
Endtime = dgiot_utils:to_binary(maps:get(<<"endtime">>, Query, dgiot_datetime:now_ms())),
{Names, Newkeys} = dgiot_product_tdengine:get_keys(ProductId, Function, Keys),
Tail =
case maps:get(<<"interval">>, Query, <<>>) of
@ -129,12 +129,13 @@ get_history_data2(Order, Channel, TableName, Interval, ProductId, StartTime, _En
dgiot_tdengine_pool:run_sql(Context#{<<"channel">> => Channel}, execute_query, Sql)
end).
get_gps_track(Channel, ProductId, DeviceId) ->
Query = #{<<"keys">> => <<"latitude,longitude">>},
get_gps_track(Channel, ProductId, DeviceId, Args) ->
Query = Args#{<<"keys">> => <<"latitude,longitude">>},
TableName = ?Table(DeviceId),
{_Names, Results} =
case dgiot_device_tdengine:get_history_data(Channel, ProductId, TableName, Query) of
{TdNames, {ok, #{<<"results">> := TdResults}}} ->
%% io:format("~s ~p TdResults = ~p.~n", [?FILE, ?LINE, TdResults]),
NewTdResults =
lists:foldl(
fun
@ -142,11 +143,11 @@ get_gps_track(Channel, ProductId, DeviceId) ->
Acc1;
(#{<<"longitude">> := null}, Acc2) ->
Acc2;
(#{<<"latitude">> := Latitude, <<"longitude">> := Longitude}, Acc) ->
(#{<<"latitude">> := Latitude, <<"longitude">> := Longitude} = X, Acc) ->
Maptype = dgiot_utils:to_binary(application:get_env(dgiot_device, map_type, "baidu")),
#{<<"longitude">> := Mglng, <<"latitude">> := Mglat} =
dgiot_gps:fromwgs84(#{<<"longitude">> => Longitude, <<"latitude">> => Latitude}, Maptype),
Acc ++ [#{<<"lat">> => Mglat, <<"lng">> => Mglng}]
Acc ++ [X#{<<"lat">> => Mglat, <<"lng">> => Mglng}]
end, [], TdResults),
{TdNames, NewTdResults};
_ ->

View File

@ -261,4 +261,4 @@ get_defult(<<"first">>) ->
get_defult(<<"last">>) ->
<<"last(createdat) createdat">>;
get_defult(_) ->
<<>>.
<<"(createdat) createdat">>.

View File

@ -167,8 +167,11 @@ gcj02tobd09(Lng, Lat) ->
%%}
%%};
wgs84togcj02(Lng, Lat) ->
Latoffset = dgiot_utils:to_float(application:get_env(dgiot_http, baidumap_latoffset, <<"-0.0002">>)),
Lngoffset = dgiot_utils:to_float(application:get_env(dgiot_http, baidumap_lngoffset, <<"-0.0000">>)),
case out_of_china(Lng, Lat) of
true -> [Lng, Lat];
true ->
[dgiot_utils:to_float(Lng + Lngoffset, 6), dgiot_utils:to_float(Lat + Latoffset, 6)];
false ->
Dlat = transformlat(Lng - 105.0, Lat - 35.0),
Dlng = transformlng(Lng - 105.0, Lat - 35.0),
@ -180,7 +183,7 @@ wgs84togcj02(Lng, Lat) ->
Dlng1 = (Dlng * 180.0) / (?A / Sqrtmagic * math:cos(Radlat) * ?PI),
Mglat = Lat + Dlat1,
Mglng = Lng + Dlng1,
[Mglng, Mglat]
[dgiot_utils:to_float(Mglng + Lngoffset, 6), dgiot_utils:to_float(Mglat + Latoffset, 6)]
end.
wgs84togcj02(Lng, Lat, Lonoffset, Latoffset) ->

View File

@ -78,7 +78,7 @@ get_sns(Jscode) ->
Url = "https://api.weixin.qq.com/sns/jscode2session?appid=" ++ dgiot_utils:to_list(AppId) ++ "&secret=" ++ dgiot_utils:to_list(Secret) ++
"&js_code=" ++ dgiot_utils:to_list(Jscode) ++ "&grant_type=authorization_code",
case dgiot_http_client:request(get, {Url, []}) of
#{<<"openid">> := OPENID, <<"session_key">> := _SESSIONKEY} ->
{ok, #{<<"openid">> := OPENID, <<"session_key">> := _SESSIONKEY}} ->
case dgiot_parse:query_object(<<"_User">>, #{<<"where">> => #{<<"tag.wechat.openid">> => OPENID}}) of
{ok, #{<<"results">> := Results}} when length(Results) > 0 ->
[#{<<"objectId">> := UserId, <<"username">> := Name} | _] = Results,
@ -100,7 +100,7 @@ get_public_sns(Code) ->
"&code=" ++ dgiot_utils:to_list(Code) ++ "&grant_type=authorization_code",
%% io:format("~s ~p Url = ~p.~n", [?FILE, ?LINE, Url]),
case dgiot_http_client:request(get, {Url, []}) of
#{<<"openid">> := OPENID, <<"access_token">> := _} ->
{ok, #{<<"openid">> := OPENID, <<"access_token">> := _}} ->
case dgiot_parse:query_object(<<"_User">>, #{<<"where">> => #{<<"tag.wechat.openid">> => OPENID}}) of
{ok, #{<<"results">> := Results}} when length(Results) > 0 ->
[#{<<"objectId">> := UserId, <<"username">> := Name} | _] = Results,