Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const NodeHelper = require("node_helper");
- const exec = require("child_process").exec;
- var cmd=require('node-cmd'); // install node-cmd (npm install node-cmd) first and add excute command osx style then change exec to cmd.get <-- return the result to analize
- module.exports = NodeHelper.create({
- start: function () {
- this.started = false;
- this.isMonitorOn(function(result) {
- console.log("MMM-MotionDetector: monitor on " + result);
- });
- },
- activateMonitor: function () {
- this.isMonitorOn(function(result) {
- if (!result) {
- cmd.get('caffeinate -u -t 1', function(err, out, code) {
- if (err) {
- console.error("MMM-MotionDetector: error activating monitor: " + code);
- } else {
- console.log("MMM-MotionDetector: monitor has been activated");
- }
- });
- }
- });
- this.started = false;
- },
- deactivateMonitor: function () {
- this.isMonitorOn(function(result) {
- if (result) {
- cmd.get('pmset displaysleepnow', function(err, out, code) {
- if (err) {
- console.error("MMM-MotionDetector: error deactivating monitor: " + code);
- } else {
- console.log("MMM-MotionDetector: monitor has been deactivated");
- }
- });
- }
- });
- this.started = false;
- },
- isMonitorOn: function(resultCallback) {
- cmd.get('pmset -g powerstate IODisplayWrangler | tail -1 | cut -c29', function(err, out, code) {
- console.log("MMM-MotionDetector: monitor " + out);
- if (err) {
- console.error("MMM-MotionDetector: error calling monitor status: " + code);
- return;
- }
- console.log("MMM-MotionDetector: monitor " + out);
- resultCallback(out.includes("4")); // <4 is off
- });
- },
- // Subclass socketNotificationReceived received.
- socketNotificationReceived: function (notification, payload) {
- if (notification === "MOTION_DETECTED" && this.started === false) {
- console.log("MMM-MotionDetector: MOTION_DETECTED, score " + payload.score);
- this.started = true;
- this.activateMonitor();
- }
- if (notification === "DEACTIVATE_MONITOR" && this.started === false) {
- console.log("MMM-MotionDetector: DEACTIVATE_MONITOR");
- this.started = true;
- this.deactivateMonitor();
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement