-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_config.py
More file actions
42 lines (34 loc) · 1.31 KB
/
Copy pathdevice_config.py
File metadata and controls
42 lines (34 loc) · 1.31 KB
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
import json
from typing import List, Dict, Optional
class DeviceConfig:
def __init__(self, config_path: str = "devices.json"):
self.config_path = config_path
self.devices = self._load_devices()
def _load_devices(self) -> List[Dict]:
with open(self.config_path, "r") as f:
return json.load(f)
def reload(self) -> List[Dict]:
self.devices = self._load_devices()
return self.devices
def _save_devices(self) -> None:
with open(self.config_path, "w") as f:
json.dump(self.devices, f, indent=2)
def get_device_by_mac(self, mac: str) -> Optional[Dict]:
for device in self.devices:
if device["mac"].lower() == mac.lower():
return device
return None
def get_device_by_name(self, name: str) -> Optional[Dict]:
for device in self.devices:
if device["name"].lower() == name.lower():
return device
return None
def list_devices(self) -> List[Dict]:
return self.devices
def update_position(self, mac: str, position: int) -> bool:
for device in self.devices:
if device["mac"].lower() == mac.lower():
device["position"] = position
self._save_devices()
return True
return False