Files
aqhomecontrol/apps/aqhome-cgi/modules/mdataclient.c
2025-09-15 19:15:51 +02:00

106 lines
3.0 KiB
C

/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2025 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 "./mdataclient.h"
#include "aqhome-cgi/service/module.h"
#include "aqhome/msg/ipc/m_ipc.h"
#include "aqhome/msg/ipc/data/m_ipcd.h"
#include <gwenhywfar/debug.h>
#include <gwenhywfar/timestamp.h>
/* ------------------------------------------------------------------------------------------------
* defs and enums
* ------------------------------------------------------------------------------------------------
*/
/* ------------------------------------------------------------------------------------------------
* global vars
* ------------------------------------------------------------------------------------------------
*/
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
/* ------------------------------------------------------------------------------------------------
* code
* ------------------------------------------------------------------------------------------------
*/
void AQH_ModDataClient_Extend(AQH_MODULE *m, AQH_SERVICE *sv, const char *baseFolder)
{
AQH_ModService_Extend(m, sv, baseFolder);
}
int AQH_ModDataClient_HandleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_MODDATACLIENT_RUN_FN runFn)
{
AQH_EVENT_LOOP *eventLoop;
AQH_DATACLIENT *dc;
GWEN_BUFFER *dbuf;
int rv;
rv=AQH_Init();
if (rv<0) {
DBG_ERROR(NULL, "here (%d)", rv);
AQCGI_SendResponseWithStatus(rq, 500, "Internal Error");
return rv;
}
eventLoop=AQH_EventLoop_new();
dc=AQH_DataClient_new(eventLoop, AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION);
rv=AQH_DataClient_ReadConfigFile(dc);
if (rv<0) {
DBG_ERROR(NULL, "here (%d)", rv);
AQCGI_SendResponseWithStatus(rq, 500, "Internal Error");
AQH_Session_free(session);
return rv;
}
rv=AQH_DataClient_ConnectWithArgs(dc, 0);
if (rv<0) {
DBG_ERROR(NULL, "Error connecting (%d)", rv);
AQH_DataClient_free(dc);
AQH_EventLoop_free(eventLoop);
AQCGI_SendResponseWithStatus(rq, 500, "Internal Error");
AQH_Session_free(session);
return GWEN_ERROR_GENERIC;
}
dbuf=GWEN_Buffer_new(0, 256, 0, 1);
AQH_ModService_AddHeader(m, "en", dbuf);
if (runFn)
runFn(m, rq, session, dc, dbuf);
AQH_ModService_AddFooter(m, "en", dbuf);
AQCGI_Request_SetBufferResponseBody(rq, dbuf);
AQCGI_Request_AddResponseHeaderData(rq, "Content-type: text/html");
AQH_DataClient_free(dc);
AQH_EventLoop_free(eventLoop);
AQH_Session_free(session);
AQH_Fini();
return AQCGI_SendResponse(rq);
}