feat: add network address mqtt sensor #66
This commit is contained in:
@@ -67,6 +67,7 @@ const init = async () => {
|
|||||||
console.log("\nModel:", getModel());
|
console.log("\nModel:", getModel());
|
||||||
console.log("Vendor:", getVendor());
|
console.log("Vendor:", getVendor());
|
||||||
console.log("Serial Number:", getSerialNumber());
|
console.log("Serial Number:", getSerialNumber());
|
||||||
|
console.log("Network Addresses:", getNetworkAddresses());
|
||||||
console.log("Host Name:", getHostName());
|
console.log("Host Name:", getHostName());
|
||||||
|
|
||||||
// Show system infos
|
// Show system infos
|
||||||
@@ -280,6 +281,32 @@ const getMachineId = () => {
|
|||||||
return "123456";
|
return "123456";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the network interfaces addresses using `os.networkInterfaces()`.
|
||||||
|
*
|
||||||
|
* @returns {Object} The network addresses of all interfaces.
|
||||||
|
*/
|
||||||
|
const getNetworkAddresses = () => {
|
||||||
|
const addresses = {};
|
||||||
|
for (const [key, interfaces] of Object.entries(os.networkInterfaces())) {
|
||||||
|
for (const interface of interfaces) {
|
||||||
|
if (interface.internal || !interface.family || !interface.address) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const name = key.charAt(0).toUpperCase() + key.slice(1);
|
||||||
|
if (!addresses[name]) {
|
||||||
|
addresses[name] = {};
|
||||||
|
}
|
||||||
|
const family = interface.family;
|
||||||
|
if (!addresses[name][family]) {
|
||||||
|
addresses[name][family] = [];
|
||||||
|
}
|
||||||
|
addresses[name][family].push(interface.address);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return addresses;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the host name of the current system using `os.hostname()`.
|
* Gets the host name of the current system using `os.hostname()`.
|
||||||
*
|
*
|
||||||
@@ -845,6 +872,7 @@ module.exports = {
|
|||||||
getVendor,
|
getVendor,
|
||||||
getSerialNumber,
|
getSerialNumber,
|
||||||
getMachineId,
|
getMachineId,
|
||||||
|
getNetworkAddresses,
|
||||||
getHostName,
|
getHostName,
|
||||||
getUpTime,
|
getUpTime,
|
||||||
getMemorySize,
|
getMemorySize,
|
||||||
|
|||||||
+34
-1
@@ -47,6 +47,7 @@ const init = async () => {
|
|||||||
serial_number: serialNumber,
|
serial_number: serialNumber,
|
||||||
identifiers: [INTEGRATION.node],
|
identifiers: [INTEGRATION.node],
|
||||||
sw_version: `${app.getName()}-v${app.getVersion()}`,
|
sw_version: `${app.getName()}-v${app.getVersion()}`,
|
||||||
|
configuration_url: "https://github.com/leukipp/touchkio",
|
||||||
};
|
};
|
||||||
|
|
||||||
// Connection settings
|
// Connection settings
|
||||||
@@ -75,6 +76,7 @@ const init = async () => {
|
|||||||
initModel();
|
initModel();
|
||||||
initSerialNumber();
|
initSerialNumber();
|
||||||
initHostName();
|
initHostName();
|
||||||
|
initNetworkAddress();
|
||||||
initUpTime();
|
initUpTime();
|
||||||
initMemorySize();
|
initMemorySize();
|
||||||
initMemoryUsage();
|
initMemoryUsage();
|
||||||
@@ -128,6 +130,7 @@ const update = async () => {
|
|||||||
updatePageNumber();
|
updatePageNumber();
|
||||||
updatePageZoom();
|
updatePageZoom();
|
||||||
updatePageUrl();
|
updatePageUrl();
|
||||||
|
updateNetworkAddress();
|
||||||
updateUpTime();
|
updateUpTime();
|
||||||
updateLastActive();
|
updateLastActive();
|
||||||
updateMemoryUsage();
|
updateMemoryUsage();
|
||||||
@@ -593,6 +596,36 @@ const updateSerialNumber = () => {
|
|||||||
publishState("serial_number", serialNumber);
|
publishState("serial_number", serialNumber);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the network address sensor.
|
||||||
|
*/
|
||||||
|
const initNetworkAddress = () => {
|
||||||
|
const root = `${INTEGRATION.root}/network_address`;
|
||||||
|
const config = {
|
||||||
|
name: "Network Address",
|
||||||
|
unique_id: `${INTEGRATION.node}_network_address`,
|
||||||
|
state_topic: `${root}/state`,
|
||||||
|
json_attributes_topic: `${root}/attributes`,
|
||||||
|
value_template: "{{ value }}",
|
||||||
|
icon: "mdi:ip-network",
|
||||||
|
device: INTEGRATION.device,
|
||||||
|
};
|
||||||
|
publishConfig("sensor", config);
|
||||||
|
updateNetworkAddress();
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the network address sensor via the mqtt connection.
|
||||||
|
*/
|
||||||
|
const updateNetworkAddress = () => {
|
||||||
|
const networkAddresses = hardware.getNetworkAddresses();
|
||||||
|
const [name] = Object.keys(networkAddresses);
|
||||||
|
const [family] = name ? Object.keys(networkAddresses[name]) : [];
|
||||||
|
const networkAddress = networkAddresses[name]?.[family]?.[0] || null;
|
||||||
|
publishState("network_address", networkAddress);
|
||||||
|
publishAttributes("network_address", networkAddresses);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the host name sensor.
|
* Initializes the host name sensor.
|
||||||
*/
|
*/
|
||||||
@@ -804,8 +837,8 @@ const updatePackageUpgrades = () => {
|
|||||||
total: packages.length,
|
total: packages.length,
|
||||||
packages: packages.map((pkg) => pkg.replace(/\s*\[.*?\]\s*/g, "").trim()),
|
packages: packages.map((pkg) => pkg.replace(/\s*\[.*?\]\s*/g, "").trim()),
|
||||||
};
|
};
|
||||||
publishAttributes("package_upgrades", attributes);
|
|
||||||
publishState("package_upgrades", attributes.total);
|
publishState("package_upgrades", attributes.total);
|
||||||
|
publishAttributes("package_upgrades", attributes);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user