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

5次阅读

共计 2849 个字符,预计需要花费 8 分钟才能阅读完成。

设施在线 / 离线状态缓存

很多场景中,咱们都须要查问设施是否在线,但 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​​

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

正文完
 0