Skip to content

Commit 0ee9d78

Browse files
committed
Add function to retrieve local IP address and update debug output with Docker status and IP address
1 parent 75574fc commit 0ee9d78

File tree

1 file changed

+63
-38
lines changed

1 file changed

+63
-38
lines changed

index.js

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,23 @@ function _status(channel, state) {
18001800
})
18011801
}
18021802

1803+
function getIPAddress() {
1804+
const interfaces = os.networkInterfaces();
1805+
for (const name of Object.keys(interfaces)) {
1806+
for (const iface of interfaces[name]) {
1807+
if (iface.family === 'IPv4' && !iface.internal) {
1808+
return iface.address;
1809+
}
1810+
}
1811+
}
1812+
return 'IP address not found';
1813+
}
1814+
1815+
1816+
1817+
1818+
1819+
18031820

18041821

18051822
async function _debug(channel, userName) {
@@ -1813,28 +1830,6 @@ async function _debug(channel, userName) {
18131830
return value;
18141831
}
18151832

1816-
function getHostIPAddress() {
1817-
try {
1818-
const result = fs.readFileSync('/proc/net/route', 'utf8');
1819-
const lines = result.split('\n');
1820-
1821-
for (const line of lines) {
1822-
const columns = line.trim().split(/\s+/);
1823-
1824-
// Look for the default route (Destination is 00000000)
1825-
if (columns[1] === '00000000') {
1826-
const hexIp = columns[2]; // Gateway IP is in the third column
1827-
const gatewayIp = hexToIp(hexIp);
1828-
return gatewayIp;
1829-
}
1830-
}
1831-
1832-
return 'Host IP address not found';
1833-
} catch (err) {
1834-
return 'Host IP address not found';
1835-
}
1836-
}
1837-
18381833
function hexToIp(hex) {
18391834
return [
18401835
parseInt(hex.slice(6, 8), 16),
@@ -1862,31 +1857,38 @@ async function _debug(channel, userName) {
18621857
};
18631858

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

1866-
if (isDocker) {
1867-
if (!ipAddress) {
1868-
const warningMessage = 'Make sure you have configured IP in the config.json';
1869-
logger.error(warningMessage);
1870-
ipAddress = warningMessage;
1871-
}
1872-
} else {
1873-
ipAddress = getIPAddress();
1874-
}
1862+
let ipAddress = 'IP address not found';
1863+
1864+
ipAddress = getIPAddress();
18751865

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

18791868
xmlToJson(url, async function (err, data) {
1880-
let sonosInfo = '';
18811869
if (err) {
18821870
logger.error('Error occurred ' + err);
1883-
sonosInfo = 'SONOS device is offline or not responding.';
1884-
} else {
1885-
sonosInfo =
1886-
'\n*Sonos Info*' +
1887-
'\nFriendly Name: ' + data.root.device[0].friendlyName;
1871+
_slackMessage('SONOS device is offline or not responding.', channel);
1872+
return;
18881873
}
18891874

1875+
// Log the full XML response for debugging
1876+
// logger.info('Full XML response: ' + JSON.stringify(data, null, 2));
1877+
1878+
const device = data.root.device[0];
1879+
const sonosInfo =
1880+
'\n*Sonos Info*' +
1881+
`\nFriendly Name: ${device.friendlyName[0]}` +
1882+
`\nRoom Name: ${device.roomName[0]}` +
1883+
`\nDisplay Name: ${device.displayName[0]}` +
1884+
`\nModel Description: ${device.modelDescription[0]}` +
1885+
`\nModelNumber: ${device.modelNumber[0]}` +
1886+
`\nSerial Number: ${device.serialNum[0]}` +
1887+
`\nMAC Address: ${device.MACAddress[0] || 'undefined'}` +
1888+
`\nSW Version: ${device.softwareVersion[0] || 'undefined'}` +
1889+
`\nHW Version: ${device.hardwareVersion[0] || 'undefined'}` +
1890+
`\nAPI Version: ${device.apiVersion[0] || 'undefined'}`;
1891+
18901892
const memoryUsage = process.memoryUsage();
18911893
const formattedMemoryUsage = `\n*Memory Usage*:\n RSS: ${Math.round(memoryUsage.rss / 1024 / 1024)} MB`;
18921894

@@ -1969,6 +1971,26 @@ async function _debug(channel, userName) {
19691971
type: 'mrkdwn',
19701972
text: '*Configuration Values*\n' + configValues
19711973
}
1974+
},
1975+
{
1976+
type: 'divider'
1977+
},
1978+
{
1979+
type: 'section',
1980+
text: {
1981+
type: 'mrkdwn',
1982+
text: `*Docker Status*\n${dockerStatus}`
1983+
}
1984+
},
1985+
{
1986+
type: 'divider'
1987+
},
1988+
{
1989+
type: 'section',
1990+
text: {
1991+
type: 'mrkdwn',
1992+
text: `*IP Address*\n${ipAddress}`
1993+
}
19721994
}
19731995
];
19741996

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

20072029

20082030

2031+
2032+
2033+
20092034
async function _blacklist(input, channel, userName) {
20102035
_logUserAction(userName, 'blacklist');
20112036
if (channel !== global.adminChannel) {

0 commit comments

Comments
 (0)