From fbef1f8e38294d5971c73e920e0e0be2fd59216f Mon Sep 17 00:00:00 2001
From: Jacques Gouno <jacques.gouno@synchrotron-soleil.fr>
Date: Thu, 9 Mar 2017 18:05:29 +0000
Subject: [PATCH] TANGODEVIC-1647: point1 & 2, fixed

---
 src/TIMIQCurl.cpp | 38 ++++++++++++++++++++++----------------
 src/TIMIQLib.cpp  | 39 +++++++++++----------------------------
 2 files changed, 33 insertions(+), 44 deletions(-)

diff --git a/src/TIMIQCurl.cpp b/src/TIMIQCurl.cpp
index 225fd4c..1ef6a72 100644
--- a/src/TIMIQCurl.cpp
+++ b/src/TIMIQCurl.cpp
@@ -46,9 +46,6 @@ TIMIQCurl::TIMIQCurl(const std::string& ip_address, const std::string& num_port)
 	
 	m_error_string = "";
 	
-	// use the static timiq internal buffer
-	// m_timiq_internal_buff.clear();
-	
 	m_hw_curl = NULL; 
 }
 
@@ -294,26 +291,31 @@ E_timiq_errno_t  TIMIQCurl::read_state_and_status(std::string& status, E_timiq_c
 // ============================================================================
 E_timiq_errno_t TIMIQCurl::connect_i()
 {
-	// std::cout << "TIMIQCurl::connect() called..." << std::endl;
-	
 	E_timiq_errno_t  err_num = timiq_internal_ERROR;
 	m_error_string = "";
 	
 	try
 	{
-		m_hw_curl = curl_easy_init();
-			
-		if (m_hw_curl) 
+		if (!m_hw_curl)
 		{
-			err_num = timiq_NO_ERROR;
+			m_hw_curl = curl_easy_init();
+				
+			if (m_hw_curl) 
+			{
+				err_num = timiq_NO_ERROR;
+			}
+			else
+			{ 
+				m_error_string = "INTERNAL_ERROR\n"; 
+				m_error_string += "TIMIQ WebServer access is not available, null pointer detected!\n"; 
+				m_error_string += "TIMIQCurl::connect()"; 
+			}
 		}
 		else
 		{ 
-			m_error_string = "INTERNAL_ERROR\n"; 
-			m_error_string += "TIMIQ WebServer access is not available, null pointer detected!\n"; 
+			m_error_string = "TIMIQ WebServer access allows only single session!\n"; 
 			m_error_string += "TIMIQCurl::connect()"; 
-		}
-		
+		}	
 	}
 	catch (...)
 	{
@@ -362,6 +364,7 @@ bool TIMIQCurl::write_i(std::string& url, std::string& strData)
 #else	
 	m_timiq_internal_buff = "\ncommand=1\n";
 	m_timiq_internal_buff += "command=2\n";
+	m_timiq_internal_buff += "command=3\n";
 	m_timiq_internal_buff += "iValue=-0.000001\n";
 	m_timiq_internal_buff += "qValue=-0.000002";
 	
@@ -447,7 +450,7 @@ bool TIMIQCurl::read_float_i(const std::string& url, const std::string& strData,
 				//- value
 				std::string floatStr =  tokfloatStr.next_token();
 				
-				//- convert data to flaot 
+				//- convert data to float 
 				value = (float)yat::XString<float>::to_num(floatStr);						
 			}
 			catch(...)
@@ -579,16 +582,19 @@ CURLcode TIMIQCurl:: send_to_webserver_i(const std::string& url)
 	curl_easy_setopt(m_hw_curl, CURLOPT_URL, url.c_str());
 	curl_easy_setopt(m_hw_curl, CURLOPT_PROXY, "");
 	curl_easy_setopt(m_hw_curl, CURLOPT_WRITEFUNCTION, &write_callback);
+	
+	/* complete within 1500 ms */
+	curl_easy_setopt(m_hw_curl,  CURLOPT_TIMEOUT_MS, 1500L);
 
 	/* ask libcurl to show us the verbose output */
 	// curl_easy_setopt(m_hw_curl, CURLOPT_VERBOSE, 1L);
 	
 	//- perform the query
 	result = curl_easy_perform(m_hw_curl);
-  
+	
 	//- clean up
 	curl_easy_cleanup(m_hw_curl);
-
+	m_hw_curl = NULL;
 	return result;
 }
 
diff --git a/src/TIMIQLib.cpp b/src/TIMIQLib.cpp
index 7e2a2b2..7bd49b7 100644
--- a/src/TIMIQLib.cpp
+++ b/src/TIMIQLib.cpp
@@ -122,7 +122,7 @@ void TIMIQLib::data_end_task()
 	{
 		if (m_timiq_task->m_ti_cfg.ti_err != timiq_NO_ERROR)
 		{
-			//std::cout <<"exit task done ..." <<std::endl;		
+			// std::cout <<"exit task done ..." <<std::endl;		
 			m_timiq_task->exit();
 			delete m_timiq_task;
 			m_timiq_task = NULL;
@@ -162,7 +162,7 @@ void TIMIQLib::set_iValue(float iValue)
 	{
 		//- force timiq state = Busy
 		m_internal_timiq_state = Busy;
-		
+
 		m_timiq_task = new ThreadedAction(static_cast<yat::Thread::IOArg>(this), l_ti_cfg);
 		if (m_timiq_task == NULL)
 		{
@@ -186,14 +186,12 @@ void TIMIQLib::iVal_end_task()
 // std::cout <<"iVal_end_task ..." <<std::endl;		
 	//- Delete task 
 	if (this->m_timiq_task)
-	{
+	{	
 		if (m_timiq_task->m_ti_cfg.ti_err != timiq_NO_ERROR)
-		{
-			//std::cout <<"exit task done ..." <<std::endl;		
+		{	
 			m_timiq_task->exit();
-			delete m_timiq_task;
 			m_timiq_task = NULL;
-			
+
 			CHECK_TIMIQ_HW;
 			
 			std::string msg_err = "Set I value error -msg: " + m_timiq_hw->get_err_msg();
@@ -201,11 +199,10 @@ void TIMIQLib::iVal_end_task()
 							static_cast<const char*>(msg_err.c_str()),
 							static_cast<const char*>("TIMIQLib::set_iValue"));
 		}	
-		
 		//- destroy otherwise 
 		m_timiq_task->exit();
-		delete m_timiq_task;
 		m_timiq_task = NULL;
+
 	}
 	
 	//- update state of TIMIQ equipment
@@ -256,9 +253,7 @@ void TIMIQLib::qVal_end_task()
 	{
 		if (m_timiq_task->m_ti_cfg.ti_err != timiq_NO_ERROR)
 		{
-			//std::cout <<"exit task done ..." <<std::endl;		
 			m_timiq_task->exit();
-			delete m_timiq_task;
 			m_timiq_task = NULL;
 			
 			CHECK_TIMIQ_HW;
@@ -271,7 +266,6 @@ void TIMIQLib::qVal_end_task()
 		
 		//- destroy otherwise 
 		m_timiq_task->exit();
-		delete m_timiq_task;
 		m_timiq_task = NULL;
 	}
 	
@@ -319,13 +313,12 @@ void TIMIQLib::boardT_end_task()
 {
 // std::cout <<"boardT_end_task ..." <<std::endl;		
 	//- Delete task 
-		if (this->m_timiq_task)
+	if (this->m_timiq_task)
 	{
 		if (m_timiq_task->m_ti_cfg.ti_err != timiq_NO_ERROR)
 		{
 			//std::cout <<"exit task done ..." <<std::endl;		
 			m_timiq_task->exit();
-			delete m_timiq_task;
 			m_timiq_task = NULL;
 			
 			CHECK_TIMIQ_HW;
@@ -338,7 +331,6 @@ void TIMIQLib::boardT_end_task()
 		
 		//- destroy otherwise 
 		m_timiq_task->exit();
-		delete m_timiq_task;
 		m_timiq_task = NULL;
 	}
 	
@@ -365,6 +357,9 @@ void TIMIQLib::set_command(E_timiq_cmd_t& cmd)
 						static_cast<const char*>(msg_err.c_str()),
 						static_cast<const char*>("TIMIQLib::set_command()"));
 	}
+	
+	//- update state of TIMIQ equipment
+	m_internal_timiq_state = OK;
 }		
 
 // ============================================================================
@@ -586,21 +581,13 @@ void TIMIQLib::ExecuteAction(timIQConfig & cfg)
 	
 	CHECK_TIMIQ_HW;
 	
-	// E_timiq_code_t l_timiq_state = m_internal_timiq_state;
-	// if ( l_timiq_state != Busy )
-	// {
-		//- force timiq state = Busy
-		// m_internal_timiq_state = Busy;
 		// Actions to be threaded
 		switch(cfg.id)
 		{
 			//- write "I" value
 			case TI_IVAL:	
-			std::cout<<"Start executeAction - "<< cfg.id<< std::endl;
 					cfg.ti_err = m_timiq_hw->write_iValue(cfg.value);
-				std::cout<<"after executeAction - "<< cfg.id<< std::endl;	
 					this->iVal_end_task();
-				std::cout<<"End executeAction - "<< cfg.id<< std::endl;	
 				break;
 				
 			//- write "Q" value
@@ -618,15 +605,11 @@ void TIMIQLib::ExecuteAction(timIQConfig & cfg)
 			//- write "BOARD Temperature" value
 			case TI_BTEMP:
 					cfg.ti_err = m_timiq_hw->write_data(cfg.value);	
-					this->data_end_task();
+					this->boardT_end_task();
 				break;					
 			default:
 				break;
 		}
-	
-		//- rollback timiq state to previous state
-		// m_internal_timiq_state = l_timiq_state;
-	// }
 }
 
 } // namespace timiqlib
-- 
GitLab