UNPKG

domotz-remote-pawn

Version:

Domotz Agent

77 lines (63 loc) 2.32 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. **/ var _ = require('underscore'); var q = require('q'); var bonjourFactory = require('bonjour'); var bonjourParser = require("./parsers/base_parser"); // Bonjour module use this options to initialize a mutlicast dns server // https://www.npmjs.com/package/multicast-dns const DEFAULT_OPTIONS = { host: '224.0.0.251', // N.B. Equivalent to ip port: 5353, type: 'udp4', interface: null, // Just for udp6 reuseAddr: true, // If another MDNS is on the system socket: null, // If you want create your own socket multicast: true, ttl: 255, loopback: true // Set Multicast Loopback }; const BONJOUR_DISCOVERY_TIMEOUT = 7000; module.exports.factory = function (opts) { opts = opts || _.extend(DEFAULT_OPTIONS); var bonjour = bonjourFactory(opts); var working = true; bonjour._server.mdns.on('error', function(err) { console.warn('BONJOUR - (mdns): Error in initializing server: %s', err.message); working = false; }); function discover(cb) { var result = []; if (!working) { console.warn('BONJOUR - (mdns): No internal server active. Another MDNS daemon is running.'); cb([]); return; } var browser = bonjour.find({}, function onUp(s) { var entry = bonjourParser.parse(s, s.type); result.push(entry); }); setTimeout(function () { browser.stop(); cb(result); //bonjour.destroy(); }, BONJOUR_DISCOVERY_TIMEOUT); } return { discover: discover }; };