Skip to content

Commit

Permalink
gb26875
Browse files Browse the repository at this point in the history
  • Loading branch information
AvantLiu committed Sep 17, 2021
1 parent 3ab8abe commit 558d958
Show file tree
Hide file tree
Showing 27 changed files with 1,380 additions and 129 deletions.
1 change: 1 addition & 0 deletions apps/dgiot/src/utils/dgiot_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ get_parity(Data) when is_list(Data) ->
lists:foldl(
fun(X, Sum) ->
((X rem 256) + Sum) rem 256

end, 0, Data).

%% CRC-16/MODBUS 算法:
Expand Down
167 changes: 72 additions & 95 deletions apps/dgiot_api/src/handler/dgiot_data_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -282,37 +282,14 @@ do_request(post_product, #{<<"file">> := FileInfo, <<"appid">> := Appid} = _Body
%% OperationId:post_device
%% 请求:POST /iotapi/post_device
do_request(post_device, Body, #{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
case dgiot_tdengine:get_channel(SessionToken) of
{error, _Error} ->
case dgiot_parse:query_object(<<"Device">>, Body#{<<"include">> => <<"product">>}, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"results">> := Result} = All} ->
NewResult = lists:foldl(fun(X, Acc) ->
Acc ++ [X#{<<"lasttime">> => 0, <<"swtopo">> => [], <<"tddata">> => []}]
end, [], Result),
{200, All#{<<"results">> => NewResult}};
{error, What} ->
{error, What}
end;
{ok, Channel} ->
case dgiot_parse:query_object(<<"Device">>, Body#{<<"include">> => <<"product">>}, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"results">> := Result} = All} ->
NewResult =
lists:foldl(fun(X, Acc) ->
#{<<"product">> := Product, <<"devaddr">> := DevAddr} = X,
case Body of
#{<<"include">> := <<"product">>} ->
get_tddata(Channel, Acc, X, Product, DevAddr);
_ ->
get_tddata(Channel, Acc, maps:without([<<"product">>], X), Product, DevAddr)
end
end, [], Result),
{200, All#{<<"results">> => NewResult}};
{error, What} ->
{error, What}
end
case dgiot_parse:query_object(<<"Device">>, Body#{<<"include">> => <<"product">>}, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"results">> := _Result} = All} ->
{200, All};
{error, What} ->
{error, What}
end;

%% Device 概要: 查询设备 描述:查询设备时序数据
%% Device 概要: 查询设备 描述:序列号添加设备
%% OperationId:post_adddevice
%% 请求:POST /iotapi/post_adddevice
do_request(post_adddevice, #{<<"devaddr">> := Devaddr, <<"productid">> := ProductId, <<"longitude">> := Longitude, <<"latitude">> := Latitude}, #{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
Expand Down Expand Up @@ -660,69 +637,69 @@ get_json_from_zip(FileInfo) ->
{error, Reason}
end.

get_tddata(Channel, Acc, #{<<"objectId">> := DeviceId} = Device, #{<<"objectId">> := ProdcutId} = Product, DevAddr) ->
[Time, Data] =
case maps:get(<<"thing">>, Product, #{}) of
#{<<"properties">> := Properties} when length(Properties) > 0 ->
case dgiot_tdengine:get_device(Channel, ProdcutId, DeviceId, DeviceId, #{<<"keys">> => <<"last_row(*)">>, <<"limit">> => 1}) of
{ok, #{<<"results">> := [Data1 | _]}} ->
%% ?LOG(info,"Data1 ~p",[Data1]),
[dgiot_tdengine:to_unixtime(maps:get(<<"createdat">>, Data1)), Data1];
_ -> [0, #{}]
end;
_ ->
[0, #{}]
end,
TdData =
lists:foldl(fun(Y, Acc2) ->
#{
<<"objectId">> := SubDeviceId,
<<"devaddr">> := SubDtuAddr,
<<"product">> := #{<<"objectId">> := SubProductId}
} = Y,
case dgiot_tdengine:get_device(Channel, SubProductId, SubDeviceId, SubDtuAddr, #{<<"keys">> => <<"last_row(*)">>, <<"limit">> => 1}) of
{ok, #{<<"results">> := [Data2 | _]}} ->
Acc2#{SubDeviceId => #{<<"productid">> => SubProductId, <<"deviceid">> => SubDeviceId, <<"data">> => maps:without([<<"createdat">>], Data2)}};
_ -> Acc2
end
end, #{DeviceId => #{<<"productid">> => ProdcutId, <<"deviceid">> => DeviceId, <<"data">> => maps:without([<<"createdat">>], Data)}},
dgiot_device:get_sub_device(DevAddr)),
Topo =
case maps:find(<<"config">>, Product) of
error -> #{};
{ok, #{<<"components">> := Components}} ->
lists:foldl(fun(X, Acc3) ->
case X of
#{<<"identifier">> := Identifier, <<"type">> := Type, <<"wumoxing">> := #{<<"identifier">> := Di, <<"subdevid">> := Subdevid2}} ->
case maps:get(Subdevid2, TdData, null) of
null -> Acc3;
#{<<"data">> := SubData} ->
case maps:get(Di, SubData, null) of
null -> Acc3;
Value ->
case Type of
<<"video">> ->
Acc3#{Identifier => SubData};
_ ->
Acc3#{Identifier => Value}
end
end
end;
#{<<"identifier">> := Identifier, <<"type">> := Type, <<"wumoxing">> := #{<<"identifier">> := Key}} ->
case maps:get(Key, Data, null) of
null ->
Acc3;
Value ->
case Type of
<<"video">> ->
Acc3#{Identifier => Data};
_ ->
Acc3#{Identifier => Value}
end
end;
_ -> Acc3
end
end, #{}, Components);
_ -> #{}
end,
Acc ++ [Device#{<<"lasttime">> => Time, <<"swtopo">> => Topo, <<"tddata">> => maps:values(TdData)}].
%%get_tddata(Channel, Acc, #{<<"objectId">> := DeviceId} = Device, #{<<"objectId">> := ProdcutId} = Product, DevAddr) ->
%% [Time, Data] =
%% case maps:get(<<"thing">>, Product, #{}) of
%% #{<<"properties">> := Properties} when length(Properties) > 0 ->
%% case dgiot_tdengine:get_device(Channel, ProdcutId, DeviceId, DeviceId, #{<<"keys">> => <<"last_row(*)">>, <<"limit">> => 1}) of
%% {ok, #{<<"results">> := [Data1 | _]}} ->
%%%% ?LOG(info,"Data1 ~p",[Data1]),
%% [dgiot_tdengine:to_unixtime(maps:get(<<"createdat">>, Data1)), Data1];
%% _ -> [0, #{}]
%% end;
%% _ ->
%% [0, #{}]
%% end,
%% TdData =
%% lists:foldl(fun(Y, Acc2) ->
%% #{
%% <<"objectId">> := SubDeviceId,
%% <<"devaddr">> := SubDtuAddr,
%% <<"product">> := #{<<"objectId">> := SubProductId}
%% } = Y,
%% case dgiot_tdengine:get_device(Channel, SubProductId, SubDeviceId, SubDtuAddr, #{<<"keys">> => <<"last_row(*)">>, <<"limit">> => 1}) of
%% {ok, #{<<"results">> := [Data2 | _]}} ->
%% Acc2#{SubDeviceId => #{<<"productid">> => SubProductId, <<"deviceid">> => SubDeviceId, <<"data">> => maps:without([<<"createdat">>], Data2)}};
%% _ -> Acc2
%% end
%% end, #{DeviceId => #{<<"productid">> => ProdcutId, <<"deviceid">> => DeviceId, <<"data">> => maps:without([<<"createdat">>], Data)}},
%% dgiot_device:get_sub_device(DevAddr)),
%% Topo =
%% case maps:find(<<"config">>, Product) of
%% error -> #{};
%% {ok, #{<<"components">> := Components}} ->
%% lists:foldl(fun(X, Acc3) ->
%% case X of
%% #{<<"identifier">> := Identifier, <<"type">> := Type, <<"wumoxing">> := #{<<"identifier">> := Di, <<"subdevid">> := Subdevid2}} ->
%% case maps:get(Subdevid2, TdData, null) of
%% null -> Acc3;
%% #{<<"data">> := SubData} ->
%% case maps:get(Di, SubData, null) of
%% null -> Acc3;
%% Value ->
%% case Type of
%% <<"video">> ->
%% Acc3#{Identifier => SubData};
%% _ ->
%% Acc3#{Identifier => Value}
%% end
%% end
%% end;
%% #{<<"identifier">> := Identifier, <<"type">> := Type, <<"wumoxing">> := #{<<"identifier">> := Key}} ->
%% case maps:get(Key, Data, null) of
%% null ->
%% Acc3;
%% Value ->
%% case Type of
%% <<"video">> ->
%% Acc3#{Identifier => Data};
%% _ ->
%% Acc3#{Identifier => Value}
%% end
%% end;
%% _ -> Acc3
%% end
%% end, #{}, Components);
%% _ -> #{}
%% end,
%% Acc ++ [Device#{<<"lasttime">> => Time, <<"swtopo">> => Topo, <<"tddata">> => maps:values(TdData)}].
Loading

0 comments on commit 558d958

Please sign in to comment.