Skip to content

Commit

Permalink
Merge pull request #4 from Tom-Hirschberger/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Tom-Hirschberger authored Aug 5, 2023
2 parents eaa1557 + bdf665c commit 8b55ea8
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 14 deletions.
43 changes: 40 additions & 3 deletions MMM-Screen-Powersave-Notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ Module.register("MMM-Screen-Powersave-Notification", {
defaults: {
delay: 60,
profiles: {},
screenOnCommand: "/usr/bin/vcgencmd display_power 1",
screenOffCommand: "/usr/bin/vcgencmd display_power 0",
screenStatusCommand: "/usr/bin/vcgencmd display_power",
screenOnCommand: "/usr/bin/vcgencmd",
screenOffCommand: "/usr/bin/vcgencmd",
screenStatusCommand: "/usr/bin/vcgencmd",
screenOnArgs: ["display_power", "1"],
screenOffArgs: ["display_power", "0"],
screenStatusArgs: ["display_power"],
turnScreenOnIfProfileDelayIsSet: true,
countDownText: "Display powersave: ",
disabledText: "disabled",
Expand Down Expand Up @@ -98,6 +101,40 @@ Module.register("MMM-Screen-Powersave-Notification", {

start: function () {
Log.info("Starting module: " + this.name);

if ( this.config.screenOnCommand.indexOf(" ") != -1 ) {
this.config.screenOnCommand = this.config.screenOnCommand.split(" ")
if (this.config.screenOnCommand.length > 1) {
this.config.screenOnArgs = this.config.screenOnCommand.slice(1)
} else {
this.config.screenOnArgs = []
}

this.config.screenOnCommand = this.config.screenOnCommand[0]
}

if ( this.config.screenOffCommand.indexOf(" ") != -1 ) {
this.config.screenOffCommand = this.config.screenOffCommand.split(" ")
if (this.config.screenOffCommand.length > 1) {
this.config.screenOffArgs = this.config.screenOffCommand.slice(1)
} else {
this.config.screenOffArgs = []
}

this.config.screenOffCommand = this.config.screenOffCommand[0]
}

if ( this.config.screenStatusCommand.indexOf(" ") != -1 ) {
this.config.screenStatusCommand = this.config.screenStatusCommand.split(" ")
if (this.config.screenStatusCommand.length > 1) {
this.config.screenStatusArgs = this.config.screenStatusCommand.slice(1)
} else {
this.config.screenStatusArgs = []
}

this.config.screenStatusCommand = this.config.screenStatusCommand[0]
}

this.sendSocketNotification("CONFIG", this.config);
this.currentDelay = this.config.delay;
this.delayDisabled = false;
Expand Down
67 changes: 57 additions & 10 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* MIT Licensed.
*/
const NodeHelper = require('node_helper')
const exec = require('child_process').exec
const execSync = require('child_process').execSync
const spawn = require('child_process').spawn
const spawnSync = require('child_process').spawnSync
const fs = require('fs')
const path = require('path')
const callbackDir = path.join(__dirname, '/callbackScripts')
Expand Down Expand Up @@ -36,7 +36,22 @@ module.exports = NodeHelper.create({
} else {
const self = this
if (self.config.screenStatusCommand !== '') {
var result = execSync(this.config.screenStatusCommand)
let spawnOutput = spawnSync(this.config.screenStatusCommand, this.config.screenStatusArgs)
result = spawnOutput.stdout
if (result != null){
result = result.toString().trim()
} else {
result = ""
}

if (spawnOutput.stderr != null){
let error = error.toString().trim()
if (error != ""){
console.log(self.name + ': Error during screen status check: ')
console.log(spawnOutput.stderr.toString())
}
}

if (result.indexOf('display_power=0') === 0) {
return false
} else {
Expand Down Expand Up @@ -67,7 +82,14 @@ module.exports = NodeHelper.create({
self.modulesHidden = true
} else {
if (self.config.screenOffCommand !== '') {
execSync(self.config.screenOffCommand)
let spawnOutput = spawnSync(this.config.screenOffCommand, this.config.screenOffArgs)
if (spawnOutput.stderr != null){
let error = spawnOutput.stderr.toString().trim()
if (error != ""){
console.log(self.name + ': Error during screen off command: ')
console.log(spawnOutput.stderr.toString())
}
}
}
}
self.runScriptsInDirectory(callbackDir + '/off')
Expand All @@ -90,7 +112,14 @@ module.exports = NodeHelper.create({
self.modulesHidden = false
} else {
if (self.config.screenOnCommand !== '') {
execSync(self.config.screenOnCommand)
let spawnOutput = spawnSync(this.config.screenOnCommand, this.config.screenOnArgs)
if (spawnOutput.stderr != null){
let error = spawnOutput.stderr.toString().trim()
if (error != ""){
console.log(self.name + ': Error during screen on command: ')
console.log(spawnOutput.stderr.toString())
}
}
}
}
self.forcedDown = false
Expand All @@ -104,7 +133,14 @@ module.exports = NodeHelper.create({
self.modulesHidden = false
} else {
if (self.config.screenOnCommand !== '') {
execSync(self.config.screenOnCommand)
let spawnOutput = spawnSync(this.config.screenOnCommand, this.config.screenOnArgs)
if (spawnOutput.stderr != null){
let error = spawnOutput.stderr.toString().trim()
if (error != ""){
console.log(self.name + ': Error during screen on command: ')
console.log(spawnOutput.stderr.toString())
}
}
}
}
self.runScriptsInDirectory(callbackDir + '/on')
Expand Down Expand Up @@ -138,11 +174,22 @@ module.exports = NodeHelper.create({
} else {
for (var i = 0; i < items.length; i++) {
console.log(self.name + ': ' + items[i])
exec(directory + '/' + items[i], function (error, stdout, stderr) {
if (error) {
console.log(stderr)
let child = spawn(directory + '/' + items[i])

let scriptErrorOutput = ""

child.stderr.on('data', (data) => {
scriptErrorOutput+=data.toString()
});

child.on('close', function(code) {
scriptErrorOutput = scriptErrorOutput.trim()

if (scriptErrorOutput != "") {
console.log(self.name + ': Error during script call: ')
console.log(scriptErrorOutput)
}
})
});
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "MMM-Screen-Powersave-Notification",
"version": "0.0.7",
"version": "0.0.8",
"description": "",
"main": "MMM-Screen-Powersave-Notification.js",
"dependencies": {
Expand Down

0 comments on commit 8b55ea8

Please sign in to comment.