Skip to content

Commit

Permalink
物模型接口
Browse files Browse the repository at this point in the history
  • Loading branch information
AvantLiu committed Aug 20, 2021
1 parent bebf3eb commit 1dbd461
Show file tree
Hide file tree
Showing 15 changed files with 419 additions and 151 deletions.
206 changes: 193 additions & 13 deletions apps/dgiot_api/priv/swagger/swagger_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,90 @@
}
},
"/thing": {
"post": {
"summary": "添加物模型",
"description": "添加物模型",
"parameters": [
{
"in": "body",
"name": "data",
"description": "关联信息",
"required": true,
"schema": {
"type": "object",
"properties": {
"productid": {
"description": "产品标识",
"type": "string",
"example": "ed09e37bbb"
},
"item": {
"description": "物模型单项指标",
"type": "object",
"example": {
"name": "电压",
"index": 11,
"isshow": true,
"dataForm": {
"data": "1",
"rate": 1,
"order": 26,
"round": "all",
"offset": 0,
"address": "0X0107",
"control": "%d",
"iscount": "0",
"slaveid": "0X01",
"protocol": "modbus",
"strategy": "20",
"collection": "%s*0.1",
"countround": "all",
"operatetype": "readHregs",
"originaltype": "short16_AB",
"countstrategy": 20,
"countcollection": "%s"
},
"dataType": {
"type": "float",
"specs": {
"max": 9999,
"min": 0,
"step": 0,
"unit": "V",
"precision": 2
}
},
"required": true,
"accessMode": "r",
"devicetype": "控制器",
"identifier": "voltage"
}
}
}
}
}
],
"responses": {
"200": {
"description": "Returns success"
},
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"500": {
"description": "Server Internal error"
}
},
"tags": [
"Product"
]
},
"put": {
"summary": "修改物模型",
"description": "修改物模型",
Expand All @@ -428,36 +512,132 @@
"productid": {
"description": "产品标识",
"type": "string",
"example": "9c5930e565"
"example": "ed09e37bbb"
},
"item": {
"description": "物模型单项指标",
"type": "object",
"example": {
"name": "电压",
"index": 11,
"isshow": true,
"dataForm": {
"data": "1",
"rate": 1,
"order": 26,
"round": "all",
"offset": 0,
"address": "0X0107",
"control": "%d",
"iscount": "0",
"slaveid": "0X01",
"protocol": "modbus",
"strategy": "20",
"collection": "%s*0.1",
"countround": "all",
"operatetype": "readHregs",
"originaltype": "short16_AB",
"countstrategy": 20,
"countcollection": "%s"
},
"dataType": {
"type": "float",
"specs": {
"max": 9999,
"min": 0,
"step": 0,
"unit": "V",
"precision": 2
}
},
"required": true,
"accessMode": "r",
"devicetype": "控制器",
"identifier": "voltage"
}
}
}
}
}
],
"responses": {
"200": {
"description": "Returns success"
},
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"500": {
"description": "Server Internal error"
}
},
"tags": [
"Product"
]
},
"delete": {
"summary": "删除物模型",
"description": "删除物模型",
"parameters": [
{
"in": "body",
"name": "data",
"description": "关联信息",
"required": true,
"schema": {
"type": "object",
"properties": {
"productid": {
"description": "产品标识",
"type": "string",
"example": "ed09e37bbb"
},
"item": {
"description": "物模型单项指标",
"type": "object",
"example": {
"name": "出口压力",
"name": "电压",
"index": 11,
"isshow": true,
"dataForm": {
"data": "1",
"rate": 1,
"order": 26,
"round": "all",
"offset": 0,
"address": "0x0001",
"slaveid": 1,
"address": "0X0107",
"control": "%d",
"iscount": "0",
"slaveid": "0X01",
"protocol": "modbus",
"quantity": 2,
"byteorder": "big",
"operatetype": "holdingRegister",
"originaltype": "int16"
"strategy": "20",
"collection": "%s*0.1",
"countround": "all",
"operatetype": "readHregs",
"originaltype": "short16_AB",
"countstrategy": 20,
"countcollection": "%s"
},
"dataType": {
"type": "float",
"specs": {
"max": 500,
"min": -500,
"step": 0.1,
"unit": "MPa"
"max": 9999,
"min": 0,
"step": 0,
"unit": "V",
"precision": 2
}
},
"required": true,
"accessMode": "r",
"identifier": "pressure_out"
"devicetype": "控制器",
"identifier": "voltage"
}
}
}
Expand Down
105 changes: 91 additions & 14 deletions apps/dgiot_api/src/handler/dgiot_data_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,23 @@ handle(OperationID, Args, Context, Req) ->
Headers = #{},
case catch do_request(OperationID, Args, Context, Req) of
{ErrType, Reason} when ErrType == 'EXIT'; ErrType == error ->
?LOG(info, "do request: ~p, ~p, ~p~n", [OperationID, Args, Reason]),
?LOG(debug, "do request: ~p, ~p, ~p~n", [OperationID, Args, Reason]),
Err = case is_binary(Reason) of
true -> Reason;
false -> dgiot_utils:format("~p", [Reason])
end,
{500, Headers, #{<<"error">> => Err}};
ok ->
?LOG(error, "do request: ~p, ~p ->ok ~n", [OperationID, Args]),
?LOG(debug, "do request: ~p, ~p ->ok ~n", [OperationID, Args]),
{200, Headers, #{}, Req};
{ok, Res} ->
?LOG(error, "do request: ~p, ~p ->~p~n", [OperationID, Args, Res]),
?LOG(debug, "do request: ~p, ~p ->~p~n", [OperationID, Args, Res]),
{200, Headers, Res, Req};
{Status, Res} ->
?LOG(error, "do request: ~p, ~p ->~p~n", [OperationID, Args, Res]),
?LOG(debug, "do request: ~p, ~p ->~p~n", [OperationID, Args, Res]),
{Status, Headers, Res, Req};
{Status, NewHeaders, Res} ->
?LOG(error, "do request: ~p, ~p ->~p~n", [OperationID, Args, Res]),
?LOG(debug, "do request: ~p, ~p ->~p~n", [OperationID, Args, Res]),
{Status, maps:merge(Headers, NewHeaders), Res, Req}
end.

Expand Down Expand Up @@ -162,26 +162,103 @@ do_request(post_graphql, Body, #{<<"sessionToken">> := SessionToken} = _Context,
Other -> Other
end;

%% Thing 概要: 导库 描述:json文件导库
%% OperationId:post_product

%% Thing 概要: 导库 描述:添加物模型
%% OperationId:post_thing
%% 请求:PUT /iotapi/post_thing
do_request(post_thing, #{<<"productid">> := ProductId, <<"item">> := Item} = _Body,
#{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
#{<<"identifier">> := Identifier} = Item,
case dgiot_parse:get_object(<<"Product">>, ProductId, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"thing">> := Thing}} ->
Properties = maps:get(<<"properties">>, Thing, []),
NewThing =
lists:foldl(fun(X, _Acc) ->
case X of
#{<<"identifier">> := Identifier} -> <<>>;
_ -> Item
end
end, <<>>, Properties),
case NewThing of
<<>> ->
{error, #{<<"code">> => 500, <<"msg">> => <<Identifier/binary, " already existed">>}};
X ->
NewProperties = Properties ++ [X],
dgiot_parse:update_object(<<"Product">>, ProductId,
#{<<"thing">> => Thing#{<<"properties">> => NewProperties}},
[{"X-Parse-Session-Token", SessionToken}], [{from, rest}])
end;
Error ->
{error, Error}
end;


%% Thing 概要: 导库 描述:修改物模型
%% OperationId:put_thing
%% 请求:PUT /iotapi/put_thing
do_request(put_thing, #{<<"productid">> := ProductId, <<"item">> := Item} = _Body,
#{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
#{<<"identifier">> := Identifier} = Item,
case dgiot_parse:get_object(<<"Product">>, ProductId, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"thing">> := Thing}} ->
#{<<"properties">> := Properties} = Thing,
NewProperties = lists:foldl(fun(X, Acc) ->
case X of
#{<<"identifier">> := Identifier} -> Acc ++ [Item];
_ -> Acc ++ [X]
end
end, [], Properties),
NewProperties =
lists:foldl(fun(X, Acc) ->
case X of
#{<<"identifier">> := Identifier} ->
Acc ++ [Item];
_ ->
Acc ++ [X]
end
end, [], Properties),
dgiot_parse:update_object(<<"Product">>, ProductId,
#{<<"thing">> => Thing#{<<"properties">> => NewProperties}},
[{"X-Parse-Session-Token", SessionToken}], [{from, rest}]);
Error ->
Error
{error, Error}
end;

%% Thing 概要: 导库 描述:删除物模型
%% OperationId:put_thing
%% 请求:PUT /iotapi/put_thing
do_request(delete_thing, #{<<"productid">> := ProductId, <<"item">> := Item} = _Body,
#{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
#{<<"identifier">> := Identifier} = Item,
case dgiot_parse:get_object(<<"Product">>, ProductId, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"thing">> := Thing}} ->
#{<<"properties">> := Properties} = Thing,
{Ids, NewProperties} =
lists:foldl(fun(X, {Ids1, Acc}) ->
case X of
#{<<"identifier">> := Identifier} ->
{Ids1, Acc};
#{<<"identifier">> := Identifier1, <<"dataForm">> := #{<<"collection">> := Collection}} ->
case binary:match(Collection, [Identifier]) of
nomatch ->
{Ids1, Acc ++ [X]};
_ ->
case Ids1 of
[] ->
{Ids1 ++ [Identifier1], Acc};
_ ->
{Ids1 ++ [<<",", Identifier1/binary>>], Acc}
end
end;
_ ->
{Ids1, Acc}
end
end, {[], []}, Properties),
case length(Ids) == 0 of
true ->
dgiot_parse:update_object(<<"Product">>, ProductId,
#{<<"thing">> => Thing#{<<"properties">> => NewProperties}},
[{"X-Parse-Session-Token", SessionToken}], [{from, rest}]);
false ->
BinIds = dgiot_utils:to_binary(Ids),
{error, #{<<"code">> => 500, <<"msg">> => <<BinIds/binary, " use ", Identifier/binary>>}}
end;
Error ->
{error, Error}
end;


Expand Down
Loading

0 comments on commit 1dbd461

Please sign in to comment.