This commit is contained in:
2021-04-22 00:00:13 +02:00
parent a652d42472
commit 7a668cbd80
7 changed files with 63 additions and 14 deletions

View File

@@ -1,16 +1,39 @@
import { NodeDef, NodeInitializer, Node } from "node-red";
import { Ts3ConfigConfig, Ts3ConfigNode } from "./types";
import { TeamSpeak, QueryProtocol } from "ts3-nodejs-library"
const nodeInit: NodeInitializer = (RED): void => {
function Ts3Config(
this: Ts3ConfigNode,
config: Ts3ConfigConfig
): void {
RED.nodes.createNode(this, config);
this.host = config.host
this.nickname = config.nickname
RED.nodes.createNode(this, config)
// TODO connect ts3 query here
const host = config.host
const nickname = config.nickname
let client: TeamSpeak | null = null
let connectPromise: Promise<TeamSpeak> | null = null
this.getConnection = async (): Promise<TeamSpeak>=>{
if (client){
return client
}else if (connectPromise){
return connectPromise
}else{
connectPromise = TeamSpeak.connect({
host: host,
protocol: QueryProtocol.RAW,
queryport: 10011,
serverport: 9987,
username: this.credentials.username,
password: this.credentials.password,
nickname: nickname
})
this.emit("connected")
return await connectPromise
}
}
}
RED.nodes.registerType("ts3-config", Ts3Config,{

View File

@@ -1,4 +1,5 @@
import { NodeDef, Node } from "node-red";
import { TeamSpeak } from "ts3-nodejs-library"
export interface Ts3ConfigProps{
host: string,
@@ -10,6 +11,11 @@ export interface Ts3ConfigCreds {
password: string
}
export interface Ts3ConfigData {
getConnection(): Promise<TeamSpeak>
}
export interface Ts3ConfigConfig extends NodeDef, Ts3ConfigProps, Ts3ConfigCreds { }
export interface Ts3ConfigNode extends Node<Ts3ConfigCreds>, Ts3ConfigProps {}
export interface Ts3ConfigNode extends Node<Ts3ConfigCreds>, Ts3ConfigData {}

View File

@@ -8,12 +8,18 @@ const nodeInit: NodeInitializer = (RED): void => {
config: Ts3GetConfig
): void {
RED.nodes.createNode(this, config);
this.warn(config.configid)
this.ts3Config = RED.nodes.getNode(config.configid) as Ts3ConfigNode
this.on("input", (msg) => {
msg.payload = this.ts3Config.credentials.username
this.send(msg)
const ts3Config = RED.nodes.getNode(config.configid) as Ts3ConfigNode
this.on("input", async (msg,send,done) => {
const client = await ts3Config.getConnection()
let clients = await client.clientList({ clientType: 0 })
msg.payload = clients
send(msg)
done()
})
}

View File

@@ -6,7 +6,6 @@ export interface Ts3GetProps {
}
export interface Ts3GetData {
ts3Config: Ts3ConfigNode
}
export interface Ts3GetConfig extends NodeDef, Ts3GetProps { }