Google cloud platform 使用Stackdriver的Google IOT每设备心跳警报
我想提醒一下,大量谷歌物联网核心设备中的任何一个都没有心跳信号(或接收到0字节)。我在Stackdriver里似乎做不到这一点。相反,它似乎让我在整个设备注册表上发出警报,而该注册表并没有提供我要查找的内容(我如何知道某个特定设备已断开连接?)Google cloud platform 使用Stackdriver的Google IOT每设备心跳警报,google-cloud-platform,stackdriver,google-cloud-iot,Google Cloud Platform,Stackdriver,Google Cloud Iot,我想提醒一下,大量谷歌物联网核心设备中的任何一个都没有心跳信号(或接收到0字节)。我在Stackdriver里似乎做不到这一点。相反,它似乎让我在整个设备注册表上发出警报,而该注册表并没有提供我要查找的内容(我如何知道某个特定设备已断开连接?) 那么,我们该如何做呢?我不知道为什么这个问题被否决为“过于宽泛” 事实上,谷歌物联网没有每台设备的警报,而是只在整个设备注册表上提供警报。如果这不是真的,请回复这个帖子。这一页清楚地说明了这一点: 云物联网核心导出可监控的使用指标 以编程方式或通过Sta
那么,我们该如何做呢?我不知道为什么这个问题被否决为“过于宽泛” 事实上,谷歌物联网没有每台设备的警报,而是只在整个设备注册表上提供警报。如果这不是真的,请回复这个帖子。这一页清楚地说明了这一点: 云物联网核心导出可监控的使用指标 以编程方式或通过Stackdriver监控访问。这些指标 在设备注册表级别聚合。您可以使用Stackdriver 创建仪表板或设置警报 每个设备发出警报的重要性包含在以下承诺中: 有关设备健康和功能的操作信息如下: 确保您的数据收集结构健康、可靠非常重要 表现良好。设备可能位于恶劣的环境中,或者位于 难以进入的位置。为您的客户监控运营情报 物联网设备是保存业务相关数据流的关键 因此,如果全球分散的众多设备中有一台失去了连接,那么今天要获得警报并不容易。一个人需要建立这一点,而这取决于他试图做什么,它将需要不同的解决方案
在我的例子中,我想提醒上次心跳时间或上次事件状态发布是否早于5分钟。为此,我需要运行一个循环函数来扫描设备注册表并定期执行此操作。此API的使用在另一篇SO帖子中进行了概述:作为参考,我刚刚编写了一个Firebase函数来检查设备的在线状态,可能需要一些调整和进一步测试,但要帮助其他人从以下方面入手:
// Example code to call this function
// const checkDeviceOnline = functions.httpsCallable('checkDeviceOnline');
// Include 'current' key for 'current' online status to force update on db with delta
// const isOnline = await checkDeviceOnline({ deviceID: 'XXXX', current: true })
export const checkDeviceOnline = functions.https.onCall(async (data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'You must be logged in to call this function!');
}
// deviceID is passed in deviceID object key
const deviceID = data.deviceID
const dbUpdate = (isOnline) => {
if (('wasOnline' in data) && data.wasOnline !== isOnline) {
db.collection("devices").doc(deviceID).update({ online: isOnline })
}
return isOnline
}
const deviceLastSeen = () => {
// We only want to use these to determine "latest seen timestamp"
const stamps = ["lastHeartbeatTime", "lastEventTime", "lastStateTime", "lastConfigAckTime", "deviceAckTime"]
return stamps.map(key => moment(data[key], "YYYY-MM-DDTHH:mm:ssZ").unix()).filter(epoch => !isNaN(epoch) && epoch > 0).sort().reverse().shift()
}
await dm.setAuth()
const iotDevice: any = await dm.getDevice(deviceID)
if (!iotDevice) {
throw new functions.https.HttpsError('failed-get-device', 'Failed to get device!');
}
console.log('iotDevice', iotDevice)
// If there is no error status and there is last heartbeat time, assume device is online
if (!iotDevice.lastErrorStatus && iotDevice.lastHeartbeatTime) {
return dbUpdate(true)
}
// Add iotDevice.config.deviceAckTime to root of object
// For some reason in all my tests, I NEVER receive anything on lastConfigAckTime, so this is my workaround
if (iotDevice.config && iotDevice.config.deviceAckTime) iotDevice.deviceAckTime = iotDevice.config.deviceAckTime
// If there is a last error status, let's make sure it's not a stale (old) one
const lastSeenEpoch = deviceLastSeen()
const errorEpoch = iotDevice.lastErrorTime ? moment(iotDevice.lastErrorTime, "YYYY-MM-DDTHH:mm:ssZ").unix() : false
console.log('lastSeen:', lastSeenEpoch, 'errorEpoch:', errorEpoch)
// Device should be online, the error timestamp is older than latest timestamp for heartbeat, state, etc
if (lastSeenEpoch && errorEpoch && (lastSeenEpoch > errorEpoch)) {
return dbUpdate(true)
}
// error status code 4 matches
// lastErrorStatus.code = 4
// lastErrorStatus.message = mqtt: SERVER: The connection was closed because MQTT keep-alive check failed.
// will also be 4 for other mqtt errors like command not sent (qos 1 not acknowledged, etc)
if (iotDevice.lastErrorStatus && iotDevice.lastErrorStatus.code && iotDevice.lastErrorStatus.code === 4) {
return dbUpdate(false)
}
return dbUpdate(false)
})
我还创建了一个用于命令的函数,用于向设备发送命令以检查设备是否在线:
export const isDeviceOnline = functions.https.onCall(async (data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'You must be logged in to call this function!');
}
// deviceID is passed in deviceID object key
const deviceID = data.deviceID
await dm.setAuth()
const dbUpdate = (isOnline) => {
if (('wasOnline' in data) && data.wasOnline !== isOnline) {
console.log( 'updating db', deviceID, isOnline )
db.collection("devices").doc(deviceID).update({ online: isOnline })
} else {
console.log('NOT updating db', deviceID, isOnline)
}
return isOnline
}
try {
await dm.sendCommand(deviceID, 'alive?', 'alive')
console.log('Assuming device is online after succesful alive? command')
return dbUpdate(true)
} catch (error) {
console.log("Unable to send alive? command", error)
return dbUpdate(false)
}
})
这也使用了我的修改版设备管理器
,您可以在此要点上找到所有示例代码(以确保使用最新更新,并在此处保留小帖子):
所有这些代码,只是为了检查设备是否在线。。。谷歌发布某种事件或添加一种简单的方式来处理这些事件,都可以轻松地处理这些事件来吧,谷歌一起行动很好的帖子和答案。我花了1个小时试图弄明白这一点,他们似乎有一个设备资源,可以在特定设备上发出警报,但是唯一可用的度量是log_字节,这没有多大意义,因为该设备没有向谷歌发送日志。也许他们正在改进这一点?是的,我希望这也正是我必须要做的事情,这很可笑,因为我的旧实现我刚刚听到了MQTT断开连接并触发了一个事件。。。使用谷歌并不容易。。。但愿我与另一家物联网服务提供商同行:(