mirror of
https://github.com/OSURoboticsClub/Rover_2017_2018.git
synced 2025-11-08 18:21:15 +00:00
Added auto_poweroff monitor. Testing now.
This commit is contained in:
80
software/environment/rover/auto_poweroff/auto_poweroff.py
Executable file
80
software/environment/rover/auto_poweroff/auto_poweroff.py
Executable file
@@ -0,0 +1,80 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
#####################################
|
||||||
|
# Imports
|
||||||
|
#####################################
|
||||||
|
# Python native imports
|
||||||
|
from time import time, sleep
|
||||||
|
from os.path import exists
|
||||||
|
from os import system
|
||||||
|
|
||||||
|
#####################################
|
||||||
|
# Global Variables
|
||||||
|
#####################################
|
||||||
|
UDEV_RULES_PATHS = ["../UDEV_rules/99-rover-usb-serial.rules", "../UDEV_rules/99-rover-cameras.rules"]
|
||||||
|
|
||||||
|
SHUTDOWN_TIMEOUT = 5
|
||||||
|
|
||||||
|
|
||||||
|
#####################################
|
||||||
|
# udev_parser Definition
|
||||||
|
#####################################
|
||||||
|
def udev_parser(rules_paths):
|
||||||
|
device_paths = {}
|
||||||
|
|
||||||
|
for current_file in rules_paths:
|
||||||
|
lines = open(current_file).readlines()
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
if line[0] != "#" and line[0] != "\n":
|
||||||
|
current_path = line.split("SYMLINK+=")[1].strip("\"\n")
|
||||||
|
device_paths["/dev/" + current_path] = time()
|
||||||
|
|
||||||
|
return device_paths
|
||||||
|
|
||||||
|
|
||||||
|
#####################################
|
||||||
|
# AutoPoweroffWatchdog Class Definition
|
||||||
|
#####################################
|
||||||
|
class AutoPoweroffWatchdog(object):
|
||||||
|
def __init__(self, devices, shutdown_timeout, do_poweroff=True):
|
||||||
|
self.watched_devices = devices
|
||||||
|
self.shutdown_timeout = shutdown_timeout
|
||||||
|
self.do_poweroff = do_poweroff
|
||||||
|
|
||||||
|
# ########## Thread Flags ##########
|
||||||
|
self.run_thread_flag = True
|
||||||
|
|
||||||
|
self.run()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while self.run_thread_flag:
|
||||||
|
self.check_and_update_devices()
|
||||||
|
self.initiate_shutdown_if_needed()
|
||||||
|
sleep(0.25)
|
||||||
|
|
||||||
|
def check_and_update_devices(self):
|
||||||
|
for device in self.watched_devices:
|
||||||
|
if exists(device):
|
||||||
|
self.watched_devices[device] = time()
|
||||||
|
|
||||||
|
def initiate_shutdown_if_needed(self):
|
||||||
|
for device in self.watched_devices:
|
||||||
|
if (time() - self.watched_devices[device]) < self.shutdown_timeout:
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.do_poweroff:
|
||||||
|
system("sudo wall -n No devices seen for %s seconds. Powering down. Poweroff script exiting." %
|
||||||
|
self.shutdown_timeout)
|
||||||
|
system("sudo poweroff")
|
||||||
|
exit()
|
||||||
|
else:
|
||||||
|
system("sudo wall -n No devices seen for %s seconds, but not powering down. Poweroff script exiting.")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
#####################################
|
||||||
|
# Main
|
||||||
|
#####################################
|
||||||
|
if __name__ == "__main__":
|
||||||
|
watched_devices = udev_parser(UDEV_RULES_PATHS)
|
||||||
|
AutoPoweroffWatchdog(watched_devices, SHUTDOWN_TIMEOUT, True)
|
||||||
Reference in New Issue
Block a user