From 92820b20654f51c5aa5802760538b3bfbcb4b90d Mon Sep 17 00:00:00 2001 From: Thomas Kluge Date: Tue, 21 Jan 2020 18:30:45 +0100 Subject: [PATCH] some additional checks for #98 --- lib/Server.js | 81 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 32 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index e4cffd0..5d3a780 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -438,33 +438,41 @@ Server.prototype.handleConfigurationRequest = function(dispatched_request) { } if (parsed.searchParams.get('daemon') !== null) { - if (this.hazDaemon()) { - dispatched_request.dispatchFile(null, 'daemon.html', this.addDefaultIndexAttributes({ - 'message': that.localization.localize('Disable HVL launch at boot'), - 'link': '/index/?disabledaemon' - })) + if (process.platform === 'linux') { + if (this.hazDaemon()) { + dispatched_request.dispatchFile(null, 'daemon.html', this.addDefaultIndexAttributes({ + 'message': that.localization.localize('Disable HVL launch at boot'), + 'link': '/index/?disabledaemon' + })) + } else { + dispatched_request.dispatchFile(null, 'daemon.html', this.addDefaultIndexAttributes({ + 'message': that.localization.localize('Enable HVL launch at boot'), + 'link': '/index/?enabledaemon' + })) + } } else { dispatched_request.dispatchFile(null, 'daemon.html', this.addDefaultIndexAttributes({ - 'message': that.localization.localize('Enable HVL launch at boot'), - 'link': '/index/?enabledaemon' + 'message': that.localization.localize('This is not supported on your os'), + 'link': '#' })) } + cfg_handled = true } if (parsed.searchParams.get('enabledaemon') !== null) { - this.enableDaemon() + let msg = this.enableDaemon() ? 'Added as service' : 'Cannot add hvl as a service' dispatched_request.dispatchFile(null, 'message.html', this.addDefaultIndexAttributes({ - 'message': that.localization.localize('Added as service'), + 'message': that.localization.localize(msg), 'link': '#' })) cfg_handled = true } if (parsed.searchParams.get('disabledaemon') !== null) { - this.disableDaemon() + let msg = this.disableDaemon() ? 'removed service' : 'unable to remove the service' dispatched_request.dispatchFile(null, 'message.html', this.addDefaultIndexAttributes({ - 'message': that.localization.localize('removed service'), + 'message': that.localization.localize(msg), 'link': '#' })) cfg_handled = true @@ -968,30 +976,39 @@ Server.prototype.currentUser = function() { Server.prototype.enableDaemon = function() { - this.configuration.setValue('daemon', true) - // Write systemd file - let tmpFile = path.join(os.tmpdir(), this.systemdFile) - if (fs.existsSync(tmpFile)) { - fs.unlinkSync(tmpFile) - }  - - let execCmd = path.join(appRoot, '/bin/hmvi') - var strDaemon = '[Unit]\nDescription=Homematic Virtual Layer\nAfter=network-online.target\n\n' - strDaemon = strDaemon + '[Service]\nType=idle\nUser=' - strDaemon = strDaemon + this.currentUser() + '\n' - strDaemon = strDaemon + 'ExecStart=' + execCmd + ' 1> /var/log/s_hvl.log 2>&1 &\n' - strDaemon = strDaemon + 'Restart=on-failure\nRestartSec=10\nKillMode=process\n\n[Install]\nWantedBy=multi-user.target' - - - fs.writeFileSync(tmpFile, strDaemon) - require('child_process').execSync('sudo mv ' + tmpFile + ' ' + this.systemdPath + this.systemdFile) - require('child_process').execSync('sudo systemctl daemon-reload') - require('child_process').execSync('sudo systemctl enable hvl') + if (process.platform === 'linux') { + const child_process = require('child_process') + this.configuration.setValue('daemon', true) + // Write systemd file + let tmpFile = path.join(os.tmpdir(), this.systemdFile) + if (fs.existsSync(tmpFile)) { + fs.unlinkSync(tmpFile) + }  + + let execCmd = path.join(appRoot, '/bin/hmvi') + var strDaemon = '[Unit]\nDescription=Homematic Virtual Layer\nAfter=network-online.target\n\n' + strDaemon = strDaemon + '[Service]\nType=idle\nUser=' + strDaemon = strDaemon + this.currentUser() + '\n' + strDaemon = strDaemon + 'ExecStart=' + execCmd + ' 1> /var/log/s_hvl.log 2>&1 &\n' + strDaemon = strDaemon + 'Restart=on-failure\nRestartSec=10\nKillMode=process\n\n[Install]\nWantedBy=multi-user.target' + + + fs.writeFileSync(tmpFile, strDaemon) + child_process.execSync('sudo mv ' + tmpFile + ' ' + this.systemdPath + this.systemdFile) + child_process.execSync('sudo systemctl daemon-reload') + child_process.execSync('sudo systemctl enable hvl') + return true + } + return false } Server.prototype.disableDaemon = function() { - this.configuration.setValue('daemon', false) - require('child_process').execSync('sudo systemctl disable hvl') + if (process.platform === 'linux') { + this.configuration.setValue('daemon', false) + require('child_process').execSync('sudo systemctl disable hvl') + return true + } + return false } Server.prototype.isPluginActive = function(type) {