/*
 * Copyright (c) 2023, Erich Styger
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#ifndef SRC_MQTT_CLIENT_H_
#define SRC_MQTT_CLIENT_H_

#ifdef __cplusplus
extern "C" {
#endif

/* which topics to use: choose only one: */
#define MQTT_CLIENT_IS_EV_CHARGER    (0)
#define MQTT_CLIENT_IS_SENSOR        (1)

#if PL_CONFIG_USE_SHELL
  #include "McuShell.h"

  /*!
   * \brief Command line and shell handler
   * \param cmd The command to be parsed
   * \param handled If command has been recognized and handled
   * \param io I/O handler to be used
   * \return error code, otherwise ERR_OK
   */
  uint8_t MqttClient_ParseCommand(const unsigned char* cmd, bool *handled, const McuShell_StdIOType *io);
#endif

#if MQTT_CLIENT_IS_SENSOR
  int MqttClient_Publish_SensorValues(float temperature, float humidity);
#endif

#if MQTT_CLIENT_IS_EV_CHARGER
  int MqttClient_Publish_ChargingPower(uint32_t powerW);
#endif

/*!
 * \brief Connect as client to the server
 * \return error code, otherwise ERR_OK
 */
uint8_t MqttClient_Connect(void);

/*!
 * \brief Disconnect from the server
 * \return error code, otherwise ERR_OK
 */
uint8_t MqttClient_Disconnect(void);

/*!
 * \brief Decides if client is set to publish or not
 * \return true if client is publishing, false otherwise
 */
bool MqttClient_GetDoPublish(void);

/*!
 * \brief Sets if the client is publishing or not
 * \param publish true to publish, false otherwise
 */
void MqttClient_SetDoPublish(bool publish);

/*!
 * \brief Module de-initialization
 */
void MqttClient_Deinit(void);

/*!
 * \brief Module initialization
 */
void MqttClient_Init(void);

typedef enum topic_ID_e {
  Topic_ID_None,
#if MQTT_CLIENT_IS_EV_CHARGER
  Topic_ID_Solar_Power,       /* power from PV panels */
  Topic_ID_Site_Power,        /* power to the house/site */
  Topic_ID_Grid_Power,        /* power from/to grid */
  Topic_ID_Battery_Power,     /* power from/to battery */
  Topic_ID_Battery_Percentage,/* battery level percentage */
  Topic_ID_Charging_Power,     /* actual charging power */
#elif MQTT_CLIENT_IS_SENSOR
  Topic_ID_Sensor_Update,
  Topic_ID_Send_Measurement
#endif
} topic_ID_e;

/*!
 * \brief Callback function for incoming messages
 */
typedef void (*MqttClient_MessageCallback_t)(topic_ID_e topic, const unsigned char *payload, size_t len);

/*!
 * \brief Register a callback function for incoming messages
 * \param cb Callback function to be registered
 */
void MqttClient_RegisterMessageCallback(MqttClient_MessageCallback_t cb);


#ifdef __cplusplus
}  /* extern "C" */
#endif

#endif /* SRC_MQTT_CLIENT_H_ */
