Add model base class and models
- Add Model base class with get_next_id() method - Add Preset model with CRUD operations - Add Profile model with tabs and palette support - Add Group model for device grouping - Add Sequence model for preset sequencing - Add Tab model for organizing device controls - Add Palette model for color collections
This commit is contained in:
42
src/models/model.py
Normal file
42
src/models/model.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import json
|
||||
import wifi
|
||||
import ubinascii
|
||||
import machine
|
||||
|
||||
class Model(dict):
|
||||
|
||||
def __init__(self):
|
||||
self.file = self.__class__.__name__ + ".json"
|
||||
super().__init__()
|
||||
|
||||
self.load() # Load settings from file during initialization
|
||||
|
||||
def set_defaults(self):
|
||||
self = {}
|
||||
|
||||
def get_next_id(self):
|
||||
"""Get the next available ID for creating a new record."""
|
||||
if not self:
|
||||
return "1"
|
||||
max_id = max((int(k) for k in self.keys() if k.isdigit()), default=0)
|
||||
return str(max_id + 1)
|
||||
|
||||
def save(self):
|
||||
try:
|
||||
j = json.dumps(self)
|
||||
with open(self.file, 'w') as file:
|
||||
file.write(j)
|
||||
print("Settings saved successfully.")
|
||||
except Exception as e:
|
||||
print(f"Error saving settings: {e}")
|
||||
|
||||
def load(self):
|
||||
try:
|
||||
with open(self.file, 'r') as file:
|
||||
loaded_settings = json.load(file)
|
||||
self.update(loaded_settings)
|
||||
print("Settings loaded successfully.")
|
||||
except Exception as e:
|
||||
print(f"Error loading settings")
|
||||
self.set_defaults()
|
||||
self.save()
|
||||
Reference in New Issue
Block a user