Advertisement
estoyarto

Nodejs MMM Motiondetector

Mar 16th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const NodeHelper = require("node_helper");
  2.  
  3. const exec = require("child_process").exec;
  4.  
  5. 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
  6.  
  7. module.exports = NodeHelper.create({
  8.     start: function () {
  9.         this.started = false;
  10.         this.isMonitorOn(function(result) {
  11.             console.log("MMM-MotionDetector: monitor on " + result);
  12.         });
  13.     },
  14.  
  15.     activateMonitor: function () {
  16.         this.isMonitorOn(function(result) {
  17.             if (!result) {
  18.                 cmd.get('caffeinate -u -t 1', function(err, out, code) {
  19.                     if (err) {
  20.                         console.error("MMM-MotionDetector: error activating monitor: " + code);
  21.                     } else {
  22.                         console.log("MMM-MotionDetector: monitor has been activated");
  23.                     }
  24.                 });
  25.             }
  26.         });
  27.         this.started = false;
  28.     },
  29.  
  30.     deactivateMonitor: function () {
  31.         this.isMonitorOn(function(result) {
  32.             if (result) {
  33.                 cmd.get('pmset displaysleepnow', function(err, out, code) {
  34.  
  35.                    
  36.                     if (err) {
  37.                         console.error("MMM-MotionDetector: error deactivating monitor: " + code);
  38.                     } else {
  39.                         console.log("MMM-MotionDetector: monitor has been deactivated");
  40.                     }
  41.                 });
  42.             }
  43.         });
  44.         this.started = false;
  45.     },
  46.  
  47.     isMonitorOn: function(resultCallback) {
  48.  
  49.  
  50.  
  51.         cmd.get('pmset -g powerstate IODisplayWrangler | tail -1 | cut -c29', function(err, out, code) {
  52.             console.log("MMM-MotionDetector: monitor " + out);
  53.  
  54.             if (err) {
  55.                 console.error("MMM-MotionDetector: error calling monitor status: " + code);
  56.                 return;
  57.             }
  58.  
  59.             console.log("MMM-MotionDetector: monitor " + out);
  60.         resultCallback(out.includes("4")); // <4 is off
  61.         });
  62.     },
  63.  
  64.     // Subclass socketNotificationReceived received.
  65.     socketNotificationReceived: function (notification, payload) {
  66.         if (notification === "MOTION_DETECTED" && this.started === false) {
  67.             console.log("MMM-MotionDetector: MOTION_DETECTED, score " + payload.score);
  68.             this.started = true;
  69.             this.activateMonitor();
  70.         }
  71.         if (notification === "DEACTIVATE_MONITOR" && this.started === false) {
  72.             console.log("MMM-MotionDetector: DEACTIVATE_MONITOR");
  73.             this.started = true;
  74.             this.deactivateMonitor();
  75.         }
  76.     }
  77. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement