Skip to content

Commit 9401b8c

Browse files
Handle subscribe/unsubscribe characteristics messages
1 parent aa95172 commit 9401b8c

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

src/app/index.ts

+54-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class App {
179179
return authenticated_user;
180180
}
181181

182-
async sendFromRenderer(event, {messageid, data}) {
182+
async sendFromRenderer(event: Electron.IpcMessageEvent, {messageid, data}) {
183183
if (this.client.connection) {
184184
if (data.type === 'list-accessories') {
185185
return event.sender.send('r', {
@@ -201,6 +201,44 @@ class App {
201201
messageid,
202202
response: this.client.home_settings,
203203
});
204+
} else if (data.type === 'subscribe-characteristics') {
205+
console.log(data);
206+
207+
try {
208+
return event.sender.send('r', {
209+
messageid,
210+
response: await Promise.all(data.ids.map(([accessory_uuid, service_id, characteristic_uuid]) => {
211+
const accessory = this.client.accessories[accessory_uuid];
212+
const service = accessory.services[service_id];
213+
const characteristic = service.characteristics[characteristic_uuid];
214+
215+
return characteristic.subscribe(event.sender);
216+
})),
217+
});
218+
} catch (err) {
219+
return event.sender.send('r', {
220+
messageid,
221+
});
222+
}
223+
} else if (data.type === 'unsubscribe-characteristics') {
224+
console.log(data);
225+
226+
try {
227+
return event.sender.send('r', {
228+
messageid,
229+
response: await Promise.all(data.ids.map(([accessory_uuid, service_id, characteristic_uuid]) => {
230+
const accessory = this.client.accessories[accessory_uuid];
231+
const service = accessory.services[service_id];
232+
const characteristic = service.characteristics[characteristic_uuid];
233+
234+
return characteristic.unsubscribe(event.sender);
235+
})),
236+
});
237+
} catch (err) {
238+
return event.sender.send('r', {
239+
messageid,
240+
});
241+
}
204242
}
205243

206244
const response = await this.client.connection.send(data);
@@ -473,6 +511,21 @@ electron_app.on('browser-window-created', (event, window) => {
473511
window.connected = app.client.connected;
474512
});
475513

514+
electron_app.on('web-contents-created', (event, webContents) => {
515+
webContents.on('destroyed', event => {
516+
// Remove this window's characteristic subscriptions
517+
Characteristic.unsubscribeAll(webContents);
518+
});
519+
webContents.on('crashed', event => {
520+
// Remove this window's characteristic subscriptions
521+
Characteristic.unsubscribeAll(webContents);
522+
});
523+
webContents.on('did-navigate', event => {
524+
// Remove this window's characteristic subscriptions
525+
Characteristic.unsubscribeAll(webContents);
526+
});
527+
});
528+
476529
electron_app.on('window-all-closed', () => {
477530
// Don't quit as we want to stay connected to the server to show notifications
478531
});

0 commit comments

Comments
 (0)