-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui_terminal.py
116 lines (89 loc) · 3.55 KB
/
ui_terminal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import json
import os
from typing import Dict, Any
def save_devices(devices: Dict[str, Any]) -> None:
file_path = os.path.join(os.path.dirname(__file__), "devices.json")
with open(file_path, "w") as file:
json.dump(devices, file, indent=4)
def load_devices() -> Dict[str, Any]:
try:
with open(os.path.join(os.path.dirname(__file__), "devices.json"), "r") as file:
return json.load(file)
except FileNotFoundError:
print("File not found.")
return {"devices": []}
except json.JSONDecodeError:
print("Error reading the file.")
return {"devices": []}
def toggle_device(index: int, devices_data: Dict[str, Any]) -> None:
device = devices_data["devices"][index]
if "status" not in device:
device["status"] = {"power": "off"}
if device["status"]["power"] == "on":
device["status"]["power"] = "off"
else:
device["status"]["power"] = "on"
save_devices(devices_data)
def print_devices(devices_data: Dict[str, Any]) -> None:
if not devices_data["devices"]:
print("No devices in the system.")
return
for index, device in enumerate(devices_data["devices"]):
power_status = device['status'].get('power', 'unknown')
print(f"{index + 1}. {device['name']} ({device['location']}) - Power: {power_status}")
def add_device(devices_data: Dict[str, Any]) -> None:
name = input("Enter device name: ")
location = input("Enter device location: ")
new_device = {"name": name, "location": location, "status": {"power": "off"}}
devices_data["devices"].append(new_device)
save_devices(devices_data)
print(f"Device '{name}' added successfully.")
def remove_device(devices_data: Dict[str, Any]) -> None:
print_devices(devices_data)
try:
device_number = int(input("Enter the device number to remove: ")) - 1
if 0 <= device_number < len(devices_data["devices"]):
removed_device = devices_data["devices"].pop(device_number)
save_devices(devices_data)
print(f"Device '{removed_device['name']}' removed successfully.")
else:
print("Invalid device number.")
except ValueError:
print("Enter a valid number.")
def menu() -> None:
devices_data = load_devices()
while True:
print("\nChoose an option:")
print("1. Show devices")
print("2. Toggle device")
print("3. Add device")
print("4. Remove device")
print("5. Save and exit")
option = input("Choose an option number: ")
if option == "1":
print_devices(devices_data)
elif option == "2":
print_devices(devices_data)
try:
device_number = int(input("Enter the device number to toggle: ")) - 1
if 0 <= device_number < len(devices_data["devices"]):
toggle_device(device_number, devices_data)
print("Device state has been changed.")
else:
print("Invalid device number.")
except ValueError:
print("Enter a valid number.")
elif option == "3":
add_device(devices_data)
elif option == "4":
remove_device(devices_data)
elif option == "5":
save_devices(devices_data)
print("Data has been saved.")
break
else:
print("Invalid option, please try again.")
if __name__ == "__main__":
print("Starting menu...")
menu()
print("Current working directory:", os.getcwd())