Skip to content

Commit

Permalink
Added device manager
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox committed Dec 7, 2023
1 parent c4dfad9 commit 7036dbb
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
3 changes: 3 additions & 0 deletions io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
"sentry": {
"dsn": "https://[email protected]/234"
}
},
"supportedMessages": {
"deviceManager": true
}
},
"protectedNative": [
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@iobroker/adapter-core": "^3.0.4",
"@project-chip/matter-node.js": "0.7.2-alpha.0-20231128-db9207d2",
"@iobroker/type-detector": "^3.0.5",
"@jey-cee/dm-utils": "^0.0.5",
"axios": "^1.6.2",
"jsonwebtoken": "^9.0.2"
},
Expand Down
85 changes: 85 additions & 0 deletions src/lib/DeviceManagement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { MatterAdapter } from '../main';

import { DeviceManagement, DeviceInfo, RetVal, DeviceStatus } from '@jey-cee/dm-utils';


class MatterAdapterDeviceManagement extends DeviceManagement<MatterAdapter> {
// contents see in the next chapters
// todo!!: this function must be async
// @ts-ignore
async listDevices(): Promise<RetVal<DeviceInfo[]>> {
const devices = await this.adapter.getDevicesAsync();
// const devices = await this.adapter.getObjectView('system', 'device', { startkey: `system.adapter.matter.${this.instance}.`, endkey: 'system.adapter.matter.999' });
const arrDevices: DeviceInfo[] = [];
for (const i in devices) {
let status: DeviceStatus = 'disconnected';

const alive = await this.adapter.getStateAsync(`${devices[i]._id}.info.connection`);
if (alive !== null && alive !== undefined) {
status = alive.val ? 'connected' : 'disconnected';
}

// const manufacturer = await this.adapter.getStateAsync(`${devices[i]._id}._info.brand`);
//
// const product = await this.adapter.getStateAsync(`${devices[i]._id}._info.product`);
// const arch = await this.adapter.getStateAsync(`${devices[i]._id}._info.arch`);
// const model = `${product?.val} ${arch?.val}`;

const res: DeviceInfo = {
id: devices[i]._id,
name: devices[i].common.name,
// icon: devices[i].common.icon ? devices[i].common.icon : null,
// manufacturer: manufacturer ? manufacturer.val : null,
// model: model ? model : null,
status,
hasDetails: true,
actions: [
/* {
id: 'delete',
icon: 'fa-solid fa-trash-can',
description: {
en: 'Delete this device',
de: 'Gerät löschen',
ru: 'Удалить это устройство',
pt: 'Excluir este dispositivo',
nl: 'Verwijder dit apparaat',
fr: 'Supprimer cet appareil',
it: 'Elimina questo dispositivo',
es: 'Eliminar este dispositivo',
pl: 'Usuń to urządzenie',
'zh-cn': '删除此设备',
uk: 'Видалити цей пристрій'
},
handler: this.handleDeleteDevice.bind(this)
},
{
id: 'rename',
icon: 'fa-solid fa-pen',
description: {
en: 'Rename this device',
de: 'Gerät umbenennen',
ru: 'Переименовать это устройство',
pt: 'Renomear este dispositivo',
nl: 'Hernoem dit apparaat',
fr: 'Renommer cet appareil',
it: 'Rinomina questo dispositivo',
es: 'Renombrar este dispositivo',
pl: 'Zmień nazwę tego urządzenia',
'zh-cn': '重命名此设备',
uk: 'Перейменуйте цей пристрій'
},
handler: this.handleRenameDevice.bind(this)
}*/
]
};
// if id contains gateway remove res.actions
if (devices[i]._id.includes('localhost')) {
res.actions = [];
}
arrDevices.push(res);
}
return arrDevices;
}
}

export default MatterAdapterDeviceManagement;
10 changes: 9 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
} from './ioBrokerStorageTypes';
import MatterController, {ControllerOptions} from './matter/ControllerNode';

import MatterAdapterDeviceManagement from './lib/DeviceManagement';

const IOBROKER_USER_API = 'https://iobroker.pro:3001';

// If the device was created by user and user defined the type of device => use this OID as given name
Expand Down Expand Up @@ -88,6 +90,7 @@ export class MatterAdapter extends utils.Adapter {
private license: { [key: string]: boolean | undefined } = {};
private controller: MatterController | null = null;
private sysLanguage: 'en' | 'de' | 'ru' | 'pt' | 'nl' | 'fr' | 'it' | 'es' | 'pl' | 'zh-cn' = 'en';
private readonly deviceManagement: MatterAdapterDeviceManagement;

public constructor(options: Partial<utils.AdapterOptions> = {}) {
super({
Expand All @@ -109,6 +112,7 @@ export class MatterAdapter extends utils.Adapter {
this.on('objectChange', (id /* , object */) => this.onObjectChange(id));
this.on('unload', callback => this.onUnload(callback));
this.on('message', this.onMessage.bind(this));
this.deviceManagement = new MatterAdapterDeviceManagement(this);

this.detector = new ChannelDetector();
}
Expand All @@ -128,6 +132,10 @@ export class MatterAdapter extends utils.Adapter {
}

async onMessage(obj: ioBroker.Message): Promise<void> {
if (obj?.command?.startsWith('dm:')) {
return;
}

if (obj?.command === 'reset') {
await this.onTotalReset();
} else if (obj?.command === 'nodeStates') {
Expand Down Expand Up @@ -788,4 +796,4 @@ if (require.main !== module) {
} else {
// otherwise start the instance directly
(() => new MatterAdapter())();
}
}

0 comments on commit 7036dbb

Please sign in to comment.