Skip to content

Commit

Permalink
some additional checks for #98
Browse files Browse the repository at this point in the history
  • Loading branch information
thkl committed Jan 21, 2020
1 parent f6875d0 commit 92820b2
Showing 1 changed file with 49 additions and 32 deletions.
81 changes: 49 additions & 32 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 92820b2

Please sign in to comment.