Skip to content

Commit

Permalink
Dev 1.6.0-dev.16
Browse files Browse the repository at this point in the history
  • Loading branch information
Luligu committed Oct 23, 2024
1 parent ef23e79 commit f040119
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
31 changes: 31 additions & 0 deletions src/matterbridgeWebsocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,33 @@ describe('Matterbridge loadInstance() and cleanup() -bridge mode', () => {
await (matterbridge as any).nodeContext.set('password', '');
}, 60000);

test('Websocket API send ping', async () => {
expect(ws).toBeDefined();
expect(ws.readyState).toBe(WebSocket.OPEN);
const message = JSON.stringify({ id: 1, dst: 'Matterbridge', src: 'Jest test', method: 'ping', params: {} });
ws.send(message);

// Set up a promise to wait for the response
const responsePromise = new Promise((resolve) => {
ws.onmessage = (event) => {
resolve(event.data);
};
});

// Wait for the response
const response = await responsePromise;
expect(response).toBeDefined();
const data = JSON.parse(response as string);
expect(data).toBeDefined();
expect(data.id).toBe(1);
expect(data.src).toBe('Matterbridge');
expect(data.dst).toBe('Jest test');
expect(data.response).toBeDefined();
expect(data.response).toBe('pong');

expect((matterbridge as any).log.log).toHaveBeenCalledWith(LogLevel.DEBUG, expect.stringMatching(/^Received message from websocket client/));
}, 60000);

test('Websocket API send /api/settings', async () => {
expect(ws).toBeDefined();
expect(ws.readyState).toBe(WebSocket.OPEN);
Expand All @@ -244,6 +271,7 @@ describe('Matterbridge loadInstance() and cleanup() -bridge mode', () => {
expect(data).toBeDefined();
expect(data.id).toBe(1);
expect(data.src).toBe('Matterbridge');
expect(data.dst).toBe('Jest test');
expect(data.response).toBeDefined();
expect(data.response.matterbridgeInformation).toBeDefined();
expect(data.response.systemInformation).toBeDefined();
Expand Down Expand Up @@ -271,6 +299,7 @@ describe('Matterbridge loadInstance() and cleanup() -bridge mode', () => {
expect(data).toBeDefined();
expect(data.id).toBe(1);
expect(data.src).toBe('Matterbridge');
expect(data.dst).toBe('Jest test');
expect(data.response).toBeDefined();
expect(data.response.length).toBe(3);

Expand All @@ -297,6 +326,7 @@ describe('Matterbridge loadInstance() and cleanup() -bridge mode', () => {
expect(data).toBeDefined();
expect(data.id).toBe(1);
expect(data.src).toBe('Matterbridge');
expect(data.dst).toBe('Jest test');
expect(data.response).toBeDefined();
expect(data.response.length).toBe(3);

Expand All @@ -323,6 +353,7 @@ describe('Matterbridge loadInstance() and cleanup() -bridge mode', () => {
expect(data).toBeDefined();
expect(data.id).toBe(1);
expect(data.src).toBe('Matterbridge');
expect(data.dst).toBe('Jest test');
expect(data.response).toBeDefined();
expect(data.response.length).toBe(1);

Expand Down
21 changes: 12 additions & 9 deletions src/matterbridgeWebsocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,28 @@ export async function wsMessageHandler(this: Matterbridge, client: WebSocket, me
data = JSON.parse(message.toString());
if (!isValidNumber(data.id) || !isValidString(data.dst) || !isValidString(data.src) || !isValidString(data.method) || !isValidObject(data.params) || data.dst !== 'Matterbridge') {
this.log.error(`Invalid message from websocket client: ${debugStringify(data)}`);
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', error: 'Invalid message' }));
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', dst: data.src, error: 'Invalid message' }));
return;
}
this.log.debug(`Received message from websocket client: ${debugStringify(data)}`);

if (data.method === '/api/login') {
if (data.method === 'ping') {
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', dst: data.src, response: 'pong' }));
return;
} else if (data.method === '/api/login') {
if (!this.nodeContext) {
this.log.error('Login nodeContext not found');
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', error: 'Internal error: nodeContext not found' }));
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', dst: data.src, error: 'Internal error: nodeContext not found' }));
return;
}
const storedPassword = await this.nodeContext.get('password', '');
if (storedPassword === '' || storedPassword === data.params.password) {
this.log.debug('Login password valid');
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', response: { valid: true } }));
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', dst: data.src, response: { valid: true } }));
return;
} else {
this.log.debug('Error wrong password');
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', error: 'Wrong password' }));
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', dst: data.src, error: 'Wrong password' }));
return;
}
} else if (data.method === '/api/settings') {
Expand All @@ -72,11 +75,11 @@ export async function wsMessageHandler(this: Matterbridge, client: WebSocket, me
this.matterbridgeInformation.matterbridgeSessionInformations = Array.from(this.matterbridgeSessionInformations.values());
this.matterbridgeInformation.profile = this.profile;
const response = { systemInformation: this.systemInformation, matterbridgeInformation: this.matterbridgeInformation };
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', response }));
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', dst: data.src, response }));
return;
} else if (data.method === '/api/plugins') {
const response = await this.getBaseRegisteredPlugins();
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', response }));
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', dst: data.src, response }));
return;
} else if (data.method === '/api/devices') {
const devices: { pluginName: string; type: string; endpoint: EndpointNumber | undefined; name: string; serial: string; uniqueId: string; cluster: string }[] = [];
Expand All @@ -99,11 +102,11 @@ export async function wsMessageHandler(this: Matterbridge, client: WebSocket, me
cluster: cluster,
});
});
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', response: devices }));
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', dst: data.src, response: devices }));
return;
} else {
this.log.error(`Invalid method from websocket client: ${debugStringify(data)}`);
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', error: 'Invalid method' }));
client.send(JSON.stringify({ id: data.id, src: 'Matterbridge', dst: data.src, error: 'Invalid method' }));
return;
}
} catch (error) {
Expand Down

0 comments on commit f040119

Please sign in to comment.