/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2023 Martin Preuss, all rights reserved.
 *
 * The license for this file can be found in the file COPYING which you
 * should have received along with this file.
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif


#include "./loop.h"
#include "./loop_tty.h"
#include "./loop_ipc.h"
#include "./aqhomed_p.h"
#include "./tty_log.h"
#include "./tty_write.h"
#include "./db.h"

#include "aqhome/msg/endpoint_tty.h"
#include "aqhome/msg/msg_node.h"
#include "aqhome/msg/msg_value2.h"
#include "aqhome/ipc/endpoint_ipc.h"
#include "aqhome/ipc/nodes/msg_ipc_forward.h"
#include "aqhome/ipc/nodes/msg_ipc_value.h"
#include "aqhome/mqtt/endpoint_mqttc.h"

#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/endpoint_tcpd.h>



/* ------------------------------------------------------------------------------------------------
 * defines
 * ------------------------------------------------------------------------------------------------
 */

#define I18N(msg) msg
#define I18S(msg) msg



/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */




/* ------------------------------------------------------------------------------------------------
 * implementations
 * ------------------------------------------------------------------------------------------------
 */

void AqHomed_Loop(AQHOMED *aqh, int timeoutInMsecs)
{
  if (aqh) {
    GWEN_MsgEndpoint_ChildrenIoLoop(aqh->rootEndpoint, timeoutInMsecs);
    AqHomed_ReadAndHandleTtyMessages(aqh);
    AqHomed_ReadAndHandleIpcMessages(aqh);

    if (AQH_NodeDb_IsModified(aqh->nodeDb)) {
      if (aqh->dbFile) {
        GWEN_DB_NODE *dbNodeDb;

        dbNodeDb=GWEN_DB_Group_new("nodeDb");
        AQH_NodeDb_toDb(aqh->nodeDb, dbNodeDb);
        GWEN_DB_WriteFile(dbNodeDb, aqh->dbFile, GWEN_DB_FLAGS_DEFAULT);
        GWEN_DB_Group_free(dbNodeDb);
      }
    }
  }
}



