mirror of
https://gitee.com/dgiiot/dgiot.git
synced 2024-12-03 12:48:30 +08:00
feat: http request
This commit is contained in:
parent
293a7b148e
commit
3694d3a9dd
@ -166,7 +166,7 @@ do_request(get_devicecard_deviceid, #{<<"deviceid">> := DeviceId} = Args, #{<<"s
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
%% TDengine 概要: 获取gps轨迹
|
%% 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
|
case DeviceId of
|
||||||
<<"all">> ->
|
<<"all">> ->
|
||||||
case dgiot_parse:query_object(<<"Device">>, #{}, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
|
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} ->
|
{ok, Channel} ->
|
||||||
case dgiot_device:lookup(ObjectId) of
|
case dgiot_device:lookup(ObjectId) of
|
||||||
{ok, #{<<"productid">> := ProductId}} ->
|
{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;
|
Results1;
|
||||||
_ ->
|
_ ->
|
||||||
[]
|
[]
|
||||||
@ -197,7 +197,7 @@ do_request(get_gps_track_deviceid, #{<<"deviceid">> := DeviceId}, #{<<"sessionTo
|
|||||||
{ok, Channel} ->
|
{ok, Channel} ->
|
||||||
case dgiot_device:lookup(DeviceId) of
|
case dgiot_device:lookup(DeviceId) of
|
||||||
{ok, #{<<"productid">> := ProductId}} ->
|
{ok, #{<<"productid">> := ProductId}} ->
|
||||||
dgiot_device_tdengine:get_gps_track(Channel, ProductId, DeviceId);
|
dgiot_device_tdengine:get_gps_track(Channel, ProductId, DeviceId, Args);
|
||||||
_ ->
|
_ ->
|
||||||
{ok, #{<<"results">> => []}}
|
{ok, #{<<"results">> => []}}
|
||||||
end
|
end
|
||||||
|
@ -58,9 +58,33 @@ get_counter({Token, <<"device_poweroff_counter">>}) ->
|
|||||||
Count = val(<<"Device_false">>, Key),
|
Count = val(<<"Device_false">>, Key),
|
||||||
{ok, #{<<"lable">> => <<"关机设备"/utf8>>, <<"value">> => Count}};
|
{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, _}) ->
|
get_counter({_Token, _}) ->
|
||||||
pass.
|
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">>}) ->
|
get_pie({Token, <<"device_poweron_poweroff">>}) ->
|
||||||
Key = get_count(Token),
|
Key = get_count(Token),
|
||||||
PowerOnCount = val(<<"Device_true">>, Key),
|
PowerOnCount = val(<<"Device_true">>, Key),
|
||||||
@ -85,16 +109,16 @@ get_realdata({Token, NodeId}) ->
|
|||||||
{error, Error} ->
|
{error, Error} ->
|
||||||
{error, Error};
|
{error, Error};
|
||||||
{ok, Channel} ->
|
{ok, Channel} ->
|
||||||
case dgiot_device:get_productid(DeviceId) of
|
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
|
||||||
not_find ->
|
{ok, #{<<"objectId">> := DeviceId, <<"product">> := #{<<"objectId">> := ProductId}}} ->
|
||||||
pass;
|
|
||||||
ProductId ->
|
|
||||||
case dgiot_device_card:get_device_card(Channel, ProductId, DeviceId, #{<<"keys">> => [Identifier]}) of
|
case dgiot_device_card:get_device_card(Channel, ProductId, DeviceId, #{<<"keys">> => [Identifier]}) of
|
||||||
{ok, #{<<"data">> := [#{<<"identifier">> := Identifier} = Data | _]}} ->
|
{ok, #{<<"data">> := [#{<<"identifier">> := Identifier} = Data | _]}} ->
|
||||||
{ok, #{<<"lable">> => NodeId, <<"data">> => Data}};
|
{ok, #{<<"lable">> => NodeId, <<"data">> => Data}};
|
||||||
_ ->
|
_ ->
|
||||||
pass
|
pass
|
||||||
end
|
end;
|
||||||
|
_ ->
|
||||||
|
pass
|
||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
@ -144,7 +168,7 @@ loop_count(QueryAcls, Key) ->
|
|||||||
dgiot_mnesia:search(Fun, #{}).
|
dgiot_mnesia:search(Fun, #{}).
|
||||||
|
|
||||||
%%['Device', Acl, Status, Now, IsEnable, dgiot_utils:to_atom(ProductId), Devaddr, DeviceSecret, Node]
|
%%['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">>, Key),
|
||||||
inc(<<"Device">>, ProductId, Key),
|
inc(<<"Device">>, ProductId, Key),
|
||||||
inc("Device_" ++ dgiot_utils:to_list(IsEnable), Key),
|
inc("Device_" ++ dgiot_utils:to_list(IsEnable), Key),
|
||||||
@ -156,6 +180,14 @@ add(['Device', _Acls, Status, _, _Time, IsEnable, ProductId | _] = _V, Key) ->
|
|||||||
false ->
|
false ->
|
||||||
inc(<<"Device_Offline">>, ProductId, Key),
|
inc(<<"Device_Offline">>, ProductId, Key),
|
||||||
inc(<<"Device_Offline">>, 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;
|
end;
|
||||||
|
|
||||||
add(_V, _Key) ->
|
add(_V, _Key) ->
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
-include_lib("dgiot/include/logger.hrl").
|
-include_lib("dgiot/include/logger.hrl").
|
||||||
|
|
||||||
-export([get_device/3, get_device/4, get_device/5]).
|
-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]).
|
-export([get_history_data2/7]).
|
||||||
|
|
||||||
%% #{<<"keys">> => <<"last_row(*)">>, <<"limit">> => 1} 查询td最新的一条device
|
%% #{<<"keys">> => <<"last_row(*)">>, <<"limit">> => 1} 查询td最新的一条device
|
||||||
@ -86,8 +86,8 @@ get_history_data(Channel, ProductId, TableName, Query) ->
|
|||||||
Function = maps:get(<<"function">>, Query, <<>>),
|
Function = maps:get(<<"function">>, Query, <<>>),
|
||||||
Keys = maps:get(<<"keys">>, Query, <<"*">>),
|
Keys = maps:get(<<"keys">>, Query, <<"*">>),
|
||||||
Limit = dgiot_tdengine_select:format_limit(Query),
|
Limit = dgiot_tdengine_select:format_limit(Query),
|
||||||
Starttime = maps:get(<<"starttime">>, Query, dgiot_utils:to_binary(dgiot_datetime:now_ms() - 25920000000)),
|
Starttime = dgiot_utils:to_binary(maps:get(<<"starttime">>, Query, dgiot_datetime:now_ms() - 25920000000)),
|
||||||
Endtime = maps:get(<<"endtime">>, Query, dgiot_utils:to_binary(dgiot_datetime:now_ms())),
|
Endtime = dgiot_utils:to_binary(maps:get(<<"endtime">>, Query, dgiot_datetime:now_ms())),
|
||||||
{Names, Newkeys} = dgiot_product_tdengine:get_keys(ProductId, Function, Keys),
|
{Names, Newkeys} = dgiot_product_tdengine:get_keys(ProductId, Function, Keys),
|
||||||
Tail =
|
Tail =
|
||||||
case maps:get(<<"interval">>, Query, <<>>) of
|
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)
|
dgiot_tdengine_pool:run_sql(Context#{<<"channel">> => Channel}, execute_query, Sql)
|
||||||
end).
|
end).
|
||||||
|
|
||||||
get_gps_track(Channel, ProductId, DeviceId) ->
|
get_gps_track(Channel, ProductId, DeviceId, Args) ->
|
||||||
Query = #{<<"keys">> => <<"latitude,longitude">>},
|
Query = Args#{<<"keys">> => <<"latitude,longitude">>},
|
||||||
TableName = ?Table(DeviceId),
|
TableName = ?Table(DeviceId),
|
||||||
{_Names, Results} =
|
{_Names, Results} =
|
||||||
case dgiot_device_tdengine:get_history_data(Channel, ProductId, TableName, Query) of
|
case dgiot_device_tdengine:get_history_data(Channel, ProductId, TableName, Query) of
|
||||||
{TdNames, {ok, #{<<"results">> := TdResults}}} ->
|
{TdNames, {ok, #{<<"results">> := TdResults}}} ->
|
||||||
|
%% io:format("~s ~p TdResults = ~p.~n", [?FILE, ?LINE, TdResults]),
|
||||||
NewTdResults =
|
NewTdResults =
|
||||||
lists:foldl(
|
lists:foldl(
|
||||||
fun
|
fun
|
||||||
@ -142,11 +143,11 @@ get_gps_track(Channel, ProductId, DeviceId) ->
|
|||||||
Acc1;
|
Acc1;
|
||||||
(#{<<"longitude">> := null}, Acc2) ->
|
(#{<<"longitude">> := null}, Acc2) ->
|
||||||
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")),
|
Maptype = dgiot_utils:to_binary(application:get_env(dgiot_device, map_type, "baidu")),
|
||||||
#{<<"longitude">> := Mglng, <<"latitude">> := Mglat} =
|
#{<<"longitude">> := Mglng, <<"latitude">> := Mglat} =
|
||||||
dgiot_gps:fromwgs84(#{<<"longitude">> => Longitude, <<"latitude">> => Latitude}, Maptype),
|
dgiot_gps:fromwgs84(#{<<"longitude">> => Longitude, <<"latitude">> => Latitude}, Maptype),
|
||||||
Acc ++ [#{<<"lat">> => Mglat, <<"lng">> => Mglng}]
|
Acc ++ [X#{<<"lat">> => Mglat, <<"lng">> => Mglng}]
|
||||||
end, [], TdResults),
|
end, [], TdResults),
|
||||||
{TdNames, NewTdResults};
|
{TdNames, NewTdResults};
|
||||||
_ ->
|
_ ->
|
||||||
|
@ -261,4 +261,4 @@ get_defult(<<"first">>) ->
|
|||||||
get_defult(<<"last">>) ->
|
get_defult(<<"last">>) ->
|
||||||
<<"last(createdat) createdat">>;
|
<<"last(createdat) createdat">>;
|
||||||
get_defult(_) ->
|
get_defult(_) ->
|
||||||
<<>>.
|
<<"(createdat) createdat">>.
|
||||||
|
@ -167,8 +167,11 @@ gcj02tobd09(Lng, Lat) ->
|
|||||||
%%}
|
%%}
|
||||||
%%};
|
%%};
|
||||||
wgs84togcj02(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
|
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 ->
|
false ->
|
||||||
Dlat = transformlat(Lng - 105.0, Lat - 35.0),
|
Dlat = transformlat(Lng - 105.0, Lat - 35.0),
|
||||||
Dlng = transformlng(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),
|
Dlng1 = (Dlng * 180.0) / (?A / Sqrtmagic * math:cos(Radlat) * ?PI),
|
||||||
Mglat = Lat + Dlat1,
|
Mglat = Lat + Dlat1,
|
||||||
Mglng = Lng + Dlng1,
|
Mglng = Lng + Dlng1,
|
||||||
[Mglng, Mglat]
|
[dgiot_utils:to_float(Mglng + Lngoffset, 6), dgiot_utils:to_float(Mglat + Latoffset, 6)]
|
||||||
end.
|
end.
|
||||||
|
|
||||||
wgs84togcj02(Lng, Lat, Lonoffset, Latoffset) ->
|
wgs84togcj02(Lng, Lat, Lonoffset, Latoffset) ->
|
||||||
|
@ -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) ++
|
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",
|
"&js_code=" ++ dgiot_utils:to_list(Jscode) ++ "&grant_type=authorization_code",
|
||||||
case dgiot_http_client:request(get, {Url, []}) of
|
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
|
case dgiot_parse:query_object(<<"_User">>, #{<<"where">> => #{<<"tag.wechat.openid">> => OPENID}}) of
|
||||||
{ok, #{<<"results">> := Results}} when length(Results) > 0 ->
|
{ok, #{<<"results">> := Results}} when length(Results) > 0 ->
|
||||||
[#{<<"objectId">> := UserId, <<"username">> := Name} | _] = Results,
|
[#{<<"objectId">> := UserId, <<"username">> := Name} | _] = Results,
|
||||||
@ -100,7 +100,7 @@ get_public_sns(Code) ->
|
|||||||
"&code=" ++ dgiot_utils:to_list(Code) ++ "&grant_type=authorization_code",
|
"&code=" ++ dgiot_utils:to_list(Code) ++ "&grant_type=authorization_code",
|
||||||
%% io:format("~s ~p Url = ~p.~n", [?FILE, ?LINE, Url]),
|
%% io:format("~s ~p Url = ~p.~n", [?FILE, ?LINE, Url]),
|
||||||
case dgiot_http_client:request(get, {Url, []}) of
|
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
|
case dgiot_parse:query_object(<<"_User">>, #{<<"where">> => #{<<"tag.wechat.openid">> => OPENID}}) of
|
||||||
{ok, #{<<"results">> := Results}} when length(Results) > 0 ->
|
{ok, #{<<"results">> := Results}} when length(Results) > 0 ->
|
||||||
[#{<<"objectId">> := UserId, <<"username">> := Name} | _] = Results,
|
[#{<<"objectId">> := UserId, <<"username">> := Name} | _] = Results,
|
||||||
|
Loading…
Reference in New Issue
Block a user