RetroFlag / retroflag-picase

RetroFlag Pi-Case Safe Shutdown
MIT License
714 stars 233 forks source link

Some issues - Better idea #12

Open DFXThomas opened 6 years ago

DFXThomas commented 6 years ago

First of all, thanks for trying to fix the "emulationstation metadata" issue. Really appreciated. However, I think your latest script does have some flaws. Let's look at this code:

    os.system("sudo killall emulationstation && sleep 5s && sudo reboot")

This causes two problems:

Well, here's my idea of how to fix these issues:

    for proc in psutil.process_iter():
        if proc.name() == procName:
            proc.terminate()
            proc.wait()

This method will work regardless if you're currently running emulationstation or not. It's also a lot faster. You'll need to install "python3-psutil" before it will work, though.

Have fun everyone! :-)

P.S. Here's my complete script. Maybe it's useful for some.

#!/usr/bin/env python3
from gpiozero import Button, LED
from signal import pause
import os, psutil

procName = "emulationstation"

resetPin = 2
powerPin = 3
powerenPin = 4
ledPin = 14

power = LED(powerenPin)
power.on()

led = LED(ledPin)
led.on()

def terminate():
    for proc in psutil.process_iter():
        if proc.name() == procName:
            proc.terminate()
            proc.wait()

def shutdown():
    terminate()
    os.system("shutdown -h now")

def reboot():
    terminate()
    os.system("reboot")

powerBtn = Button(powerPin)
powerBtn.when_pressed = shutdown

resetBtn = Button(resetPin)
resetBtn.when_pressed = reboot

pause()
praefectum commented 6 years ago

Tried both scripts, I recommend the one from DFXThomas. Thanks for posting! Just one note: in order to replicate features of original script, a led.blink(.2,.2) is missing.

DFXThomas commented 6 years ago

I deliberately left out led.blink, because it regularly caused problems for me. The LED would sometimes turn off several seconds before shutdown was actually completed. I'd keep the light on until it is 100% safe to turn the unit off.

praefectum commented 6 years ago

I see, thanks for the feedback, I thought it just had been overlooked. I like the blinking, I'll try tonight to add it and see if it works for me.