Release WiFiTrip format v2.0.0
This commit is contained in:
+95
-36
@@ -59,6 +59,42 @@ def log(message):
|
||||
)
|
||||
|
||||
|
||||
def colorize(message, color, stream=None):
|
||||
stream = stream or sys.stdout
|
||||
|
||||
if "NO_COLOR" in os.environ or not stream.isatty():
|
||||
return message
|
||||
|
||||
colors = {
|
||||
"green": "\033[1;92m",
|
||||
"yellow": "\033[1;93m",
|
||||
"red": "\033[1;91m",
|
||||
"reset": "\033[0m"
|
||||
}
|
||||
|
||||
return f"{colors[color]}{message}{colors['reset']}"
|
||||
|
||||
|
||||
def print_success(message):
|
||||
print(colorize(message, "green"), flush=True)
|
||||
|
||||
|
||||
def print_warning(message):
|
||||
print(colorize(message, "yellow"), flush=True)
|
||||
|
||||
|
||||
def print_error(message):
|
||||
print(
|
||||
colorize(message, "red", sys.stderr),
|
||||
file=sys.stderr,
|
||||
flush=True
|
||||
)
|
||||
|
||||
|
||||
def print_info(message):
|
||||
print(message, flush=True)
|
||||
|
||||
|
||||
def project_python():
|
||||
return PROJECT_ROOT / "venv" / "bin" / "python"
|
||||
|
||||
@@ -663,9 +699,11 @@ def start_service():
|
||||
)
|
||||
|
||||
if active_project == PROJECT_ROOT:
|
||||
log("Менеджер уже запущен из текущего проекта.")
|
||||
print_warning(
|
||||
"Менеджер уже запущен из текущего проекта."
|
||||
)
|
||||
else:
|
||||
log(
|
||||
print_warning(
|
||||
"Менеджер уже запущен из другого проекта: "
|
||||
f"{active_project}. Второй экземпляр не запущен."
|
||||
)
|
||||
@@ -687,6 +725,7 @@ def start_service():
|
||||
|
||||
command = [
|
||||
"systemd-run",
|
||||
"--quiet",
|
||||
"--user",
|
||||
f"--unit={UNIT_NAME}",
|
||||
"--service-type=exec",
|
||||
@@ -700,7 +739,8 @@ def start_service():
|
||||
|
||||
subprocess.run(
|
||||
command,
|
||||
check=True
|
||||
check=True,
|
||||
stdout=subprocess.DEVNULL
|
||||
)
|
||||
|
||||
send_sudo_authentication()
|
||||
@@ -721,7 +761,7 @@ def start_service():
|
||||
timeout=1
|
||||
) as response:
|
||||
if response.status == 200:
|
||||
log(
|
||||
print_success(
|
||||
f"Приложение запущено: "
|
||||
f"http://{APP_HOST}:{APP_PORT}"
|
||||
)
|
||||
@@ -739,7 +779,7 @@ def start_service():
|
||||
|
||||
def stop_service():
|
||||
if not unit_is_active():
|
||||
log("Менеджер не запущен.")
|
||||
print_warning("Менеджер уже остановлен.")
|
||||
return 0
|
||||
|
||||
active_project = active_service_project()
|
||||
@@ -765,7 +805,7 @@ def stop_service():
|
||||
f"{UNIT_NAME}.service"
|
||||
)
|
||||
|
||||
log(
|
||||
print_warning(
|
||||
"Запрошено штатное завершение. "
|
||||
"Если сканер активен, останови его в веб-интерфейсе."
|
||||
)
|
||||
@@ -774,33 +814,32 @@ def stop_service():
|
||||
|
||||
while unit_is_active():
|
||||
if not notified:
|
||||
log(
|
||||
print_warning(
|
||||
"Ожидание завершения приложения и фоновых задач..."
|
||||
)
|
||||
notified = True
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
log("Сервис остановлен.")
|
||||
print_success("Сервис успешно остановлен.")
|
||||
return 0
|
||||
|
||||
|
||||
def show_status():
|
||||
active = unit_is_active()
|
||||
|
||||
print(
|
||||
"Application:",
|
||||
"RUNNING" if active else "STOPPED"
|
||||
)
|
||||
if active:
|
||||
print_success("Application: RUNNING")
|
||||
else:
|
||||
print_error("Application: STOPPED")
|
||||
|
||||
if active:
|
||||
active_project = active_service_project()
|
||||
print(
|
||||
"Active project:",
|
||||
active_project if active_project is not None else "UNKNOWN"
|
||||
print_info(
|
||||
"Active project: "
|
||||
f"{active_project if active_project is not None else 'UNKNOWN'}"
|
||||
)
|
||||
|
||||
if active:
|
||||
try:
|
||||
with urllib.request.urlopen(
|
||||
f"http://{APP_HOST}:{APP_PORT}/api/status",
|
||||
@@ -827,23 +866,38 @@ def show_status():
|
||||
f"STOPPED (health: {scanner_health})"
|
||||
)
|
||||
|
||||
print("Scanner:", scanner_state)
|
||||
print(
|
||||
"GPS test:",
|
||||
gps.get("state", "unknown")
|
||||
)
|
||||
print(
|
||||
"Network action:",
|
||||
"BUSY" if data.get("network_actions_busy") else "IDLE"
|
||||
)
|
||||
if scanner_health == "ACTIVE":
|
||||
print_success(f"Scanner: {scanner_state}")
|
||||
elif scanner_health in {"WARNING", "STOPPING"}:
|
||||
print_warning(f"Scanner: {scanner_state}")
|
||||
elif scanner_health == "STOPPED":
|
||||
print_error(f"Scanner: {scanner_state}")
|
||||
else:
|
||||
print_info(f"Scanner: {scanner_state}")
|
||||
|
||||
gps_state = gps.get("state", "unknown")
|
||||
|
||||
if gps_state in {"FIX", "CONNECTED"}:
|
||||
print_success(f"GPS test: {gps_state}")
|
||||
elif gps_state in {"STARTING", "NMEA"}:
|
||||
print_warning(f"GPS test: {gps_state}")
|
||||
elif gps_state == "ERROR":
|
||||
print_error(f"GPS test: {gps_state}")
|
||||
else:
|
||||
print_info(f"GPS test: {gps_state}")
|
||||
|
||||
if data.get("network_actions_busy"):
|
||||
print_warning("Network action: BUSY")
|
||||
else:
|
||||
print_success("Network action: IDLE")
|
||||
|
||||
except Exception as error:
|
||||
print(f"Application API: unavailable ({error})")
|
||||
print_error(f"Application API: unavailable ({error})")
|
||||
|
||||
print(
|
||||
"TileServer:",
|
||||
"AVAILABLE" if style_is_available() else "UNAVAILABLE"
|
||||
)
|
||||
if style_is_available():
|
||||
print_success("TileServer: AVAILABLE")
|
||||
else:
|
||||
print_error("TileServer: UNAVAILABLE")
|
||||
|
||||
if active:
|
||||
result = systemctl_user(
|
||||
@@ -855,7 +909,7 @@ def show_status():
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
print(result.stdout.strip())
|
||||
print_info(result.stdout.strip())
|
||||
|
||||
|
||||
def install():
|
||||
@@ -970,7 +1024,15 @@ def main():
|
||||
return stop_service()
|
||||
|
||||
if args.command == "restart":
|
||||
stop_service()
|
||||
if unit_is_active():
|
||||
print_info("Перезапуск: остановка сервиса.")
|
||||
stop_service()
|
||||
else:
|
||||
print_warning(
|
||||
"Сервис уже остановлен. Этап остановки пропущен."
|
||||
)
|
||||
|
||||
print_info("Перезапуск: запуск сервиса.")
|
||||
return start_service()
|
||||
|
||||
parser.print_help()
|
||||
@@ -981,10 +1043,7 @@ def main():
|
||||
subprocess.CalledProcessError,
|
||||
OSError
|
||||
) as error:
|
||||
print(
|
||||
f"Ошибка: {error}",
|
||||
file=sys.stderr
|
||||
)
|
||||
print_error(f"Ошибка: {error}")
|
||||
return 1
|
||||
|
||||
|
||||
|
||||
-2731
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user