/*
 * Sai power definitions src/power/b-private.h
 *
 * Copyright (C) 2019 - 2025 Andy Green <andy@warmcat.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation:
 *  version 2.1 of the License.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301  USA
 */

#include "../common/include/private.h"

#include <sys/stat.h>
#if defined(WIN32)
#include <direct.h>
#define read _read
#define open _open
#define close _close
#define write _write
#define mkdir(x,y) _mkdir(x)
#define rmdir _rmdir
#define unlink _unlink
#define HAVE_STRUCT_TIMESPEC
#if defined(pid_t)
#undef pid_t
#endif
#endif
#include <pthread.h>

#define SAI_POWERDOWN_HOLDOFF_US	(50 * LWS_US_PER_SEC)

typedef struct tasmota_data {
	unsigned int		voltage_v;
	unsigned int		current_ma;
	unsigned int		active_power_w;
	unsigned int		apparent_power_va;
	unsigned int		reactive_power_var;
	unsigned int		power_factor_scaled_1000;
	unsigned int		energy_today_wh;
	unsigned int		energy_yesterday_wh;
	unsigned int		energy_total_wh;
} tasmota_data_t;

typedef struct tasmota_parse {
	tasmota_data_t		td;
	struct lws_tokenize	ts;
	uint16_t		match;
	uint8_t			s;
} tasmota_parse_t;

typedef struct saip_pcon {
	struct lws_dll2		list; /* sai_power.sai_pcon_owner */

	lws_dll2_owner_t	controlled_plats_owner; /* saip_server_plat_t */

	const char		*name;
	const char		*type;
	const char		*url;

	char			url_on[128];
	char			url_off[128];
	char			url_monitor[128];

	struct lws_ss_handle	*ss_tasmota_on;
	struct lws_ss_handle	*ss_tasmota_off;
	struct lws_ss_handle	*ss_tasmota_monitor;

	char			on;
} saip_pcon_t;

struct saip_ws_pss;

typedef struct saip_server_plat {
	lws_dll2_t		list;
	lws_dll2_t		dependencies_list;
	lws_dll2_t		pcon_list; /* saip_pcon->controlled_plats_owner */

	lws_dll2_owner_t	dependencies_owner; /* saip_server_plat_t->dependencies_list */

	lws_sorted_usec_list_t	sul_delay_off;

	const char		*name;
	const char		*host;
	const char		*depends; /* depended-on plat must stay powered if we need power */
	const char		*power_on_type;
	const char		*power_on_url;
	const char		*power_on_mac;
	const char		*power_off_type;
	const char		*power_off_url;

	char			stay;
	char			needed;

} saip_server_plat_t;

typedef struct saip_server {
	lws_dll2_t		list;

	lws_dll2_owner_t	sai_plat_owner; /* list of platforms we offer */

	struct lws_ss_handle	*ss;

	const char		*url;
	const char		*name;
} saip_server_t;

/*
 * This represents this power process as a whole
 */

struct sai_power {
	lws_dll2_owner_t	sai_server_owner; /* servers we connect to */

	lws_dll2_owner_t	sai_pcon_owner; /* saip_pcon_t */

	struct lwsac		*ac_conf_head;
	struct lws_context	*context;
	struct lws_vhost	*vhost;

	lws_sorted_usec_list_t	sul_idle;

	const char		*power_off;

	const char		*wol_if;

	const char		*bind;		/* listen socket binding */
	const char		*perms;		/* user:group */

	const char		*port;		/* port we listen on */
};

saip_server_plat_t *
find_platform(struct sai_power *pwr, const char *host);


struct jpargs {
	struct sai_power	*power;

	saip_server_t		*sai_server;
	saip_server_plat_t	*sai_server_plat;
	saip_pcon_t		*sai_pcon;

	sai_plat_server_ref_t	*mref;

	int			next_server_index;
	int			next_plat_index;
};

LWS_SS_USER_TYPEDEF
        char                    payload[200];
        size_t                  size;
        size_t                  pos;

	struct lws_buflist	*bl_pwr_to_srv;
} saip_server_link_t;



extern struct sai_power power;
extern const lws_ss_info_t ssi_saip_server_link_t, ssi_saip_smartplug_t;
extern const struct lws_protocols protocol_com_warmcat_sai;
extern struct lws_spawn_piped *lsp_wol;
int
saip_config_global(struct sai_power *power, const char *d);
extern int saip_config(struct sai_power *power, const char *d);
extern void saip_config_destroy(struct sai_power *power);
extern void
saip_notify_server_power_state(const char *plat_name, int up, int down);

void
saip_set_stay(const char *builder_name, int stay_on);
int
saip_queue_stay_info(saip_server_t *sps);
saip_pcon_t *
saip_pcon_by_name(struct sai_power *power, const char *name);

int
saip_parse_tasmota_status(tasmota_parse_t *tp);
int
saip_builder_bringup(saip_server_t *sps, saip_server_plat_t *sp,
		     saip_server_link_t *pss);
void
saip_ss_create_tasmota(void);