| 1234567891011121314151617181920212223242526272829 |
- import os
- import json
- class Storage:
- def __init__(self):
- self._file_path = os.path.join(os.path.dirname(__file__), "device_data.json")
- self._cache = None
- def get_device_data(self):
- if self._cache is not None:
- return self._cache
- if not os.path.exists(self._file_path):
- return {}
- try:
- with open(self._file_path, "r", encoding="utf-8") as f:
- self._cache = json.load(f)
- return self._cache
- except Exception:
- return {}
- def set_device_data(self, data):
- if not isinstance(data, dict):
- raise ValueError("data must be a dict")
- self._cache = data
- try:
- with open(self._file_path, "w", encoding="utf-8") as f:
- json.dump(data, f, ensure_ascii=False)
- except Exception:
- pass
|