thingsboard 测试地址:https://www.iotschool.com/topics/8
测试代码如下:
attributes-demo.js
// requires node.js and mqtt library installed.
var mqtt = require('mqtt');
var os = require("os");
const thingsboardhost = "host_address";
// reads the access token from arguments
const accesstoken = "c1_test_token";
// const accesstoken = process.argv[2];
// default topics. see http://thingsboard.io/docs/reference/mqtt-api/ for more details.
const attributestopic = 'v1/devices/me/attributes';
const telemetrytopic = 'v1/devices/me/telemetry';
const attributesrequesttopic = 'v1/devices/me/attributes/request/1';
const attributesresponsetopic = attributesrequesttopic.replace('request', 'response');
// initialization of mqtt client using thingsboard host and device access token
console.log('connecting to: %s using access token: %s', thingsboardhost, accesstoken);
var client = mqtt.connect('mqtt://' thingsboardhost, {username: accesstoken});
var firmwareversion = '1.0.1';
var appstate;
// telemetry upload is once per 5 seconds by default;
var currentfrequency = 5;
var uploadinterval;
// triggers when client is successfully connected to the thingsboard server
client.on('connect', function () {
console.log('client connected!');
// upload firmware version and serial number as device attribute using 'v1/devices/me/attributes' mqtt topic
client.publish(attributestopic, json.stringify({
'firmwareversion': '1.0.1',
'serialnumber': 'sn-' random()
}));
// subscribe to shared attribute changes on the server side
client.subscribe(attributestopic);
// publish request for 'appstate' client attribute
// and two shared attributes 'uploadfrequency' and 'latestversion'
client.publish(attributesrequesttopic, json.stringify({
'clientkeys': 'appstate',
'sharedkeys': 'uploadfrequency,latestfirmwareversion'
}));
// schedule os stats upload
console.log('uploading os stats with interval %s (sec)...', currentfrequency);
uploadinterval = setinterval(uploadstats, currentfrequency * 1000);
});
client.on('message', function (topic, message) {
if (topic === attributestopic) {
// process attributes update notification
console.log('received attribute update notification: %s', message.tostring());
var data = json.parse(message);
if (data.uploadfrequency && data.uploadfrequency != currentfrequency) {
// reschedule upload using new frequency
reschedulestatsupload(data.uploadfrequency);
}
if (data.latestfirmwareversion && data.latestfirmwareversion != firmwareversion) {
// received new upload frequency configuration
console.log('new firmware version is available: %s', data.latestfirmwareversion);
}
} else if (topic === attributesresponsetopic) {
// process response to attributes request
console.log('received response to attribute request: %s', message.tostring());
var data = json.parse(message);
if (data.client && data.client.appstate) {
appstate = data.client.appstate;
console.log('restore app state to: %s', appstate);
} else {
appstate = random();
console.log('this is first application launch. going to publish random application state: %s', appstate);
client.publish(attributestopic, json.stringify({'appstate': appstate}));
}
if (data.shared) {
if (data.shared.uploadfrequency && data.shared.uploadfrequency != currentfrequency) {
// received new upload frequency configuration
reschedulestatsupload(data.shared.uploadfrequency);
}
if (data.shared.latestfirmwareversion && data.shared.latestfirmwareversion != firmwareversion) {
// received new upload frequency configuration
console.log('new firmware version is available: %s', data.shared.latestfirmwareversion);
}
}
}
})
// reschedule of stats upload timer
function reschedulestatsupload(uploadfrequency) {
clearinterval(uploadinterval);
currentfrequency = uploadfrequency;
console.log('uploading os stats with new interval %s (sec)...', currentfrequency);
uploadinterval = setinterval(uploadstats, currentfrequency * 1000);
}
// upload os stats using 'v1/devices/me/telemetry' mqtt topic
function uploadstats() {
var data = {};
data.type = os.type();
data.uptime = os.uptime();
data.mem = os.freemem() / os.totalmem();
// console.log('publishing os info & stats: %s', json.stringify(data));
client.publish(telemetrytopic, json.stringify(data));
}
function random() {
return math.floor(math.random() * 1000);
}
// catches ctrl c event
process.on('sigint', function () {
console.log();
console.log('disconnecting...');
client.end();
console.log('exited!');
process.exit(2);
});
// catches uncaught exceptions
process.on('uncaughtexception', function (e) {
console.log('uncaught exception...');
console.log(e.stack);
process.exit(99);
});
2.代码分析:
client.publish(attributestopic, json.stringify({
'firmwareversion': '1.0.1',
'serialnumber': 'sn-' random()
}));
给 device 新增 client attribute 2 个,firmwareversion 和 serialnumber。
client.subscribe(attributestopic);
订阅属性变更频道,这样属性的相关变化都会接受到。
client.publish(attributesrequesttopic, json.stringify({
'clientkeys': 'appstate',
'sharedkeys': 'uploadfrequency,latestfirmwareversion'
}));
发布了一个查询的请求,查询内容是 client 属性 appstate 以及 shared 属性 uploadfrequency,latestfirmwareversion,这个结果将会在 message 里面接受到。
测试
message 的代码逻辑注释都很清楚,就不赘述了。
下面进入测试:
1.在你的 device 下,新增 shared 属性 uploadfrequency,latestfirmwareversion;
执行 node attributes-demo.js 后,进入 console。
在 tb 的页面里面,修改 uploadfrequency 属性,可以看到代码上传的频率被修改了。
该例子的视频地址在
感谢:寒碧
原文: