device status
Closed this issue · 5 comments
willin commented
https://github.com/Azure/azure-event-hubs-node/blob/master/send_receive/lib/receiver.js#L35
are there any events can listen device online/offline?
anthonyvercolano commented
Willin, could you please give a bit more of the scenario that you are thinking of?
willin commented
CREATE TABLE `device` (
`did` varchar(32) NOT NULL DEFAULT '',
`state` tinyint(1) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`did`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
state: 0 means offline, state: 1 means online.
when device connected to iothub(via mqtt or any else), a trigger event update the table set state:1, disconnected set state:0.
willin commented
here is an example of sending message
const clientFromConnectionString = require('azure-iot-device-mqtt').clientFromConnectionString;
const Message = require('azure-iot-device').Message;
const connectionString = 'HostName=[修改连接主机];DeviceId=[deviceID];SharedAccessKey=[连接密钥]';
const client = clientFromConnectionString(connectionString);
function printResultFor(op) {
return function printResult(err, res) {
if (err) console.log(`${op} error: ${err.toString()}`);
if (res) console.log(`${op} status: ${res.constructor.name}`);
};
}
const connectCallback = function (err) {
if (err) {
console.log(`Could not connect: ${err}`);
} else {
console.log('Client connected');
// Create a message and send it to the IoT Hub every second
setInterval(() => {
const windSpeed = 10 + (Math.random() * 4);
const data = JSON.stringify({ deviceId: 'myFirstNodeDevice', windSpeed });
const message = new Message(data);
// 随机发送到路由或默认事件上
if (Math.round(Math.random()) === 1) {
message.properties.add('route', 'test');
}
console.log(`Sending message: ${message.getData()}`);
client.sendEvent(message, printResultFor('send'));
}, 1000);
}
};
client.open(connectCallback);
how to send mqtt Last Will
/ Testament
message to specific route?
willin commented
const receiver = await client.createReceiver('$Default', partitionId, {
// how to start after last disconnected time
startAfterTime: Date.now() // this problem is also very important too.
});
pierreca commented
@willin I believe this is the same question as Azure/azure-iot-sdk-node#65. If that's the case please let's just leave only one open. I replied over there.