Skip to content

Commit

Permalink
Controller revamp (#109)
Browse files Browse the repository at this point in the history
* Update matter.js

* Export type

* Storage fix

* Update Device things

* Controller refactoring (WIP but works)

* Adjustments to matter.js update

* Adds TODO

* Logic fixes and rounding

* Make linter happy
  • Loading branch information
Apollon77 authored Oct 22, 2024
1 parent 7dcef83 commit b827693
Show file tree
Hide file tree
Showing 26 changed files with 19,333 additions and 7,456 deletions.
25,332 changes: 18,496 additions & 6,836 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
"url": "https://github.com/ioBroker/ioBroker.matter"
},
"optionalDependencies": {
"@project-chip/matter-node-ble.js": "0.10.4"
"@matter/nodejs-ble": "0.11.0-alpha.0-20241018-34fe42e99"
},
"dependencies": {
"@iobroker/adapter-core": "^3.1.6",
"@iobroker/dm-utils": "^0.3.1",
"@iobroker/type-detector": "^4.0.1",
"@project-chip/matter-node.js": "0.10.4",
"@project-chip/matter.js": "0.10.4",
"@matter/main": "0.11.0-alpha.0-20241018-34fe42e99",
"@project-chip/matter.js": "0.11.0-alpha.0-20241018-34fe42e99",
"axios": "^1.7.7",
"jsonwebtoken": "^9.0.2"
},
Expand Down
2 changes: 1 addition & 1 deletion src/lib/SubscribeManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type SubscribeCallback = (state: ioBroker.State) => void;
export type SubscribeCallback = (state: ioBroker.State) => void;

class SubscribeManager {
/** List of all registered subscribed state ids. */
Expand Down
20 changes: 12 additions & 8 deletions src/lib/devices/DeviceStateObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ export class DeviceStateObject<T> {
object?: ioBroker.Object;
modes?: { [key: string]: T };

protected min: number | undefined;
protected max: number | undefined;
protected realMin: number | undefined;
protected realMax: number | undefined;
protected unit: string | undefined;
protected min?: number;
protected max?: number;
protected realMin?: number;
protected realMax?: number;
protected unit?: string;

static async create<T>(
adapter: ioBroker.Adapter,
Expand Down Expand Up @@ -186,7 +186,7 @@ export class DeviceStateObject<T> {
return this.unit;
}

protected parseMinMax(percent: boolean = false): void {
protected parseMinMax(percent = false): void {
if (!this.object) {
throw new Error(`Object not initialized`);
}
Expand All @@ -206,7 +206,7 @@ export class DeviceStateObject<T> {
if (this.min !== undefined && this.max === undefined) {
this.max = 100;
} else if (this.min === undefined && this.max !== undefined) {
this.max = 0;
this.min = 0;
}
this.unit = obj?.common?.unit;
}
Expand Down Expand Up @@ -295,7 +295,7 @@ export class DeviceStateObject<T> {

await this.adapter.setForeignStateAsync(this.state.id, realValue as ioBroker.StateValue);
} else if (valueType === 'string') {
const realValue: string = value.toString();
const realValue: string = String(value);
await this.adapter.setForeignStateAsync(this.state.id, realValue as ioBroker.StateValue);
} else if (valueType === 'json') {
const realValue: string = JSON.stringify(value);
Expand All @@ -313,6 +313,10 @@ export class DeviceStateObject<T> {
if (object.common.max !== undefined && value > object.common.max) {
throw new Error(`Value ${value} is greater than max ${object.common.max}`);
}

if (typeof value === 'number') {
value = parseFloat(value.toFixed(4)) as T;
}
}

await this.adapter.setForeignStateAsync(this.state.id, value as ioBroker.StateValue);
Expand Down
19 changes: 8 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import '@project-chip/matter-node.js';

import * as utils from '@iobroker/adapter-core';
import ChannelDetector, { DetectorState, PatternControl, Types } from '@iobroker/type-detector';
import { Environment, StorageService } from '@project-chip/matter.js/environment';
import { Level, Logger } from '@project-chip/matter.js/log';
import { Environment, LogLevel, Logger, StorageService } from '@matter/main';
import axios from 'axios';
import jwt from 'jsonwebtoken';
import fs from 'node:fs';
Expand Down Expand Up @@ -306,23 +303,23 @@ export class MatterAdapter extends utils.Adapter {

async prepareMatterEnvironment(): Promise<void> {
const config: MatterAdapterConfig = this.config as MatterAdapterConfig;
Logger.defaultLogLevel = Level.DEBUG;
Logger.log = (level: Level, formattedLog: string) => {
Logger.defaultLogLevel = LogLevel.DEBUG;
Logger.log = (level: LogLevel, formattedLog: string) => {
switch (level) {
case Level.DEBUG:
case LogLevel.DEBUG:
config.debug ? this.log.debug(formattedLog) : this.log.silly(formattedLog);
break;

case Level.INFO:
case LogLevel.INFO:
this.log.debug(formattedLog);
break;
case Level.WARN:
case LogLevel.WARN:
this.log.info(formattedLog);
break;
case Level.ERROR:
case LogLevel.ERROR:
this.log.warn(formattedLog);
break;
case Level.FATAL:
case LogLevel.FATAL:
this.log.error(formattedLog);
break;
}
Expand Down
4 changes: 1 addition & 3 deletions src/matter/BaseServerNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { SessionsBehavior } from '@project-chip/matter.js/behavior/system/sessions';
import { Logger } from '@project-chip/matter.js/log';
import { ServerNode } from '@project-chip/matter.js/node';
import { Logger, ServerNode, SessionsBehavior } from '@matter/main';
import type { MatterAdapter } from '../main';
import { GeneralNode, MessageResponse } from './GeneralNode';

Expand Down
15 changes: 6 additions & 9 deletions src/matter/BridgedDevicesNode.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { BridgedDeviceBasicInformationServer } from '@project-chip/matter.js/behavior/definitions/bridged-device-basic-information';
import { VendorId } from '@project-chip/matter.js/datatype';
import { DeviceTypes } from '@project-chip/matter.js/device';
import { Endpoint } from '@project-chip/matter.js/endpoint';
import { AggregatorEndpoint, BridgedNodeEndpoint } from '@project-chip/matter.js/endpoint/definitions';
import { ServerNode } from '@project-chip/matter.js/node';
import { Endpoint, ServerNode, VendorId } from '@matter/main';
import { BridgedDeviceBasicInformationServer } from '@matter/main/behaviors';
import { AggregatorEndpoint, BridgedNodeEndpoint } from '@matter/main/endpoints';
import { inspect } from 'util';
import { BridgeDeviceDescription } from '../ioBrokerStorageTypes';
import { GenericDevice } from '../lib';
import { md5 } from '../lib/utils';
import type { MatterAdapter } from '../main';
import { BaseServerNode } from './BaseServerNode';
import { initializeBridgedUnreachableStateHandler } from './devices/SharedStateHandlers';
import matterDeviceFactory from './matterFactory';
import matterDeviceFactory from './to-matter/matterFactory';
import { initializeBridgedUnreachableStateHandler } from './to-matter/SharedStateHandlers';

export interface BridgeCreateOptions {
parameters: BridgeOptions;
Expand Down Expand Up @@ -126,7 +123,7 @@ class BridgedDevices extends BaseServerNode {
});

const deviceName = this.#parameters.deviceName || 'Matter Bridge device';
const deviceType = DeviceTypes.AGGREGATOR.code;
const deviceType = AggregatorEndpoint.deviceType;
const vendorName = 'ioBroker';

// product name / id and vendor id should match what is in the device certificate
Expand Down
Loading

0 comments on commit b827693

Please sign in to comment.