/**
* @brief
websocket定时发送线程类

* @version
  V 1.0.0

* @author
  王益俊

* @date
  创建时间:  2018-08-17\n

* @note
  2018-08-17  创建类。\n

* @warning

* @bug
  
*/

#pragma once

#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/atomic.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/condition.hpp>
#include <boost/serialization/singleton.hpp>

#include <ctime>

#include "thread_safe_map.h"
#include "ws_common.h"
#include "jsonBuilder.h"

namespace YA
{
	class wsTimerThread
	{
	private:
		boost::shared_ptr<boost::thread> __Thread;//线程
		boost::atomic<bool> __Enable;//线程是否继续运行的标志
		boost::atomic<bool> __Running;//线程是否正在运行的标志
		boost::condition_variable_any __ExitCond;//条件变量
		boost::mutex __ExitMutex;//锁(配合__ExitCond)
		_THREAD_CONFIG_ __Config;//线程配置
		thread_safe_map<uint64_t, _CARD_POS_> __CardPosList;//卡位置列表
		std::tm __LastSendTime;//上一次发送的时间
		jsonBuilder __jsBuilder;//json构造器类
	private:
		/**
		* @brief
		  重置函数。

		* @param  无\n

		* @return 无\n

		* @note

		* @warning

		* @bug

		*/
		void __Reset();
		/**
		* @brief
		  检查线程配置是否合法函数。

		* @param  无\n

		* @return 无\n

		* @note

		* @warning

		* @bug

		*/
		void __ChkConfig();
		/**
		* @brief
		  发送卡位置函数。

		* @param  无\n

		* @return 无\n

		* @note

		* @warning

		* @bug

		*/
		void __SendCardPos();
	protected:
		/**
		* @brief
		  线程函数。

		* @param  [in] wsTimerThread* pOwner  线程所属者\n

		* @return 无\n

		* @note

		* @warning

		* @bug

		*/
		void _ThreadFunc( wsTimerThread* pOwner );
	public:
		wsTimerThread();
		~wsTimerThread();
		/**
		* @brief
		  启动线程函数。

		* @param  无\n

		* @return 无\n

		* @note

		* @warning

		* @bug

		*/
		void Start();
		/**
		* @brief
		  停止线程函数。

		* @param  无\n

		* @return 无\n

		* @note

		* @warning

		* @bug

		*/
		void Stop();
		/**
		* @brief
		  初始化函数。

		* @param  [in] const _THREAD_CONFIG_& Config  线程配置\n

		* @return 无\n

		* @note

		* @warning

		* @bug

		*/
		void Init( const _THREAD_CONFIG_& Config );
		/**
		* @brief
		  更新卡位置函数。

		* @param  [in] const _CARD_POS_& pos  卡位置结构体\n

		* @return 无\n

		* @note

		* @warning

		* @bug

		*/
		void upt_card_pos( const _CARD_POS_& pos );
		/**
		* @brief
		  删除卡位置函数。

		* @param  [in] const _CARD_POS_& pos  卡位置结构体\n

		* @return 无\n

		* @note

		* @warning

		* @bug

		*/
		void del_card_pos( const _CARD_POS_& pos );
	};
}

//单件定义
typedef boost::serialization::singleton<YA::wsTimerThread> singleton_wsTimerThread;
#define swsTimerThrd singleton_wsTimerThread::get_mutable_instance()
#define swsTimerThrd_const singleton_wsTimerThread::get_const_instance()