// QuickViewMngr.cpp: implementation of the CQuickViewMngr class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "6046DryEnd.h"
#include "QuickViewMngr.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CQuickViewMngr::CQuickViewMngr()
{
	m_strVersion = "No Version Loaded";	
	bSensorIdSet = FALSE;


	m_generalDetails = new QV_GENERAL[QV_HISTORY];

	for(int i = 0; i<QV_HISTORY; i++)
	{
		m_generalDetails[i].bLogging	 = FALSE;
		sprintf(m_generalDetails[i].fileName,"-");
		m_generalDetails[i].fileSize	 = -1;
		m_generalDetails[i].maxFileSize = 0;
		m_generalDetails[i].maxFileSize = 0;
	}
}

CQuickViewMngr::~CQuickViewMngr()
{
	if(m_generalDetails)
		delete [] m_generalDetails;
	m_generalDetails = NULL;

}

BOOL CQuickViewMngr::SetDisplayPtr(CListCtrl *pQV, CTabManager *pTab, HWND ParentWnd)
{
	m_hNotifyWnd = ParentWnd;

	if(pQV != NULL)
	{
	  m_pQuickView = pQV;
	  InitDisplay();
	  if(pTab != NULL)
	  	m_pTabMngr = pTab;
	  else 
		return FALSE;
	
	  return TRUE;
	}

	return FALSE;
}

void CQuickViewMngr::InitDisplay()
{
	m_pQuickView->SetBkColor(RGB(0,35,0));
	m_pQuickView->SetTextBkColor(RGB(0,35,0));
	m_pQuickView->SetTextColor(RGB(0,255,55));
    
	currentTypeDisplayed = unknown;


	//DisplayData(currentTypeDisplayed);
}

void CQuickViewMngr::DisplayNoSync(	)
{
	m_pQuickView->DeleteAllItems();
	m_pQuickView->InsertItem(0,""); 							 
	m_pQuickView->SetItemText(0,1,"Not Synchronised");
}


void CQuickViewMngr::ClearLists()
{
	m_vecRunlist.clear();
	m_vecFileDetail.clear();
	m_vecModules.clear();
	m_vecHFSSPulseFiles.clear();
	m_vecLFSSPulseFiles.clear();
	m_vecSBPPulseFiles.clear();
}

void CQuickViewMngr::GetSyncInfo(QV_GENERAL *dest)
{
	try{
		memcpy(dest, &m_generalDetails, sizeof(QV_GENERAL));
	}
	catch(...)
	{
		TRACE("SYNC INFO FAILURE\r\n");
	}
}

BOOL CQuickViewMngr::AddData(EMESSAGETYPE msgType, CString msg)
{

	switch(msgType)
	{
	case general:{	 
					 float ver = atof(msg);
					 int Maj,Min,Upd;
					 Maj = (int)ver;
					 ver -= Maj;
					 Min = (int)(ver*100);
					 ver = (ver*100)-Min;
					 Upd = (int)(ver*100);

					 m_strVersion.Format("%2d.%02d.%02d", Maj,Min,Upd); 
					 
					 if(currentTypeDisplayed == general){
						 DisplayData(currentTypeDisplayed);
						 break;
					 }
					 break;
					}
	case runlist:{	
					m_vecRunlist.clear();
					
					ParseRunlistString(msg);

					if(bSensorIdSet == FALSE)
					{
						SetSensorIds();
						bSensorIdSet = TRUE;
					}
					
					if(currentTypeDisplayed == runlist){
						 DisplayData(currentTypeDisplayed);
					}
					break;
					}
	case report:{	 //TRACE("PARSING REPORT MSG...\r\n"); 
					 ParseReportString(msg);
					 if(currentTypeDisplayed == general){
						 DisplayData(currentTypeDisplayed);
						 break;
					 }
					 break;
					}
	case modules:{	//TRACE("PARSING MODULE MSG...\r\n");
				    m_vecModules.clear();
					ParseModulesString(msg);
					if(currentTypeDisplayed == report){
						DisplayData(currentTypeDisplayed);
						break;
					}
					
					break;
				 }
	case version:{
					break;
				 }
	case health: {					
					//TRACE("HEALTH MSG: %s\r\n", msg);
					ParseHealthString(msg);
					if(currentTypeDisplayed == health)
						DisplayData(currentTypeDisplayed);
					
					break;
				 }	
	}

	return FALSE;
}

void CQuickViewMngr::AddPulseFiles(UINT eSensorType, CString msg)
{
	//Empty the old pulse files
	switch(eSensorType)
	{
	case HFSS:  m_vecHFSSPulseFiles.clear();break;
	case LFSS:  m_vecLFSSPulseFiles.clear();break;
	case SBPSS:  m_vecSBPPulseFiles.clear();break;
	}
	//First parse the message and extract file names
	ParsePulseFiles(eSensorType, msg);

	//Now add each file name to appropriate tabbed dlg
	UpdatePulseFiles(eSensorType);

}

/*void CQuickViewMngr::AddModule(int ID, float version, CString name)
{
	MODULE temp;

	temp.moduleID		= ID;
	temp.moduleVersion	= version;
	temp.moduleName		= name;

	m_vecModules.push_back(temp);

	if(currentTypeDisplayed == modules)
		DisplayData(currentTypeDisplayed);

}
*/

///////////////////////////////////////////////////////////////////////////////
//  Format the data and display it in the list ctrl

void CQuickViewMngr::PrepareListColumns(EMESSAGETYPE type)
{
	switch(type)
	{
	case	general:	{   
							for(int i = 0; i <= MAX_COLUMNS; i++)
							{
								switch(i)
								{
								case 0: {m_pQuickView->InsertColumn(i,"Logging",   LVCFMT_CENTER, 55,  NULL);break;}
								case 1: {m_pQuickView->InsertColumn(i,"File Name", LVCFMT_CENTER, 140, NULL);break;}
								case 2: {m_pQuickView->InsertColumn(i,"File Size", LVCFMT_CENTER, 100, NULL);break;}
								case 3:	{m_pQuickView->InsertColumn(i,"Version",   LVCFMT_CENTER, 75,  NULL);break;}	
								//case 4: {m_pQuickView->InsertColumn(i,"MAX File Size", LVCFMT_CENTER, 100, NULL);break;}
								//case 5: {m_pQuickView->InsertColumn(i,"MAX Overlap", LVCFMT_CENTER, 100, NULL);break;}
								}	
							}//
							break;
						}
	
	case	modules:	{   
							for(int j = 0; j <= MAX_COLUMNS; j++)
							{
								if(j == 0)
									m_pQuickView->InsertColumn(j,"Name",  LVCFMT_CENTER, 260, NULL);	
								if(j == 1)
									m_pQuickView->InsertColumn(j,"Version",  LVCFMT_CENTER, 65, NULL);
								if(j == 2)
									m_pQuickView->InsertColumn(j,"ID #",  LVCFMT_CENTER, 48, NULL);
								if(j > 2)
									break;
										
							}
							
							break;						
						}
	
	case	runlist:	{ 
							for(int k = 0; k <= MAX_COLUMNS; k++)
							{
								if(k == 0)
									m_pQuickView->InsertColumn(k,"Name",  LVCFMT_CENTER, 210, NULL);
								if(k == 1)
									m_pQuickView->InsertColumn(k,"Index",  LVCFMT_CENTER, 43, NULL);
								if(k == 2)
									m_pQuickView->InsertColumn(k,"Port",  LVCFMT_CENTER, 40, NULL);
								if(k == 3)
									m_pQuickView->InsertColumn(k,"Subsystem ID",  LVCFMT_CENTER, 80, NULL);
								if(k > 3)
									break;
							}					 
							break;
						}
	case	health:		{
							for(int j = 0; j <= MAX_COLUMNS; j++)
							{
								if(j == 0)
									m_pQuickView->InsertColumn(j,"Name",  LVCFMT_CENTER, 200, NULL);	
								if(j == 1)
									m_pQuickView->InsertColumn(j,"ID#",   LVCFMT_CENTER, 65, NULL);
								if(j == 2)
									m_pQuickView->InsertColumn(j,"Status",  LVCFMT_CENTER, 100, NULL);
								if(j > 2)
									break;			
							}
							
							break;					
						}
	//Same handler
	case	unknown:	{	break;}	
	default:			{	break;}

					 
	}

}


void CQuickViewMngr::DisplayData(EMESSAGETYPE msgType)
{	

	m_pQuickView->DeleteAllItems();

	if(currentTypeDisplayed != msgType)
	{
	
	
		for(int i=0; i < MAX_COLUMNS; i++)
			m_pQuickView->DeleteColumn(0);
		
		currentTypeDisplayed = msgType;
		PrepareListColumns(currentTypeDisplayed);
	}

	switch(msgType)
	{
	//Includes report
	case general:	{
					 CString temp;
					 
					 for(int i = 0; i<1/*QV_HISTORY*/; i++)
					 {
						if(m_generalDetails[i].fileSize == -1)
							continue;
	
						if(m_generalDetails[i].bLogging == TRUE)
						{	 
							temp.Format("ON");
							//::PostMessage(m_hNotifyWnd,LOGGING_ON,0,0); 
						}
						else
						{
							temp.Format("OFF");
							//::PostMessage(m_hNotifyWnd,LOGGING_OFF,0,0);
						}
					 
						m_pQuickView->InsertItem(i,temp); 
					 
					 
						m_pQuickView->SetItemText(i,1,m_generalDetails[i].fileName);
						temp.Format("%.3lfMB", m_generalDetails[i].fileSize);
						m_pQuickView->SetItemText(i,2,temp);
						m_pQuickView->SetItemText(i,3,m_strVersion);
						temp.Empty();
					 }

					 break;
							
					}
	
	case modules:	{

					if(m_vecModules.size() == 0)
						return;
					
					int ver,maj,min,upd; //for version 
					MODULE mod;
					CString id,version;
					for(int i=0; i<m_vecModules.size();i++)
					{
						mod = m_vecModules[i];
						m_pQuickView->InsertItem(i,mod.moduleName);
						id.Format("%d", (int)mod.moduleID);
						
						ver = mod.moduleVersion;
						maj = (int)ver;
						ver -= maj;
						min = (int)(ver*100);
						ver = (ver*100)-min;
						upd = (int)(ver*100);


						version.Format("%2d.%02d.%02d", maj,min,upd); 
						
						m_pQuickView->SetItemText(i,1,version);
						m_pQuickView->SetItemText(i,2,id);
						
					}
					break;
					}
	case runlist:	{
					//TRACE("RUNLIST SIZE = %d\r\n", m_vecRunlist.size());
					if(m_vecRunlist.size() == 0)
						return;

					SENSOR sensor;
					CString name,index,port,subID;
					for(int j = 0; j < m_vecRunlist.size(); j++)
					{
						sensor = m_vecRunlist[j];
						//TRACE("SENSOR pop = %d, ID = %d, type = %d, port = %d, media = %d, Sub ID = %d, name = %s\r\n",sensor.index,sensor.ID,sensor.sensorType,sensor.mediaPort,sensor.mediaType, sensor.subID, sensor.name);		

						m_pQuickView->InsertItem(j, sensor.name);
						
						index.Format("%d" , sensor.index);
						m_pQuickView->SetItemText(j,1,index);
						
						port.Format("%u", sensor.mediaPort);
						m_pQuickView->SetItemText(j,2,port);
						
						subID.Format("%d", sensor.subID);
						m_pQuickView->SetItemText(j,3,subID);

					}
					break;
					}
	case health:	{
						if(m_vecRunlist.size() == 0)
							return;

						SENSOR sensor;
						CString stringy;
						for(int i=0; i<m_vecRunlist.size(); i++)
						{
							sensor = m_vecRunlist[i];
							//TRACE("SENSOR pop = %d, ID = %d, type = %d, port = %d, media = %d, Sub ID = %d, name = %s\r\n",sensor.index,sensor.ID,sensor.sensorType,sensor.mediaPort,sensor.mediaType, sensor.subID, sensor.name);		

							m_pQuickView->InsertItem(i, sensor.name);
						
							stringy.Format("%d" , sensor.index);
							m_pQuickView->SetItemText(i,1,stringy);
						
							if(sensor.health == -1)
							{//this means we havn't yet updated the health
								stringy.Format("UNKNOWN");
							}
							if(sensor.health == 0)
							{
								stringy.Format("FAIL");
							}
							if(sensor.health == 1)
								stringy.Format("PASS");

							m_pQuickView->SetItemText(i,2,stringy);
						}
					}
	}
}

EMESSAGETYPE CQuickViewMngr::GetDisplayedType()
{
	return currentTypeDisplayed;
}

//MESSAGE PARSING HELPER FUNCTIONS

void CQuickViewMngr::ParseRunlistString(CString msg)
{
	SENSOR  sensor;
	CString str;

	TRACE("RUNLIST:  %s \r\n", msg);
	//First Get number of modules
	int length	= msg.GetLength();
	int pos1	= msg.Find(": ", 0);
	int pos2	= msg.Find(", Sensor =",0);
	
	CString nSensors = msg.Mid((pos1+1), (length - (length-(pos2)))-pos1-1);
	int count =  atoi((LPCSTR)nSensors);
	
	//Check valid string
	if(count == 0)
		return;

	//Hardcoded for fun
	CString moduleBuffer[50];

	//Extract Each "Sensor=..." String
	pos1 = pos2;
	for(int i = 0; i < count; i++)
	{	
		pos2 = msg.Find(", Sensor =",pos1+3);
		if(pos2 == -1)
			pos2 = length;
		str  = msg.Mid((pos1+1), pos2-pos1);
		moduleBuffer[i] = str;
		pos1 = pos2;
	}
	//DEBUG
	//for(i=0; i < count;i++)
	//{
	//	TRACE( "%s\r\n", moduleBuffer[i]);
	//}
	

	//RESET indexes
	pos1 = pos2 = 0;

	int indexBuffer[50];
	ZeroMemory(indexBuffer, sizeof(int)*25);
	
	//Extracet module info
	for(int x = 0;x < count; x++)
	{
		int  foundCommas = 0,length = 0;
		
		int		index, ID, sensorType, mediaPort, mediaType, subID ;
		CString msg, sensorName, temp;

		msg		= moduleBuffer[x];
		
		//Check if there is a msg in buffer
		if(msg.GetLength() == 0)
			continue;

		//Get msg length
		length	= msg.GetLength();

		//find start char
		pos1 = msg.Find("=");
		
		//Find comma indices for this msg
		for(int k = 0; k < msg.GetLength(); k++)
		{
			if(msg.GetAt(k) == ',')
			{
				indexBuffer[foundCommas] = k;
				//TRACE("Comma found: %d\r\n", k);
				foundCommas++;
			}
		}	
	
		//extract substrings	
		//
		//index
		pos2 = indexBuffer[0];
		temp = msg.Mid((pos1+1),pos2-pos1); 
		index = atoi((LPCSTR)temp);
		temp.Empty();

		//ID - predetermined int value 
		pos1 = pos2;
		pos2 = indexBuffer[1];
		temp = msg.Mid((pos1+1),pos2-pos1-1); 
		ID   = atoi((LPCSTR)temp);
		temp.Empty();

		//type
		pos1 = pos2;
		pos2 = indexBuffer[2];
		temp = msg.Mid((pos1+1),pos2-pos1-1); 
		sensorType = atoi((LPCSTR)temp);	
		temp.Empty();

		//port
		pos1 = pos2;
		pos2 = indexBuffer[3];
		temp = msg.Mid((pos1+1),pos2-pos1-1); 
		mediaPort = atoi((LPCSTR)temp);
		temp.Empty();

		//Media type
		pos1 = pos2;
		pos2 = indexBuffer[4];
		temp = msg.Mid((pos1+1),pos2-pos1-1); 
		mediaType = atoi((LPCSTR)temp);
		temp.Empty();

		//Subsystem ID
		pos1 = pos2;
		pos2 = indexBuffer[5];
		temp = msg.Mid((pos1+1),pos2-pos1-1); 
		subID = atoi((LPCSTR)temp);
		temp.Empty();

		//Name
		pos1 = pos2;
		pos2 = length;
		if((x+1) == count)
			temp = msg.Mid((pos1+1),pos2-pos1-1); 
		else
			temp = msg.Mid((pos1+1),pos2-pos1-2); 
		sensorName.Format("%s", temp);

		//Check
		TRACE("SENSOR index = %d, ID = %d, type = %d, port = %d, media = %d, Sub ID = %d, name = %s\r\n",index,ID,sensorType,mediaPort,mediaType, subID, sensorName);		
	
		sensor.index		= index;
		sensor.ID			= ID;
		sensor.sensorType	= sensorType;
		sensor.subID		= subID;
		sensor.mediaPort	= mediaPort;
		sensor.mediaType	= mediaType;
		sensor.name			= sensorName;
		sensor.health		= -1;

		//Associate the hardcoded ID's with the runtime ID's
		if(sensor.name.Find("Subbottom") >= 0 || sensor.name.Find("SBP") > 0)
			sensor.hardCodedID = SBPSS;
		else if(sensor.name.Find("Low") >= 0 || sensor.name.Find("LF") > 0)
			sensor.hardCodedID = LFSS;
		else if(sensor.name.Find("High") >= 0 || sensor.name.Find("HF") > 0)
			sensor.hardCodedID = HFSS;
		else if(sensor.name.Find("71") >= 0 || sensor.name.Find("7k") >0 ||  sensor.name.Find("RESON"))
			sensor.hardCodedID = SYS7K;
		if(sensor.name.Find(" Blue") >= 0 || sensor.name.Find(" BlueFin") > 0)
			sensor.hardCodedID = BLUEFIN;


		//check
		//TRACE("SENSOR index = %d, ID = %d, type = %d, port = %d, media = %d, Sub ID = %d, name = %s\r\n",sensor.index,sensor.ID,sensor.sensorType,sensor.mediaPort,sensor.mediaType, sensor.subID, sensor.name);		

		m_vecRunlist.push_back(sensor);
		
	}
}


void CQuickViewMngr::ParseModulesString(CString msg)
{
	CString str;
	MODULE  module;

	//First Get number of modules
	int length	= msg.GetLength();
	int pos1	= msg.Find(": ", 0);
	int pos2	= msg.Find(", Module =",0);
	
	CString nModules = msg.Mid((pos1+1), pos2-pos1-1);
	int count =  atoi((LPCSTR)nModules);
	
	//Check valid string
	if(count == 0)
		return;

	//Create buffer for each sub message
	CString *moduleBuffer = new CString[count];

	//Extract Each Module String
	pos1 = pos2;
	for(int i = 0; i < count; i++)
	{	
		pos2 = msg.Find(", Module =",pos1+3);
		if(pos2 == -1)
			pos2 = length;
		str  = msg.Mid((pos1+1), pos2-pos1);
		moduleBuffer[i] = str;
		pos1 = pos2;
	}
	
	//DEBUG
	//TRACE("DEBUGGING OUTPUT OF MODULES:  %s\r\n", msg);
	//for(i=0; i < count;i++)
	//{
	//	TRACE( "%s\r\n", moduleBuffer[i]);
	//}

	//RESET indexes
	pos1 = pos2 = 0;

	int *indexBuffer = new int[3];
	ZeroMemory(indexBuffer, sizeof(int)*3);
	
	//Extract module info
	for(int x = 0;x < count; x++)
	{
		int     foundCommas = 0,length = 0;
		int		ID;
		double	version;
		CString msg, moduleName, temp;

		//Get a msg
		msg		= moduleBuffer[x];

		//Get msg length
		length	= msg.GetLength();

		if(length == 0)
			continue;

		//find start char
		pos1 = msg.Find("=");
		
		//Find comma indices for this msg
		for(int k = 0; k < msg.GetLength(); k++)
		{
			if(msg.GetAt(k) == ',')
			{
				indexBuffer[foundCommas] = k;
				//TRACE("Comma found: %d\r\n", k);
				foundCommas++;
			}
		}	
		//extract substrings	
		//ID
		pos2 = indexBuffer[0];
		temp = msg.Mid((pos1+1),pos2-pos1-1); 
		ID = atoi((LPCSTR)temp);
		temp.Empty();
		//NAME
		pos1 = pos2;
		pos2 = indexBuffer[1];
		temp = msg.Mid((pos1+1),pos2-pos1-1);
		moduleName.Format("%s", temp);
		temp.Empty();
		//VERSION
		pos1 = pos2;
		pos2 = indexBuffer[2];
		if(pos2 == 0)
			pos2 = length;
		temp = msg.Mid((pos1+1),pos2-pos1); 
		version = atof(temp);
		temp.Empty();
		
		//Finally, add the module to out module list
		module.moduleID			= ID;
		module.moduleVersion	= version;
		module.moduleName		= moduleName;		
		m_vecModules.push_back(module);	

	}
	//Cleanup
	delete [] indexBuffer;
	delete [] moduleBuffer;
	indexBuffer = NULL;
	moduleBuffer = NULL;
}


void CQuickViewMngr::ParseReportString(CString msg)
{
	int length	= msg.GetLength();
	int pos1	= msg.Find("Logging");
	int pos2	= 0;

	int *indexBuffer = new int[3];

	int foundCommas = 0;

	//invalid string
	if(pos1 == -1)
		return;

//	TRACE("REPORT: %s", msg);

	pos1 = msg.Find("=");

	for(int k = 0; k < msg.GetLength(); k++)
	{
		if(msg.GetAt(k) == ',')
		{
			indexBuffer[foundCommas] = k;
			foundCommas++;
		}
	}	
	
	pos2 = indexBuffer[0];

	UINT logging = atoi(msg.Mid(pos1+1, pos2-pos1-1));

	pos1 = pos2;
	pos2 = indexBuffer[1];

	CString fileName = msg.Mid(pos1+1, pos2-pos1-1);

	pos1 = pos2;
	pos2 = length;

	float size = atof(msg.Mid(pos1+1, pos2-pos1));

	memmove(&m_generalDetails[1], &m_generalDetails[0], sizeof(QV_GENERAL)*(QV_HISTORY-1));

	m_generalDetails[0].bLogging = (BOOL)logging;
	m_generalDetails[0].fileSize = size;
	sprintf(m_generalDetails[0].fileName,"%s",fileName);

	//Only update the logging directory when we
	//get an actual string.  
	if(logging > 0)
	{
		//extract the logging directory
		char drive[_MAX_DRIVE];
		char dir[_MAX_DIR];
		char fname[_MAX_FNAME];
		char ext[_MAX_EXT];

		_splitpath( m_generalDetails[0].fileName, drive, dir, fname, ext );
		m_pTabMngr->UpdateLoggingDir(dir);
	}
	
	if(m_hNotifyWnd)
	{
		if(logging > 0 )
			::PostMessage(m_hNotifyWnd, LOGGING_ON, 0, 0);
		else
			::PostMessage(m_hNotifyWnd, LOGGING_OFF, 0, 0);
	}
	
	delete [] indexBuffer;
	indexBuffer = NULL;
}

void CQuickViewMngr::ParsePulseFiles(UINT sensorID, CString msg)
{
	int length	= msg.GetLength();
	int pos1	= msg.Find(":");
	int pos2;

	//Get number and index of commas
	int foundToken = 0;
	int indexBuffer[15];
	ZeroMemory(&indexBuffer, (sizeof(int))*15);

	for(int i = 0; i<length; i++)
	{
		if(msg.GetAt(i) == ',')
		{
			indexBuffer[foundToken] = i;
			foundToken++;
		}
	}
	
	//Get number of pulse files
	int totalFiles = 0;
	CString temp;
	temp = msg.Mid(pos1+1, indexBuffer[0]-1);
	totalFiles = atoi(temp);
//	TRACE("Total Files = %d\r\n", totalFiles);
	temp.Empty();

	//Invalid string
	if(totalFiles == 0)
		return;

	pos1 = indexBuffer[0];
	pos2 = indexBuffer[1];

	for(i=0; i < foundToken; i++)
	{
		//extract string
		temp = msg.Mid(pos1+1, pos2-pos1-1);
		
		if(temp.Find(".spf",0) == -1)
			continue;

		switch(sensorID)
		{
		case SBPSS:	{ m_vecSBPPulseFiles.push_back(temp); break;}
		case HFSS:	{ m_vecHFSSPulseFiles.push_back(temp); break;}
		case LFSS:  { m_vecLFSSPulseFiles.push_back(temp); break;}	
		}

		pos1 = pos2;
		pos2 = indexBuffer[i+2];
		if(pos2 == 0)
			pos2 = length;
	}
}

void CQuickViewMngr::ParseHealthString(CString msg)
{

	TRACE("%s\r\n",msg);

	int pos1, pos2, length;
	CString str;
	CString extract;
	int sensorID, healthVal;

	//First Get number of modules
	length	= msg.GetLength();
	pos1	= msg.Find(": ", 0);
	pos2	= msg.Find(",",0);

	CString nSensors = msg.Mid((pos1+1), (length - (length-(pos2)))-pos1-1);
	int count =  atoi((LPCSTR)nSensors);
	
	//Check valid string
	if(count == 0)
		return;

	int indexBuffer[15];
	int foundToken = 0;
	ZeroMemory(&indexBuffer, (sizeof(int))*15);

	for(int i = 0; i<length; i++)
	{
		if(msg.GetAt(i) == ',')
		{
			indexBuffer[foundToken] = i;
			foundToken++;
		}
	}
	
	int ind = 0;
	pos1 = indexBuffer[ind++];
	pos2 = indexBuffer[ind++];
	for( i=0; i<foundToken/2; i++)
	{
		extract = msg.Mid((pos1+1), (pos2 - pos1-1));
		sensorID = atoi((LPCSTR)extract);
		pos1 = pos2;
		pos2 = indexBuffer[ind++];
		if(pos2 ==0)
			pos2 = length;
		extract = msg.Mid((pos1+1), (pos2 - pos1-1));
		healthVal = atoi((LPCSTR)extract);

		pos1 = pos2;
		pos2 = indexBuffer[ind++];

	

		for(int j=0; j<m_vecRunlist.size(); j++)
		{
			if(m_vecRunlist[j].index == sensorID)
				m_vecRunlist[j].health = healthVal;
		}
	}

}

void CQuickViewMngr::SetSensorIds()
{
	int nSensors = m_vecRunlist.size();

	SENSOR *temp;

	for(int i=0; i < nSensors; i++)
	{
		//TRACE("SET SENSOR ID");
		temp = &m_vecRunlist[i];
		switch(temp->hardCodedID)
		{
		case SBPSS:  {m_pTabMngr->UpdateSensorIdx(SBPSS,	temp->index);break;}
		case LFSS:   {m_pTabMngr->UpdateSensorIdx(LFSS,		temp->index);break;}
		case HFSS:   {m_pTabMngr->UpdateSensorIdx(HFSS,		temp->index);break;}
		case SYS7K:  {m_pTabMngr->UpdateSensorIdx(SYS7K,	temp->index);break;}
		case BLUEFIN:{m_pTabMngr->UpdateSensorIdx(BLUEFIN,	temp->index);break;}
		}
	}

}

void CQuickViewMngr::UpdatePulseFiles(UINT sensorID)
{
	int size;
	switch(sensorID)
	{
	case SBPSS: { size = m_vecSBPPulseFiles.size();
				  for(int i = 0; i<size; i++)
				  {
					  m_pTabMngr->UpdatePulseFile(sensorID, m_vecSBPPulseFiles[i]);
				  }
				  break;
				}
	case LFSS: { size = m_vecLFSSPulseFiles.size();
				  for(int i = 0; i<size; i++)
				  {
						m_pTabMngr->UpdatePulseFile(sensorID, m_vecLFSSPulseFiles[i]);
				  }
				  break;
				}
	case HFSS: { size = m_vecHFSSPulseFiles.size();
				  for(int i = 0; i<size; i++)
				  {
						m_pTabMngr->UpdatePulseFile(sensorID, m_vecHFSSPulseFiles[i]);
				  }
				  break;
				}
	}
}