fix endpoint init logic

This commit is contained in:
2025-03-19 09:27:12 +01:00
parent 3e09737532
commit c3a74c2a21
@@ -37,38 +37,43 @@ CURRENT_ENDPOINT_CONFIG = {}
def init_endpoint(endpoint: Endpoint, languages: list[str]):
"""Initialize a specific endpoint for multicast."""
current_config = CURRENT_ENDPOINT_CONFIG.get(endpoint.id)
if current_config is not None:
current_langs = [big.language for big in current_config.bigs]
# if languages are unchanged and the caster client status is initiailized, skip init
if current_langs == languages and multicast_client.get_status(base_url=endpoint.url)['is_initialized']:
log.info('Endpoint %s was already initialized', endpoint.name)
return
log.info(f"Initializing endpoint: {endpoint.name} at {endpoint.url}")
# Create a config for this endpoint
# Load a default config
config = auracast_config.AuracastConfigGroup(
bigs=[getattr(auracast_config, f"AuracastBigConfig{lang.capitalize()}")()
for lang in languages]
)
# overwrite some default configs
# Configure the transport
config.transport = 'auto'
config.auracast_device_address = ':'.join(f"{random.randint(0, 255):02X}" for _ in range(6))
# Configure the bigs
for big in config.bigs:
big.loop = False
big.name = endpoint.name
big.program_info = big.program_info + ' ' + endpoint.name
big.id = random.randint(0, 2**16) #TODO: how many bits is this ?
big.random_address = ':'.join(f"{random.randint(0, 255):02X}" for _ in range(6))
# TODO: init happens all the time
# Initialize the endpoint if config changed or if it's not already initialized
if not multicast_client.get_status(base_url=endpoint.url)['is_initialized'] or config != CURRENT_ENDPOINT_CONFIG.get(endpoint.id):
ret = multicast_client.init(config, base_url=endpoint.url)
# if ret != 200: # TODO: this is not working, should probably be handled async
# log.error('Init of endpoint %s was unsucessfull', endpoint.name)
# raise Exception(f"Init was of endpoint {endpoint.name} was unsucessfull")
CURRENT_ENDPOINT_CONFIG[endpoint.id] = config.model_copy()
else:
log.info('Endpoint %s was already initialized', endpoint.name)
big.loop = False
big.name = endpoint.name
big.random_address = ':'.join(f"{random.randint(0, 255):02X}" for _ in range(6))
big.id = random.randint(0, 2**16) #TODO: how many bits is this ?
#big.program_info = big.program_info + ' ' + endpoint.name
ret = multicast_client.init(config, base_url=endpoint.url)
# if ret != 200: # TODO: this is not working, should probably be handled async
# log.error('Init of endpoint %s was unsucessfull', endpoint.name)
# raise Exception(f"Init was of endpoint {endpoint.name} was unsucessfull")
CURRENT_ENDPOINT_CONFIG[endpoint.id] = config.model_copy()
log.info(f"Endpoint {endpoint.name} initialized successfully")
#else:
# log.info('Endpoint %s was already initialized', endpoint.name)
async def make_announcement(text: str, ep_group: EndpointGroup):
@@ -221,28 +226,12 @@ async def monitor_streaming_completion(ep_group: EndpointGroup):
log.error(f"Max wait time reached for group {ep_group.id}. Forcing completion.")
async def reset_endpoints_after_delay(endpoint_ids, delay_seconds):
"""Reset endpoints after a delay."""
await asyncio.sleep(delay_seconds)
for endpoint_id in endpoint_ids:
if endpoint_id in endpoint_status and endpoint_status[endpoint_id]["active"]:
try:
endpoint_status[endpoint_id]["broadcasts"] = 0
log.info(f"Reset broadcasts count for endpoint {endpoint_id}")
except Exception as e:
log.error(f"Failed to reset endpoint {endpoint_id}: {e}")
@app.get("/groups")
async def get_groups():
"""Get all endpoint groups with their current status."""
return endpoints_db.get_all_groups()
@app.post("/groups")
async def create_group(group: endpoints_db.EndpointGroup):
"""Add a new endpoint group."""