[feature] integrated Logger

This commit is contained in:
Janis
2023-06-29 21:17:31 +02:00
parent e411c1c126
commit f0290bb44d
3 changed files with 40 additions and 63 deletions

View File

@@ -89,7 +89,7 @@ export class TS5ConnectionHandler implements ITS5ConnectionHandler {
this.ws.onmessage = (event) => { this.ws.onmessage = (event) => {
const data = JSON.parse(event.data); const data = JSON.parse(event.data);
Logger.wsRecieved(data) Logger.wsReceived(data)
switch (data.type) { switch (data.type) {
case "auth": case "auth":
@@ -107,7 +107,6 @@ export class TS5ConnectionHandler implements ITS5ConnectionHandler {
break; break;
case "serverPropertiesUpdated": case "serverPropertiesUpdated":
this.messageHandler.handleServerPropertiesUpdatedMessage(data); this.messageHandler.handleServerPropertiesUpdatedMessage(data);
//this.ws.close();
break; break;
case "connectStatusChanged": case "connectStatusChanged":
this.messageHandler.handleConnectStatusChangedMessage(data); this.messageHandler.handleConnectStatusChangedMessage(data);
@@ -119,7 +118,7 @@ export class TS5ConnectionHandler implements ITS5ConnectionHandler {
this.messageHandler.handleChannelsMessage(data); this.messageHandler.handleChannelsMessage(data);
break; break;
default: default:
console.log(`No handler for event type: ${data.type}`); Logger.log(`No handler for event type: ${data.type}`);
break; break;
} }
}; };

View File

@@ -1,3 +1,4 @@
import Logger from "@/utils/logger";
import { IConnection, IChannel, IClient, ITS5DataHandler } from "@interfaces/teamspeak"; import { IConnection, IChannel, IClient, ITS5DataHandler } from "@interfaces/teamspeak";
@@ -58,88 +59,88 @@ export class TS5DataHandler implements ITS5DataHandler {
// Add data to local lists and update states // Add data to local lists and update states
addConnection(connection: IConnection) { addConnection(connection: IConnection) {
console.log("Adding connection...", connection) Logger.log("Adding connection...", connection)
const existingConnection: IConnection | undefined = this.localConnections.find((localConnection: IConnection) => localConnection.id === connection.id); const existingConnection: IConnection | undefined = this.localConnections.find((localConnection: IConnection) => localConnection.id === connection.id);
if (existingConnection == undefined) { if (existingConnection == undefined) {
this.localConnections.push(connection); this.localConnections.push(connection);
this.updateConnectionsState(); this.updateConnectionsState();
console.log("Connection added") Logger.log("Connection added")
} else { } else {
console.log("Connection already exists") Logger.log("Connection already exists")
} }
} }
addChannel(channel: IChannel) { addChannel(channel: IChannel) {
console.log("Adding channel...", channel) Logger.log("Adding channel...", channel)
const existingChannel: IChannel | undefined = this.localChannels.find((localChannel: IChannel) => localChannel.id === channel.id && localChannel.connection.id === channel.connection.id); const existingChannel: IChannel | undefined = this.localChannels.find((localChannel: IChannel) => localChannel.id === channel.id && localChannel.connection.id === channel.connection.id);
if (existingChannel == undefined) { if (existingChannel == undefined) {
this.localChannels.push(channel); this.localChannels.push(channel);
this.updateChannelsState(); this.updateChannelsState();
console.log("Channel added") Logger.log("Channel added")
} else { } else {
console.log("Channel already exists") Logger.log("Channel already exists")
} }
} }
addClient(client: IClient) { addClient(client: IClient) {
console.log("Adding client...", client) Logger.log("Adding client...", client)
const existingClient: IClient | undefined = this.localClients.find((localClient: IClient) => localClient.id === client.id && localClient.channel?.connection.id === client.channel?.connection.id); const existingClient: IClient | undefined = this.localClients.find((localClient: IClient) => localClient.id === client.id && localClient.channel?.connection.id === client.channel?.connection.id);
if (existingClient == undefined) { if (existingClient == undefined) {
this.localClients.push(client); this.localClients.push(client);
this.updateClientsState(); this.updateClientsState();
console.log("Client added") Logger.log("Client added")
} else { } else {
console.log("Client already exists") Logger.log("Client already exists")
} }
} }
// Update data in local lists and update states // Update data in local lists and update states
updateConnection(connection: IConnection) { updateConnection(connection: IConnection) {
console.log("Updating connection...", connection) Logger.log("Updating connection...", connection)
const existingConnection: IConnection | undefined = this.localConnections.find((localConnection: IConnection) => localConnection.id === connection.id); const existingConnection: IConnection | undefined = this.localConnections.find((localConnection: IConnection) => localConnection.id === connection.id);
if (existingConnection !== undefined) { if (existingConnection !== undefined) {
this.localConnections[this.localConnections.indexOf(existingConnection)] = connection; this.localConnections[this.localConnections.indexOf(existingConnection)] = connection;
this.updateConnectionsState(); this.updateConnectionsState();
console.log("Connection updated") Logger.log("Connection updated")
} else { } else {
console.log("Connection does not exist") Logger.log("Connection does not exist")
} }
} }
updateChannel(channel: IChannel) { updateChannel(channel: IChannel) {
console.log("Updating channel...", channel) Logger.log("Updating channel...", channel)
const existingChannel: IChannel | undefined = this.localChannels.find((localChannel: IChannel) => localChannel.id === channel.id && localChannel.connection.id === channel.connection.id); const existingChannel: IChannel | undefined = this.localChannels.find((localChannel: IChannel) => localChannel.id === channel.id && localChannel.connection.id === channel.connection.id);
if (existingChannel !== undefined) { if (existingChannel !== undefined) {
this.localChannels[this.localChannels.indexOf(existingChannel)] = channel; this.localChannels[this.localChannels.indexOf(existingChannel)] = channel;
this.updateChannelsState(); this.updateChannelsState();
console.log("Channel updated") Logger.log("Channel updated")
} else { } else {
console.log("Channel does not exist") Logger.log("Channel does not exist")
} }
} }
updateClient(client: IClient) { updateClient(client: IClient) {
console.log("Updating client...", client) Logger.log("Updating client...", client)
const existingClient: IClient | undefined = this.localClients.find((localClient: IClient) => localClient.id === client.id && localClient.channel?.connection.id === client.channel?.connection.id); const existingClient: IClient | undefined = this.localClients.find((localClient: IClient) => localClient.id === client.id && localClient.channel?.connection.id === client.channel?.connection.id);
if (existingClient !== undefined) { if (existingClient !== undefined) {
this.localClients[this.localClients.indexOf(existingClient)] = client; this.localClients[this.localClients.indexOf(existingClient)] = client;
this.updateClientsState(); this.updateClientsState();
console.log("Client updated") Logger.log("Client updated")
} else { } else {
console.log("Client does not exist") Logger.log("Client does not exist")
} }
} }
// Remove data from local lists and update states // Remove data from local lists and update states
removeConnection(connection: IConnection) { removeConnection(connection: IConnection) {
console.log("Removing connection...", connection) Logger.log("Removing connection...", connection)
const existingConnection: IConnection | undefined = this.localConnections.find((localConnection: IConnection) => localConnection.id === connection.id); const existingConnection: IConnection | undefined = this.localConnections.find((localConnection: IConnection) => localConnection.id === connection.id);
if (existingConnection !== undefined) { if (existingConnection !== undefined) {
@@ -152,14 +153,14 @@ export class TS5DataHandler implements ITS5DataHandler {
this.updateChannelsState(); this.updateChannelsState();
this.updateClientsState(); this.updateClientsState();
this.updateConnectionsState(); this.updateConnectionsState();
console.log("Connection removed") Logger.log("Connection removed")
} else { } else {
console.log("Connection does not exist") Logger.log("Connection does not exist")
} }
} }
removeChannel(channel: IChannel) { removeChannel(channel: IChannel) {
console.log("Removing channel...", channel) Logger.log("Removing channel...", channel)
const existingChannel: IChannel | undefined = this.localChannels.find((localChannel: IChannel) => localChannel.id === channel.id && localChannel.connection.id === channel.connection.id); const existingChannel: IChannel | undefined = this.localChannels.find((localChannel: IChannel) => localChannel.id === channel.id && localChannel.connection.id === channel.connection.id);
if (existingChannel !== undefined) { if (existingChannel !== undefined) {
@@ -170,22 +171,22 @@ export class TS5DataHandler implements ITS5DataHandler {
this.updateClientsState(); this.updateClientsState();
this.updateChannelsState(); this.updateChannelsState();
console.log("Channel removed") Logger.log("Channel removed")
} else { } else {
console.log("Channel does not exist") Logger.log("Channel does not exist")
} }
} }
removeClient(client: IClient) { removeClient(client: IClient) {
console.log("Removing client...", client) Logger.log("Removing client...", client)
const existingClient: IClient | undefined = this.localClients.find((localClient: IClient) => localClient.id === client.id && localClient.channel?.connection.id === client.channel?.connection.id); const existingClient: IClient | undefined = this.localClients.find((localClient: IClient) => localClient.id === client.id && localClient.channel?.connection.id === client.channel?.connection.id);
if (existingClient !== undefined) { if (existingClient !== undefined) {
this.localClients.splice(this.localClients.indexOf(existingClient), 1); this.localClients.splice(this.localClients.indexOf(existingClient), 1);
this.updateClientsState(); this.updateClientsState();
console.log("Client removed") Logger.log("Client removed")
} else { } else {
console.log("Client does not exist") Logger.log("Client does not exist")
} }
} }

View File

@@ -1,3 +1,4 @@
import Logger from "@/utils/logger";
import { IChannelInfos, IConnection, IChannel, IAuthMessage, IClientInfo, IClientMovedMessage, IClient, IClientPropertiesUpdatedMessage, ITalkStatusChangedMessage, IClientSelfPropertyUpdatedMessage, IServerPropertiesUpdatedMessage, IConnectStatusChangedMessage, IChannelsMessage, ITS5MessageHandler, ITS5DataHandler } from "@interfaces/teamspeak"; import { IChannelInfos, IConnection, IChannel, IAuthMessage, IClientInfo, IClientMovedMessage, IClient, IClientPropertiesUpdatedMessage, ITalkStatusChangedMessage, IClientSelfPropertyUpdatedMessage, IServerPropertiesUpdatedMessage, IConnectStatusChangedMessage, IChannelsMessage, ITS5MessageHandler, ITS5DataHandler } from "@interfaces/teamspeak";
// Handle incoming messages from TS5 client // Handle incoming messages from TS5 client
@@ -36,8 +37,6 @@ export class TS5MessageHandler implements ITS5MessageHandler {
// This message is sent by the TS5 server when the client is connected // This message is sent by the TS5 server when the client is connected
// It contains the initial data // It contains the initial data
handleAuthMessage(data: IAuthMessage) { handleAuthMessage(data: IAuthMessage) {
console.log("handleAuthMessage", data);
localStorage.setItem("apiKey", data.payload.apiKey); localStorage.setItem("apiKey", data.payload.apiKey);
// Process auth payload and add initial data // Process auth payload and add initial data
@@ -67,8 +66,6 @@ export class TS5MessageHandler implements ITS5MessageHandler {
// This message is sent by the TS5 server when a client moves a channel OR joins/leaves the server // This message is sent by the TS5 server when a client moves a channel OR joins/leaves the server
handleClientMovedMessage(data: IClientMovedMessage) { handleClientMovedMessage(data: IClientMovedMessage) {
console.log("handleClientMoved", data);
const client: IClient | undefined = this.dataHandler.getClientById(data.payload.clientId, data.payload.connectionId); const client: IClient | undefined = this.dataHandler.getClientById(data.payload.clientId, data.payload.connectionId);
@@ -76,7 +73,6 @@ export class TS5MessageHandler implements ITS5MessageHandler {
if (+data.payload.oldChannelId == 0) { // Create new client(when connecting to server) if (+data.payload.oldChannelId == 0) { // Create new client(when connecting to server)
//set timout to wait for channels to be created //set timout to wait for channels to be created
setTimeout(() => { setTimeout(() => {
console.log("---> New Client created")
const newChannel = this.dataHandler.getChannelById(data.payload.newChannelId, data.payload.connectionId); const newChannel = this.dataHandler.getChannelById(data.payload.newChannelId, data.payload.connectionId);
if (newChannel !== undefined) { if (newChannel !== undefined) {
this.dataHandler.addClient( this.dataHandler.addClient(
@@ -86,25 +82,23 @@ export class TS5MessageHandler implements ITS5MessageHandler {
channel: newChannel, channel: newChannel,
properties: data.payload.properties, properties: data.payload.properties,
}); });
Logger.ts(`New Client found (${data.payload.connectionId} - ${data.payload.clientId} - ${data.payload.properties.nickname})`)
} }
}, 2000); }, 2000);
} else {//* This gets called when a client moves a channel OR joins/leaves the server } else {//* This gets called when a client moves a channel OR joins/leaves the server
const newChannel: IChannel | undefined = this.dataHandler.getChannelById(data.payload.newChannelId, data.payload.connectionId); const newChannel: IChannel | undefined = this.dataHandler.getChannelById(data.payload.newChannelId, data.payload.connectionId);
if (newChannel === undefined || newChannel.id === 0) { if (newChannel === undefined || newChannel.id === 0) {
console.log("---> Client left") Logger.ts(`Client left (${data.payload.connectionId} - ${data.payload.clientId} - ${data.payload.properties.nickname})`)
if (client !== undefined) { if (client !== undefined) {
this.dataHandler.removeClient(client); this.dataHandler.removeClient(client);
} }
return; return;
} }
if (client !== undefined) { // Client already exists if (client !== undefined) { // Client already exists
Logger.ts(`Client moved (${client.channel.connection.id} - ${client.id} - ${client.properties.nickname})`)
// Client moved
console.log("---> Client moved")
this.dataHandler.updateClient({ this.dataHandler.updateClient({
...client, ...client,
@@ -113,7 +107,8 @@ export class TS5MessageHandler implements ITS5MessageHandler {
} else { // Client does not exist } else { // Client does not exist
// Client joined // Client joined
console.log("---> New Client joined") Logger.ts(`Client joined (${data.payload.connectionId} - ${data.payload.clientId} - ${data.payload.properties.nickname})`)
this.dataHandler.addClient( this.dataHandler.addClient(
{ {
id: data.payload.clientId, id: data.payload.clientId,
@@ -127,8 +122,6 @@ export class TS5MessageHandler implements ITS5MessageHandler {
} }
handleClientPropertiesUpdatedMessage(data: IClientPropertiesUpdatedMessage) { handleClientPropertiesUpdatedMessage(data: IClientPropertiesUpdatedMessage) {
console.log("handleClientPropertiesUpdate", data);
const client: IClient | undefined = this.dataHandler.getClientById(data.payload.clientId, data.payload.connectionId); const client: IClient | undefined = this.dataHandler.getClientById(data.payload.clientId, data.payload.connectionId);
if (client !== undefined) { if (client !== undefined) {
@@ -140,8 +133,6 @@ export class TS5MessageHandler implements ITS5MessageHandler {
} }
handleTalkStatusChangedMessage(data: ITalkStatusChangedMessage) { handleTalkStatusChangedMessage(data: ITalkStatusChangedMessage) {
console.log("handleTalkStatusChanged", data);
const client: IClient | undefined = this.dataHandler.getClientById(data.payload.clientId, data.payload.connectionId); const client: IClient | undefined = this.dataHandler.getClientById(data.payload.clientId, data.payload.connectionId);
if (client !== undefined) { if (client !== undefined) {
@@ -151,14 +142,12 @@ export class TS5MessageHandler implements ITS5MessageHandler {
}); });
} }
console.log(this.dataHandler.localConnections) // console.log(this.dataHandler.localConnections)
console.log(this.dataHandler.localChannels) // console.log(this.dataHandler.localChannels)
console.log(this.dataHandler.localClients) // console.log(this.dataHandler.localClients)
} }
handleClientSelfPropertyUpdatedMessage(data: IClientSelfPropertyUpdatedMessage) { handleClientSelfPropertyUpdatedMessage(data: IClientSelfPropertyUpdatedMessage) {
console.log("handleClientSelfPropertyUpdated", data);
const connection: IConnection | undefined = this.dataHandler.getConnectionById(this.activeConnectionId); const connection: IConnection | undefined = this.dataHandler.getConnectionById(this.activeConnectionId);
if (data.payload.flag == "inputHardware" || connection == undefined) { // sadly thats the only way to detect if a server is active or not if (data.payload.flag == "inputHardware" || connection == undefined) { // sadly thats the only way to detect if a server is active or not
@@ -167,8 +156,6 @@ export class TS5MessageHandler implements ITS5MessageHandler {
} }
handleServerPropertiesUpdatedMessage(data: IServerPropertiesUpdatedMessage) { handleServerPropertiesUpdatedMessage(data: IServerPropertiesUpdatedMessage) {
console.log("handleServerPropertiesUpdated", data);
const connection: IConnection | undefined = this.dataHandler.getConnectionById(data.payload.connectionId); const connection: IConnection | undefined = this.dataHandler.getConnectionById(data.payload.connectionId);
if (connection !== undefined) { // Update existing connection if (connection !== undefined) { // Update existing connection
@@ -180,8 +167,6 @@ export class TS5MessageHandler implements ITS5MessageHandler {
} }
handleConnectStatusChangedMessage(data: IConnectStatusChangedMessage) { handleConnectStatusChangedMessage(data: IConnectStatusChangedMessage) {
console.log("handleConnectStatusChanged", data);
if (data.payload.status === 0) { // Disconnected from server if (data.payload.status === 0) { // Disconnected from server
const connection: IConnection | undefined = this.dataHandler.getConnectionById(data.payload.connectionId); const connection: IConnection | undefined = this.dataHandler.getConnectionById(data.payload.connectionId);
@@ -201,19 +186,11 @@ export class TS5MessageHandler implements ITS5MessageHandler {
} }
handleChannelsMessage(data: IChannelsMessage) { handleChannelsMessage(data: IChannelsMessage) {
console.log("handleChannels", data);
// Wait a bit for the connection to be added // Wait a bit for the connection to be added
setTimeout(() => { setTimeout(() => {
console.log(this.dataHandler.localConnections);
console.log(data.payload.connectionId)
console.log(this.dataHandler.localConnections.filter((connection: IConnection) => connection.id === data.payload.connectionId)[0]);
console.log(this.dataHandler.localConnections.find((connection: IConnection) => connection.id === data.payload.connectionId));
const connection: IConnection | undefined = this.dataHandler.getConnectionById(data.payload.connectionId); const connection: IConnection | undefined = this.dataHandler.getConnectionById(data.payload.connectionId);
console.log(connection);
if (connection !== undefined) { if (connection !== undefined) {
this.parseChannelInfos(data.payload.info, connection); this.parseChannelInfos(data.payload.info, connection);
console.log(data.payload.info)
} }
}, 1000); }, 1000);
} }