'use strict';

const fs = require('fs');
const FIFO = require('fifo-js');
const spawn = require('child_process').spawn;

let Service, Characteristic, Accessory;

module.exports = (homebridge) => {
  Service = homebridge.hap.Service;
  Characteristic = homebridge.hap.Characteristic;
 // uuid = homebridge.hap.uuid;
 // StreamController = homebridge.hap.StreamController;
  Accessory = homebridge.platformAccessory;
 // hap = homebridge.hap;

  homebridge.registerAccessory('homebridge-motion-fifo', 'MotionFifo', MotionFifoAccessory, true);
};

// An accessory with a MotionSensor service
class MotionFifoAccessory
{
  constructor(log, config, api) {
    log(`MotionFifo accessory starting`);
    this.log = log;
    this.api = api;
    
    config = config || {};
    this.name = config.name || 'Motion Detector';
    this.pipePath = config.motion_pipe || '/tmp/motion-pipe';
    this.timeout = config.motion_timeout !== undefined ? config.motion_timeout : 5000;

    this.pipe = new FIFO(this.pipePath);
    this.pipe.setReader(this.onPipeRead.bind(this));

    this.motionService = new Service.MotionSensor(this.name);
    this.setMotion(false);
  }

  setMotion(detected) {
    this.motionService
      .getCharacteristic(Characteristic.MotionDetected)
      .setValue(detected);
  }

  onPipeRead(text) {
    console.log(`got from pipe: |${text}|`);
    // on_picture_save printf '%f\t%n\t%v\t%i\t%J\t%K\t%L\t%N\t%D\n' > /tmp/camera-pipe
    // http://htmlpreview.github.io/?https://github.com/Motion-Project/motion/blob/master/motion_guide.html#conversion_specifiers
    // %f filename with full path
    // %n number indicating filetype
    // %v event
    // %i width of motion area
    // %J height of motion area
    // %K X coordinates of motion center
    // %L Y coordinates of motion center
    // %N noise level
    // %D changed pixels
    const [filename, filetype, event, width, height, x, y, noise, dpixels] = text.trim().split('\t');
    console.log('filename is',filename);

    this.setMotion(true);

    setTimeout(() => this.setMotion(false), this.timeout); // TODO: is this how this works?
  }

  getServices() {
    return [this.motionService];
  }
}




@@ -8,21 +8,21 @@
 rict';%0A%0A
-const
+//var
  fs = re
@@ -34,21 +34,20 @@
 ('fs');%0A
-const
+var 
  FIFO = 
@@ -66,21 +66,21 @@
 o-js');%0A
-const
+//var
  spawn =
@@ -113,19 +113,19 @@
 spawn;%0A%0A
-let
+var
  Service
@@ -171,16 +171,24 @@
 ports = 
+function
 (homebri
@@ -191,19 +191,16 @@
 ebridge)
- =%3E
  %7B%0A  Ser
@@ -533,131 +533,102 @@
 sory
-, true
 );%0A%7D
-;
 %0A%0A
-// An accessory with a MotionSensor service%0Aclass MotionFifoAccessory%0A%7B%0A  constructor(log, config, api) %7B%0A 
+function MotionFifoAccessory(log,config) %7B%0A%0A    this.log = log;%0A//    this.api = api;%0A
   
- 
 log(
@@ -665,79 +665,59 @@
 %60);%0A
+//
     
-this.log = log;%0A    this.api = api;%0A    %0A    config = config %7C%7C %7B%7D;
+ config = config;%0A  //  if (!config) return;%0A
 %0A   
@@ -1097,19 +1097,51 @@
 false);%0A
-  %7D
+%7D%0AMotionFifoAccessory.prototype - %7B
 %0A%0A  setM
@@ -1145,16 +1145,26 @@
 etMotion
+: function
 (detecte
@@ -1165,17 +1165,16 @@
 etected)
- 
 %7B%0A    th
@@ -1272,24 +1272,25 @@
 tected);%0A  %7D
+,
 %0A%0A  onPipeRe
@@ -1291,16 +1291,26 @@
 PipeRead
+: function
 (text) %7B
@@ -1318,690 +1318,8 @@
     
-console.log(%60got from pipe: %7C$%7Btext%7D%7C%60);%0A    // on_picture_save printf '%25f%5Ct%25n%5Ct%25v%5Ct%25i%5Ct%25J%5Ct%25K%5Ct%25L%5Ct%25N%5Ct%25D%5Cn' %3E /tmp/camera-pipe%0A    // http://htmlpreview.github.io/?https://github.com/Motion-Project/motion/blob/master/motion_guide.html#conversion_specifiers%0A    // %25f filename with full path%0A    // %25n number indicating filetype%0A    // %25v event%0A    // %25i width of motion area%0A    // %25J height of motion area%0A    // %25K X coordinates of motion center%0A    // %25L Y coordinates of motion center%0A    // %25N noise level%0A    // %25D changed pixels%0A    const %5Bfilename, filetype, event, width, height, x, y, noise, dpixels%5D = text.trim().split('%5Ct');%0A    console.log('filename is',filename);%0A%0A
     
@@ -1439,12 +1439,27 @@
 ?%0A  
-%7D
+ %7D.bind(this),
 %0A%0A 
+//
  get
@@ -1470,14 +1470,26 @@
 ices
+: function
 () %7B%0A 
+//
    r
@@ -1521,12 +1521,12 @@
 %5D;%0A 
+//
  %7D%0A%7D
-%0A%0A%0A
+;
 %0A

Update index.js