2024-08-27 15:52:59 -05:00
const os = require ( "node:os" );
const fs = require ( "node:fs" );
2024-01-04 22:38:53 +01:00
const si = require ( "systeminformation" );
2025-09-30 20:12:58 +02:00
const Log = require ( "logger" );
2017-02-28 01:41:21 -03:00
2024-08-27 15:52:59 -05:00
const modulePositions = []; // will get list from index.html
const regionRegEx = /"region ([^"]*)/i ;
const indexFileName = "index.html" ;
const discoveredPositionsJSFilename = "js/positions.js" ;
2021-01-05 18:37:01 +01:00
module . exports = {
2024-01-04 22:38:53 +01:00
2025-07-12 08:24:09 +02:00
async logSystemInformation ( mirrorVersion ) {
2024-01-04 22:38:53 +01:00
try {
2025-07-12 08:24:09 +02:00
const system = await si . system ();
const osInfo = await si . osInfo ();
const versions = await si . versions ();
const usedNodeVersion = process . version . replace ( "v" , "" );
const installedNodeVersion = versions . node ;
const totalRam = ( os . totalmem () / 1024 / 1024 ). toFixed ( 2 );
const freeRam = ( os . freemem () / 1024 / 1024 ). toFixed ( 2 );
const usedRam = (( os . totalmem () - os . freemem ()) / 1024 / 1024 ). toFixed ( 2 );
2024-01-14 09:15:30 +01:00
2025-07-10 07:39:23 +02:00
let systemDataString = [
2025-07-12 08:24:09 +02:00
"\n#### System Information ####" ,
`- SYSTEM: manufacturer: ${ system . manufacturer } ; model: ${ system . model } ; virtual: ${ system . virtual } ; MM: ${ mirrorVersion } ` ,
`- OS: platform: ${ osInfo . platform } ; distro: ${ osInfo . distro } ; release: ${ osInfo . release } ; arch: ${ osInfo . arch } ; kernel: ${ versions . kernel } ` ,
`- VERSIONS: electron: ${ process . versions . electron } ; used node: ${ usedNodeVersion } ; installed node: ${ installedNodeVersion } ; npm: ${ versions . npm } ; pm2: ${ versions . pm2 } ` ,
`- ENV: XDG_SESSION_TYPE: ${ process . env . XDG_SESSION_TYPE } ; MM_CONFIG_FILE: ${ process . env . MM_CONFIG_FILE } ` ,
` WAYLAND_DISPLAY: ${ process . env . WAYLAND_DISPLAY } ; DISPLAY: ${ process . env . DISPLAY } ; ELECTRON_ENABLE_GPU: ${ process . env . ELECTRON_ENABLE_GPU } ` ,
`- RAM: total: ${ totalRam } MB; free: ${ freeRam } MB; used: ${ usedRam } MB` ,
`- OTHERS: uptime: ${ Math . floor ( os . uptime () / 60 ) } minutes; timeZone: ${ Intl . DateTimeFormat (). resolvedOptions (). timeZone } `
2025-07-10 07:39:23 +02:00
]. join ( "\n" );
2024-01-04 22:38:53 +01:00
Log . info ( systemDataString );
2024-01-16 21:54:55 +01:00
2025-11-03 19:47:01 +01:00
// Return is currently only for tests
2024-01-16 21:54:55 +01:00
return systemDataString ;
2025-07-12 08:24:09 +02:00
} catch ( error ) {
Log . error ( error );
2024-01-04 22:38:53 +01:00
}
2024-06-24 21:51:54 +02:00
},
// return all available module positions
getAvailableModulePositions () {
2024-08-27 15:52:59 -05:00
return modulePositions ;
2024-06-24 21:51:54 +02:00
},
2024-09-18 07:37:09 +02:00
// return if position is on modulePositions Array (true/false)
2024-06-24 21:51:54 +02:00
moduleHasValidPosition ( position ) {
if ( this . getAvailableModulePositions (). indexOf ( position ) === - 1 ) return false ;
return true ;
2024-08-27 15:52:59 -05:00
},
getModulePositions () {
2024-09-28 08:52:09 -05:00
// if not already discovered
if ( modulePositions . length === 0 ) {
// get the lines of the index.html
2024-12-28 11:57:39 -06:00
const lines = fs . readFileSync ( indexFileName ). toString (). split ( "\n" );
2024-09-28 08:52:09 -05:00
// loop thru the lines
lines . forEach (( line ) => {
// run the regex on each line
const results = regionRegEx . exec ( line );
// if the regex returned something
if ( results && results . length > 0 ) {
// get the position parts and replace space with underscore
const positionName = results [ 1 ]. replace ( " " , "_" );
2025-11-03 19:47:01 +01:00
// add it to the list only if not already present (avoid duplicates)
if ( ! modulePositions . includes ( positionName )) {
modulePositions . push ( positionName );
}
2024-09-28 08:52:09 -05:00
}
});
2024-12-18 10:37:51 -06:00
try {
fs . writeFileSync ( discoveredPositionsJSFilename , `const modulePositions= ${ JSON . stringify ( modulePositions ) } ` );
}
catch ( error ) {
2025-06-09 16:43:45 +02:00
Log . error ( "unable to write js/positions.js with the discovered module positions\nmake the MagicMirror/js folder writeable by the user starting MagicMirror" );
2024-12-18 10:37:51 -06:00
}
2024-09-28 08:52:09 -05:00
}
2024-08-27 15:52:59 -05:00
// return the list to the caller
return modulePositions ;
2017-02-28 01:41:21 -03:00
}
};