From 5afff59785e9b534bf4880d8075638f8c14a2b9e Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Fri, 6 Nov 2020 16:59:43 +0000 Subject: [PATCH] Added internet, RAM, and storage checkers --- openflexure_microscope/rescue/auto.py | 3 +- openflexure_microscope/rescue/check_system.py | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 openflexure_microscope/rescue/check_system.py diff --git a/openflexure_microscope/rescue/auto.py b/openflexure_microscope/rescue/auto.py index 8fcce894..842c9c17 100644 --- a/openflexure_microscope/rescue/auto.py +++ b/openflexure_microscope/rescue/auto.py @@ -9,7 +9,7 @@ from openflexure_microscope.paths import ( PREFERRED_OPENFLEXURE_VAR_PATH, ) -from . import check_capture_reload, check_settings, check_picamera, check_sangaboard +from . import check_capture_reload, check_settings, check_picamera, check_sangaboard, check_system # Paths for suggestions LOGS_PATHS = [ @@ -43,6 +43,7 @@ if __name__ == "__main__": error_sources = [] + error_sources.extend(check_system.main()) error_sources.extend(check_settings.main()) error_sources.extend(check_picamera.main()) error_sources.extend(check_capture_reload.main()) diff --git a/openflexure_microscope/rescue/check_system.py b/openflexure_microscope/rescue/check_system.py new file mode 100644 index 00000000..b5736195 --- /dev/null +++ b/openflexure_microscope/rescue/check_system.py @@ -0,0 +1,53 @@ +from urllib.request import urlopen +from urllib.error import URLError +import psutil + +from .error_sources import WarningSource + +def internet_on(): + test_urls = ( + 'https://build.openflexure.org', # Bath server + 'https://openflexure.org' # GitHub pages + ) + for url in test_urls: + try: + urlopen(url, timeout=10) + # If any test passes, return True + return True + except URLError: + pass + # If no test passed, return False + return False + + +def main(): + error_sources = [] + + # Check internet + if not internet_on(): + error_sources.append(WarningSource( + "No internet connection detected. Updates may not be available." + )) + + # Check memory + mem = psutil.virtual_memory() + total_gb = mem.total / 1E9 + + if total_gb <= 1: + error_sources.append(WarningSource( + ("Less than 1GB total memory available.", + "For small scans, or control from another device, this is usually fine.", + "\n More complex usage may require additional resources.") + )) + + # Check disks + data_partitions = [disk for disk in psutil.disk_partitions() if "rw" in disk.opts and disk.mountpoint != "/boot"] + for part in data_partitions: + usage = psutil.disk_usage(part.mountpoint) + if int(usage.percent) >= 90: + error_sources.append(WarningSource( + (f"Disk {part.device}, at {part.mountpoint} is {int(usage.percent)}% full.", + "Captures may fail to save soon.") + )) + + return error_sources