Skip to content

Commit

Permalink
Merge pull request #31 from volumio/impro/discoveryImpros
Browse files Browse the repository at this point in the history
Impro/discovery impros
  • Loading branch information
volumio committed Nov 7, 2021
2 parents 01f8973 + 5709cac commit 9d7a8c4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ CoreCommandRouter.prototype.volumioSearch = function (data) {

CoreCommandRouter.prototype.volumioPushState = function (state) {
this.pushConsoleMessage('CoreCommandRouter::volumioPushState');
this.executeOnPlugin('system_controller', 'volumiodiscovery', 'saveDeviceInfo', state);

// Announce new player state to each client interface
var self = this;
var res = libQ.all(
Expand Down
1 change: 0 additions & 1 deletion app/plugins/audio_interface/alsa_controller/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var io = require('socket.io-client');
var fs = require('fs-extra');
var exec = require('child_process').exec;
var execSync = require('child_process').execSync;
Expand Down
2 changes: 1 addition & 1 deletion app/plugins/system_controller/networkfs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ ControllerNetworkfs.prototype.mountPartition = function (partitionData) {
try {
execSync(mountCMD, {uid: 1000, gid: 1000});
self.storeMountedFolder(partitionData.mountFolder);
if (checkLabelForInternalDiskToBeMounted(partitionData.label)) {
if (self.checkLabelForInternalDiskToBeMounted(partitionData.label)) {
self.bindInternalMemoryPosition();
} else {
var message = partitionData.label + ' ' + self.commandRouter.getI18nString('COMMON.CONNECTED');
Expand Down
41 changes: 11 additions & 30 deletions app/plugins/system_controller/volumiodiscovery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ ControllerVolumioDiscovery.prototype.startAdvertisement = function () {
self.forceRename = undefined;

try {
var systemController = self.commandRouter.pluginManager.getPlugin('system_controller', 'system');
var name = systemController.getConf('playerName');
var uuid = systemController.getConf('uuid');
var name = self.commandRouter.sharedVars.get('system.name');
var uuid = self.commandRouter.sharedVars.get('system.uuid');
var serviceName = config.get('service');
var servicePort = config.get('port');

Expand All @@ -106,23 +105,8 @@ ControllerVolumioDiscovery.prototype.startAdvertisement = function () {
self.logger.info('Discovery: Started advertising with name: ' + name);

self.ad = mdns.createAdvertisement(mdns.tcp(serviceName), servicePort, {txtRecord: txt_record}, function (error, service) {
var lowerServer = serviceName.toLowerCase();
var theName = service.name.replace(lowerServer, serviceName);
if ((theName != name) && (forceRename === false)) {
self.logger.info('Discovery: Changing my name to: ' + service.name);
systemController.setConf('playerName', theName);
self.ad.stop();
txt_record.volumioName = theName;
setTimeout(
function () {
self.ad = mdns.createAdvertisement(mdns.tcp(serviceName), servicePort, {txtRecord: txt_record}, function (error, service) {
if (error) {
self.logger.error('Discovery: advertisement start error: ' + error);
}
});
},
5000
);
if (error) {
self.logger.error('MDNS Advertisement error: ' + error);
}
});
self.ad.on('error', function (error) {
Expand Down Expand Up @@ -248,8 +232,7 @@ ControllerVolumioDiscovery.prototype.initSocket = function (data) {
// Wait untill the current connection times out
setTimeout(() => {
// If this device is in our mDNS cache and we got this message, then the device was offline and went back online. We reconnect the socketIO.
var systemController = self.commandRouter.pluginManager.getPlugin('system_controller', 'system');
var myuuid = systemController.getConf('uuid');
var myuuid = self.commandRouter.sharedVars.get('system.uuid');
if (foundVolumioInstances.get(data.id + '.name') && !self.remoteConnections.has(data.id) && myuuid != data.id) {
var addresses = foundVolumioInstances.get(data.id + '.addresses');
if (addresses && addresses[0] && addresses[0].value && addresses[0].value[0].value) {
Expand All @@ -264,8 +247,7 @@ ControllerVolumioDiscovery.prototype.initSocket = function (data) {
ControllerVolumioDiscovery.prototype.connectToRemoteVolumio = function (uuid, ip) {
var self = this;

var systemController = self.commandRouter.pluginManager.getPlugin('system_controller', 'system');
var myuuid = systemController.getConf('uuid');
var myuuid = self.commandRouter.sharedVars.get('system.uuid');

if ((!self.remoteConnections.has(uuid)) && (myuuid != uuid)) {
var socket = io.connect('http://' + ip + ':3000');
Expand Down Expand Up @@ -344,8 +326,7 @@ ControllerVolumioDiscovery.prototype.saveDeviceInfo = function (data) {
var uuid = data.uuid;

if (uuid == undefined) {
var systemController = self.commandRouter.pluginManager.getPlugin('system_controller', 'system');
uuid = systemController.getConf('uuid');
uuid = self.commandRouter.sharedVars.get('system.uuid');
// console.log("Using self UUID");
}
foundVolumioInstances.set(uuid + '.status', data.status);
Expand All @@ -363,8 +344,8 @@ ControllerVolumioDiscovery.prototype.saveDeviceInfo = function (data) {

ControllerVolumioDiscovery.prototype.getDevices = function () {
var self = this;
var systemController = self.commandRouter.pluginManager.getPlugin('system_controller', 'system');
var myuuid = systemController.getConf('uuid');

var myuuid = self.commandRouter.sharedVars.get('system.uuid');

var response = {
misc: {debug: true},
Expand Down Expand Up @@ -451,15 +432,15 @@ ControllerVolumioDiscovery.prototype.getThisDevice = function () {
var thisDevice = {};
var thisState = self.commandRouter.volumioGetState();
var ipAddresses = self.commandRouter.executeOnPlugin('system_controller', 'network', 'getCachedPAddresses', '');
thisDevice.id = self.commandRouter.executeOnPlugin('system_controller', 'system', 'getConf', 'uuid');
thisDevice.id = self.commandRouter.sharedVars.get('system.uuid');
if (ipAddresses && ipAddresses.eth0 && ipAddresses.eth0 != '') {
thisDevice.host = 'http://' + ipAddresses.eth0;
} else if (ipAddresses && ipAddresses.wlan0 && ipAddresses.wlan0 !== '192.168.211.1') {
thisDevice.host = 'http://' + ipAddresses.wlan0;
} else {
thisDevice.host = 'http://127.0.0.1';
}
thisDevice.name = self.commandRouter.executeOnPlugin('system_controller', 'system', 'getConf', 'playerName');
thisDevice.name = self.commandRouter.sharedVars.get('system.name');
thisDevice.type = config.get('device_type', 'device');
thisDevice.serviceName = config.get('service');
thisDevice.state = {
Expand Down
15 changes: 13 additions & 2 deletions app/statemachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function CoreStateMachine (commandRouter) {
this.isVolatile = false;
this.currentDisableVolumeControl = false;
this.previousTrackonPrev = false;
this.lastSavedStateToString = '{}';
/**
* This field tells the system if it is currenty running in consume mode
* @type {boolean} true or false wether the system is in consume mode
Expand Down Expand Up @@ -486,12 +487,12 @@ CoreStateMachine.prototype.getcurrentVolume = function () {
// Announce updated Volumio state
CoreStateMachine.prototype.pushState = function () {
this.commandRouter.pushConsoleMessage('CoreStateMachine::pushState');

var self = this;
var promise = libQ.defer();

var state = this.getState();
self.saveCurrenState(state);

var self = this;
self.commandRouter.volumioPushState(state)
.then(function (data) {
self.checkFavourites(state)
Expand All @@ -503,6 +504,16 @@ CoreStateMachine.prototype.pushState = function () {
return promise.promise;
};

CoreStateMachine.prototype.saveCurrenState = function (state) {
var self = this;

let newStateToString = JSON.stringify(state);
if (self.lastSavedStateToString !== newStateToString) {
self.lastSavedStateToString = newStateToString;
return this.commandRouter.executeOnPlugin('system_controller', 'volumiodiscovery', 'saveDeviceInfo', state);
}
};

CoreStateMachine.prototype.pushEmptyState = function () {
this.commandRouter.pushDebugConsoleMessage('CoreStateMachine::pushEmptyState');

Expand Down

0 comments on commit 9d7a8c4

Please sign in to comment.