-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #289 from pdbjjens/master
Add: myvbus.js detector & vbus.js method
- Loading branch information
Showing
4 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
'use strict'; | ||
|
||
const tools = require('../tools.js'); | ||
|
||
|
||
//const port80 = ':80'; | ||
const port3000 = ':3000'; | ||
|
||
const debug = false; | ||
|
||
function parseDeviceInformation(string) { | ||
const result = {}; | ||
|
||
const re = /([\w]+)[\s]*=[\s]*"([^"\r\n]*)"/g; | ||
|
||
let md; | ||
while ((md = re.exec(string)) !== null) { | ||
result [md [1]] = md [2]; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
|
||
function detect(ip, device, options, callback) { | ||
if (device._source !== 'RESOL VBus') return callback(null, false, ip); | ||
if (debug) options.log.debug(`Start detecting VBus device information on ${ip}...`); | ||
|
||
const port = port3000; | ||
|
||
const devInfoURL = 'http://' + ip + port + '/cgi-bin/get_resol_device_information'; | ||
|
||
if (debug) options.log.debug(`Device Information URL ${devInfoURL}`); | ||
|
||
tools.httpGet(devInfoURL, 2000, (err, data) => { | ||
//if (debug) options.log.debug(`Device Information of ${ip} ` + data); | ||
const devInfo = parseDeviceInformation(data); | ||
if (debug) options.log.debug(`Parsed Device Information of ${ip} ` + JSON.stringify(devInfo)); | ||
if (!err && data && devInfo) { | ||
|
||
let instance = tools.findInstance (options, 'myvbus', obj => obj.native.connectionIdentifier === ip); | ||
if (!instance) { | ||
const name = device._name ? devInfo.name ? devInfo.name : device._name : ''; | ||
device._name = name; | ||
let deviceType = 'lan'; | ||
if (devInfo.product === 'DL2' || devInfo.product === 'KM2') { | ||
deviceType = 'dl2'; | ||
} else if (devInfo.product === 'DL3') { | ||
deviceType = 'dl3'; | ||
} | ||
//if (debug) options.log.debug(`Device ` + name + ' ' + JSON.stringify(device)); | ||
|
||
instance = { | ||
_id: tools.getNextInstanceID ('myvbus', options), | ||
common: { | ||
name: 'myvbus', | ||
title: 'RESOL Device (' + ip + (name ? (' - ' + name) : '') + ')' | ||
}, | ||
native: { | ||
connectionIdentifier: ip, | ||
connectionDevice: deviceType, | ||
vbusPassword: 'vbus' | ||
}, | ||
comment: { | ||
add: [`${devInfo.vendor} ${name} (${ip})`] | ||
} | ||
}; | ||
options.newInstances.push (instance); | ||
return callback (null, true, ip); | ||
} | ||
} | ||
callback(null, false, ip); | ||
}); | ||
} | ||
|
||
exports.detect = detect; | ||
exports.type = ['vbus']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
'use strict'; | ||
|
||
const tools = require('../tools.js'); | ||
|
||
|
||
//const port80 = ':80'; | ||
const port3000 = ':3000'; | ||
|
||
const debug = false; | ||
|
||
function parseDeviceInformation(string) { | ||
const result = {}; | ||
|
||
const re = /([\w]+)[\s]*=[\s]*"([^"\r\n]*)"/g; | ||
|
||
let md; | ||
while ((md = re.exec(string)) !== null) { | ||
result [md [1]] = md [2]; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
|
||
function detect(ip, device, options, callback) { | ||
if (device._source !== 'RESOL VBus') return callback(null, false, ip); | ||
if (debug) options.log.debug(`Start detecting VBus device information on ${ip}...`); | ||
|
||
const port = port3000; | ||
|
||
const devInfoURL = 'http://' + ip + port + '/cgi-bin/get_resol_device_information'; | ||
|
||
if (debug) options.log.debug(`Device Information URL ${devInfoURL}`); | ||
|
||
tools.httpGet(devInfoURL, 2000, (err, data) => { | ||
//if (debug) options.log.debug(`Device Information of ${ip} ` + data); | ||
const devInfo = parseDeviceInformation(data); | ||
if (debug) options.log.debug(`Parsed Device Information of ${ip} ` + JSON.stringify(devInfo)); | ||
if (!err && data && devInfo) { | ||
|
||
let instance = tools.findInstance (options, 'resol', obj => obj.native.connectionIdentifier === ip); | ||
if (!instance) { | ||
const name = device._name ? devInfo.name ? devInfo.name : device._name : ''; | ||
device._name = name; | ||
let deviceType = 'lan'; | ||
if (devInfo.product === 'DL2' || devInfo.product === 'KM2') { | ||
deviceType = 'dl2'; | ||
} else if (devInfo.product === 'DL3') { | ||
deviceType = 'dl3'; | ||
} | ||
//if (debug) options.log.debug(`Device ` + name + ' ' + JSON.stringify(device)); | ||
|
||
instance = { | ||
_id: tools.getNextInstanceID ('resol', options), | ||
common: { | ||
name: 'resol', | ||
title: 'RESOL Device (' + ip + (name ? (' - ' + name) : '') + ')' | ||
}, | ||
native: { | ||
connectionIdentifier: ip, | ||
connectionDevice: deviceType, | ||
vbusPassword: 'vbus' | ||
}, | ||
comment: { | ||
add: [`${devInfo.vendor} ${name} (${ip})`] | ||
} | ||
}; | ||
options.newInstances.push (instance); | ||
return callback (null, true, ip); | ||
} | ||
} | ||
callback(null, false, ip); | ||
}); | ||
} | ||
|
||
exports.detect = detect; | ||
exports.type = ['vbus']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Detects RESOL devices which support the VBus discovery protocol | ||
* e.g. DL2, DL3, KM2, VBus/LAN | ||
*/ | ||
|
||
// The results are devices of the form | ||
// { | ||
// _addr: device_ip | ||
// _name: 'RESOL VBus' | ||
// } | ||
// } | ||
// | ||
// the type of this discovery module is | ||
// type: 'vbus' | ||
// To identify the adapter which handles the device, dedicated detector modules are required. | ||
// | ||
|
||
'use strict'; | ||
|
||
//const tools = require('../tools.js'); | ||
const dgram = require('dgram'); | ||
|
||
const bcastAddress = '255.255.255.255'; | ||
const bcastPort = 7053; | ||
const queryString = '---RESOL-BROADCAST-QUERY---'; | ||
const replyString = '---RESOL-BROADCAST-REPLY---'; | ||
const debug = false; | ||
|
||
function discover(self) { | ||
|
||
debug && self.adapter.log.debug('Discovering VBus devices...'); | ||
|
||
const socket = dgram.createSocket({type: 'udp4', reuseAddr: true}); | ||
|
||
const addressList = []; | ||
|
||
socket.on('message', (msg, rinfo) => { | ||
if ((rinfo.family === 'IPv4') && (rinfo.port === 7053) && (msg.length >= replyString.length)) { | ||
const msgString = msg.slice(0, replyString.length).toString(); | ||
if (msgString === replyString) { | ||
const { address } = rinfo; | ||
if (addressList.indexOf(address) < 0) { | ||
addressList.push(address); | ||
debug && self.adapter.log.debug(`VBus Device with IP ${rinfo.address} discovered...`); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
socket.bind(0, () => { | ||
socket.setBroadcast(true); | ||
const queryBuffer = Buffer.from(queryString); | ||
socket.send(queryBuffer, 0, queryBuffer.length, bcastPort, bcastAddress); | ||
debug && self.adapter.log.debug(`Send discovery request: ${queryString}`); | ||
}); | ||
|
||
setTimeout(() => { | ||
socket.close(); | ||
for (const address of addressList) { | ||
self.addDevice({ | ||
_addr: address, | ||
_name: 'RESOL VBus Device', | ||
}); | ||
} | ||
self.done(); | ||
}, self.timeout); | ||
} | ||
|
||
module.exports = { | ||
browse: discover, | ||
type: 'vbus', | ||
source: 'RESOL VBus', | ||
timeout: 1500 | ||
}; |