/*
 * SPDX-FileCopyrightText: 2025 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */
#include <tango/client/DelayedEventSubThread.h>

#include <tango/client/DelayEvent.h>
#include <tango/server/WriterLock.h>

namespace Tango
{

void DelayedEventSubThread::run(TANGO_UNUSED(void *ptr))
{
    //
    // Subscribe to the event
    //

    DelayEvent de(ev_cons);
    WriterLock w(EventConsumer::get_map_modification_lock());

    std::vector<std::string> v_s;
    try
    {
        ev_cons->connect_event(device, attribute, et, callback, ev_queue, v_s, event_name, ev_id, event_sub_mode);
    }
    catch(Tango::DevFailed &e)
    {
        //
        // when the subscribe event has not worked, store the connection data in a vector of not
        // yet connected events.
        // Retry to connect in the next heartbeat period.
        //

        EventNotConnected conn_params;
        conn_params.device = device;
        conn_params.attribute = attribute;
        conn_params.event_type = et;
        conn_params.event_name = event_name;
        conn_params.callback = callback;
        conn_params.ev_queue = ev_queue;
        conn_params.filters = v_s;
        conn_params.last_heartbeat = Tango::get_current_system_datetime();
        conn_params.event_id = ev_id;
        conn_params.event_sub_mode = event_sub_mode;

        ev_cons->add_not_connected_event(e, conn_params);
    }
}

} // namespace Tango
