2 Star 10 Fork 5

lora/lorawan-server

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
lorawan_connector_http_in.erl 1.82 KB
一键复制 编辑 原始数据 按行查看 历史
Petr Gotthard 提交于 6年前 . Happy New Year
%
% Copyright (c) 2016-2019 Petr Gotthard <petr.gotthard@centrum.cz>
% All rights reserved.
% Distributed under the terms of the MIT License. See the LICENSE file.
%
-module(lorawan_connector_http_in).
-export([init/2]).
-export([is_authorized/2]).
-export([allowed_methods/2]).
-export([content_types_accepted/2]).
-export([resource_exists/2]).
-export([handle_downlink/2]).
-include("lorawan_db.hrl").
-record(state, {connector, bindings}).
init(Req, [Connector]) ->
Bindings = lorawan_admin:parse(cowboy_req:bindings(Req)),
{cowboy_rest, Req, #state{connector=Connector, bindings=Bindings}}.
is_authorized(Req, State) ->
case lorawan_admin:handle_authorization(Req, {[], [{<<"device:send">>, '*'}]}) of
{true, _AuthFields} ->
{true, Req, State};
Else ->
{Else, Req, State}
end.
allowed_methods(Req, State) ->
{[<<"OPTIONS">>, <<"PUT">>, <<"POST">>], Req, State}.
content_types_accepted(Req, State) ->
{[
{{<<"application">>, <<"octet-stream">>, '*'}, handle_downlink},
{{<<"application">>, <<"json">>, '*'}, handle_downlink},
{{<<"application">>, <<"x-www-form-urlencoded">>, '*'}, handle_downlink}
], Req, State}.
handle_downlink(Req, #state{connector=Connector, bindings=Bindings}=State) ->
{ok, Msg, Req2} = cowboy_req:read_body(Req),
case lorawan_connector:decode_and_downlink(Connector, Msg, Bindings) of
ok ->
{true, Req2, State};
{error, {Object, Error}} ->
lorawan_utils:throw_error(Object, Error),
{stop, cowboy_req:reply(400, Req2), State};
{error, Error} ->
lorawan_utils:throw_error({connector, Connector#connector.connid}, Error),
{stop, cowboy_req:reply(400, Req2), State}
end.
resource_exists(Req, State) ->
{true, Req, State}.
% end of file
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Erlang
1
https://gitee.com/mlora/lorawan-server.git
git@gitee.com:mlora/lorawan-server.git
mlora
lorawan-server
lorawan-server
master

搜索帮助