Add method for checking OS version.

This commit is contained in:
Julian Stirling 2026-04-30 14:14:24 +01:00
parent 62d1724db6
commit 5b14f8d820
4 changed files with 64 additions and 0 deletions

View file

@ -5,6 +5,7 @@ the microscope, server, and thing states to the web API.
"""
import os
import re
import socket
import subprocess
import time
@ -22,6 +23,8 @@ from openflexure_microscope_server.utilities import VersionData, robust_version_
SHUTDOWN_CMD = ["sudo", "shutdown", "-h", "now"]
REBOOT_CMD = ["sudo", "shutdown", "-r", "now"]
OS_VERSION_FILE = "/usr/lib/os-release"
class CommandOutput(BaseModel):
"""A pydantic model passing the STDOUT and STDERR from a subprocess over HTTP."""
@ -81,6 +84,24 @@ class OpenFlexureSystem(lt.Thing):
"""Return True if running on a Raspberry Pi."""
return os.path.exists("/usr/bin/raspi-config")
@lt.property
def os_version(self) -> Optional[str]:
"""Return the OS version of the Pi. Returns None if not on a Pi."""
if not self.is_raspberrypi:
return None
if not os.path.isfile(OS_VERSION_FILE):
return "unknown"
with open(OS_VERSION_FILE, "r", encoding="utf-8") as os_file:
os_data = os_file.read()
version_match = re.search(r"^VERSION_CODENAME=(.+)$", os_data, re.MULTILINE)
if not version_match:
return "unknown"
return version_match.group(1)
@lt.action
def shutdown(self) -> CommandOutput:
"""Attempt to shutdown the device."""