Skip to content

Commit

Permalink
refactor devices
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan1986 committed Mar 18, 2024
1 parent 3876ccd commit f9a4653
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 146 deletions.
4 changes: 0 additions & 4 deletions src/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@
<script src="dht.js"></script>

<script src="lib/device/generic.js"></script>
<script src="lib/device/airplay.js"></script>
<script src="lib/device/dlna.js"></script>
<script src="lib/device/chromecast.js"></script>
<script src="lib/device/ext-player.js"></script>
<script src="lib/subtitle/generic.js"></script>
<script src="lib/subtitle/server.js"></script>
<script src="lib/streamer.js"></script>
Expand Down
24 changes: 12 additions & 12 deletions src/app/lib/device/airplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
'use strict';

var airplayer = require('airplayer'),
list = airplayer(),
netw = require('network-address'),
collection = App.Device.Collection;

class Airplay extends App.Device.Generic {
class Airplay extends App.Device.Loaders.Device {
constructor(attrs) {
super(Object.assign( {
type: 'airplay',
Expand Down Expand Up @@ -40,18 +39,19 @@
unpause() {
this.device.resume();
}
}


list.on('update', function (player) {
win.info('Found A Device Device: %s at %s', player.name, player.host);
collection.add(new Airplay({
device: player
}));
});
static scan() {
airplayer().on('update', function (player) {
win.info('Found A Device Device: %s at %s', player.name, player.host);
collection.add(new Airplay({
device: player
}));
});

win.info('Scanning: Local Network for Airplay devices');
}
}

win.info('Scanning: Local Network for Airplay devices');
App.Device.Airplay = Airplay;
App.Device.Loaders.Airplay = Airplay;

})(window.App);
24 changes: 13 additions & 11 deletions src/app/lib/device/chromecast.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
client = new ChromecastAPI(),
collection = App.Device.Collection;

class Chromecast extends App.Device.Generic {
class Chromecast extends App.Device.Loaders.Device {
constructor(attrs) {
super(Object.assign( {
type: 'chromecast',
Expand Down Expand Up @@ -185,16 +185,18 @@
App.vent.trigger('device:status', status);
}
}
}

win.info('Scanning: Local Network for Chromecast devices');
client.update();
client.on('device', function (player) {
win.info('Found Chromecast Device: %s at %s', player.friendlyName, player.host);
collection.add(new Chromecast({
device: player
}));
});
static scan() {
win.info('Scanning: Local Network for Chromecast devices');
client.update();
client.on('device', function (player) {
win.info('Found Chromecast Device: %s at %s', player.friendlyName, player.host);
collection.add(new Chromecast({
device: player
}));
});
}
}

App.Device.Chromecast = Chromecast;
App.Device.Loaders.Chromecast = Chromecast;
})(window.App);
34 changes: 17 additions & 17 deletions src/app/lib/device/dlna.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var collection = App.Device.Collection;


class Dlna extends App.Device.Generic {
class Dlna extends App.Device.Loaders.Device {
constructor(attrs) {
super(Object.assign({
type: 'dlna',
Expand Down Expand Up @@ -118,24 +118,24 @@
App.vent.trigger('device:status', status);
}
}
}

static scan() {
dlnacasts.on('update', function(player) {
if (collection.where({
id: player.host
}).length === 0) {
win.info('Found DLNA Device: %s at %s', player.name, player.host);
collection.add(new Dlna({
id: player.host,
player: player
}));
}
});

dlnacasts.on('update', function(player) {
if (collection.where({
id: player.host
}).length === 0) {
win.info('Found DLNA Device: %s at %s', player.name, player.host);
collection.add(new Dlna({
id: player.host,
player: player
}));
win.info('Scanning: Local Network for DLNA devices');
dlnacasts.update();
}
});

win.info('Scanning: Local Network for DLNA devices');
dlnacasts.update();

}

App.Device.Dlna = Dlna;
App.Device.Loaders.Dlna = Dlna;
})(window.App);
168 changes: 87 additions & 81 deletions src/app/lib/device/ext-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,17 @@
}
};

extPlayerlst = Object.getOwnPropertyNames(players).join(', ').replace(/MPC-HC64, |MPC-BE64, |Mini64/gi, '').replace('Extended', 'Ext.').replace('mpvnet', 'mpv.net').replace(/,([^,]*)$/, ' &$1');
/* map name back into the object as we use it in match */
_.each(players, function (v, k) {
players[k].name = k;
});

extPlayerlst = Object.getOwnPropertyNames(players).join(', ')
.replace(/MPC-HC64, |MPC-BE64, |Mini64/gi, '')
.replace('Extended', 'Ext.')
.replace('mpvnet', 'mpv.net')
.replace(/,([^,]*)$/, ' &$1')
;

function getPlayerName(loc) {
return path.basename(loc).replace(path.extname(loc), '');
Expand Down Expand Up @@ -149,7 +159,7 @@
return players[name].fs || '';
}

class ExtPlayer extends App.Device.Generic {
class ExtPlayer extends App.Device.Loaders.Device {
constructor(attrs) {
super(Object.assign({
type: 'ext-app',
Expand Down Expand Up @@ -215,93 +225,89 @@
stop() {}

unpause() {}
}

/* map name back into the object as we use it in match */
_.each(players, function (v, k) {
players[k].name = k;
});

var searchPaths = {
linux: [],
darwin: [],
win32: []
};
static scan() {
var searchPaths = {
linux: [],
darwin: [],
win32: []
};

var addPath = function (path) {
if (fs.existsSync(path)) {
searchPaths[process.platform].push(path);
}
};
var addPath = function (path) {
if (fs.existsSync(path)) {
searchPaths[process.platform].push(path);
}
};

// linux
addPath('/usr/bin');
addPath('/usr/local/bin');
addPath('/snap/bin');
addPath('/var/lib/flatpak/app/org.videolan.VLC/current/active'); //Fedora Flatpak VLC Dir
addPath(process.env.HOME + '/.nix-profile/bin'); // NixOS
addPath('/run/current-system/sw/bin'); // NixOS
// darwin
addPath('/Applications');
addPath(process.env.HOME + '/Applications');
// win32
addPath(process.env.SystemDrive + '\\Program Files\\');
addPath(process.env.SystemDrive + '\\Program Files (x86)\\');
addPath(process.env.LOCALAPPDATA + '\\Apps\\2.0\\');
// linux
addPath('/usr/bin');
addPath('/usr/local/bin');
addPath('/snap/bin');
addPath('/var/lib/flatpak/app/org.videolan.VLC/current/active'); //Fedora Flatpak VLC Dir
addPath(process.env.HOME + '/.nix-profile/bin'); // NixOS
addPath('/run/current-system/sw/bin'); // NixOS
// darwin
addPath('/Applications');
addPath(process.env.HOME + '/Applications');
// win32
addPath(process.env.SystemDrive + '\\Program Files\\');
addPath(process.env.SystemDrive + '\\Program Files (x86)\\');
addPath(process.env.LOCALAPPDATA + '\\Apps\\2.0\\');

var folderName = '';
var birthtimes = {};
var birthtimes = {};

async.each(searchPaths[process.platform], function (folderName, pathcb) {
folderName = path.resolve(folderName);
win.info('Scanning: ' + folderName);
var appIndex = -1;
var fileStream = readdirp({
root: folderName,
depth: 3
});
fileStream.on('data', function (d) {
var app = d.name.replace('.app', '').replace('.exe', '').toLowerCase();
var match = _.filter(players, function (v, k) {
return k.toLowerCase() === app;
});
async.each(searchPaths[process.platform], function (folderName, pathcb) {
folderName = path.resolve(folderName);
win.info('Scanning: ' + folderName);
var appIndex = -1;
var fileStream = readdirp({
root: folderName,
depth: 3
});
fileStream.on('data', function (d) {
var app = d.name.replace('.app', '').replace('.exe', '').toLowerCase();
var match = _.filter(players, function (v, k) {
return k.toLowerCase() === app;
});

if (match.length) {
match = match[0];
var birthtime = d.stat.birthtime;
var previousBirthTime = birthtimes[match.name];
if (!previousBirthTime || birthtime > previousBirthTime) {
if (!previousBirthTime) {
collection.add(new ExtPlayer({
id: match.name,
type: 'external-' + match.type,
name: match.name,
path: d.fullPath
}));
win.info('Found External Player: ' + match.name + ' in ' + d.fullParentDir);
} else {
collection.findWhere({
id: match.name
}).set('path', d.fullPath);
win.info('Updated External Player: ' + app + ' with more recent version found in ' + d.fullParentDir);
if (match.length) {
match = match[0];
var birthtime = d.stat.birthtime;
var previousBirthTime = birthtimes[match.name];
if (!previousBirthTime || birthtime > previousBirthTime) {
if (!previousBirthTime) {
collection.add(new ExtPlayer({
id: match.name,
type: 'external-' + match.type,
name: match.name,
path: d.fullPath
}));
win.info('Found External Player: ' + match.name + ' in ' + d.fullParentDir);
} else {
collection.findWhere({
id: match.name
}).set('path', d.fullPath);
win.info('Updated External Player: ' + app + ' with more recent version found in ' + d.fullParentDir);
}
birthtimes[match.name] = birthtime;
}
}
birthtimes[match.name] = birthtime;
}
}
});
fileStream.on('end', function () {
pathcb();
});
}, function (err) {
});
fileStream.on('end', function () {
pathcb();
});
}, function (err) {

if (err) {
win.error('External Players: scan', err);
return;
} else {
win.info('External Players: scan finished');
return;
if (err) {
win.error('External Players: scan', err);
return;
} else {
win.info('External Players: scan finished');
return;
}
});
}
});
}

App.Device.ExtPlayer = ExtPlayer;
App.Device.Loaders.ExtPlayer = ExtPlayer;
})(window.App);
Loading

0 comments on commit f9a4653

Please sign in to comment.