/******************************************************************
** Copyright (c) 2009  Wuhan Mozit Technology Co., Ltd .
** FileName:	decode.c
** Author:	 	BigHead
** Mail:		jsrenyw@sohu.com
** Date:		2007-5-31 下午07:17:32
** Editor:		YellowBug
** Date:		2010-5-26 13:34
** Version: 	2010-5-26
** File Description: 后台框架结构
******************************************************************/
#include "../include/decode.h"
#include "../core/core.h"

/******************************************************************
 *	Function Name:	RepeatRun
 *	Arguments:
 *	Return Value:	void
 *  Editor:			YellowBug
 *	Date: 			2010-5-26
 *	Description:	负责监测用户状态，发送公告等
 ******************************************************************/
void RepeatRun(void)
{
	// 该线程关闭, 目前不在启动
	log("This is in RepeatRun! pid=%d  tid=%ld  lwpid=%lu\n", getpid(), pthread_self(), syscall(SYS_gettid));
	while (1)
	{
		sleep(10);
	}
}

/******************************************************************
 *	Function Name:	AlarmGateway
 *	Arguments:
 *	Return Value:	void
 *	Date: 			2023-12-5
 *	Editor:			cc
 *	Description:	扫描数据库,处理网关报警
 ******************************************************************/
void AlarmGateway(void)
{
	unsigned int nowTime;
	//static MYSQL *_db = NULL;
	int ret = 0;
	unsigned int lastHeartTime = 0;

	log("This is in AlarmGateway decode! pid=%d  tid=%ld  lwpid=%lu\n", getpid(), pthread_self(), syscall(SYS_gettid));

	while (1)
	{
		usleep(GAME_HEART_BEAT_g);
		nowTime = timeGloble_g;

		//每隔30s扫描数据库设备状态表
		if (nowTime-lastHeartTime >30 )
		{
			CoreAlarmGateway();
			lastHeartTime = nowTime;
			log("AlarmGateway used:%ds=(%d-%d)\n", timeGloble_g - nowTime, timeGloble_g, nowTime);
			fflush(stdout);
		}
	}

	log("AlarmGateway thread be closed!\n");
	exit(0);
}

/******************************************************************
 *	Function Name:	AlarmNode
 *	Arguments:
 *	Return Value:	void
 *	Date: 			2023-12-8
 *	Editor:			cc
 *	Description:	扫描数据库,处理节点告警
 ******************************************************************/
void AlarmNode(void)
{
	unsigned int nowTime;
	//static MYSQL *_db = NULL;
	int ret = 0;
	unsigned int lastHeartTime = 0;

	log("This is in AlarmNode decode! pid=%d  tid=%ld  lwpid=%lu\n", getpid(), pthread_self(), syscall(SYS_gettid));

	while (1)
	{
		usleep(GAME_HEART_BEAT_g);
		nowTime = timeGloble_g;

		//每隔30s扫描数据库设备状态表
		if (nowTime-lastHeartTime >30 )
		{
			CoreAlarmNode();
			lastHeartTime = nowTime;
			log("AlarmNode used:%ds=(%d-%d)\n", timeGloble_g - nowTime, timeGloble_g, nowTime);
			fflush(stdout);
		}
	}

	log("AlarmNode thread be closed!\n");
	exit(0);
}

/******************************************************************
 *      Function Name:  pthTime
 *      Arguments:      void
 *      Return Value:   void
 *      Date:                   2006-11-2
 *      Description:  时间计数器，每秒一次心跳，每秒自加一次
 *                     以单独线程方式运行
 ******************************************************************/
void pthTime(void)
{
	static struct timeval tv;

	timeGloble_g = timeBegin_g = time(NULL);
	srand(time(NULL) + getpid() + random() + timeGloble_g);
	sleep(1);
	log("在pthTime 中 初始化随机种子完毕,请勿再次初始化随机\n");
	srandom(time(NULL) + getpid() + random() + random());
	log("This is in  pthTime 心跳计数现场! pid=%d  tid=%ld  lwpid=%lu\n", getpid(), pthread_self(), syscall(SYS_gettid));

	gettimeofday(&tv, NULL);
	timeGloble_g = tv.tv_sec;

	while (1)
	{
		//edit by liuqing 20181201 调整精度 从原来的1s调整到0.3s   5hz
		usleep(200000);
		gettimeofday(&tv, NULL);
		timeGloble_g = tv.tv_sec;
	}
}
