Skip to content

Commit

Permalink
remove nextport and hm-discover
Browse files Browse the repository at this point in the history
  • Loading branch information
ptweety committed Oct 11, 2022
1 parent c3ddd3a commit acff33d
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 50 deletions.
6 changes: 3 additions & 3 deletions nodes/ccu-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const fs = require('node:fs');
const crypto = require('node:crypto');

const stringSimilarity = require('string-similarity');
const nextport = require('nextport');
const hmDiscover = require('hm-discover');
const Rega = require('homematic-rega');
const xmlrpc = require('homematic-xmlrpc');
const binrpc = require('binrpc');
const discover = require('./lib/discover.js');
const nextport = require('./lib/nextport.js');

const pkg = require(path.join(__dirname, '..', 'package.json'));

Expand Down Expand Up @@ -47,7 +47,7 @@ module.exports = function (RED) {
});
}

hmDiscover(result => {
discover(result => {
ccu.network.discover = result;
});

Expand Down
94 changes: 94 additions & 0 deletions nodes/lib/discover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Based on https://github.com/hobbyquaker/hm-discover by Sebastian Raff (Hobbyquaker)
*/

const net = require('node:net');
const dgram = require('node:dgram');
const buf = require('node:buffer').Buffer;

const checkService = (id, host, port) =>
new Promise(resolve => {
const c = net.connect({port, host, timeout: this.timeout}, () => {
c.end();
resolve({id, port, active: true});
});
c.on('error', () => {
resolve({id, port, active: false});
});
});

const discover = (options, callback) => {
if (typeof options === 'function') {
callback = options;
options = {};
} else if (typeof options !== 'object') {
options = {};
}

const timeout = options.timeout || 1200;
const remoteport = 43_439;
const nullByte = buf.from([0x00]);
const message = buf.from([0x02, 0x8F, 0x91, 0xC0, 0x01, 'e', 'Q', '3', 0x2D, 0x2A, 0x00, 0x2A, 0x00, 0x49]);
const header = message.subarray(0, 5);
const found = [];
const foundAddresses = [];
const client = dgram.createSocket('udp4');

client.on('message', async (response, remote) => {
if (response.subarray(0, 5).equals(header)) {
const indexType = response.indexOf(nullByte, 5);
const indexSerial = response.indexOf(nullByte, indexType + 1);
const indexVersion = response.indexOf(nullByte, indexSerial + 1 + 3);

const device = {
type: response.toString('utf8', 5, indexType),
serial: response.toString('utf8', indexType + 1, indexSerial),
version: response.toString('utf8', indexSerial + 1 + 3, indexVersion),
address: remote.address,
interfaces: {},
};

if (!foundAddresses.includes(remote.address)) {
foundAddresses.push(remote.address);

const requests = [
{id: 'ReGaHSS', port: 1999},
{id: 'BidCos-Wired', port: 2000},
{id: 'BidCos-RF', port: 2001},
{id: 'HmIP-RF', port: 2010},
{id: 'VirtualDevices', port: 9292},
{id: 'CUxD', port: 8701},
// {id: 'CCU-Jack (VEAP)', port: 2121},
// {id: 'CCU-Jack (secure VEAP)', port: 2122},
// {id: 'CCU-Jack (MQTT)', port: 1883},
// {id: 'CCU-Jack (secure MQTT)', port: 8883},
];

const returnValues = await Promise.all(
requests.map(async item => checkService(item.id, remote.address, item.port)),
);

for (const item of returnValues) {
device.interfaces[item.id] = {
port: item.port,
active: item.active,
};
}

found.push(device);
}
}
});

client.bind(() => {
client.setBroadcast(true);
client.send(message, 0, message.length, remoteport, '255.255.255.255');
});

setTimeout(() => {
client.close();
callback(found);
}, timeout);
};

module.exports = discover;
35 changes: 35 additions & 0 deletions nodes/lib/nextport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Based on https://github.com/hobbyquaker/nextport by Sebastian Raff (Hobbyquaker)
*/

const net = require('node:net');

const getPort = (port, address, cb) => {
if (typeof address === 'function') {
cb = address;
address = '0.0.0.0';
}

const server = net.createServer();

server.on('listening', () => {
server.close();
});

server.on('close', () => {
cb(port);
});

server.on('error', () => {
port += 1;
if (port <= 65_535) {
getPort(port, address, cb);
} else {
cb(null);
}
});

server.listen(port, address);
};

module.exports = getPort;
45 changes: 0 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@
],
"dependencies": {
"binrpc": "^3.3.1",
"hm-discover": "^1.1.3",
"homematic-rega": "^1.5.2",
"homematic-xmlrpc": "^1.0.2",
"mqtt-wildcard": "^3.0.9",
"nextport": "^1.0.0",
"obj-ease": "^1.0.1",
"string-similarity": "^4.0.4"
},
Expand Down

0 comments on commit acff33d

Please sign in to comment.