Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save interfaces to the settings object, and try to restore on app load. #18

Merged
merged 13 commits into from
Jun 10, 2024
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class KNXApp extends Homey.App {
const homeyIP = address.split(':', 1).toString();

this.log('Homey IP + Parsed IP', address, homeyIP);
this.knxInterfaceManager = new KNXInterfaceManager(homeyIP);
this.knxInterfaceManager = new KNXInterfaceManager(homeyIP, this.homey);
}

/**
Expand Down
20 changes: 3 additions & 17 deletions lib/GenericKNXDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,6 @@ class GenericKNXDriver extends Homey.Driver {
this.knxInterfaceManager = this.homey.app.getKNXInterfaceManager();
}

/**
* Return a list of KNX interfaces
*
* @returns {{ip: *, name: *, mac: *}[]}
*/
getInterfaceList() {
return Object.values(this.knxInterfaceManager.getKNXInterfaceList()).map((knxInterface) => {
return {
name: knxInterface.name,
mac: knxInterface.macAddress,
ip: knxInterface.ipAddress,
};
});
}

// Obtain a stored ETS export for the selected IP interface.
getGroupAddressList() {
Expand Down Expand Up @@ -56,15 +42,15 @@ class GenericKNXDriver extends Homey.Driver {

// List al the found or added IP interfaces.
session.setHandler('list_interfaces', async () => {
return this.getInterfaceList();
return this.knxInterfaceManager.getSimpleInterfaceList();
});

// However the structure seems to be the same as the manual entry, which does work.
// Trigger the interface manager to search for KNX IP interfaces
session.setHandler('search_interfaces', async () => {
try {
await this.knxInterfaceManager.searchInterfaces();
return this.getInterfaceList();
return this.knxInterfaceManager.getSimpleInterfaceList();
} catch (error) {
throw new Error(this.homey.__(`errors.ip.${error.message}`));
}
Expand All @@ -74,7 +60,7 @@ class GenericKNXDriver extends Homey.Driver {
session.setHandler('manual_ip_address', async (manualIPAddress) => {
try {
await this.knxInterfaceManager.discoverKNXInterfaceOnIP(manualIPAddress);
return this.getInterfaceList();
return this.knxInterfaceManager.getSimpleInterfaceList();
} catch (error) {
throw new Error(this.homey.__(`errors.ip.${error.message}`));
}
Expand Down
34 changes: 33 additions & 1 deletion lib/KNXInterfaceManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ const KNXInterface = require('./KNXInterface');

class KNXInterfaceManager extends EventEmitter {

constructor(localIP) {
constructor(localIP, homey) {
super();

this.homey = homey;

// Formatted logging
this.log = console.log.bind(this, '[KNX interface manager]');
this.errorLog = console.error.bind(this, '[KNX interface manager] ERROR:');
Expand All @@ -23,10 +26,18 @@ class KNXInterfaceManager extends EventEmitter {

if (ip.isV4Format(localIP)) {
this.localIPBuffer = ip.toBuffer(ip.address());
const interfaces = this.homey.settings.get(`interfaces`) || [];

this.findKNXInterfaces()
.catch((error) => {
this.log('findKNXinterfaces error', error);
}).finally(() => {
for (const i of interfaces) {
// Force recheck interfaces from previous session.
if (i && i.mac && i.ip && !this.KNXInterfaces[i.mac]) {
this.checkKNXInterface(i.ip);
}
}
});
} else {
this.errorLog('IP address is not a valid IPv4 format');
Expand Down Expand Up @@ -59,6 +70,21 @@ class KNXInterfaceManager extends EventEmitter {
getKNXInterfaceList() {
return this.KNXInterfaces;
}

/**
* Return a list of KNX interfaces
*
* @returns {{ip: *, name: *, mac: *}[]}
*/
getSimpleInterfaceList() {
return Object.values(this.KNXInterfaces).map(knxInterface => {
return {
name: knxInterface.name,
mac: knxInterface.macAddress,
ip: knxInterface.ipAddress,
};
});
}

// Check if a given IP is a KNX IP Interface
async discoverKNXInterfaceOnIP(ipAddress) {
Expand Down Expand Up @@ -199,6 +225,10 @@ class KNXInterfaceManager extends EventEmitter {
// Use the Interface MAC as the key value
this.KNXInterfaces[knxInterface.interfaceMac] = new KNXInterface(knxInterface);
}

const interfaces = this.getSimpleInterfaceList();
this.homey.settings.set(`interfaces`, interfaces);

this.emit('interface_found', this.KNXInterfaces[knxInterface.interfaceMac]);
return resolve(this.KNXInterfaces[knxInterface.interfaceMac]);
} catch (error) {
Expand Down Expand Up @@ -313,6 +343,8 @@ class KNXInterfaceManager extends EventEmitter {
try {
// Use the Interface MAC as the key value
this.KNXInterfaces[knxInterface.interfaceMac] = new KNXInterface(knxInterface);
const interfaces = this.getSimpleInterfaceList();
this.homey.settings.set(`interfaces`, interfaces);
this.emit('interface_found', this.KNXInterfaces[knxInterface.interfaceMac]);
return resolve(this.KNXInterfaces[knxInterface.interfaceMac]);
} catch (err) {
Expand Down