diff --git a/apps/dgiot/src/transport/dgiot_serial_client.erl b/apps/dgiot/src/transport/dgiot_serial_client.erl index e0b117aa..da08ae5b 100644 --- a/apps/dgiot/src/transport/dgiot_serial_client.erl +++ b/apps/dgiot/src/transport/dgiot_serial_client.erl @@ -15,9 +15,10 @@ %%-------------------------------------------------------------------- -module(dgiot_serial_client). -behaviour(gen_server). --define(SERIAL, ets). +-dgiot_data("ets"). +-define(SERIAL, dgiot_serial_ets). -export([ - init/0, + init_ets/0, open/1, open/2, close/1, @@ -63,7 +64,7 @@ package_send_count = 0 :: integer() }). -init() -> +init_ets() -> dgiot_data:init(?SERIAL). %%-------------------------------------------------------------------- @@ -118,6 +119,7 @@ start_link(Dev, Opt) -> %%% Callbacks %%-------------------------------------------------------------------- init([ParentPid, Serialport, Opt]) -> +%% io:format("~s ~p Serialport = ~p.~n", [?FILE, ?LINE, Serialport]), dgiot_data:insert(?SERIAL, Serialport, self()), process_flag(trap_exit, true), BSpeed = proplists:get_value(speed, Opt, b9600), @@ -143,7 +145,6 @@ init([ParentPid, Serialport, Opt]) -> fun(N) -> serctl:ospeed(N, BSpeed) end ] ), - <<"b", Speed/binary>> = dgiot_utils:to_binary(BSpeed), ok = serctl:tcsetattr(FD, tcsanow, Termios), @@ -172,14 +173,15 @@ handle_call({send, Data}, _From, #state{port = Port} = State) -> error:Error -> {error, Error} end, {reply, Reply, State}; -handle_call({write, Data}, _From, #state{fd = FD} = State) -> - Reply = - try serctl:write(FD, Data) of - ok -> ok - catch - error:Error -> {error, Error} - end, - {reply, Reply, State}; +handle_call({write, Data}, _From, #state{fd = FD, package_send_count = Package_send_count} = State) -> + try serctl:write(FD, Data) of + ok -> + {reply, ok, State#state{package_send_count = Package_send_count + 1}} + catch + error:Error -> + {reply, {error, Error}, State} + end; + handle_call(close, _From, State) -> {stop, normal, ok, State}; handle_call({controlling_process, Pid}, {Owner, _}, #state{pid = Owner} = State) -> diff --git a/apps/dgiot/src/utils/dgiot_utils.erl b/apps/dgiot/src/utils/dgiot_utils.erl index 773f5b8f..013e8ec2 100644 --- a/apps/dgiot/src/utils/dgiot_utils.erl +++ b/apps/dgiot/src/utils/dgiot_utils.erl @@ -110,6 +110,8 @@ , get_computerconfig/0 , get_ipbymac/1 , get_ifaddrs/0 + , get_ifaddr/1 + , get_ifip/1 , ping_all/0 , get_ipbymac/2 , get_ipv4/1 @@ -920,6 +922,35 @@ get_ifaddrs() -> _ -> [] end. +get_ifaddr(Inetsname) when is_list(Inetsname) -> + case inet:getifaddrs() of + {ok, Iflist} -> + Inets = proplists:get_value(Inetsname, Iflist, []), + Hwaddr = proplists:get_value(hwaddr, Inets, []), + dgiot_utils:binary_to_hex(iolist_to_binary(Hwaddr)); + _ -> <<>> + end; + +get_ifaddr(Inetsname) -> + get_ifaddr(dgiot_utils:to_list(Inetsname)). + +get_ifip(Inetsname) when is_list(Inetsname) -> + case inet:getifaddrs() of + {ok, Iflist} -> + Inets = proplists:get_value(Inetsname, Iflist, []), + case proplists:get_value(addr, Inets, not_find) of + not_find -> + <<>>; + Ip -> + get_ip(Ip) + end; + _ -> + <<>> + end; + +get_ifip(Inetsname) -> + get_ifip(dgiot_utils:to_list(Inetsname)). + get_ipv4(Hostent) -> lists:foldl(fun({K, V}, Acc) -> case K of diff --git a/apps/dgiot_bamis/priv/swagger/swagger_view.json b/apps/dgiot_bamis/priv/swagger/swagger_view.json index d6138137..7fd11efe 100644 --- a/apps/dgiot_bamis/priv/swagger/swagger_view.json +++ b/apps/dgiot_bamis/priv/swagger/swagger_view.json @@ -42,6 +42,73 @@ "View" ] } + }, + "/amis": { + "post": { + "summary": "amis变量替换", + "description": "amis变量替换", + "parameters": [ + { + "name": "viewid", + "description": "视图Id", + "in": "query", + "required": true, + "type": "string", + "default": "18d4597c44" + }, + { + "in": "body", + "name": "render", + "required": true, + "schema": { + "type": "object", + "properties": {} + } + } + ], + "responses": { + "200": { + "description": "Returns operation status", + "schema": { + "type": "object", + "properties": { + "status": { + "type": "string" + }, + "data": { + "type": "object", + "properties": { + "resultType": { + "type": "string" + }, + "result": { + "type": "array", + "items": { + "type": "string" + } + } + } + } + } + } + }, + "400": { + "description": "Bad Request" + }, + "401": { + "description": "Unauthorized" + }, + "403": { + "description": "Forbidden" + }, + "500": { + "description": "Server Internal error" + } + }, + "tags": [ + "System" + ] + } } } } diff --git a/apps/dgiot_bamis/src/dgiot_view_handler.erl b/apps/dgiot_bamis/src/dgiot_view_handler.erl index 8afdb40d..850b7e4a 100644 --- a/apps/dgiot_bamis/src/dgiot_view_handler.erl +++ b/apps/dgiot_bamis/src/dgiot_view_handler.erl @@ -97,7 +97,26 @@ do_request(post_dashboard, Arg, Context, _Req) -> Data = dgiot_dashboard:post_dashboard(Arg, Context), {200, Data}; +%% iot_hub 概要: amis 变量替换 +%% OperationId:get_amis +%% 请求:POST /iotapi/get_amis +do_request(post_amis, #{<<"viewid">> := Viewid, <<"render">> := Render} = _Arg, #{<<"sessionToken">> := SessionToken} = _Context, _Req) -> + Amisdata = + case dgiot_parse:get_object(<<"View">>, Viewid, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of + {ok, #{<<"data">> := Data} = View} -> + Json = jsx:encode(Data), + NewJson = + maps:fold(fun(K, V, Acc) -> + re:replace(Acc, <<"%{", K/binary, "}">>, V, [global, {return, binary}]) + end, Json, Render), + View#{<<"data">> := jsx:decode(NewJson)}; + _ -> + #{} + end, + {200, Amisdata}; + %% 服务器不支持的API接口 do_request(_OperationId, _Args, _Context, _Req) -> - ?LOG(info, "_OperationId:~p~n", [_OperationId]), +%% io:format("~s ~p _OperationId = ~p.~n", [?FILE, ?LINE, _OperationId]), +%% io:format("~s ~p _Args = ~p.~n", [?FILE, ?LINE, _Args]), {error, <<"Not Allowed.">>}. diff --git a/apps/dgiot_modbus/src/dgiot_modbusrtu_tcp.erl b/apps/dgiot_modbus/src/dgiot_modbusrtu_tcp.erl index 8c20b160..d31e7f04 100644 --- a/apps/dgiot_modbus/src/dgiot_modbusrtu_tcp.erl +++ b/apps/dgiot_modbus/src/dgiot_modbusrtu_tcp.erl @@ -175,7 +175,7 @@ create_device(DeviceId, ProductId, DTUMAC, DTUIP, Dtutype) -> {ok, #{<<"ACL">> := Acl, <<"devType">> := DevType}} -> dgiot_device:create_device(#{ <<"devaddr">> => DTUMAC, - <<"name">> => <>, + <<"name">> => <>, <<"ip">> => DTUIP, <<"isEnable">> => true, <<"product">> => ProductId, diff --git a/apps/dgiot_modbus/src/modbus/modbus_rtu.erl b/apps/dgiot_modbus/src/modbus/modbus_rtu.erl index a2a44d29..3e202d5a 100644 --- a/apps/dgiot_modbus/src/modbus/modbus_rtu.erl +++ b/apps/dgiot_modbus/src/modbus/modbus_rtu.erl @@ -274,8 +274,8 @@ set_params(Payload, _ProductId, _DevAddr) -> %rtu modbus parse_frame(<<>>, Acc, _State) -> {<<>>, Acc}; -parse_frame(Buff, _Acc, _State) when length(Buff) < 6 -> - {<<>>, #{}}; +parse_frame(Buff, Acc, _State) when size(Buff) < 6 -> + {<<>>, Acc}; parse_frame(<> = Buff, Acc, #{<<"addr">> := DtuAddr} = State) -> @@ -614,6 +614,7 @@ format_value(Buff, #{<<"identifier">> := BitIdentifier, <<"registersnumber">> := Num, <<"originaltype">> := Originaltype} } -> + IntOffset = dgiot_utils:to_int(Offset), IntNum = dgiot_utils:to_int(Num), IntLen = get_len(IntNum, Originaltype),