Skip to content

Commit

Permalink
Dev 2.1.6-dev.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Luligu committed Feb 13, 2025
1 parent 8c3f719 commit d0edc1d
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 65 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ matterbridge-zigbee2mqtt v. 2.4.4
matterbridge-somfy-tahoma v. 1.2.3
matterbridge-hass v. 0.0.8

## [2.1.6] - 2025-02-12
## [2.1.6] - 2025-02-13

### Added

Expand All @@ -45,6 +45,7 @@ matterbridge-hass v. 0.0.8
### Changed

- [package]: Update matter.js to 0.12.4-alpha.0-20250212-b2729c9eb
- [package]: Update matter.js to 0.12.4-alpha.0-20250213-1187f81eb

<a href="https://www.buymeacoffee.com/luligugithub">
<img src="./yellow-button.png" alt="Buy me a coffee" width="120">
Expand Down
84 changes: 42 additions & 42 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@
"install:jest": "npm install --save-dev jest ts-jest @types/jest eslint-plugin-jest && npm run test"
},
"dependencies": {
"@matter/main": "^0.12.4-alpha.0-20250212-b2729c9eb",
"@matter/main": "^0.12.4-alpha.0-20250213-1187f81eb",
"archiver": "7.0.1",
"express": "4.21.2",
"glob": "11.0.1",
"https": "1.0.0",
"node-ansi-logger": "3.0.0",
"node-persist-manager": "1.0.8",
"ws": "8.18.0"
"ws": "8.18.0"
},
"devDependencies": {
"@eslint/js": "9.20.0",
Expand Down
90 changes: 88 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,25 @@
import { Matterbridge } from './matterbridge.js';

import type { Session } from 'node:inspector';
import type os from 'node:os';

let instance: Matterbridge | undefined;
let session: Session;
let memoryCheckInterval: NodeJS.Timeout;
let prevCpus: os.CpuInfo[];
let lastCpuUsage = 0;

const cli = '\u001B[32m';
const er = '\u001B[38;5;9m';
const rs = '\u001B[40;0m';
const db = '\u001B[38;5;245m';
const CYAN = '\u001B[36m';
const YELLOW = '\u001B[33m';
const BRIGHT = '\u001B[1m';

async function main() {
if (process.argv.includes('-memorycheck')) await startMemoryCheck();

if (process.argv.includes('-inspector')) await startInspector();

if (process.argv.includes('-debug')) console.log(cli + `CLI: ${process.argv.includes('-edge') ? 'MatterbridgeEdge' : 'Matterbridge'}.loadInstance() called` + rs);
Expand All @@ -41,7 +53,77 @@ async function main() {
if (process.argv.includes('-debug')) console.log(cli + `CLI: ${process.argv.includes('-edge') ? 'MatterbridgeEdge' : 'Matterbridge'}.loadInstance() exited` + rs);
}

let session: Session;
async function startMemoryCheck() {
const os = await import('node:os');
console.log(cli + `CLI: Memory check started` + rs);
prevCpus = os.cpus();

const formatMemoryUsage = (bytes: number): string => {
if (bytes >= 1024 ** 3) {
return `${(bytes / 1024 ** 3).toFixed(2)} GB`;
} else if (bytes >= 1024 ** 2) {
return `${(bytes / 1024 ** 2).toFixed(2)} MB`;
} else {
return `${(bytes / 1024).toFixed(2)} KB`;
}
};

const interval = () => {
// Get the cpu usage
const currCpus = os.cpus();
let cpuUsageLog: string;
if (currCpus.length !== prevCpus.length) {
prevCpus = currCpus; // Reset the previous cpus
cpuUsageLog = lastCpuUsage.toFixed(2);
}
let totalIdle = 0,
totalTick = 0;

prevCpus.forEach((prevCpu, i) => {
const currCpu = currCpus[i];
const idleDiff = currCpu.times.idle - prevCpu.times.idle;
const totalDiff = (Object.keys(currCpu.times) as (keyof typeof currCpu.times)[]).reduce((acc, key) => acc + (currCpu.times[key] - prevCpu.times[key]), 0);
totalIdle += idleDiff;
totalTick += totalDiff;
});
const cpuUsage = 100 - (totalIdle / totalTick) * 100;
if (totalTick === 0 || isNaN(cpuUsage) || !isFinite(cpuUsage) || cpuUsage <= 0) {
cpuUsageLog = lastCpuUsage.toFixed(2);
}
prevCpus = currCpus;
lastCpuUsage = cpuUsage;
cpuUsageLog = cpuUsage.toFixed(2);

// Get the memory usage
const memoryUsageRaw = process.memoryUsage();
const memoryUsage = {
rss: formatMemoryUsage(memoryUsageRaw.rss),
heapTotal: formatMemoryUsage(memoryUsageRaw.heapTotal),
heapUsed: formatMemoryUsage(memoryUsageRaw.heapUsed),
external: formatMemoryUsage(memoryUsageRaw.external),
arrayBuffers: formatMemoryUsage(memoryUsageRaw.arrayBuffers),
};
console.log(
`${YELLOW}${BRIGHT}Cpu usage:${db} ${CYAN}${cpuUsageLog.padStart(6, ' ')} %${db} ${YELLOW}${BRIGHT}Memory usage:${db} rss ${CYAN}${memoryUsage.rss}${db} heapTotal ${CYAN}${memoryUsage.heapTotal}${db} heapUsed ${CYAN}${memoryUsage.heapUsed}${db} external ${memoryUsage.external} arrayBuffers ${memoryUsage.arrayBuffers}` +
rs,
);
};
interval();
memoryCheckInterval = setInterval(interval, 1000);
}

async function stopMemoryCheck() {
console.log(cli + `CLI: Stopping memory check in 5 minute` + rs);
instance = undefined;
setTimeout(
() => {
console.log(cli + `CLI: Memory check stopped` + rs);
clearInterval(memoryCheckInterval);
process.exit(0);
},
5 * 60 * 1000,
);
}

async function startInspector() {
const { Session } = await import('node:inspector');
Expand Down Expand Up @@ -79,7 +161,11 @@ async function shutdown() {

if (process.argv.includes('-inspector')) await stopInspector();

process.exit(0);
if (process.argv.includes('-memorycheck')) {
await stopMemoryCheck();
} else {
process.exit(0);
}
}

async function restart() {
Expand Down
16 changes: 0 additions & 16 deletions src/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,22 +928,6 @@ export class Frontend {
}

async stop() {
// Start the memory check. This will not allow the process to exit but will log the memory usage for 5 minutes.
if (hasParameter('memorycheck')) {
this.wssSendSnackbarMessage('Memory check started', getIntParameter('memorycheck') ?? 5 * 60 * 1000);
await new Promise<void>((resolve) => {
this.log.debug(`***Memory check started for ${getIntParameter('memorycheck') ?? 5 * 60 * 1000} ms`);
setTimeout(
() => {
this.wssSendSnackbarMessage('Memory check stopped', 10);
this.log.debug(`***Memory check stopped after ${getIntParameter('memorycheck') ?? 5 * 60 * 1000} ms`);
resolve();
},
getIntParameter('memorycheck') ?? 5 * 60 * 1000,
);
});
}

// Close the http server
if (this.httpServer) {
this.httpServer.close();
Expand Down
3 changes: 1 addition & 2 deletions src/matter/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ export {
SwitchesTag,
} from '@matter/main';
export { AttributeElement, ClusterElement, ClusterModel, CommandElement, EventElement, FieldElement } from '@matter/main/model';
// export { logEndpoint, MdnsService, Val } from '@matter/main/protocol';
export { logEndpoint, MdnsService } from '@matter/main/protocol';
export { logEndpoint, MdnsService, Val } from '@matter/main/protocol';

0 comments on commit d0edc1d

Please sign in to comment.