【ChirpStack 】如何获取 JWT TOKEN并利用 API 下发数据?

LoRa App Server 提供了两类 API 接口,其中 RESTful JSON API 提供了一个 API console,在AS地址的基础上使用 /api 即可访问,罗列了 API 端点和文档介绍,测试起来非常方便。

本文主要介绍 如何使用 chirpstack 的API 进行测试以及如何获取 JWT TOKEN。

首先展示效果:

在这里插入图片描述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_39217004/article/details/130587131
————————————————
版权声明:本文为CSDN博主「Ethernet_Comm」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_39217004/article/details/130587131

1.ChirpStack Application Server REST API 如何使用?

1.1 直接使用报错

我在本地搭建的 chirpstack 环境,直接使用 api 的步骤如下:

  • 启动 chirpstack 服务后在访问 http://127.0.0.1:8080/api

  • 利用api接口, get /api/application 时,报错。

比如,我想获取所有的 application,则直接请求这个 api接口时,会报错以下信息,具体的操作如图:

image-20230509191549537

cp1

为何会报错呢?根据官方的信息介绍,是需要认证才可以。

1.2 获取token认证

官方介绍: RESTful JSON API

大部分 API 端点都需要有效的 JWT token 才能使用,它们可以填写在右上角的输入区域。

根据项目介绍,API 的认证机制采用了 JSON web-tokens。

我们可以简单使用 login API 来得到一个短期的 token,也可以使用如下方式得到一个更灵活的 token。

认证过程参考:

LoRaWAN LTE-M Gateway Outdoor version

LoRaServer 笔记 2.4 使用 RESTful JSON API

1.2.1 获取 JWT TOKEN

  1. InternalService块中,利用的方法获取 JWT TOKEN,在 POST /api/internal/login中操作如下:输入用户名密码,生成密钥

image-20230509192131271

image-20230509192250632

cp2

1.2.2 将 JWT TOKEN 输入web网页,并刷新页面

注意:输入 token后不要点击回车,直接刷新页面

cp3

1.2.3 请求 api 接口,获取到数据

比如我的 chirpstackapplication 中创建了 2 个应用,我通过请求以下两个 api 可以获取 application 的信息

cp4

1.3 整个流程如下描述

e. Chirpstack API

A web interface allows you to communicate with the Chirpstack API, to do this, simply enter 192.168.0.1:8080/api in the search bar of the browser:

This interface allows for example to send a downlink message, to add or remove a device from a program outside Chirpstack.

Before performing any operation, it is necessary to obtain a JWT TOKEN, for this you must go to the InternalService section, then POST /api/internal/login and fill in the body field as below.

Then by clicking on Try it out! a response containing the JWT TOKEN is generated in JSON form:

{ "jwt":"JWT_TOKEN" }

The JWT_TOKEN can then be copied and pasted in the box provided at the top of the page. It is then necessary to refresh the page to take the token into account.

All operations are then released

1.4 注意事项

实测在 搜狗浏览器和系统自带的 Microsof tedge 浏览器以及 google 浏览器 中输入 key之后不要刷新页面,直接进行其他操作请求即可,刷新页面后,key会丢失
火狐浏览器中是可以刷新页面的,刷新页面后key不会丢失

2. 如何利用api下发数据?

功能描述:使用 /api/devices/{device_queue_item.dev_eui}/queue这个指令给节点下发数据,

【前提条件】

  • 节点已经上限
  • 网页已经验证过 JWT TOKEN

注意:以下 data 字段的内容是 base64 编码,表示 hello,可以用 base64在线转换网站

{
  "deviceQueueItem": 
  {
    "confirmed": false,
    "devEUI": "8800011100020003",
    "fCnt": 0,
	"data":"aGVsbG8=",
    "fPort": 10,
  }
}

实验结果描述:

将以上的 JSON 数据填写到 body 栏中,给lm401节点发送数据 hello,节点接收到数据 68 65 6c 6c 6f (hello)

cp5

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
你可以使用Spring WebFlux来调用ChirpStack API。首先,你需要在你的Spring Boot项目中添加WebFlux的依赖。 在你的pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- Spring Boot WebFlux --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <!-- ChirpStack API client --> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.6.2</version> </dependency> </dependencies> ``` 接下来,你需要创建一个ChirpStack API的客户端。你可以使用Swagger Codegen来生成一个基于API定义的Java客户端。 1. 在Swagger Codegen的官方网站上选择ChirpStack API的定义文件(通常是一个OpenAPI规范文件,如JSON或YAML格式)。 2. 使用Swagger Codegen生成Java客户端代码。你可以选择生成Jersey、OkHttp或其他HTTP客户端的代码。确保选择生成Java代码,并将其保存到你的项目中。 在你的Spring Boot应用程序中,创建一个用于调用ChirpStack API的服务类。这个服务类应该使用生成的ChirpStack API客户端来执行API调用。 ```java import io.swagger.client.ApiClient; import io.swagger.client.ApiException; import io.swagger.client.api.DefaultApi; import io.swagger.client.model.Device; import org.springframework.stereotype.Service; @Service public class ChirpStackService { private final DefaultApi chirpStackApi; public ChirpStackService() { // 创建一个ChirpStack API客户端 ApiClient apiClient = new ApiClient(); apiClient.setBasePath("http://your-chirpstack-api-host"); this.chirpStackApi = new DefaultApi(apiClient); } public Device getDevice(String deviceId) throws ApiException { // 调用ChirpStack API获取设备信息 return chirpStackApi.getDevice(deviceId); } } ``` 在你的控制器类中,注入ChirpStackService并使用它来调用ChirpStack API。 ```java import io.swagger.client.model.Device; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/devices") public class DeviceController { private final ChirpStackService chirpStackService; @Autowired public DeviceController(ChirpStackService chirpStackService) { this.chirpStackService = chirpStackService; } @GetMapping("/{deviceId}") public Device getDevice(@PathVariable String deviceId) throws ApiException { return chirpStackService.getDevice(deviceId); } } ``` 这样,你就可以通过调用`GET /devices/{deviceId}`接口来获取ChirpStack设备的信息。 请注意,这只是一个基本的示例。你可能需要根据ChirpStack API的具体要求进行进一步的开发和调整。另外,确保在使用ChirpStack API时提供正确的API端点和身份验证信息。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

积跬步、至千里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值