简体   繁体   English

使用 GCP IoT Core 使用 JWT 的 MQTT 上的持久设备连接状态

[英]Persistent device connection status over MQTT with JWT, using GCP IoT Core

I have a few devices that produce data over MQTT sent to GCP IoT core.我有一些设备通过 MQTT 生成数据发送到 GCP IoT 核心。 I want to monitor if the device is currently connected or disconnected from the MQTT broker.我想监控设备当前是否与 MQTT 代理连接或断开连接。 This was already answered elsewhere on SO and this is the approach I'm currently using: monitor Stackdriver logs for CONNECT/DISCONNECT messages and publish them to a separate Pub/Sub topic.这已经在 SO 的其他地方得到了回答,这是我目前使用的方法:监控 Stackdriver 日志中的 CONNECT/DISCONNECT 消息并将它们发布到单独的 Pub/Sub 主题。 This topic is then read by a Firebase Function to update my device online/offline field.然后由 Firebase Function 阅读此主题以更新我的设备在线/离线字段。

This works in theory, but due to the JWT expiration field, I am forced to re-connect my devices every time the token is expired, causing a lot of spurious online/offline triggers.这在理论上可行,但由于 JWT 过期字段,每次令牌过期时我都被迫重新连接我的设备,从而导致大量虚假的在线/离线触发器。 Is there a way perhaps refresh the token while keeping the connection?有没有办法在保持连接的同时刷新令牌? Or a better approach to simply monitor connectivity of a device?还是一种更好的方法来简单地监控设备的连接性?

I can probably use the hearbeat event and only consider device being offline if no hearbeat was received within N amount of minutes, or even periodically sending some kind of ping command to the device.我可能可以使用 heartbeat 事件,并且仅在 N 分钟内未收到任何 heartbeat 或什至定期向设备发送某种 ping 命令时才考虑设备处于脱机状态。 Although, it would be nice to simply know if device is currently connected or not.虽然,很高兴知道设备当前是否已连接。

For a pure MQTT solution, what I was already doing, and what Orlandog suggested is correct, but doesn't solve the underlying problem of receiving DISCONNECT/CONNECT messages everytime a JWT token is refreshed.对于纯粹的 MQTT 解决方案,我已经在做的事情以及 Orlandog 的建议是正确的,但并没有解决每次刷新 JWT 令牌时接收 DISCONNECT/CONNECT 消息的潜在问题。 That's just how the protocol is supposed to work.这就是协议应该如何工作的方式。

What I ended up with is using balena.io on my devices, which runs docker containers with my apps on balena OS, and the OS itself is connected through a VPN.我最终得到的是在我的设备上使用balena.io ,它在 balena OS 上运行 docker 容器和我的应用程序,并且操作系统本身通过 VPN 连接。 So regardless of the MQTT state of my app I can use balena API to monitor device online state.因此,无论我的应用程序的 MQTT state 是什么,我都可以使用 balena API 在线监控设备 state。 This API is then polled using firebase cloud function.然后使用 firebase 云 function 轮询此 API。

To summarise:总结一下:

  • IoT Core app sending/receiving MQTT with JWT refresh as a docker container IoT Core 应用程序发送/接收 MQTT 与 JWT 刷新为 docker 容器
  • Balena OS with configured network, connected to Balena VPN Balena OS 已配置网络,连接到 Balena VPN
  • Balena API which lets you query device status Balena API 可让您查询设备状态
  • Cloud Function which calls balena API and stores values in Firebase云 Function 调用 balena API 并将值存储在 Firebase

Balena is free up to 10 devices and they support a good range of hardware. Balena 最多可免费使用 10 台设备,它们支持多种硬件。

I think that the way you are monitoring the devices through the implementation with Stackdriver logging and PubSub is the most appropriate.我认为您通过 Stackdriver 日志记录和 PubSub 实现监控设备的方式是最合适的。

At the same time, regarding to the expiration of the JWT token, I recommend you verify this documentation , it provides several examples of how to refresh the token and thus prevent devices from being disconnected.同时,关于 JWT 令牌的过期问题,我建议您验证此文档,它提供了几个如何刷新令牌从而防止设备断开连接的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM