// YAServerDlg.h : 头文件
//

#pragma once
#include "afxwin.h"
#include "resource.h"
#include "structdef.h"
#include "constdef.h"
#include "def_msg.h"
#include "socketdef.h"
#include "ReportCtrl/ReportCtrl.h"

#include <sio_client.h>
#include "../thirdparty/include/iocp/Src/TcpServer.h"
#include "../thirdparty/include/iocp/Global/helper.h"
#include "afxcmn.h"
#include "./SyncTime/SyncManager.h"
#include "classdef.h"
#include "QueueStrManager.h"
#include "uwb_msg_def.h"
#include <json/json.h>

extern BOOL m_log_sys_status;

using namespace sio;
using namespace std;
using namespace HostServer;


struct  _SLS // system_limit_setting
{
	unsigned int over_count_person;
	unsigned int over_count_vehicle;
	unsigned int over_time_person;
	unsigned int over_time_vehicle;
	double over_speed;
};

struct _DBS // db setting
{
	string host;
	string user;
	string password;
	string dbname;
	string encoding;
	int port;
};

struct _SOCK
{
	CString sip;
	DWORD dwConnID;
	int state; // 0 连接
	DWORD reader_id; // 分站号
	DEVICE_TYPE reader_type; // 通信分站、读卡分站
};

struct _SOCKET_BUFFER
{
	DWORD dwConnID;
	BYTE pData[LENGTH_MSG_1M]; 
	WORD wLen;
	WORD wReadLen;

	_SOCKET_BUFFER(){
		dwConnID = 0;
		wLen = 0;
		wReadLen = 0;
		memset(pData,0,LENGTH_MSG_1M*sizeof(BYTE));
	}
};

//extern DWORD WINAPI _exec_sql(LPVOID lparam);
extern DWORD WINAPI _exec_sql(const char* lparam);
extern DWORD WINAPI _write_error_log(LPVOID lparam);

// CYAServerDlg 对话框
class CYAServerDlg : public CDialogEx, public CTcpServerListener
{
	typedef map<string, std::shared_ptr<Card>> CardMap;
	typedef map<CString, std::shared_ptr<_SOCK>> SocketMap;
	typedef map<CString, std::shared_ptr<_SOCKET_BUFFER>> SocketBufferMap;
	typedef map<INT64, std::shared_ptr<YaEvent>> EventMap;
	typedef map<int, std::shared_ptr<LightCallInfo>> LightCallMap;

	struct LogMsg
	{
		CString strFile;
		CString strLog;
		bool isNeedTime;
	};
	struct DataMsg
	{
		CONNID dwConnID;
		int iLength;
		BYTE* pData;		
	};
	// 构造
public:
	CYAServerDlg(CWnd* pParent = NULL);	// 标准构造函数
	~CYAServerDlg();
	// 对话框数据
	enum { IDD = IDD_YASERVER_DIALOG };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV 支持
private:
	virtual EnHandleResult OnPrepareListen(ITcpServer* pSender, SOCKET soListen);
	virtual EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, SOCKET soClient);
	virtual EnHandleResult OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength);
	virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength);
	virtual EnHandleResult OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode);
	virtual EnHandleResult OnShutdown(ITcpServer* pSender);
	virtual void OnReConnect(unsigned, unsigned);
	// 实现
protected:
	HICON m_hIcon;

	// 生成的消息映射函数
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
public:
	CButton m_btStart;
	CButton m_btStop;
	CMenu m_mnMain;
	afx_msg LRESULT on_monitor(WPARAM wParam, LPARAM lParam);
	afx_msg void OnBnStop();
	afx_msg void OnBnStart();	
	afx_msg void OnBnExit();
	afx_msg void OnBnSyncTime();
	afx_msg void OnMenuDb();
	afx_msg void OnMenuLog();
	afx_msg void OnMenuParam();
	afx_msg void OnBnCallHisData();
public: 
	// ui operation
	CTcpServer _server;
	HostServer::SyncManager syncmanager;
	EnAppState m_enState;
	CStdioFile cfErrorLog;
	CRITICAL_SECTION m_csWriteLog;
	CRITICAL_SECTION m_csRemoveSocket;
	CRITICAL_SECTION m_csRefreshList;
	CRITICAL_SECTION m_csParseDataServer;
	CRITICAL_SECTION m_csSyncTime;
	CRITICAL_SECTION m_csCallInfo;

	QueueStrManager* m_qsmSQL;

	bool is_start_tcp;
	void set_app_state(EnAppState eas);
	void start_tcp_server();
	void stop_tcp_server();	
public:
	CString m_strConncetion; // 数据库连接字符串
	int m_nPort; // 启动端口
	int m_nCountClick;

	CWinThread* m_thread_monitor; // 与监控程序通信线程
	CWinThread* m_thread_timer; // timer的线程，脱离UI线程，不使用ontimer
	unsigned int m_uint_state; // 采集状态
	// bit 0, 服务状态，运行或者关闭
	// bit 1, 采集状态，正常或异常
	// bit 2, 分站状态，有中断或没有
	// bit 3, websocket状态，连接未连接
	// bit 4, 数据库状态，连接未连接
	time_t m_time_last_rec; // 最后接收到分站数据的时间
	bool is_websocket_ok; 
	bool is_websocket_login;
	bool is_server_ok; 
	bool is_database_ok; 

	int m_shift_id;
	BOOL m_bUseFilter;

	int m_nFilterType;
	int m_nMallocTimes;

	//BOOL m_log_sys_status;
	BOOL m_log_raw_data;
	BOOL m_log_parse_data;
	BOOL m_log_send_package;
	BOOL m_log_recv_package;
	BOOL m_log_locate_data;

	SocketMap mp_socket_list;
	SocketBufferMap mp_socket_buffer_list;

	ReaderMap mp_reader_list; // 所有分站
	ReaderMap mp_ctrl_reader_list; // 所有控制分站
	AreaMap mp_area_list; // 所有区域
	DrivingfaceWarningPointMap mp_drivingface_warning_point_list; // 掘进面告警点列表
	SpecilaAreaEntryMap mp_special_area_entry_list; //特殊区域入口
	DrivingfaceRenderMap mp_driving_face_render_list; //掘进面分站基点配置信息
	MapInfoMap mp_map_list; // 所有地图
	LandmarkInfoMap mp_landmark_list; // 所有地图地标
	MapDirectionInfoMap mp_map_direction_list; //地图坐标系X轴正方向与地理北方的夹角
	DeptMap mp_dept_list; // 所有部门
	BanShiftMap mp_banshift_list; // 班次信息
	OccLevelMap mp_occlevel_list; // 所有职务级别
	AlarmTypeMap mp_alarm_type_list;
	//ReaderPathMap mp_reader_path_list;	//当前地图的所有分站的覆盖范围
	SectionMap mp_section_list;
	ChamberMap mp_chamber_list;
	LightMap   mp_light_list;
	LightGroupMap mp_light_group_list;

	CardMap mp_card_list_all; // 所有卡
	CardMap mp_card_list_down_vehicle; // 井下车辆
	CardMap mp_card_list_down_person;  // 井下人员
	CardMap mp_card_list_up_person; // 升井人员
	CardMap mp_card_list_down_adhoc;   //井下自组网卡
	PatrolTaskCheckMap mp_card_task_list; // 巡检任务

    std::shared_ptr<ReaderPathMap> mp_reader_path_list;	//当前地图的所有分站的覆盖范围
	std::shared_ptr<TDOAReaderPathMap> mp_reader_path_list_tdoa; 
	std::shared_ptr<CardMap> mp_card_list_over_time_person;	// 超时人员	
	std::shared_ptr<CardMap> mp_card_list_forbidden_person; // 进入限制区域人员
	std::shared_ptr<CardMap> mp_card_list_help; // 呼救人员
	std::shared_ptr<CardMap> mp_card_list_area_over_time_person; // 区域超时是人员
	std::shared_ptr<CardMap> mp_card_list_forbidden_vehicle; // 进入限制区域车辆
	std::shared_ptr<CardMap> mp_card_list_over_time_vehicle; // 超时车辆
	std::shared_ptr<CardMap> mp_card_list_over_speed_vehicle; // 超速车辆
	std::shared_ptr<CardMap> mp_card_list_area_over_time_vehicle; // 区域超时车辆
	std::shared_ptr<CardMap> mp_card_list_over_count_person; // 人员超员

	std::shared_ptr<AreaMap> mp_area_over_count_person; // 人员超员区域
	std::shared_ptr<AreaMap> mp_area_over_time_person; // 人员超时区域
	std::shared_ptr<AreaMap> mp_area_forbidden_person; // 有人限制区域
	std::shared_ptr<AreaMap> mp_area_over_count_vehicle; // 车辆超员区域
	std::shared_ptr<AreaMap> mp_area_over_time_vehicle; // 车辆超时区域
	std::shared_ptr<AreaMap> mp_area_forbidden_vehicle; // 有车限制区域

	std::shared_ptr<EventMap> mp_event_list; // 事件列表
	std::shared_ptr<LightCallMap> mp_light_call_list; // 控制红绿等命令列表

	unordered_map<unsigned long long, unsigned short> umpRootSyncNum;  // 每个root的同步序号

	CallInfoUserMap mp_user_call_info; // 用户呼叫信息

	_SLS sls_setting; // 系统参数配置
	_DBS m_db_setting; // 数据库参数配置
	string m_ws_url; // web服务器url
	CString m_tcp_host; // 本机接收分站数据的ip

	double m_reader_interval_time;
	INT64 m_event_list[MINE_EVENT_COUNT];
	int cur_shift_id;
	time_t cur_shift_start_time;
private:
	int init_smart_pointer_var();

public:
	void init_ui(); // 初始化界面
	void init_param(); // 初始化运行参数
	void init_queuestring(); // 初始化数据库缓存队列
	void load_param_from_ini(); // 加载配置文件中参数
	void database_connect(); // 连接数据库
	void stop_and_exit(); // 停止并退出程序
	void set_run_state(); // 修改界面按钮状态

	// 从数据库中初始化数据 
	void init_base_data(); // 
	int init_setting(); // 系统配置
	int init_alarm_type(); // 报警类型
	int init_shift(); // 载入班次信息
	int init_dept(); // 部门列表
	int init_level(); // 级别
	int init_card(string card_id = "");
	int init_staffer(string staff_id = "", bool is_card = false); // 人员
	int init_vehicle(string vehicle_id = "", bool is_card = false); // 车辆
	int init_adhoc(string adhoc_id = "",bool is_card = false); // 自组网卡
	int init_map(int map_id = 0); // 地图
	int init_landmark(void); //路标信息
	int init_map_direction(void);//地图坐标系X轴正方向与地理坐标系北方的夹角
	int init_area(int area_id = 0); // 区域
	int init_special_area_entry(void); // 特殊区域入口
	int init_drivingface_render(void); //掘进面分站基准点配置
	int init_reader(int reader_id = 0); // 分站
	int init_ctrl_reader(int reader_id = 0); // 控制分站
	//int init_antenna(int reader_id = 0); // 天线
	int init_dev_adhoc(int reader_id = 0); // 自组网坐标
	int init_antenna_sync(); // 初始化分站间距离，组装时间同步所需数据
	int init_all_readers_coverage(int reader_id = 0);			//从数据库读取地图集覆盖范围,初始化所有分站的地图集
	int init_tdoa_all_readers_coverage(int reader_id = 0);			//从数据库读取地图集覆盖范围,初始化所有分站的地图集
	int init_traffic_lights(int light_id = 0);		//初始化红绿灯
	int init_chamber(int chamber_id = 0);				//初始化硐室
	int init_section(int section_id = 0);				//初始化路段
	int init_light_group(int group_id = 0);				//初始化红绿灯组
	int init_light_state(CONNID dwConnID,std::string strIp);

	//int init_patrol_info(int task_id = 0); // 加载巡检任务及巡检路线
	int init_patrol_info(std::string card_id);
	int delete_patrol_info(std::string card_id);
	int get_help_card_nums();
	int get_map_path_nums(const int& start_reader_id,const int& end_reader_id);	//获得地图集中两分站之间的路径数量

	// 载入历史数据
	void load_his_data();
	void load_his_area(); // 所在区域
	void load_his_reader(); // 分站状态
	void load_his_card_postion_vehicle(); // 所在位置
	void load_his_card_postion_staff();
	void load_his_driving_face_card_postion(); // 所在位置

	void load_his_driving_face_card_state(); // 载入掘进面卡状态
	void init_drivingface_warning_point();
	void load_his_card_state(); // 加载卡状态
	void init_drivingface_card_curshift_position();
	float get_drivingface_warning_point_dis(std::shared_ptr<Card> card);

	// 载入报警数据
	void load_alarm_reader();
	// 接口数据解析
	void parse_edit_map(int map_id);
	void parse_edit_area(int area_id);
	void parse_edit_reader(int reader_id);
	void parse_edit_card(string card_id);
	void parse_edit_staffer(string staffer_id);
	void parse_edit_vehicle(string vehicle_id);
	void parse_edit_path(int path_id);
	void parse_set_limit_value();
	void parse_call_card();
	void parse_clear_card();
	void parse_deal_help();
	// 初始化websocket
	void ws_init();
	void ws_open();
	void ws_close();
	void ws_login();
	void ws_logout();

	void clear_listener();
	void clear_all_list();
	// socket接收数据
	void parse_data_server(const BYTE * pData, int nLen, DWORD dwConnId); // 处理服务器端粘包
	void parse_package_data(BYTE* DataBuffer, int nLen, DWORD dwConnID); // 处理客户端粘包
	// 解析定位数据
	void parse_data_locate_reader(BYTE * DataBuffer, int& nCurPos, int nLen, DWORD dwConnID, int& reader_id);
	void parse_data_ctrl_reader(BYTE * DataBuffer, int& nCurPos, int nLen, DWORD dwConnID, int& reader_id);
    int parse_inav_msg(INAVLocateMsg* msgPtr);
	void deal_inav_locate_msg(INAVLocateMsg* msgPtr, int card_num);
	INAV_STATUS calculate_inav_state(INAVLocateData* cardData);
	void calculate_inav_locate_by_magic(std::shared_ptr<Card> card, INAVLocateData* cardData);
	void write_inav_locate_magic_data(std::shared_ptr<Card> card, INAVLocateData* cardData);
    void log_card_inav_state(INAV_STATUS card_state, string card_id);
	void parse_data_lights_state(BYTE* DataBuffer, int& nCurPos, int reader_id, unsigned short wChr);
	int parse_data_locate_card(BYTE* DataBuffer, int& nCurPos, int reader_id, unsigned short wChr);
	int get_locate_msg_type(BYTE* DataBuffer);
	std::shared_ptr<Card> parse_tof_locate_msg(TofLocateMsg* DataBuffer, int cardNum);
	
	
	//void parse_data_locate_card_vehicle(BYTE* DataBuffer, int& nCurPos,  Card* card, unsigned short wChr);
	void parse_data_locate_card_vehicle(BYTE* DataBuffer, int& nCurPos,  std::shared_ptr<Card> card, unsigned short wChr);
	void parse_data_locate_reader_his(BYTE * DataBuffer, int nLen, int& nCurPos, int& reader_id, int& nTick, CString& strtime, unsigned short wChr);
	void parse_data_locate_card_his(BYTE* DataBuffer, int& nCurPos, int reader_id, DWORD wTickCount, CString strTime, unsigned short wChr);
	void parse_data_locate_card_his_tdoa(BYTE* DataBuffer, int nLen, int reader_id, DWORD wTickCount, CString strTime);
	void parse_data_locate_card_his_tdoa_package(BYTE* DataBuffer, int nLen, int reader_id, DWORD wTickCount, CString strTime, CString strHistTime);
	void parse_data_locate_card_his_tof_card(BYTE* DataBuffer, int nLen, int reader_id, DWORD wTickCount, CString strTime, CString strHistTime);
	void parse_data_locate_card_his_tdoa_card(BYTE* DataBuffer, int nLen, int reader_id, DWORD wTickCount, CString strTime, CString strHistTime);
	void parse_data_locate_card_his_sync_time(BYTE* DataBuffer, int nLen, CString strTime,  CString strHistTime, WORD wChr);
	// 解析自组网数据
	void parse_data_adhoc(BYTE* DataBuffer, int& nCurPos,  int reader_id);
	// 解析分站时间同步
	void parse_data_reader_synctime(BYTE *DataBuffer, int nLen, int& nCurPos, WORD wChr);

	std::shared_ptr<_SOCK> get_socket_by_Id(DWORD dwConnID);
	void add_socket_to_list(DWORD dwConnID, DWORD reader_id, DEVICE_TYPE rt);
	void remove_socket(DWORD dwConnID);
	// 发送数据
	//void SendData(DWORD dwConnID, WORD wCharacterWord, int wLength, BYTE* DataBuffer);
	BOOL SendData(DWORD dwConnID, WORD wCharacterWord, int wLength, BYTE* DataBuffer);
	// 将设置的灯添加到列表,同一个灯的状态会覆盖，使用最后一个状态控制
	void add_light_to_call_list(LIGHTS_SELECT_TYPE lst, LIGHT_COLOR clr, LIGHT_CTRL_STATE ctrl, int tkid, LIGHT_SHAPE ls,int light_id = 0); 
	void send_call_light();
	void send_call_light(BYTE ctrl_cmd, int light_id, int ctrl_type, int ctrl_color,int light_shape);

	// 获取json串
	string get_json_login();
	string get_json_logout();
	string get_json_down_and_up(std::shared_ptr<Card> card);
	string get_json_special_area_process(std::shared_ptr<Card> card);
	string get_json_all_special_area_data();
	string get_json_counting();
	string get_json_position();
	string get_json_card_position(std::shared_ptr<Card> card);
	string get_json_drivingface_card_position(std::shared_ptr<Card> card);
	string get_json_position_all();
	int insert_data(int index, int key, StatisticDataStr** header);
	void delete_memory(StatisticDataStr *header);
	Json::Value get_json_statistic_vehicle_data(Json::Value& vehicle_data);
	Json::Value get_json_statistic_staff_data(Json::Value& staff_data);
	Json::Value get_json_statistic_data(StatisticDataStr* header);
	int get_json_node_position(std::shared_ptr<Card> card,Json::Value& detailNode);
	void ReportCardStatistic();
	string get_json_card_statistic();
	string get_json_all_data();
	string get_json_device_state();
	string get_json_alarm();
	string get_json_call_test();
	string get_json_help();			//求救json
	string get_json_event();
	string get_json_call_list();
	string get_json_new_device_state();
	string get_json_count_area();	//统计区域人数

	Json::Value get_section_json_alarm();
	Json::Value get_light_json_alarm();
	Json::Value get_red_light_json_alarm();
	Json::Value get_json_count_list(int req_type,int area_id);
	Json::Value get_card_info(std::shared_ptr<Card> pCard);

	void send_json_data(string cmd, string data, bool is_login = false);
	// 业务处理
	void deal_readers();
	void deal_areas();
	void deal_card_msg(std::shared_ptr<Card> card, bool is_hist = false);
	void deal_hist_card_postion();
	void deal_his_card_list(std::shared_ptr<Card> card);

	void card_enter_mine(std::shared_ptr<Card> card);
	void card_enter_mine(std::shared_ptr<Card> card, POS_DEVICE_TYPE pdt);
	void card_down_mine(std::shared_ptr<Card> card);
	void card_up_mine(std::shared_ptr<Card> card);
	void card_enter_map(std::shared_ptr<Card> card);
	void card_enter_area(std::shared_ptr<Card> card);
	void card_enter_special_area(std::shared_ptr<Card> card, int area_id);
	void card_leave_area(std::shared_ptr<Card> card, std::shared_ptr<Area> area);
	void deal_card_state(std::shared_ptr<Card> card);
	void deal_alarm_card(std::shared_ptr<Card> card, std::shared_ptr<CardMap> dest_card_list, bool is_alarm, ALARM_FLAG tag);
    void deal_drivingface_alarm(std::shared_ptr<Card> card, bool is_alarm, ALARM_FLAG tag);
	void deal_alarm_area(std::shared_ptr<Area> area, std::shared_ptr<AreaMap> dest_area_list, bool is_alarm, ALARM_FLAG tag);
	void deal_card_lost_state();
	int get_special_area_id(shared_ptr<Card> card);
	int  deal_traffic_lights(std::shared_ptr<Card> card);
	int  deal_area_over_speed(std::shared_ptr<Card> card);		//处理区域超速
	int  card_enter_chamber(std::shared_ptr<Card> card,std::shared_ptr<Section> section);
	int  select_pos_output(std::shared_ptr<Card> card);
	void reset_map_card();
	bool is_in_rectangle(_point p, double dblleft, double dbltop, double dblright, double dblbottom);
	bool is_in_circle(_point p1, _point p2, double r, double scale);
	// save to database
	void execute_sql(const char* sql); // 保存到数据库

    void calculate_card_postion_by_tof(std::shared_ptr<Card> card); // 保存卡位置
    void process_driving_face_start_service(std::shared_ptr<Card> card); // 处理采掘面
    void send_drivingface_card_position_json();
    void write_drivingface_shift_start_data(std::shared_ptr<Card> card);
	void write_drivingface_shift_end_data(std::shared_ptr<Card> card);
    void driving_face_shift_start();
	void driving_face_shift_end();
    void process_driving_face_end_service(std::shared_ptr<Card> card);
	void store_driving_face_data();
	void update_driving_face_data();
	void write_his_drivingface_real(std::shared_ptr<Card> card);
	void save_card_postion(std::shared_ptr<Card> card); // 保存卡位置
	void save_raw_data_card_tof(std::shared_ptr<Card> card);
	void save_card_postion_ex(std::shared_ptr<Card> card); // 补录卡数据
	bool IsInLine(_point p1, _point p2, _point p3);

	void store_data_card(std::shared_ptr<Card> card, int tag);
	void store_data_event(std::shared_ptr<YaEvent> ev);
	void save_patrol_data(std::shared_ptr<PatrolTask> pTask, std::shared_ptr<PatrolPoint> pPoint);

	int  store_data_light(std::shared_ptr<Light> light,int tag);
	int  store_data_traffic_red_light(std::shared_ptr<Light> light,std::shared_ptr<Card> card);	//存储闯红灯数据
	int  store_data_section(std::shared_ptr<Section> section,time_t cur_time,int tag);

	// 打包数据
	void package_data();
	void package_data_test();
	// 通用方法
	int API_TimeToString(string &strDateStr,const time_t &timeData);
	int API_StringToTime(const string &strDateStr,time_t &timeData);
	//void formatByteArray(CString &strBuf, BYTE * buf, int nLen);
	void formatByteArray(std::string &strBuf, BYTE * buf, int nLen);
	void writeErrorLog(const CString strFile, const CString strErr ,bool bTime = true);
	void writeErrorLogEx(const CString strFile, const CString strErr ,bool bTime = true);
	void writeErrorLogEx(const char* strFile, const char* strErr ,bool bTime = true);
	BOOL openLogFile(const CString strFile);
	BOOL openLogFile(const char* strFile);
	void show_log( LPCTSTR szlog, BOOL bSave = true);
	void AddItem(CString strvalue, int readerid, BOOL is_closed, DEVICE_TYPE rt);
	// 响应接口
	void parse_json_data_set_limit_value(const char* sz);
	void parse_json_data_call_card_cancel(message::ptr const& data);
	void parse_json_data_call_card(message::ptr const& data);//(const char* sz);
	void parse_json_data_clear_card(const char* sz);
	void parse_json_data_deal_help(const char* sz);
	int  parse_json_data_set_light_color(message::ptr const& data);
	int  parse_json_data_request_counts_info(message::ptr const& data);
	int parse_json_data_reader_pos_change(message::ptr const& data);

	void deal_call_edit_map(int id, EDIT_TYPE_ID edit_type_id);
	void deal_call_edit_area(int id, EDIT_TYPE_ID edit_type_id);
	void deal_call_edit_reader(int id, EDIT_TYPE_ID edit_type_id);
	void deal_call_edit_card(string id, EDIT_TYPE_ID edit_type_id);
	void deal_call_edit_staff(string id, EDIT_TYPE_ID edit_type_id);
	void deal_call_edit_vehicle(string id, EDIT_TYPE_ID edit_type_id);
	void deal_call_edit_adhoc(string id, EDIT_TYPE_ID edit_type_id);
	int  deal_call_edit_section(int id,EDIT_TYPE_ID edit_type_id);		//web端编辑路段信息
	int  deal_call_edit_light(int id,EDIT_TYPE_ID edit_type_id);		//web端编辑灯信息
	int  deal_call_edit_chamber(int id,EDIT_TYPE_ID edit_type_id);		//web端编辑硐室信息

	void deal_call_edit_patrol_task(string id, EDIT_TYPE_ID edit_type_id);
	void remove_card(string card_id);

	void add_event(std::shared_ptr<YaEvent> ev);
	void remove_event(std::shared_ptr<YaEvent> ev);

	void check_patrol();
	void send_call_info();
	void send_call_his_data();
	void send_sync_reader();
	int send_heart_package();
	void clear_call_info();
	int get_card_id_len(int card_type);
	int modify_card_speed(int val);

	bool is_has_chamber(int section_id);	//路段内存在可用的硐室
	bool isInMainTunel(std::shared_ptr<Card> pCard);		//此卡是否在主巷道
	int getTunelProperty(std::shared_ptr<Card> pCard);		//获得卡的巷道值
	void AnalysisAlgoFailedData(int interval = 60000 /*std::shared_ptr<Card> pCard*/);

	int GetLandmarkID(_point card_point);
	double GetDistance(_point p1, _point p2);
	time_t GetCurShiftStartTime(int& shift_id);
	time_t GetCurShiftEndTime();
	time_t GetLastShiftStartTime(int& shift_id);
	DIRECTION_TYPE GetDirectionType(_point card_point, _point landmark_point, int map_id);

public:
	//测试函数
	void Test();
	void ReadFileTest();
	int  GenerateAllDataJsonTest();
	
	//格子算法
	std::shared_ptr<CellPathMap> mpCellPath;
	CWinThread* pCellThread;			//500ms补充格子线程
	CWinThread* pSendCellThread;		//发送格子数据的线程
	HANDLE hEvent;						//格子线程事件
	bool bSend;

	int SaveCellData2DB();
	int WriteLog();
	int ModifyCardInfo();
	int DivideCell(double width, std::shared_ptr<TDOAReaderPathMap> pTdoaReaderPathMap);
	int InitCellBranch();
	int ExecuteSql(std::string sql);
	LRESULT UpdateCardMessage(WPARAM wParam,LPARAM lParam);
	std::string get_json_position(std::shared_ptr<Card> pCard);
	void TestLandmarkRequirement();

public:
	std::unique_ptr<client> _io;
	CListBox m_list_log;
	CReportCtrl m_listctrl_reader;

	void OnPushMessage();
	void OnCallMessage(string const& name, message::ptr const& data, bool isAck,message::list &ack_resp); 
	//void OnCallMessage(string const& name, message::ptr const& data, bool isAck = false,message::list &ack_resp = nullptr); 
	void OnLoginCallBack(message::list const& msglist = nullptr);
	void OnLogin(string const& name, message::ptr const& data, bool isAck, message::list &ack_resp);

	void OnConnected();
	void OnClosed(client::close_reason const& reason);
	void OnFailed();
protected:
	afx_msg LRESULT OnRemoveSocket(WPARAM wParam, LPARAM lParam);
	afx_msg LRESULT OnMsgRefreshIpList(WPARAM wParam, LPARAM lParam);
	afx_msg LRESULT OnWriteErrorLog(WPARAM wParam, LPARAM lParam);
public:
	UINT m_reader_id_hist;
	afx_msg void OnEnUpdateEditReaderid();
	afx_msg void OnAbout();
	afx_msg void OnBnClickedButton1();

public:
	//初始化格子地图集
	int init_cell_path_map();
};

typedef struct {
	CYAServerDlg* handle;
	int len;
	DWORD dwConnID;
	BYTE buf[LENGTH_MSG_4K];
}ParseData;

typedef struct 
{
	CYAServerDlg* handle;
	bool useTime;
	char* path;
	char* strLog;
}LogInfo;