Skip to content

Commit

Permalink
Add function to retrieve local IP address and update debug output wit…
Browse files Browse the repository at this point in the history
…h Docker status and IP address
  • Loading branch information
htilly committed Jan 8, 2025
1 parent 75574fc commit 0ee9d78
Showing 1 changed file with 63 additions and 38 deletions.
101 changes: 63 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,23 @@ function _status(channel, state) {
})
}

function getIPAddress() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
return 'IP address not found';
}








async function _debug(channel, userName) {
Expand All @@ -1813,28 +1830,6 @@ async function _debug(channel, userName) {
return value;
}

function getHostIPAddress() {
try {
const result = fs.readFileSync('/proc/net/route', 'utf8');
const lines = result.split('\n');

for (const line of lines) {
const columns = line.trim().split(/\s+/);

// Look for the default route (Destination is 00000000)
if (columns[1] === '00000000') {
const hexIp = columns[2]; // Gateway IP is in the third column
const gatewayIp = hexToIp(hexIp);
return gatewayIp;
}
}

return 'Host IP address not found';
} catch (err) {
return 'Host IP address not found';
}
}

function hexToIp(hex) {
return [
parseInt(hex.slice(6, 8), 16),
Expand Down Expand Up @@ -1862,31 +1857,38 @@ async function _debug(channel, userName) {
};

const isDocker = isRunningInDocker();
const dockerStatus = isDocker ? 'Running in Docker' : 'Not running in Docker';

if (isDocker) {
if (!ipAddress) {
const warningMessage = 'Make sure you have configured IP in the config.json';
logger.error(warningMessage);
ipAddress = warningMessage;
}
} else {
ipAddress = getIPAddress();
}
let ipAddress = 'IP address not found';

ipAddress = getIPAddress();

const dockerIPAddress = isDocker ? getHostIPAddress() : null;
const nodeVersion = JSON.stringify(process.versions);

xmlToJson(url, async function (err, data) {
let sonosInfo = '';
if (err) {
logger.error('Error occurred ' + err);
sonosInfo = 'SONOS device is offline or not responding.';
} else {
sonosInfo =
'\n*Sonos Info*' +
'\nFriendly Name: ' + data.root.device[0].friendlyName;
_slackMessage('SONOS device is offline or not responding.', channel);
return;
}

// Log the full XML response for debugging
// logger.info('Full XML response: ' + JSON.stringify(data, null, 2));

const device = data.root.device[0];
const sonosInfo =
'\n*Sonos Info*' +
`\nFriendly Name: ${device.friendlyName[0]}` +
`\nRoom Name: ${device.roomName[0]}` +
`\nDisplay Name: ${device.displayName[0]}` +
`\nModel Description: ${device.modelDescription[0]}` +
`\nModelNumber: ${device.modelNumber[0]}` +
`\nSerial Number: ${device.serialNum[0]}` +
`\nMAC Address: ${device.MACAddress[0] || 'undefined'}` +
`\nSW Version: ${device.softwareVersion[0] || 'undefined'}` +
`\nHW Version: ${device.hardwareVersion[0] || 'undefined'}` +
`\nAPI Version: ${device.apiVersion[0] || 'undefined'}`;

const memoryUsage = process.memoryUsage();
const formattedMemoryUsage = `\n*Memory Usage*:\n RSS: ${Math.round(memoryUsage.rss / 1024 / 1024)} MB`;

Expand Down Expand Up @@ -1969,6 +1971,26 @@ async function _debug(channel, userName) {
type: 'mrkdwn',
text: '*Configuration Values*\n' + configValues
}
},
{
type: 'divider'
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Docker Status*\n${dockerStatus}`
}
},
{
type: 'divider'
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*IP Address*\n${ipAddress}`
}
}
];

Expand Down Expand Up @@ -2006,6 +2028,9 @@ async function _debug(channel, userName) {






async function _blacklist(input, channel, userName) {
_logUserAction(userName, 'blacklist');
if (channel !== global.adminChannel) {
Expand Down

0 comments on commit 0ee9d78

Please sign in to comment.