opencode ergebnis V0.1
This commit is contained in:
1 parent
61d86a3d83
commit
7e498ebc3f
71 files changed
+28982
-46
No files matched your search
@@ -0,0 +1,3 @@
|
||||
from .groups_service import GroupsService
|
||||
from .streets_service import StreetsService
|
||||
from .accounts_service import AccountsService
|
||||
@@ -0,0 +1,29 @@
|
||||
"""
|
||||
Accounts Service - Geschäftslogik für Accounts
|
||||
"""
|
||||
from cmbnet.models.accounts import AccountModel
|
||||
from cmbnet.auth import verify_password, hash_password
|
||||
|
||||
|
||||
class AccountsService:
|
||||
@staticmethod
|
||||
def verify_account_password(account, password):
|
||||
"""Überprüft Passwort gegen Account"""
|
||||
if not account:
|
||||
return False
|
||||
return verify_password(account['password_hash'], password)
|
||||
|
||||
@staticmethod
|
||||
def has_role(account, roles):
|
||||
"""Prüft, ob Account eine der angegebenen Rollen hat"""
|
||||
if isinstance(roles, str):
|
||||
roles = [roles]
|
||||
if not account:
|
||||
return False
|
||||
return account['role'] in roles
|
||||
|
||||
@staticmethod
|
||||
def create_account(tenant_id, name, password, role):
|
||||
"""Erstellt einen neuen Account"""
|
||||
password_hash = hash_password(password)
|
||||
return AccountModel.create(tenant_id, name, password_hash, role)
|
||||
@@ -0,0 +1,95 @@
|
||||
"""
|
||||
Groups Service - Geschäftslogik für Gruppen mit In-Memory-Caching
|
||||
"""
|
||||
from cmbnet.models.groups import GroupModel
|
||||
from cmbnet.models.streets import StreetModel
|
||||
from cmbnet.models.accounts import AccountModel
|
||||
from cmbnet.db import get_db
|
||||
|
||||
|
||||
class GroupsService:
|
||||
# In-Memory Cache: groups_cache[tenant_id][group_id] = group_dict
|
||||
_cache = {}
|
||||
|
||||
@classmethod
|
||||
def load_from_db(cls, tenant_id):
|
||||
"""Lädt alle Gruppen eines Mandanten aus der Datenbank in den Cache"""
|
||||
groups = GroupModel.get_all_by_tenant(tenant_id)
|
||||
if tenant_id not in cls._cache:
|
||||
cls._cache[tenant_id] = {}
|
||||
|
||||
for group in groups:
|
||||
cls._cache[tenant_id][group['id']] = {
|
||||
'id': group['id'],
|
||||
'tenant_id': group['tenant_id'],
|
||||
'aliasname': group['aliasname'],
|
||||
'state': group['state'],
|
||||
'last_lat': group['last_lat'],
|
||||
'last_lon': group['last_lon'],
|
||||
'last_pos_at': group['last_pos_at'],
|
||||
'account_id': group['account_id'],
|
||||
'created_at': group['created_at'],
|
||||
'updated_at': group['updated_at']
|
||||
}
|
||||
return cls._cache[tenant_id]
|
||||
|
||||
@classmethod
|
||||
def get_all(cls, tenant_id, force_reload=False):
|
||||
"""Gibt alle Gruppen für einen Mandanten zurück (aus Cache)"""
|
||||
if force_reload or tenant_id not in cls._cache or not cls._cache[tenant_id]:
|
||||
cls.load_from_db(tenant_id)
|
||||
return cls._cache.get(tenant_id, {})
|
||||
|
||||
@classmethod
|
||||
def get_by_id(cls, tenant_id, group_id):
|
||||
"""Gibt eine spezifische Gruppe aus dem Cache zurück"""
|
||||
if tenant_id not in cls._cache:
|
||||
cls.load_from_db(tenant_id)
|
||||
return cls._cache.get(tenant_id, {}).get(group_id)
|
||||
|
||||
@classmethod
|
||||
def invalidate(cls, tenant_id):
|
||||
"""Invalidiert den Cache für einen Mandanten (erzwingt Neuladen)"""
|
||||
if tenant_id in cls._cache:
|
||||
cls._cache[tenant_id] = {}
|
||||
|
||||
@classmethod
|
||||
def invalidate_all(cls):
|
||||
"""Invalidiert den gesamten Cache"""
|
||||
cls._cache = {}
|
||||
|
||||
@classmethod
|
||||
def is_account_linked(cls, account_id, group_id, tenant_id):
|
||||
"""Prüft, ob ein Account mit einer Gruppe verknüpft ist"""
|
||||
group = cls.get_by_id(tenant_id, group_id)
|
||||
if not group:
|
||||
return False
|
||||
return group.get('account_id') == account_id
|
||||
|
||||
@classmethod
|
||||
def get_accounts_for_group(cls, group_id, tenant_id):
|
||||
"""Gibt alle mit einer Gruppe verknüpften Accounts zurück"""
|
||||
group = cls.get_by_id(tenant_id, group_id)
|
||||
if not group or not group.get('account_id'):
|
||||
return []
|
||||
account = AccountModel.get_by_id(group['account_id'], tenant_id)
|
||||
return [account] if account else []
|
||||
|
||||
@classmethod
|
||||
def get_streets_for_group(cls, group_id, tenant_id):
|
||||
"""Gibt alle mit einer Gruppe verknüpften Straßen zurück"""
|
||||
return StreetModel.get_by_group_id(group_id, tenant_id)
|
||||
|
||||
@classmethod
|
||||
def update_cache_entry(cls, tenant_id, group_id, data):
|
||||
"""Aktualisiert einen Eintrag im Cache.
|
||||
|
||||
Der Cache wird vor dem Aktualisieren vollständig geladen, damit ein
|
||||
gezieltes Update (z. B. nach Code 200/300) keinen Teil-Cache erzeugt,
|
||||
der anschließend beim Initialload unvollständige Gruppendaten liefert.
|
||||
"""
|
||||
if tenant_id not in cls._cache or not cls._cache[tenant_id]:
|
||||
cls.load_from_db(tenant_id)
|
||||
if group_id not in cls._cache[tenant_id]:
|
||||
cls._cache[tenant_id][group_id] = {}
|
||||
cls._cache[tenant_id][group_id].update(data)
|
||||
@@ -0,0 +1,276 @@
|
||||
"""
|
||||
History Service für Revert von States
|
||||
"""
|
||||
import json
|
||||
from datetime import datetime
|
||||
from cmbnet.db import get_db
|
||||
from cmbnet.models.streets import StreetModel
|
||||
from cmbnet.models.groups import GroupModel
|
||||
from cmbnet.services.groups_service import GroupsService
|
||||
from cmbnet.utils.helpers import log_transaction
|
||||
from cmbnet.ws import broker
|
||||
from cmbnet.ws.protocol import WSMessage
|
||||
|
||||
|
||||
class HistoryService:
|
||||
@staticmethod
|
||||
def revert_street(tenant_id, street_id, hist_id, actor_account_id):
|
||||
db = get_db()
|
||||
history = db.execute(
|
||||
'SELECT * FROM street_state_history WHERE id = ? AND tenant_id = ? AND street_id = ?',
|
||||
(hist_id, tenant_id, street_id)
|
||||
).fetchone()
|
||||
if not history:
|
||||
return False, 'Historieneintrag nicht gefunden'
|
||||
|
||||
street = StreetModel.get_by_id(street_id, tenant_id)
|
||||
if not street:
|
||||
return False, 'Straße nicht gefunden'
|
||||
|
||||
old_state = street['state']
|
||||
target_state = history['to_state']
|
||||
target_group_id = history['group_id']
|
||||
|
||||
if target_state == 'unbearbeitet':
|
||||
target_group_id = None
|
||||
|
||||
# Update Straße
|
||||
StreetModel.update(street_id, tenant_id, state=target_state, group_id=target_group_id)
|
||||
|
||||
# Neuen History-Eintrag für den Revert
|
||||
db.execute(
|
||||
'''INSERT INTO street_state_history
|
||||
(tenant_id, street_id, from_state, to_state, group_id, actor_account_id, reason)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)''',
|
||||
(tenant_id, street_id, old_state, target_state, target_group_id, actor_account_id, f'revert_from_hist_{hist_id}')
|
||||
)
|
||||
db.commit()
|
||||
|
||||
log_transaction('street.revert', 'street', street_id, {
|
||||
'from': old_state,
|
||||
'to': target_state,
|
||||
'group_id': target_group_id,
|
||||
'hist_id': hist_id
|
||||
})
|
||||
|
||||
# Code 100 verschicken
|
||||
msg = WSMessage(
|
||||
sender_id=actor_account_id,
|
||||
code=100,
|
||||
msg=json.dumps({
|
||||
'name': street['name'],
|
||||
'state': target_state,
|
||||
'actor': actor_account_id,
|
||||
'street_id': street_id,
|
||||
'group_id': target_group_id
|
||||
})
|
||||
)
|
||||
try:
|
||||
broker.route_message(tenant_id, msg)
|
||||
except Exception as e:
|
||||
print(f'Broker error: {e}')
|
||||
|
||||
return True, 'Zustand wiederhergestellt'
|
||||
|
||||
@staticmethod
|
||||
def revert_group(tenant_id, group_id, hist_id, actor_account_id):
|
||||
db = get_db()
|
||||
history = db.execute(
|
||||
'SELECT * FROM group_state_history WHERE id = ? AND tenant_id = ? AND group_id = ?',
|
||||
(hist_id, tenant_id, group_id)
|
||||
).fetchone()
|
||||
if not history:
|
||||
return False, 'Historieneintrag nicht gefunden'
|
||||
|
||||
group = GroupModel.get_by_id(group_id, tenant_id)
|
||||
if not group:
|
||||
return False, 'Gruppe nicht gefunden'
|
||||
|
||||
old_state = group['state']
|
||||
target_state = history['to_state']
|
||||
target_lat = history['last_lat']
|
||||
target_lon = history['last_lon']
|
||||
target_pos_at = history['last_pos_at']
|
||||
|
||||
GroupModel.update(group_id, tenant_id,
|
||||
state=target_state,
|
||||
last_lat=target_lat,
|
||||
last_lon=target_lon,
|
||||
last_pos_at=target_pos_at)
|
||||
|
||||
# Neuen History-Eintrag
|
||||
db.execute(
|
||||
'''INSERT INTO group_state_history
|
||||
(tenant_id, group_id, from_state, to_state, last_lat, last_lon, last_pos_at, actor_account_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)''',
|
||||
(tenant_id, group_id, old_state, target_state, target_lat, target_lon, target_pos_at, actor_account_id)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
# Cache aktualisieren
|
||||
GroupsService.update_cache_entry(tenant_id, group_id, {
|
||||
'state': target_state,
|
||||
'last_lat': target_lat,
|
||||
'last_lon': target_lon,
|
||||
'last_pos_at': target_pos_at
|
||||
})
|
||||
|
||||
log_transaction('group.revert', 'group', group_id, {
|
||||
'from': old_state,
|
||||
'to': target_state,
|
||||
'hist_id': hist_id
|
||||
})
|
||||
|
||||
# Code 200
|
||||
msg200 = WSMessage(
|
||||
sender_id=actor_account_id,
|
||||
code=200,
|
||||
msg=json.dumps({
|
||||
'group_id': group_id,
|
||||
'state': target_state,
|
||||
'actor': actor_account_id
|
||||
})
|
||||
)
|
||||
try:
|
||||
broker.route_message(tenant_id, msg200)
|
||||
except:
|
||||
pass
|
||||
|
||||
# Code 300 wenn Position vorhanden
|
||||
if target_lat is not None and target_lon is not None:
|
||||
msg300 = WSMessage(
|
||||
sender_id=actor_account_id,
|
||||
code=300,
|
||||
msg=json.dumps({
|
||||
'group_id': group_id,
|
||||
'lat': target_lat,
|
||||
'lon': target_lon,
|
||||
'ts': target_pos_at,
|
||||
'actor': actor_account_id
|
||||
})
|
||||
)
|
||||
try:
|
||||
broker.route_message(tenant_id, msg300)
|
||||
except:
|
||||
pass
|
||||
|
||||
return True, 'Gruppe wiederhergestellt'
|
||||
|
||||
@staticmethod
|
||||
def _send_group_position(tenant_id, group_id, lat, lon, pos_at, actor_account_id):
|
||||
"""Meldet eine Positionsänderung an die berechtigten Clients (Code 300)."""
|
||||
msg = WSMessage(
|
||||
sender_id=actor_account_id,
|
||||
code=300,
|
||||
msg=json.dumps({
|
||||
'group_id': group_id,
|
||||
'lat': lat,
|
||||
'lon': lon,
|
||||
'ts': pos_at,
|
||||
'actor': actor_account_id
|
||||
})
|
||||
)
|
||||
try:
|
||||
broker.route_message(tenant_id, msg)
|
||||
except Exception as e:
|
||||
print(f'Broker error: {e}')
|
||||
|
||||
@staticmethod
|
||||
def set_group_position_from_history(tenant_id, group_id, hist_id, actor_account_id):
|
||||
"""Übernimmt ausschließlich die Position eines Historieneintrags.
|
||||
|
||||
Der State der Gruppe bleibt unverändert; die Übernahme wird selbst als
|
||||
neuer Historieneintrag festgehalten.
|
||||
"""
|
||||
db = get_db()
|
||||
history = db.execute(
|
||||
'SELECT * FROM group_state_history WHERE id = ? AND tenant_id = ? AND group_id = ?',
|
||||
(hist_id, tenant_id, group_id)
|
||||
).fetchone()
|
||||
if not history:
|
||||
return False, 'Historieneintrag nicht gefunden'
|
||||
|
||||
target_lat = history['last_lat']
|
||||
target_lon = history['last_lon']
|
||||
if target_lat is None or target_lon is None:
|
||||
return False, 'Historieneintrag enthält keine Position'
|
||||
|
||||
group = GroupModel.get_by_id(group_id, tenant_id)
|
||||
if not group:
|
||||
return False, 'Gruppe nicht gefunden'
|
||||
|
||||
target_pos_at = history['last_pos_at']
|
||||
GroupModel.update(
|
||||
group_id, tenant_id,
|
||||
last_lat=target_lat,
|
||||
last_lon=target_lon,
|
||||
last_pos_at=target_pos_at
|
||||
)
|
||||
|
||||
db.execute(
|
||||
'''INSERT INTO group_state_history
|
||||
(tenant_id, group_id, from_state, to_state, last_lat, last_lon, last_pos_at, actor_account_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)''',
|
||||
(tenant_id, group_id, group['state'], group['state'],
|
||||
target_lat, target_lon, target_pos_at, actor_account_id)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
GroupsService.update_cache_entry(tenant_id, group_id, {
|
||||
'last_lat': target_lat,
|
||||
'last_lon': target_lon,
|
||||
'last_pos_at': target_pos_at
|
||||
})
|
||||
|
||||
log_transaction('group.position_from_history', 'group', group_id, {
|
||||
'hist_id': hist_id,
|
||||
'lat': target_lat,
|
||||
'lon': target_lon
|
||||
})
|
||||
HistoryService._send_group_position(
|
||||
tenant_id, group_id, target_lat, target_lon, target_pos_at, actor_account_id
|
||||
)
|
||||
|
||||
return True, 'Position übernommen'
|
||||
|
||||
@staticmethod
|
||||
def clear_group_position(tenant_id, group_id, actor_account_id):
|
||||
"""Löscht die aktuelle Position einer Gruppe."""
|
||||
group = GroupModel.get_by_id(group_id, tenant_id)
|
||||
if not group:
|
||||
return False, 'Gruppe nicht gefunden'
|
||||
|
||||
if group['last_lat'] is None and group['last_lon'] is None:
|
||||
return True, 'Position ist bereits unbekannt'
|
||||
|
||||
GroupModel.update(
|
||||
group_id, tenant_id,
|
||||
last_lat=None,
|
||||
last_lon=None,
|
||||
last_pos_at=None
|
||||
)
|
||||
|
||||
db = get_db()
|
||||
db.execute(
|
||||
'''INSERT INTO group_state_history
|
||||
(tenant_id, group_id, from_state, to_state, last_lat, last_lon, last_pos_at, actor_account_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)''',
|
||||
(tenant_id, group_id, group['state'], group['state'],
|
||||
None, None, None, actor_account_id)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
GroupsService.update_cache_entry(tenant_id, group_id, {
|
||||
'last_lat': None,
|
||||
'last_lon': None,
|
||||
'last_pos_at': None
|
||||
})
|
||||
|
||||
log_transaction('group.position_clear', 'group', group_id, {})
|
||||
|
||||
# Null-Koordinaten signalisieren den Clients, die Markierung zu entfernen.
|
||||
HistoryService._send_group_position(
|
||||
tenant_id, group_id, None, None, None, actor_account_id
|
||||
)
|
||||
|
||||
return True, 'Position gelöscht'
|
||||
@@ -0,0 +1,53 @@
|
||||
"""
|
||||
Streets Service - Geschäftslogik für Straßen
|
||||
"""
|
||||
from cmbnet.models.streets import StreetModel
|
||||
from cmbnet.db import get_db
|
||||
from cmbnet.utils.helpers import log_transaction
|
||||
import json
|
||||
|
||||
|
||||
class StreetsService:
|
||||
@staticmethod
|
||||
def get_all_for_init(tenant_id):
|
||||
"""Gibt alle Straßen für den AJAX-Initialload zurück"""
|
||||
streets = StreetModel.get_all_by_tenant(tenant_id)
|
||||
result = []
|
||||
for street in streets:
|
||||
result.append({
|
||||
'id': street['id'],
|
||||
'name': street['name'],
|
||||
'state': street['state'],
|
||||
'group_id': street['group_id'],
|
||||
'hn_min': street['hn_min'],
|
||||
'hn_max': street['hn_max'],
|
||||
'bbox': json.loads(street['bbox']) if street['bbox'] else None
|
||||
})
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def update_street(street_id, tenant_id, changes, actor_account_id=None):
|
||||
"""Aktualisiert eine Straße und schreibt History/Log"""
|
||||
db = get_db()
|
||||
old_street = StreetModel.get_by_id(street_id, tenant_id)
|
||||
if not old_street:
|
||||
return False
|
||||
|
||||
StreetModel.update(street_id, tenant_id, **changes)
|
||||
|
||||
# History bei State-Wechsel
|
||||
if 'state' in changes and changes['state'] != old_street['state']:
|
||||
db.execute(
|
||||
'''INSERT INTO street_state_history
|
||||
(tenant_id, street_id, from_state, to_state, group_id, actor_account_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?)''',
|
||||
(tenant_id, street_id, old_street['state'], changes['state'],
|
||||
changes.get('group_id', old_street['group_id']), actor_account_id)
|
||||
)
|
||||
db.commit()
|
||||
log_transaction('street.state_change', 'street', street_id,
|
||||
{'from': old_street['state'], 'to': changes['state']})
|
||||
else:
|
||||
log_transaction('street.update', 'street', street_id, changes)
|
||||
|
||||
return True
|
||||
Reference in new issue
Block a user