Advertisement
Guest User

MagicMirror2 | MMM-PIR-Sensor | vcgencmd v3

a guest
Apr 5th, 2018
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. /* Magic Mirror
  4.  * Module: MMM-PIR-Sensor
  5.  *
  6.  * By Paul-Vincent Roll http://paulvincentroll.com
  7.  * MIT Licensed.
  8.  */
  9.  
  10. const NodeHelper = require('node_helper');
  11. const Gpio = require('onoff').Gpio;
  12. const exec = require('child_process').exec;
  13.  
  14. module.exports = NodeHelper.create({
  15.   start: function () {
  16.     this.started = false;
  17.   },
  18.  
  19.   activateMonitor: function () {
  20.     if (this.config.relayPIN != false) {
  21.       this.relay.writeSync(this.config.relayOnState);
  22.     }
  23.     else if (this.config.relayPIN == false){
  24.       // Check if hdmi output is already on
  25.       exec("vcgencmd display_power").stdout.on('data', function(data) {
  26.         if (data.indexOf("display_power=0") == 0)
  27.           exec("vcgencmd display_power 1", null);
  28.       });
  29.     }
  30.   },
  31.  
  32.   deactivateMonitor: function () {
  33.     if (this.config.relayPIN != false) {
  34.       this.relay.writeSync(this.config.relayOffState);
  35.     }
  36.     else if (this.config.relayPIN == false){
  37.       exec("vcgencmd display_power 0", null);
  38.     }
  39.   },
  40.  
  41.   // Subclass socketNotificationReceived received.
  42.   socketNotificationReceived: function(notification, payload) {
  43.     if (notification === 'CONFIG' && this.started == false) {
  44.       const self = this;
  45.       this.config = payload;
  46.       self.deactivateMonitorTimeout;
  47.  
  48.       // Setup value which represent on and off
  49.       const valueOn = this.config.invertSensorValue ? 0 : 1;
  50.       const valueOff = this.config.invertSensorValue ? 1 : 0;
  51.  
  52.       //Setup pins
  53.       this.pir = new Gpio(this.config.sensorPIN, 'in', 'both');
  54.       // exec("echo '" + this.config.sensorPIN.toString() + "' > /sys/class/gpio/export", null);
  55.       // exec("echo 'in' > /sys/class/gpio/gpio" + this.config.sensorPIN.toString() + "/direction", null);
  56.  
  57.       if (this.config.relayPIN) {
  58.         this.relay = new Gpio(this.config.relayPIN, 'out');
  59.         this.relay.writeSync(this.config.relayOnState);
  60.         exec("vcgencmd display_power 1", null);
  61.       }
  62.  
  63.       //Detected movement
  64.       this.pir.watch(function(err, value) {
  65.         if (value == valueOn) {
  66.           self.sendSocketNotification("USER_PRESENCE", true);
  67.           if (self.config.powerSaving){
  68.             clearTimeout(self.deactivateMonitorTimeout);
  69.             self.activateMonitor();
  70.             self.deactivateMonitorTimeout = setTimeout(function() {
  71.               self.sendSocketNotification("USER_PRESENCE", false);
  72.               self.deactivateMonitor();
  73.             }, self.config.powerSavingDelay * 1000);
  74.           } else if (!self.config.powerSaving){
  75.               self.activateMonitor();
  76.           }
  77.         }
  78.         else if (value == valueOff) {
  79.           if (!self.config.powerSaving){
  80.               self.sendSocketNotification("USER_PRESENCE", false);
  81.               self.deactivateMonitor();
  82.           }
  83.         }
  84.       });
  85.  
  86.       exec("vcgencmd display_power 0", null);
  87.       this.started = true;
  88.  
  89.     } else if (notification === 'SCREEN_WAKEUP') {
  90.       this.activateMonitor();
  91.     }
  92.   }
  93.  
  94. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement