You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.3 KiB
41 lines
1.3 KiB
2 years ago
|
'use strict';
|
||
|
const mqtt = require('mqtt');
|
||
|
|
||
|
module.exports = async function factory (app, opts) {
|
||
|
console.info(`mqtt connecting ${opts.mqtt.mqttVideoServer}`);
|
||
|
|
||
|
const client = mqtt.connect(opts.mqtt.mqttVideoServer);
|
||
|
|
||
|
client.on('connect', function () {
|
||
|
console.info(`mqtt connect success ${opts.mqtt.mqttVideoServer}`);
|
||
|
client.subscribe('topic/test', { qos: 0 });//订阅主题为test的消息
|
||
|
})
|
||
|
client.on('error', function (e) {
|
||
|
console.error(`mqtt connect failed ${opts.mqtt.mqttVideoServer}`);
|
||
|
app.fs.logger.error('info', '[FS-AUTH-MQTT]', `mqtt connect failed ${opts.mqtt.mqttVideoServer}`);
|
||
|
})
|
||
|
|
||
|
client.on('message', async (top, message) => {
|
||
|
let msgStr = message.toString();
|
||
|
let msg = JSON.parse(msgStr.replace(/\\/g, ''));
|
||
|
if (msg.id && msg.online) {
|
||
|
const { cameraStatePush } = app.fs.utils
|
||
|
const { models } = app.fs.dc
|
||
|
const gbCameraRes = await models.GbCamera.findOne({
|
||
|
where: {
|
||
|
id: msg.id
|
||
|
}
|
||
|
})
|
||
|
if (gbCameraRes) {
|
||
|
cameraStatePush({
|
||
|
gbId: msg.id,
|
||
|
online: msg.online,
|
||
|
ipctype: gbCameraRes.ipctype,
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
app.mqttVideoServer = client
|
||
|
}
|