Browse Source

write data to db

ThinkPad 8 years ago
parent
commit
b74c0bcf65
5 changed files with 90 additions and 57 deletions
  1. 17 11
      MysqlConnPool.cpp
  2. 1 1
      MysqlConnPool.h
  3. 60 45
      YAServerDlg.cpp
  4. 9 0
      classdef.cpp
  5. 3 0
      classdef.h

+ 17 - 11
MysqlConnPool.cpp

@@ -268,7 +268,7 @@ int CMysqlConn::Open()
 	HRESULT hr = ::CoInitialize(NULL);
 	if(FAILED(hr))
 		return -1;
-
+	m_clientflag |=  CLIENT_MULTI_STATEMENTS;
 	if(!mysql_real_connect(m_pConn, m_host, m_user, m_pwd, m_db, m_port, m_unix_socket, m_clientflag))
 		return 1;
 
@@ -307,19 +307,25 @@ MYSQL_RES * CMysqlConn::Execute( const char * strSQL, int &err)
 	return pRes;
 }
 
-MYSQL_RES * CMysqlConn::MultiExecute( const char * strSQL, int &err)
+void CMysqlConn::MultiExecute( const char * strSQL, int &err)
 {
 	if(strSQL == "") 
-		return NULL;
+		err = 0;
 	MYSQL_RES * pRes;
-	mysql_set_server_option(m_pConn,MYSQL_OPTION_MULTI_STATEMENTS_ON);
 	int _error = mysql_query(m_pConn, strSQL);
-	mysql_set_server_option(m_pConn,MYSQL_OPTION_MULTI_STATEMENTS_OFF);
-	err = _error;
-	if(_error){
-		return NULL;
-	}
 
-	pRes = mysql_use_result(m_pConn);
-	return pRes;
+	//每条语句对应一个pRes,需要释放每一个结果集;避免数据错乱
+	//不然通过mysql_error()会得知是Commands out of sync; you can't run this command now错误
+	do 
+	{
+		pRes = mysql_use_result(m_pConn);
+		mysql_free_result(pRes);
+	} while (!mysql_next_result(m_pConn));
+
+	/*if (_error != 0)
+	{
+		CString str = _T("");
+		str = mysql_error(m_pConn);
+		TRACE(str);
+	}*/
 }

+ 1 - 1
MysqlConnPool.h

@@ -23,7 +23,7 @@ public:
 	// query
 
 	MYSQL_RES * Execute(const char * strSQL, int &err);
-	MYSQL_RES * MultiExecute( const char * strSQL, int &err);
+	void MultiExecute( const char * strSQL, int &err);
 
 private:
 	MYSQL * m_pConn;

+ 60 - 45
YAServerDlg.cpp

@@ -200,27 +200,26 @@ DWORD WINAPI _exec_sql(LPVOID lparam)
 	if(pConn == NULL){
 		delete[] sql;
 		sql = NULL;
-		//TRACE(_T("failed \n"));
 		return 1;
 	}
-	MYSQL_RES* pRes;
+	//MYSQL_RES* pRes;
 	int err = 0;
 	//pRes = pConn->Execute(sql, err);
+	//mysql_free_result(pRes);
 
-	pRes = pConn->MultiExecute(sql,err);
+	pConn->MultiExecute(sql,err);
 
 	if(err > 0){
 		TRACE(_T("sql error \n"));
 	}
-	mysql_free_result(pRes);
 
-	/*ofstream out("SQL_S.log");
+	/*ofstream out("SQL_S.log",ios::out | ios::app);
 	if (out.is_open())
 	{
-		out<<sql;
-		out.close();
+	out<<sql;
+	out<<err;
+	out.close();
 	}*/
-	//TRACE(_T("passed\n"));
 	delete[] sql;
 	sql = NULL;
 	Sleep(1);
@@ -3461,6 +3460,7 @@ void CYAServerDlg::card_enter_park( Card* card )
 			//card->down_time = card->deal_time;
 			card->down_time = CFunctions::systime_to_timet(card->deal_time);
 			store_data_card(card, RPT_ATTEND_DOWN);
+			TRACE(_T("1111"));
 		}
 	}
 }
@@ -3498,6 +3498,11 @@ void CYAServerDlg::card_enter_map( Card* card )
 //卡card进入区域
 void CYAServerDlg::card_enter_area( Card* card)
 {
+	if(!card->b_pos_change){
+		//如果坐标无变化,不需要进区域判断逻辑
+		return;
+	}
+	//TRACE(_T("card_enter_area \r\n"));
 	_point p1, p2;
 	double r, scale;
 	bool b_in_area = false;
@@ -3584,6 +3589,7 @@ void CYAServerDlg::card_enter_area( Card* card)
 			}
 		}
 	}
+	TRACE(_T("x: %f, y: %f, lx: %f,ly: %f,area_id: %d \r\n"),card->x,card->y,card->last_x,card->last_y,card->area_id);
 	// 保存区域信息
 	store_data_card(card, HIS_AREA_LOCATION_ENTER);
 	// 升入井判断
@@ -3848,18 +3854,36 @@ void CYAServerDlg::store_data_card( Card* card, int tag )
 {
 	char sql[LENGTH_SQL] = {0};
 	char _time[STR_LEN_TIME], _time_ex[STR_LEN_TIME];
-	struct tm local_time, local_time_ex;
+	struct tm local_time, local_time_ex; 
+	time_t t = time_t(NULL);
 	//localtime_s(&local_time, &card->deal_time);
 	//strftime(_time, 30, "%Y-%m-%d %H:%M:%S", &local_time);
 
 	bool b_exec = false;
 
-	sprintf_s(_time,
-		STR_LEN_TIME,
-		"%u-%u-%u %u:%u:%u.%u",
-		card->deal_time.wYear,card->deal_time.wMonth,card->deal_time.wDay,
-		card->deal_time.wHour + 8,card->deal_time.wMinute,card->deal_time.wSecond,card->deal_time.wMilliseconds);
-
+	switch(tag){
+	case TEST_LOCATE_COMPARE:
+	case TEST_LOCATE_DATA:
+	case HIS_RAW_DATA:
+	case HIS_AREA_LOCATION_ENTER:
+	case HIS_AREA_LOCATION_LEAVE:
+	case ALARM_CARD_LOW_POWER_START:
+	case ALARM_CARD_LOW_POWER_END:
+	case RPT_ATTEND_DOWN:
+	case RPT_ATTEND_UP:
+		t = CFunctions::systime_to_timet(card->deal_time);
+		localtime_s(&local_time, &t);
+		strftime(_time, 30, "%Y-%m-%d %H:%M:%S", &local_time);
+		break;
+	case HIS_LOCATION:
+		sprintf_s(_time,
+			STR_LEN_TIME,
+			"%u-%u-%u %u:%u:%u.%u",
+			card->deal_time.wYear,card->deal_time.wMonth,card->deal_time.wDay,
+			card->deal_time.wHour + 8,card->deal_time.wMinute,card->deal_time.wSecond,card->deal_time.wMilliseconds);
+		break;
+	}
+	
 	switch(tag){
 	case TEST_LOCATE_COMPARE:
 		{
@@ -3931,20 +3955,17 @@ void CYAServerDlg::store_data_card( Card* card, int tag )
 	case RPT_ATTEND_DOWN:
 		{
 			if(card->map_id != 0 && card->area_id != 0){
-				b_exec = true;
-				//sprintf_s(sql, LENGTH_SQL,
-				//	"INSERT INTO rpt_attendance(card_id, start_time, att_date) VALUES(%s, '%s', '%s');",
-				//	card->card_id.c_str(), _time, _time);
+				b_exec = true;	
 				sprintf_s(sql, LENGTH_SQL,
 					"CALL add_att(%s, '%s', '%s');", card->card_id.c_str(), _time, _time);
 
-				CString str = _T("");
-				//str.Format(_T("%s"),sql);
-				CString strtmp = _T("");
-				strtmp.Format(_T("%d"),RPT_ATTEND_DOWN);
-				str = sql;
-				str += strtmp;
-				writeErrorLog(_T("SQL_S"),str,false);
+				CString str_log = _T("");
+				CString str_tmp = _T("");
+				str_tmp.Format(_T("%d"),RPT_ATTEND_DOWN);
+				str_log += str_tmp + _T(",");
+				str_tmp = sql;
+				str_log += str_tmp + _T("\r\n");
+				writeErrorLog(_T("SQL_S"),str_log,false);
 			}
 			break;
 		}
@@ -3952,24 +3973,17 @@ void CYAServerDlg::store_data_card( Card* card, int tag )
 		{
 			if(card->map_id != 0 && card->area_id != 0){
 				b_exec = true;
-				//localtime_s(&local_time_ex, &card->down_time);
-				//strftime(_time_ex, 30, "%Y-%m-%d %H:%M:%S", &local_time_ex);
-
 				string str_down_time = CFunctions::time_t2string(card->down_time);
-				//sprintf_s(sql, LENGTH_SQL,
-				//	"UPDATE rpt_attendance SET end_time = '%s' WHERE card_id = %s AND start_time = '%s';",
-				//	_time, card->card_id.c_str(), _time_ex);
-				/*sprintf_s(sql, LENGTH_SQL,
-				"CALL add_att(%s, '%s', '%s');", card->card_id.c_str(), _time, _time_ex);*/
 				sprintf_s(sql, LENGTH_SQL,
-					"CALL add_att(%s, '%s', '%s');", card->card_id.c_str(), _time, str_down_time.c_str());
-
-				CString str = _T("");
-				CString strtmp = _T("");
-				strtmp.Format(_T("%d"),RPT_ATTEND_UP);
-				str = sql;
-				str += strtmp;
-				writeErrorLog(_T("SQL_S"),str,false);
+					"CALL add_att(%s, '%s', '%s');", card->card_id.c_str(), str_down_time.c_str(), _time);
+
+				CString str_log = _T("");
+				CString str_tmp = _T("");
+				str_tmp.Format(_T("%d"),RPT_ATTEND_UP);
+				str_log += str_tmp + _T(",");
+				str_tmp = sql;
+				str_log += str_tmp + _T("\r\n");
+				writeErrorLog(_T("SQL_S"),str_log,false);
 			}
 			break;
 		}
@@ -5831,6 +5845,7 @@ void CYAServerDlg::card_up_mine( Card* card )
 
 void CYAServerDlg::card_down_mine( Card* card )
 {
+	TRACE(_T("card_down_mine \r\n"));
 	//card->down_time = card->deal_time;
 	card->down_time = CFunctions::systime_to_timet(card->deal_time);
 	if(CT_PERSON == card->card_type){
@@ -7046,12 +7061,12 @@ void CYAServerDlg::deal_card_msg( Card* card, bool is_hist /*= false*/ )
 					strLog = _T("");
 					strLog.Format(_T("%d,%f,%f,%f,%f,%f,iw: %f,uw: %f,ct: %d, ov: %f,cv: %f,av: %f, lsn: %d,cardid: %s"),card->m_nCalcSyncNum,card->origin_locate.x,card->origin_locate.y,card->x, card->y,card->z,card->ins_weight,card->uwb_weight,card->time_stamp_cal,card->origin_locate.v,card->v,card->last_locate.acceleration,card->m_nSyncNumInList,p_card_id);
 					writeErrorLog(_T("KALMAN_S"),strLog,false);
-				}else{
+				}else{		
 					if(card->is_deal_by_algo){
 						strLog = _T("");
 						strLog.Format(_T("%d,%f,%f,%f,%f,%f,iw: %f,uw: %f,ct: %d, ov: %f,cv: %f,av: %f, lsn: %d,cardid: %s"),card->m_nCalcSyncNum,card->origin_locate.x,card->origin_locate.y,card->x, card->y,card->z,card->ins_weight,card->uwb_weight,card->time_stamp_cal,card->origin_locate.v,card->v,card->last_locate.acceleration,card->m_nSyncNumInList,p_card_id);
 						writeErrorLog(_T("KALMAN_S"),strLog,false);
-						card->is_deal_by_algo = false;
+						card->is_deal_by_algo = false;	
 					}
 				}
 				if(p_card_id){
@@ -7205,7 +7220,7 @@ void CYAServerDlg::writeErrorLogEx( const char* strFile, const char* strErr ,boo
 
 void CYAServerDlg::init_queuestring()
 {
-	m_qsmSQL = new QueueStrManager(50, 7500, _exec_sql);
+	m_qsmSQL = new QueueStrManager(15, 7500, _exec_sql);//50,7500
 	//m_qsmSQL = new QueueStrManager(2, 200, _exec_sql);
 }
 

+ 9 - 0
classdef.cpp

@@ -17,7 +17,9 @@ Card::Card( string cardid, int cardtype, double z_offset, double offset_x /*= 12
 	this->z_offset = z_offset;
 	
 	GetSystemTime(&deal_time);
+	//GetSystemTime(&down_time);
 	down_time = up_time = enter_area_time = enter_reader_time = rec_time = time(NULL);
+	
 	time_over_time = time_area_over_time = time_area_forbidden = time_over_speed = time_low_power = time(NULL);
 
 	x =	y = z = last_x = last_y = last_z = stored_x = stored_y = stored_z = 0;
@@ -964,6 +966,13 @@ void Card::add_dist(_coordinate* dist)
 		get_coordinate();
 		remove_dist_head();
 	}
+
+	if (this->x != this->last_x || this->y != this->last_y)
+	{
+		this->b_pos_change = true;
+	}else{
+		this->b_pos_change = false;
+	}
 	LeaveCriticalSection(&m_csCard);
 }
 

+ 3 - 0
classdef.h

@@ -628,6 +628,7 @@ public:
 
 	//time_t deal_time; // 最后处理卡逻辑时间
 	SYSTEMTIME deal_time;
+	//SYSTEMTIME down_time;
 	time_t rec_time; // 最后接收时间
 	time_t down_time; // 入井时间
 	time_t up_time; // 升井时间
@@ -791,6 +792,8 @@ public:
 	int FindDistMap(int cardstamp);
 private:
 	bool b_long_interval;	//上一次定位的间隔时间差大于10s
+public:
+	bool b_pos_change;		//位置发生了改变
 public: // 采集到的底层数据
 	int reader_tickcount; 
 	int time_stamp; // 定位时间戳