thingsboard|ThingsBoard 使用 REST API HTTP 方法获取遥测数据 属性数据等

一、方法参考

  1. 想通过http方式拿遥测数据,没有通过设备 访问令牌可以获取到的API,HTTP提供的API,只能拿到属性数据
    可参考:http://www.ithingsboard.com/docs/reference/http-api/
  2. 对此,有2中方法,可以拿到遥测数据
    (1).通过REST API的方法,去拿遥测数据(下文介绍的方法
    (2).通过RPC命令,规则链中加脚本,拿到遥测数据后,再回复RPC命令,但这里可以拿到的数据,是服务器范围,我没搞懂什么意思,就没继续研究了,可参考:http://www.ithingsboard.com/docs/user-guide/rule-engine-2-0/tutorials/rpc-reply-tutorial/#%E5%88%9B%E5%BB%BArelated-thermostat-temperature%E8%A7%84%E5%88%99%E9%93%BE
二、方法步骤
  1. 参考方法的地址
    http://www.ithingsboard.com/docs/api/
    thingsboard|ThingsBoard 使用 REST API HTTP 方法获取遥测数据 属性数据等
    文章图片

    http://www.ithingsboard.com/docs/user-guide/telemetry/#data-query-api
  2. 我们也可以打开一下我们自己的swagger-ui.htm看看所有的命令,着重关注于 /values/timeseries
    【thingsboard|ThingsBoard 使用 REST API HTTP 方法获取遥测数据 属性数据等】thingsboard|ThingsBoard 使用 REST API HTTP 方法获取遥测数据 属性数据等
    文章图片

三、实施步骤
  1. 使用Postman发送HTTP的请求
    注意设置一下Request timeout in ms久一点,具体要看服务器的网络情况,如果太短,会出现失败的情况
    设置位置:File-->Settings
    thingsboard|ThingsBoard 使用 REST API HTTP 方法获取遥测数据 属性数据等
    文章图片

  2. 获取JWT_TOKEN
    配置参数
    地址 http://xxxx:8080/api/auth/login
    方法 POST
    content-type application /json
    BODY 数据 {“username”:“tenant@thingsboard.org”,“password”:“tenant”}
    thingsboard|ThingsBoard 使用 REST API HTTP 方法获取遥测数据 属性数据等
    文章图片

    thingsboard|ThingsBoard 使用 REST API HTTP 方法获取遥测数据 属性数据等
    文章图片

    之后获取到Token
    thingsboard|ThingsBoard 使用 REST API HTTP 方法获取遥测数据 属性数据等
    文章图片

    整个请求的Log如下
    POST http://xxxx:8080/api/auth/loginPOST /api/auth/login HTTP/1.1 content-type: application /json User-Agent: PostmanRuntime/7.26.8 Cache-Control: no-cache Postman-Token: d0d5e779-3880-49b2-972c-24bbab216b3f Host: xxxx:8080 Connection: keep-alive Content-Length: 57{"username":"tenant@thingsboard.org","password":"tenant"}HTTP/1.1 200 OK Content-Length: 944 Cache-Control: no-cache, no-store, max-age=0, must-revalidate Connection: keep-alive Content-Type: application/json; charset=ISO-8859-1 Date: Mon, 23 May 2022 07:08:10 GMT Expires: 0 Keep-Alive: timeout=4 Pragma: no-cache Proxy-Connection: keep-alive Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers X-Content-Type-Options: nosniff X-Xss-Protection: 1; mode=block{"token":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0ZW5hbnRAdGhpbmdzYm9hcmQub3JnIiwic2NvcGVzIjpbIlRFTkFOVF9BRE1JTiJdLCJ1c2VySWQiOiI2N2I1OTZkMC01NWRkLTExZWMtYjg0Ni0wZDY5OTIxNWZlNjUiLCJlbmFibGVkIjp0cnVlLCJpc1B1YmxpYyI6ZmFsc2UsInRlbmFudElkIjoiNjcwMjI1NTAtNTVkZC0xMWVjLWI4NDYtMGQ2OTkyMTVmZTY1IiwiY3VzdG9tZXJJZCI6IjEzODE0MDAwLTFkZDItMTFiMi04MDgwLTgwODA4MDgwODA4MCIsImlzcyI6InRoaW5nc2JvYXJkLmlvIiwiaWF0IjoxNjUzMjg5NjkwLCJleHAiOjE2NTMyOTg2OTB9.GLD9zkXqnh0L1Hvs-s8zUgFACpFRoEr8CDVsslg9mi0DYtNP0I8tzdzlwCdj7dHw0Gki_d4A-cNdBELXtFFwpQ","refreshToken":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0ZW5hbnRAdGhpbmdzYm9hcmQub3JnIiwic2NvcGVzIjpbIlJFRlJFU0hfVE9LRU4iXSwidXNlcklkIjoiNjdiNTk2ZDAtNTVkZC0xMWVjLWI4NDYtMGQ2OTkyMTVmZTY1IiwiaXNQdWJsaWMiOmZhbHNlLCJpc3MiOiJ0aGluZ3Nib2FyZC5pbyIsImp0aSI6IjdlOTk2NzEwLTk2MjctNGM5My1iYWU3LWQyM2YwOTIzMDE2NiIsImlhdCI6MTY1MzI4OTY5MCwiZXhwIjoxNjUzODk0NDkwfQ.x4wBicPBzIO6mOMPVgsGpqG3ygGj26ozLcWymWhxC0ts--3W8DMb-mEGgdVW-IfzXSdvQX-gz4abwPO-GFaJyg"}

  3. 获取遥测数据
    配置参数
    地址 http://xxxx:8080/api/plugins/telemetry/DEVICE/4afd0a80-58cd-11ec-9209-b7aebb52f70b/values/timeseries (注意这里DEVICE后面的是设备ID)(最后可通过 ? 来对数据进行筛选)
    方法 GET
    content-type application /json
    X-Authorization Bearer (注意这里后面有一个空格)加上JWT_TOKEN
    BODY 数据
    thingsboard|ThingsBoard 使用 REST API HTTP 方法获取遥测数据 属性数据等
    文章图片

    整个请求的Log如下
GET http://xxxx/api/plugins/telemetry/DEVICE/4afd0a80-58cd-11ec-9209-b7aebb52f70b/values/timeseriesGET /api/plugins/telemetry/DEVICE/4afd0a80-58cd-11ec-9209-b7aebb52f70b/values/timeseries HTTP/1.1 Content-Type: application/json X-Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0ZW5hbnRAdGhpbmdzYm9hcmQub3JnIiwic2NvcGVzIjpbIlRFTkFOVF9BRE1JTiJdLCJ1c2VySWQiOiI2N2I1OTZkMC01NWRkLTExZWMtYjg0Ni0wZDY5OTIxNWZlNjUiLCJlbmFibGVkIjp0cnVlLCJpc1B1YmxpYyI6ZmFsc2UsInRlbmFudElkIjoiNjcwMjI1NTAtNTVkZC0xMWVjLWI4NDYtMGQ2OTkyMTVmZTY1IiwiY3VzdG9tZXJJZCI6IjEzODE0MDAwLTFkZDItMTFiMi04MDgwLTgwODA4MDgwODA4MCIsImlzcyI6InRoaW5nc2JvYXJkLmlvIiwiaWF0IjoxNjUzMjg2MDE5LCJleHAiOjE2NTMyOTUwMTl9.YWJ4xH5ckX-6lKpaXWFYAl65CmM_LyPb-acCrozSI9X2no9ANEzDqWp4NmmnV3ZQ-kK2ukoI0XwM6KiZy8debw User-Agent: PostmanRuntime/7.26.8 Cache-Control: no-cache Postman-Token: 3e41bf5d-e669-46b2-ab05-9eaa93329ed4 Host: xxxx:8080 Connection: keep-aliveHTTP/1.1 200 OK Connection: close Transfer-Encoding: chunked Cache-Control: no-cache, no-store, max-age=0, must-revalidate Content-Type: application/json Date: Mon, 23 May 2022 07:21:04 GMT Expires: 0 Pragma: no-cache Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers X-Content-Type-Options: nosniff X-Xss-Protection: 1; mode=block{"STATE":[{"ts":1647799599288,"value":"ISP TIMEOUT"}],"time":[{"ts":1649305093493,"value":"1649305093"}],"temp":[{"ts":1647800235226,"value":"30\r\n"}],"IR2_EVT":[{"ts":1649304130498,"value":"57"}],"IR1_EVT":[{"ts":1649296043339,"value":"0"}]}

    推荐阅读