Skip to content

Commit

Permalink
Implemented better display of Adapter Update rule: #2731
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox committed Oct 5, 2024
1 parent 9cb98ef commit 5d950cf
Show file tree
Hide file tree
Showing 20 changed files with 405 additions and 483 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@fnando/sparkline": "^0.3.10",
"@foxriver76/iob-component-lib": "^0.1.6",
"@honkhonk/vite-plugin-svgr": "^1.1.0",
"@iobroker/adapter-react-v5": "^7.2.2",
"@iobroker/adapter-react-v5": "^7.2.3",
"@iobroker/admin-component-easy-access": "^1.0.1",
"@iobroker/build-tools": "^2.0.5",
"@iobroker/dm-utils": "^0.5.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/admin/src-admin/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,7 @@ class App extends Router<AppProps, AppState> {
}
}

const instances = await this.instancesWorker.getInstances();
const instances = await this.instancesWorker.getObjects();

this.setState({ noNotifications, notifications: { notifications, instances } });
};
Expand All @@ -1586,7 +1586,7 @@ class App extends Router<AppProps, AppState> {
const result = notifications[host].result;

if (result?.system && Object.keys(result.system.categories).length) {
await this.instancesWorker.getInstances().then(instances =>
await this.instancesWorker.getObjects().then(instances =>
this.setState({
showHostWarning: {
host,
Expand Down
159 changes: 10 additions & 149 deletions packages/admin/src-admin/src/Workers/AdaptersWorker.tsx
Original file line number Diff line number Diff line change
@@ -1,166 +1,27 @@
import { type AdminConnection } from '@iobroker/adapter-react-v5';
import AdminUtils from '../helpers/AdminUtils';
import GenericWorker, { type EventType, type GenericEvent } from './GenericWorker';

export type AdapterEventType = 'new' | 'changed' | 'deleted';
export type AdapterEventType = EventType;

export interface AdapterEvent {
id: string;
obj?: ioBroker.AdapterObject;
type: AdapterEventType;
oldObj?: ioBroker.AdapterObject;
}

export default class AdaptersWorker {
private readonly socket: AdminConnection;

private readonly handlers: ((events: AdapterEvent[]) => void)[];
export type AdapterEvent = GenericEvent<'adapter'>;

export default class AdaptersWorker extends GenericWorker<'adapter'> {
private readonly repositoryHandlers: (() => void)[];

private promise: Promise<void | Record<string, ioBroker.AdapterObject>> | null;

private forceUpdate: boolean;

private connected: boolean;

private objects: Record<string, ioBroker.AdapterObject> | null;

private repoTimer: ReturnType<typeof setTimeout> | null;

constructor(socket: AdminConnection) {
this.socket = socket;
this.handlers = [];
super(socket, 'system.adapter', 'adapter');
this.repositoryHandlers = [];
this.promise = null;
this.forceUpdate = false;

socket.registerConnectionHandler(this.connectionHandler);
this.connected = this.socket.isConnected();

this.objects = null;
}

objectChangeHandler = (id: string, obj: ioBroker.AdapterObject): void => {
this.objects = this.objects || {};
// if instance
if (id.match(/^system\.adapter\.[^.]+$/)) {
let type: AdapterEventType;
let oldObj: ioBroker.AdapterObject | undefined;

if (obj) {
if (obj.type !== 'adapter') {
return;
}

AdminUtils.fixAdminUI(obj);

if (this.objects[id]) {
oldObj = this.objects[id];
if (JSON.stringify(this.objects[id]) !== JSON.stringify(obj)) {
type = 'changed';
this.objects[id] = obj;
} else {
// no changes
return;
}
} else {
type = 'new';
this.objects[id] = obj;
}
} else if (this.objects[id]) {
oldObj = this.objects[id];
type = 'deleted';
delete this.objects[id];
} else {
// deleted unknown instance
return;
}

this.socket.getAdaptersResetCache();
this.socket.getInstalledResetCache('');
this.forceUpdate = true;
this.promise = null;

this.handlers.forEach(cb =>
cb([
{
id,
obj,
type,
oldObj,
},
]),
);
}
};

isForceUpdate(): boolean {
return this.forceUpdate;
protected checkObjectId(id: string, obj: ioBroker.AdapterObject | null | undefined): boolean {
return id.match(/^system\.adapter\.[^.]+$/) && (!obj || obj.type === this.objectType);
}

// be careful with this object. Do not change them.
getAdapters(update?: boolean): Promise<void | Record<string, ioBroker.AdapterObject>> {
if (!update && this.promise instanceof Promise) {
return this.promise;
}

update = update || this.forceUpdate;
this.forceUpdate = false;

this.promise = this.socket
.getAdapters(null, update)
.then(objects => {
this.objects = {};
objects.forEach(obj => (this.objects[obj._id] = obj));
return this.objects;
})
.catch(e => window.alert(`Cannot get adapters: ${e}`));

return this.promise;
}

connectionHandler = (isConnected: boolean): void => {
if (isConnected && !this.connected) {
this.connected = true;

if (this.handlers.length) {
this.socket
.subscribeObject('system.adapter.*', this.objectChangeHandler)
.catch(e => window.alert(`Cannot subscribe on object: ${e}`));

void this.getAdapters(true).then(
adapters =>
adapters && Object.keys(adapters).forEach(id => this.objectChangeHandler(id, adapters[id])),
);
}
} else if (!isConnected && this.connected) {
this.connected = false;
}
};

registerHandler(cb: (events: AdapterEvent[]) => void): void {
if (!this.handlers.includes(cb)) {
this.handlers.push(cb);

if (this.handlers.length === 1 && this.connected) {
this.socket
.subscribeObject('system.adapter.*', this.objectChangeHandler)
.catch(e => window.alert(`Cannot subscribe on object: ${e}`));
}
}
}

unregisterHandler(cb: (events: AdapterEvent[]) => void): void {
const pos = this.handlers.indexOf(cb);
if (pos !== -1) {
this.handlers.splice(pos, 1);
}

if (!this.handlers.length && this.connected) {
this.socket
.unsubscribeObject('system.adapter.*', this.objectChangeHandler)
.catch(e => window.alert(`Cannot unsubscribe on object: ${e}`));
}
protected postProcessing(_id: string, _obj: ioBroker.AdapterObject | null | undefined): void {
this.socket.getAdaptersResetCache();
this.socket.getInstalledResetCache('');
}

repoChangeHandler = (/* id, obj */): void => {
Expand Down
Loading

0 comments on commit 5d950cf

Please sign in to comment.