mirror of
https://github.com/unraid/api.git
synced 2026-05-01 04:31:31 -05:00
65 lines
1.3 KiB
TypeScript
65 lines
1.3 KiB
TypeScript
/*!
|
|
* Copyright 2019-2020 Lime Technology Inc. All rights reserved.
|
|
* Written by: Alexis Tyler
|
|
*/
|
|
|
|
import mqtt from 'mqtt';
|
|
import { Notifier, NotifierOptions, NotifierSendOptions } from './notifier';
|
|
|
|
interface Options extends NotifierOptions {
|
|
connectionUri: string;
|
|
username: string;
|
|
password: string;
|
|
topic: string;
|
|
}
|
|
|
|
/**
|
|
* MQTT notifier.
|
|
*/
|
|
export class MqttNotifier extends Notifier {
|
|
private static instance: MqttNotifier;
|
|
|
|
/** The current MQTT client. */
|
|
private readonly client;
|
|
/** The current MQTT topic. */
|
|
private readonly topic;
|
|
|
|
constructor(options: Options) {
|
|
super(options);
|
|
|
|
if (MqttNotifier.instance) {
|
|
// eslint-disable-next-line no-constructor-return
|
|
return MqttNotifier.instance;
|
|
}
|
|
|
|
// Set for later
|
|
this.topic = options.topic;
|
|
|
|
const { connectionUri = 'tcp://localhost:1883', username, password } = options;
|
|
|
|
// Prevents us reconnecting every time we want to send a notification
|
|
this.client = mqtt.connect(connectionUri, {
|
|
username,
|
|
password
|
|
});
|
|
|
|
MqttNotifier.instance = this;
|
|
}
|
|
|
|
async send(options: NotifierSendOptions) {
|
|
const { client, topic } = this;
|
|
const { title, ...rest } = options;
|
|
|
|
// Reconect if needed
|
|
if (!client.connected) {
|
|
await client.reconnect();
|
|
}
|
|
|
|
// Send
|
|
return client.publish(topic, JSON.stringify({
|
|
title,
|
|
...rest
|
|
}));
|
|
}
|
|
}
|