This commit is contained in:
lsxredrain 2021-07-19 17:03:24 +08:00
commit e4bf39eaaf
7 changed files with 146 additions and 24 deletions

View File

@ -18,8 +18,6 @@ group.vcon_total = 1
## 集中器每页大小
group.page_size = 1
## Global GC Interval.
##
## Value: Duration

View File

@ -147,6 +147,62 @@
]
}
},
"/wechat_map": {
"get": {
"description": "设备地图",
"parameters": [],
"responses": {
"200": {
"description": "Returns operation status"
},
"400": {
"description": "Bad Request"
},
"403": {
"description": "Forbidden"
},
"500": {
"description": "Server Internal error"
}
},
"summary": "设备地图",
"tags": [
"Wechat"
]
}
},
"/device_info": {
"get": {
"description": "设备详情",
"parameters": [
{
"name": "deviceid",
"in": "query",
"type": "string",
"default": "d357f550ca",
"description": "deviceid"
}
],
"responses": {
"200": {
"description": "Returns operation status"
},
"400": {
"description": "Bad Request"
},
"403": {
"description": "Forbidden"
},
"500": {
"description": "Server Internal error"
}
},
"summary": "设备详情",
"tags": [
"Wechat"
]
}
},
"/sendsubscribe": {
"post": {
"basePath": "/",

View File

@ -99,13 +99,28 @@ do_request(get_wechat_unbind, _Args, #{<<"sessionToken">> := SessionToken}, _Req
{error, <<"Not Allowed.">>}
end;
%% iot_hub : api资源 :wechat解绑
%% iot_hub : api资源 :
%% OperationId:post_login
%% :POST /iotapi/post_login
do_request(get_wechat_index, _Args, #{<<"sessionToken">> := SessionToken}, _Req) ->
?LOG(info, "SessionToken = ~p ", [SessionToken]),
dgiot_wechat:get_wechat_index(SessionToken);
%% iot_hub : api资源 :
%% OperationId:post_login
%% :POST /iotapi/post_login
do_request(get_wechat_map, _Args, #{<<"sessionToken">> := SessionToken}, _Req) ->
?LOG(info, "SessionToken = ~p ", [SessionToken]),
dgiot_wechat:get_wechat_map(SessionToken);
%% iot_hub : api资源 :
%% OperationId:post_login
%% :POST /iotapi/post_login
do_request(get_device_info, #{<<"deviceid">> := Deviceid}, #{<<"sessionToken">> := SessionToken}, _Req) ->
?LOG(info, "SessionToken = ~p ", [SessionToken]),
dgiot_wechat:get_device_info(Deviceid, SessionToken);
%% iot_hub : api资源 :
%% OperationId:post_sendsubscribe
%% :POST /iotapi/post_sendsubscribe

View File

@ -29,23 +29,32 @@
unbind_sns/1,
get_wechat_index/1,
sendSubscribe/2,
sendTemplate/0
sendTemplate/0,
get_wechat_map/1,
get_device_info/2
]).
%% https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
%% wechat绑定
post_sns(UserName, Password, OpenId) ->
case dgiot_parse:login(UserName, Password) of
{ok, #{<<"objectId">> := _UserId, <<"tag">> := #{<<"wechat">> := #{<<"openid">> := OpenId}}}} when size(OpenId) > 0 ->
{error, <<"is bind">>};
{ok, #{<<"objectId">> := UserId, <<"tag">> := Tag, <<"username">> := Name}} ->
dgiot_parse:update_object(<<"_User">>, UserId, #{<<"tag">> => Tag#{<<"wechat">> => #{<<"openid">> => OpenId}}}),
{ok, UserInfo} = dgiot_parse_handler:create_session(UserId, dgiot_auth:ttl(), Name),
{ok, UserInfo};
{error, Msg} ->
{error, Msg}
case dgiot_parse:query_object(<<"_User">>, #{<<"where">> => #{<<"tag.wechat.openid">> => OpenId}}) of
{ok, #{<<"results">> := [#{<<"objectId">> := _UserId, <<"username">> := Name} | _]}} ->
%% {ok, UserInfo} = dgiot_parse_handler:create_session(UserId, dgiot_auth:ttl(), Name),
{error, <<OpenId/binary, " is bind ", Name/binary>>};
_ ->
case dgiot_parse:login(UserName, Password) of
{ok, #{<<"objectId">> := _UserId, <<"tag">> := #{<<"wechat">> := #{<<"openid">> := OPENID}}}} when size(OPENID) > 0 ->
{error, <<UserName/binary, "is bind">>};
{ok, #{<<"objectId">> := UserId, <<"tag">> := Tag, <<"username">> := Name}} ->
dgiot_parse:update_object(<<"_User">>, UserId, #{<<"tag">> => Tag#{<<"wechat">> => #{<<"openid">> => OpenId}}}),
{ok, UserInfo} = dgiot_parse_handler:create_session(UserId, dgiot_auth:ttl(), Name),
{ok, UserInfo};
{error, Msg} ->
{error, Msg}
end
end.
%% wechat解绑
unbind_sns(UserId) ->
NewTag =
@ -163,7 +172,7 @@ get_wechat_index(SessionToken) ->
<<"panalarmDevice">> => 0, <<"unPanalarmDevice">> => 0,
<<"carousel">> => [#{<<"imgurl">> => <<"https://www.baidu.com/img/flexible/logo/pc/peak-result.png">>, <<"webUrl">> => <<"www.baidu.com">>}]}};
_ ->
pass
{error, <<"no device">>}
end.
@ -205,3 +214,43 @@ sendTemplate() ->
_Error ->
_Error
end.
get_wechat_map(SessionToken) ->
case dgiot_parse:query_object(<<"Device">>, #{<<"keys">> => [<<"count(*)">>], <<"limit">> => 1000}, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"results">> := Results}} ->
NewResult =
lists:foldl(fun(X, Acc) ->
case X of
#{<<"objectId">> := ObjectId, <<"name">> := Name, <<"status">> := Status, <<"location">> := #{<<"latitude">> := Latitude, <<"longitude">> := Longitude}} ->
Acc ++ [#{<<"id">> => ObjectId, <<"title">> => Name, <<"status">> => Status, <<"latitude">> => Latitude, <<"longitude">> => Longitude, <<"joinCluster">> => true}];
_ ->
Acc
end
end, [], Results),
{ok, #{<<"results">> => NewResult}};
_ ->
{error, <<"no device">>}
end.
get_device_info(Deviceid, SessionToken) ->
case dgiot_parse:get_object(<<"Device">>, Deviceid, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"product">> := #{<<"objectId">> := ProductId}, <<"basedata">> := Basedata} = Result} ->
NewParams =
case dgiot_parse:get_object(<<"Product">>, ProductId) of
{ok, #{<<"config">> := #{<<"basedate">> := #{<<"params">> := Params}}}} ->
lists:foldl(fun(Param, Acc) ->
Identifier = maps:get(<<"identifier">>, Param),
case maps:find(Identifier, Basedata) of
error ->
Acc;
{ok, Value} ->
Acc ++ [Param#{<<"value">> => Value}]
end
end, [], Params);
_ ->
[]
end,
{ok, Result#{<<"params">> => NewParams}};
_ ->
{error, []}
end.

View File

@ -11,7 +11,8 @@
deviceId = <<>>,
scale = 10,
temperature = 0,
env = <<>>
env = <<>>,
dtutype = <<>>
}).
-define(READ_DISCRETE_INPUTS, 2).

View File

@ -74,7 +74,7 @@
zh => <<"填写正则表达式匹配login"/utf8>>
}
},
<<"DTUTYPE">> => #{
<<"dtutype">> => #{
order => 4,
type => string,
required => true,
@ -123,7 +123,8 @@ init(?TYPE, ChannelId, #{
<<"heartbeat">> := Heartbeat,
<<"regtype">> := Type,
<<"regular">> := Regular,
<<"product">> := Products
<<"product">> := Products,
<<"dtutype">> := Dtutype
} = _Args) ->
[{ProdcutId, App} | _] = get_app(Products),
{Header, Len} = get_header(Regular),
@ -133,7 +134,8 @@ init(?TYPE, ChannelId, #{
head = Header,
len = Len,
app = App,
product = ProdcutId
product = ProdcutId,
dtutype = Dtutype
},
dgiot_data:insert({ChannelId, heartbeat}, {Heartbeat, Port}),
@ -188,6 +190,7 @@ get_header(Regular) ->
lists:foldl(fun(X, {Header, Len}) ->
case X of
"**" -> {Header, Len + length(X)};
"*" -> {Header, Len + length(X)};
_ -> {Header ++ X, Len + length(X)}
end
end, {[], 0},

View File

@ -49,7 +49,7 @@ init(#tcp{state = #state{id = ChannelId}} = TCPState) ->
%% 9C A5 25 CD 00 DB
%% 11 04 02 06 92 FA FE
handle_info({tcp, Buff}, #tcp{socket = Socket, state = #state{id = ChannelId, devaddr = <<>>, head = Head, len = Len, product = ProductId} = State} = TCPState) ->
handle_info({tcp, Buff}, #tcp{socket = Socket, state = #state{id = ChannelId, devaddr = <<>>, head = Head, len = Len, product = ProductId, dtutype = Dtutype} = State} = TCPState) ->
dgiot_bridge:send_log(ChannelId, "DTU revice from ~p", [dgiot_utils:binary_to_hex(Buff)]),
DTUIP = dgiot_utils:get_ip(Socket),
DtuAddr = dgiot_utils:binary_to_hex(Buff),
@ -60,7 +60,7 @@ handle_info({tcp, Buff}, #tcp{socket = Socket, state = #state{id = ChannelId, de
case re:run(DtuAddr, Head, [{capture, first, list}]) of
{match, [Head]} when length(List) == Len ->
{DevId, Devaddr} =
case create_device(DeviceId, ProductId, DtuAddr, DTUIP) of
case create_device(DeviceId, ProductId, DtuAddr, DTUIP, Dtutype) of
{<<>>, <<>>} ->
{<<>>, <<>>};
{DevId1, Devaddr1} ->
@ -70,7 +70,7 @@ handle_info({tcp, Buff}, #tcp{socket = Socket, state = #state{id = ChannelId, de
_Error ->
case re:run(Buff, Head, [{capture, first, list}]) of
{match, [Head]} when length(List1) == Len ->
create_device(DeviceId, ProductId, Buff, DTUIP),
create_device(DeviceId, ProductId, Buff, DTUIP, Dtutype),
{noreply, TCPState#tcp{buff = <<>>, state = State#state{devaddr = Buff}}};
Error1 ->
?LOG(info, "Error1 ~p Buff ~p ", [Error1, dgiot_utils:to_list(Buff)]),
@ -172,7 +172,7 @@ get_deviceid(ProdcutId, DevAddr) ->
dgiot_parse:get_objectid(<<"Device">>, #{<<"product">> => ProdcutId, <<"devaddr">> => DevAddr}),
DeviceId.
create_device(DeviceId, ProductId, DTUMAC, DTUIP) ->
create_device(DeviceId, ProductId, DTUMAC, DTUIP, Dtutype) ->
case dgiot_parse:get_object(<<"Product">>, ProductId) of
{ok, #{<<"ACL">> := Acl, <<"devType">> := DevType}} ->
case dgiot_parse:get_object(<<"Device">>, DeviceId) of
@ -184,14 +184,14 @@ create_device(DeviceId, ProductId, DTUMAC, DTUIP) ->
_ ->
dgiot_device:create_device(#{
<<"devaddr">> => DTUMAC,
<<"name">> => <<"USRDTU", DTUMAC/binary>>,
<<"name">> => <<Dtutype/binary, DTUMAC/binary>>,
<<"ip">> => DTUIP,
<<"isEnable">> => true,
<<"product">> => ProductId,
<<"ACL">> => Acl,
<<"status">> => <<"ONLINE">>,
<<"location">> => #{<<"__type">> => <<"GeoPoint">>, <<"longitude">> => 120.161324, <<"latitude">> => 30.262441},
<<"brand">> => <<"USRDTU">>,
<<"brand">> => Dtutype,
<<"devModel">> => DevType
}),
dgiot_task:save_pnque(ProductId, DTUMAC, ProductId, DTUMAC),