sync on device change

This commit is contained in:
Koushik Dutta
2021-09-08 21:47:22 -07:00
parent 4e574b0326
commit a693bd8d38
3 changed files with 26 additions and 5 deletions

View File

@@ -1,12 +1,12 @@
{
"name": "@scrypted/google-home",
"version": "0.0.12",
"version": "0.0.13",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@scrypted/google-home",
"version": "0.0.12",
"version": "0.0.13",
"dependencies": {
"actions-on-google": "^2.13.0",
"axios": "^0.21.1",

View File

@@ -35,5 +35,5 @@
"@types/mdns": "^0.0.34",
"@types/url-parse": "^1.4.3"
},
"version": "0.0.12"
"version": "0.0.13"
}

View File

@@ -1,4 +1,4 @@
import { EngineIOHandler, HttpRequest, HttpRequestHandler, HttpResponse, MixinDeviceBase, MixinProvider, Refresh, RTCAVMessage, ScryptedDevice, ScryptedDeviceBase, ScryptedDeviceType, ScryptedInterface, ScryptedMimeTypes } from '@scrypted/sdk';
import { EngineIOHandler, HttpRequest, HttpRequestHandler, HttpResponse, MixinDeviceBase, MixinProvider, Refresh, RTCAVMessage, ScryptedDevice, ScryptedDeviceBase, ScryptedDeviceType, ScryptedInterface, ScryptedInterfaceProperty, ScryptedMimeTypes } from '@scrypted/sdk';
import sdk from '@scrypted/sdk';
import type { SmartHomeV1DisconnectRequest, SmartHomeV1DisconnectResponse, SmartHomeV1ExecuteRequest, SmartHomeV1ExecuteResponse, SmartHomeV1ExecuteResponseCommands, SmartHomeV1QueryRequest, SmartHomeV1QueryResponse, SmartHomeV1ReportStateRequest, SmartHomeV1SyncRequest, SmartHomeV1SyncResponse } from 'actions-on-google/dist/service/smarthome/api/v1';
import { smarthome } from 'actions-on-google/dist/service/smarthome';
@@ -44,7 +44,10 @@ class GoogleHome extends ScryptedDeviceBase implements HttpRequestHandler, Engin
});
reportQueue = new Set<string>();
reportStateThrottled = throttle(() => this.reportState(), 2000);
throttleSync = throttle(() => this.requestSync(), 15000);
throttleSync = throttle(() => this.requestSync(), 15000, {
leading: false,
trailing: true,
});
plugins: Promise<any>;
defaultIncluded: any;
localEndpoint: http.Server;
@@ -81,6 +84,24 @@ class GoogleHome extends ScryptedDeviceBase implements HttpRequestHandler, Engin
this.queueReportState(source);
});
systemManager.listen((eventSource, eventDetails, eventData) => {
if (eventDetails.eventInterface !== ScryptedInterface.ScryptedDevice)
return;
if (!eventDetails.changed)
return;
if (eventDetails.property !== ScryptedInterfaceProperty.id) {
if (this.storage.getItem(`link-${eventSource?.id}`) !== this.linkTracker) {
return;
}
}
const device = systemManager.getDeviceById(eventSource?.id);
this.log.i(`Device descriptor changed: ${device?.name}. Requesting sync.`);
this.throttleSync();
});
this.plugins = systemManager.getComponent('plugins');
this.localEndpoint = new http.Server((req, res) => {