277 lines
9.4 KiB
Python
277 lines
9.4 KiB
Python
"""
|
|
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'
|