mirror of
https://gitee.com/dgiiot/dgiot.git
synced 2024-11-29 18:57:41 +08:00
feat: DGIOT_EDGE_CHANNEL
This commit is contained in:
parent
477c134689
commit
88cb61b619
@ -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) ->
|
||||
|
@ -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
|
||||
|
@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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.">>}.
|
||||
|
@ -175,7 +175,7 @@ create_device(DeviceId, ProductId, DTUMAC, DTUIP, Dtutype) ->
|
||||
{ok, #{<<"ACL">> := Acl, <<"devType">> := DevType}} ->
|
||||
dgiot_device:create_device(#{
|
||||
<<"devaddr">> => DTUMAC,
|
||||
<<"name">> => <<Dtutype/binary, DTUMAC/binary>>,
|
||||
<<"name">> => <<Dtutype/binary, "_", DTUMAC/binary>>,
|
||||
<<"ip">> => DTUIP,
|
||||
<<"isEnable">> => true,
|
||||
<<"product">> => ProductId,
|
||||
|
@ -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(<<MbAddr:8, BadCode:8, ErrorCode:8, Crc:2/binary>> = 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),
|
||||
|
Loading…
Reference in New Issue
Block a user