50 lines
1.1 KiB
Python
50 lines
1.1 KiB
Python
from labthings.server.view import View, ActionView
|
|
import subprocess
|
|
import os
|
|
from sys import platform
|
|
|
|
|
|
def is_raspberrypi(raise_on_errors=False):
|
|
"""
|
|
Checks if Raspberry Pi.
|
|
"""
|
|
# I mean, if it works, it works...
|
|
return os.path.exists("/usr/bin/raspi-config")
|
|
|
|
|
|
class ShutdownAPI(ActionView):
|
|
"""
|
|
Attempt to shutdown the device
|
|
"""
|
|
|
|
def post(self):
|
|
"""
|
|
Attempt to shutdown the device
|
|
"""
|
|
p = subprocess.Popen(
|
|
["sudo", "shutdown", "-h", "now"],
|
|
stderr=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,
|
|
)
|
|
|
|
out, err = p.communicate()
|
|
return {"out": out, "err": err}
|
|
|
|
|
|
class RebootAPI(ActionView):
|
|
"""
|
|
Attempt to reboot the device
|
|
"""
|
|
|
|
def post(self):
|
|
"""
|
|
Attempt to reboot the device
|
|
"""
|
|
p = subprocess.Popen(
|
|
["sudo", "shutdown", "-r", "now"],
|
|
stderr=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,
|
|
)
|
|
|
|
out, err = p.communicate()
|
|
return {"out": out, "err": err}
|