(function () {
    const {spawn, exec} = require("child_process");
    const {NodeCec, CEC} = require( "node-cec" );

    function OMXController() {
        this.stopRequest = true;
        this.setupCEC();
    }

    OMXController.prototype.play = function (url, options) {
        var thiz = this;
        this.args = {
            url: url,
            options: options
        };

        return new Promise(function (resolve, reject) {
            thiz.stop().then(function () {
                var cmd = "omxplayer -o hdmi --blank ";
                if ((options && options.live) || url.indexOf("tv.asf")) cmd += "--live ";
                cmd += url;
                cmd += " > ~/omxplayer.log 2>&1";

                thiz.stopRequested = false;
                thiz.process = exec(cmd, function (error, stdout, stderr) {
                });

                resolve();

                thiz.process.on("exit", function () {
                    console.log("OMXPlayer exit, stopRequested = " + thiz.stopRequested);
                    if (!thiz.stopRequested) {
                        console.log("  > Restart playback automatically...");
                        thiz.play(thiz.args.url, thiz.args.opions);
                    }
                })
            });
        });
    };
    OMXController.prototype.stop = function () {
        var thiz = this;
        this.stopRequested = true;
        if (this.process) this.process.removeAllListeners();
        return new Promise(function (resolve, reject) {
            exec("killall omxplayer.bin", function (error, stdout, stderr) {
                resolve();
            });
        });
    };
    OMXController.prototype.showNotification = function (title, message) {
        return new Promise(function (resolve, reject) {
            resolve();
            // exec("notify-send", function (error, stdout, stderr) {
            //     resolve();
            // });
        });
    };

    OMXController.prototype.setupCEC = function () {
        this.cec = new NodeCec("node-cec-monitor");
        process.on("SIGINT", function() {
            if (this.cec != null) {
                this.cec.stop();
            }
            process.exit(0);
        });

        this.cec.on("PLAY", function () {
            console.log("CEC: PLAY");
        });
        this.cec.on("PAUSE", function () {
            console.log("CEC: PAUSE");
        });
        this.cec.on("STOP", function () {
            console.log("CEC: STOP");
        });

        this.cec.start("cec-client");
        console.log("CEC started.");
    };


    module.exports = OMXController;
})();


//http://127.0.0.1:12001/jsonrpc?request={"jsonrpc":"2.0","id":"1","method":"Player.Open","params":{"item":{"file":"http://clips.vorwaerts-gmbh.de/VfE_html5.mp4"}}}

@@ -2235,20 +2235,39 @@
 cec.on(%22
-PLAY
+VENDOR_REMOTE_BUTTON_UP
 %22, funct
@@ -2263,36 +2263,238 @@
 _UP%22, function (
+packet
 ) %7B%0A
+            if (!packet %7C%7C !packet.args %7C%7C !packet.args%5B0%5D) return;%0A            console.log(packet);%0A            var code = packet.args%5B0%5D;%0A            if (code == CEC.UserControlCode.PLAY) %7B%0A    
             cons
@@ -2527,55 +2527,166 @@
     
+ 
-%7D);%0A        this.cec.on(%22PAUSE%22, function () %7B%0A
+   %7D else if (code == CEC.UserControlCode.PAUSE) %7B%0A                console.log(%22CEC: PAUSE%22);%0A            %7D else if (code == CEC.UserControlCode.STOP) %7B%0A    
     
@@ -2703,37 +2703,36 @@
 nsole.log(%22CEC: 
-PAUSE
+STOP
 %22);%0A        %7D);%0A
@@ -2731,54 +2731,182 @@
     
+  
-%7D);%0A        this.cec.on(%22STOP%22, function () %7B%0A
+  %7D else if (code == CEC.UserControlCode.FAST_FORWARD) %7B%0A                console.log(%22CEC: FAST_FORWARD%22);%0A            %7D else if (code == CEC.UserControlCode.REWIND) %7B%0A    
     
@@ -2923,39 +2923,55 @@
 nsole.log(%22CEC: 
-STOP%22);
+REWIND%22);%0A            %7D
 %0A        %7D);%0A%0A  

Fix CEC event