/****************************************************************************
 * 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 "./c_getlastdatapoint.h"
#include "./aqhome_data_p.h"
#include "aqhome/ipc/data/ipc_data.h"
#include "aqhome/ipc/data/msg_data_getdata.h"
#include "aqhome/ipc/data/msg_data_multidata.h"
#include "aqhome/ipc/endpoint_ipc.h"
#include "aqhome/ipc/msg_ipc_result.h"
#include "aqhome/ipc/msg_ipc_tag16.h"

#include <gwenhywfar/debug.h>
#include <gwenhywfar/text.h>



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



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



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


void AqHomeData_HandleGetLastDataPoint(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *ep, GWEN_MSG *recvdMsg)
{
  GWEN_MSG *outMsg;
  int resultCode=AQH_MSG_IPC_SUCCESS;

  if (AQH_IpcEndpoint_GetPermissions(ep) & AQH_IPCENDPOINT_PERMS_READDATA) {
    char *valueName;

    AQH_GetDataDataIpcMsg_Parse(recvdMsg, 0);
    valueName=AQH_Tag16IpcMsg_GetTagDataAsNewString(recvdMsg, AQH_MSGDATA_GETDATA_TAGS_NAME, NULL);
    if (valueName && *valueName) {
      const AQH_VALUE *storedValue;
 
      storedValue=AQH_Storage_GetValueByNameForSystem(aqh->storage, valueName);
      if (storedValue) {
        uint64_t timestamp=0;
        double data=0.0;
        int rv;
    
        rv=AQH_Storage_GetLastDataPoint(aqh->storage, AQH_Value_GetId(storedValue), &timestamp, &data);
        if (rv<0) {
          switch(rv) {
          case GWEN_ERROR_INVALID:   resultCode=AQH_MSG_IPC_ERROR_INVALID; break;
          case GWEN_ERROR_NO_DATA:   resultCode=AQH_MSG_IPC_ERROR_NODATA; break;
          default:                   resultCode=AQH_MSG_IPC_ERROR_GENERIC; break;
          }
        }
        else {
          outMsg=AQH_MultiDataDataIpcMsg_newForOne(AQH_MSGTYPE_IPC_DATA_GETLASTDATA_RSP, storedValue, timestamp, data);
          GWEN_MsgEndpoint_AddSendMessage(ep, outMsg);
          free(valueName);
          return;
        }
      }
      else {
        DBG_INFO(NULL, "Value \"%s\" not found", valueName);
        resultCode=AQH_MSG_IPC_ERROR_NOTFOUND;
      }
      free(valueName);
    }
    else {
      DBG_INFO(NULL, "No name for value");
      resultCode=AQH_MSG_IPC_ERROR_NOTFOUND;
    }
  }
  else {
    DBG_ERROR(AQH_LOGDOMAIN, "No permissions to read data");
    resultCode=AQH_MSG_IPC_ERROR_PERMS;
  }

  outMsg=AQH_ResultIpcMsg_new(AQH_MSGTYPE_IPC_DATA_RESULT, resultCode);
  GWEN_MsgEndpoint_AddSendMessage(ep, outMsg);
}



