关于存储:设备在线离线状态的缓存方案实践类

设施在线/离线状态缓存

很多场景中,咱们都须要查问设施是否在线,但POP API的拜访频次受限,须要咱们本人零碎缓存设施状态

技术计划

  1. 配置规定引擎监听设施状态变更音讯,流转到函数计算FC
  2. 因为音讯乱序,在函数计算比照表格存储/Redis中设施状态和以后状态的lastTime,存储最新的数据
  3. 业务零碎从表格存储/Redis中疾速查问设施以后在线/离线状态

1.设施在线/离线状态变更音讯

当设施连贯到IoT物联网平台,设施离线,在线状态变更会生成特定topic的音讯,咱们服务端能够通过订阅这个topic取得设施状态变更信息。**

设施的在线/离线状态流转的Topic格局:

/as/mqtt/status/{productKey}/{deviceName}

payload数据格式:

{
    "status":"online|offline",
    "productKey":"pk13543",
    "deviceName":"dn1234",
    "time":"2018-08-31 15:32:28.205",
    "utcTime":"2018-08-31T07:32:28.205Z",
    "lastTime":"2018-08-31 15:32:28.195",
    "utcLastTime":"2018-08-31T07:32:28.195Z",
    "clientIp":"123.123.123.123"
}

参数阐明:

2.通过规定引擎流转设施状态

2.1 配置SQL

SELECT productKey,deviceName,
timestamp() as timestamp ,
status,
time as currentTime ,lastTime,clientIp
FROM "/as/mqtt/status/a1Xr8ofpSst/+" WHERE 

这样咱们就能够从音讯体获取到设施的status,currentTime和lastTime了。
规定引擎数据处理操作界面

2.2 配置数据流转函数计算FC

规定引擎数据流转操作界面

2.3 函数计算FC实现

nodejs实现代码参考:
因为音讯乱序,须要比照表格存储/Redis中已存储的设施状态和以后状态的lastTime,找出最新状态存储到缓存中。

var TableStore = require('tablestore');

var options = {
    accessKeyId: '你的accessKeyId',
    accessKeySecret: '你的accessKeySecret',
}

var otsClient = new TableStore.Client({
    accessKeyId: options.accessKeyId,
    secretAccessKey: options.accessKeySecret,
    endpoint: '你的endpoint',
    instancename: '你的instancename',
    maxRetries: 20 //默认20次重试,能够省略这个参数。
});

var response = {
    isBase64Encoded: false,
    statusCode: 200
};

module.exports.handler = function(event, context, callback) {

    var eventJson = JSON.parse(event.toString());
    var deviceId = eventJson.deviceName;
    var productKey = eventJson.productKey;
    var lastTime = new Date(eventJson.lastTime);

    var params = {
        tableName: "device_status_table",
        primaryKey: [{ 'deviceId': deviceId },{'productKey': productKey}],
        maxVersions: 1
    };

    try {
        otsClient.getRow(params, function(err, data) {
            if (err) {
                response.body = { msg: 'error', code: 404 };
                callback(null, response);
                return;
            }
            //有数据,拿进去比拟lastTime
            if (data.row.primaryKey) {
                var attributes = data.row.attributes;
                var dbTime = '';
                attributes.forEach(function(item) {
                    if (item.columnName == 'lastTime') {
                        dbTime = new Date(item.columnValue);
                    }
                })
                //转换成毫秒进行比拟
                if (lastTime.getTime() < dbTime.getTime()) {

                    return;
                }
            }

            var iot_data = {
                tableName: "device_status_table",
                condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),

                primaryKey: [{ "deviceId": deviceId },{'productKey': productKey}],

                attributeColumns: [
                    { 'lastTime': eventJson.lastTime },
                    { 'clientIp': eventJson.clientIp },
                    { 'status': eventJson.status }
                ],

                returnContent: { returnType: TableStore.ReturnType.Primarykey }
            }

            otsClient.putRow(iot_data, function(err, data) {
                if (err) {
                    response.body = { msg: 'error', code: 404 };
                    callback(null, response);
                    return;
                }

                response.body = { msg: 'ok', code: 200 };
                callback(null, response);
                return;
            });

        });

    } catch (err) {
        response.body = { msg: 'error', code: 404 };
        callback(null, response);
    }

};

2.4 表格存储OTS

在device_status_table表中,查看设施高低线状况

【往期回顾】

​​1、39张IoT传感器工作原理GIF图汇总​​
​​2、IoT 设施发送 MQTT 申请的波折经验​​​​​​
​​3、20元体 Arduino 环境监测仪开发​​
​​​4、智能手持测温枪开发实际​​​​
​​​​5、JMeter 压测 MQTT 服务性能实战​​

物联网平台产品介绍详情:​​https://www.aliyun.com/product/iot/iot_instc_public_cn​​

              阿里云物联网平台客户交换群

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理