// DeviceDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Dashboard.h"
#include "DeviceDlg.h"
#include "DvlDlg.h"
#include "DashboardRecipient.h"
#include "DashboardDlg.h"
#include "TailconeDlg.h"
#include "AhrsDlg.h"
#include "DepthSensorDlg.h"
#include "PowerBoardDlg.h"
#include "BatteryDlg.h"
#include "HomingDlg.h"
#include "GpsDlg.h"
#include "OasDlg.h"
#include "SvsDlg.h"
#include "AvtrakDlg.h"
#include "LN250Dlg.h"
#include <remote/MMUtils.h>
#include <fstream>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define DEVICE_TIMER 1
#define DEVICE_TIMER_ELAPSE 2000

/////////////////////////////////////////////////////////////////////////////
// CDeviceDlg dialog


CDeviceDlg::CDeviceDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDeviceDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDeviceDlg)
	m_device1 = _T("");
	m_device10 = _T("");
	m_device11 = _T("");
	m_device12 = _T("");
	m_device13 = _T("");
	m_device14 = _T("");
	m_device15 = _T("");
	m_device16 = _T("");
	m_device2 = _T("");
	m_device3 = _T("");
	m_device4 = _T("");
	m_device5 = _T("");
	m_device6 = _T("");
	m_device7 = _T("");
	m_device8 = _T("");
	m_device9 = _T("");
	//}}AFX_DATA_INIT

	for(int i=0; i < 16; i++) {
		passed[i] = True;
		initPassed[i] = False;
	}

	// Init pointers
	m_recipient = NULL;
	m_tailconeDlg = NULL;
	m_ahrsDlg = NULL;
	m_depthSensorDlg = NULL;
	m_batteryDlg = NULL;
	m_dvlDlg = NULL;
	m_homingDlg = NULL;
	m_gpsDlg = NULL;
	m_powerBoardDlg = NULL;
	m_oasDlg = NULL;
	m_svsDlg = NULL;
	m_avtrakDlg = NULL;
	m_ln250Dlg = NULL;

	m_dataRequested = False;

	m_active = false;
	EnableToolTips(TRUE);
}

CDeviceDlg::~CDeviceDlg()
{
	if (m_tailconeDlg) delete m_tailconeDlg;
	if (m_ahrsDlg) delete m_ahrsDlg;
	if (m_depthSensorDlg) delete m_depthSensorDlg;
	if (m_powerBoardDlg) delete m_powerBoardDlg;
	if (m_batteryDlg) delete m_batteryDlg;
	if (m_dvlDlg) delete m_dvlDlg;
	if (m_homingDlg) delete m_homingDlg;
	if (m_gpsDlg) delete m_gpsDlg;
}

void CDeviceDlg::setRecipient(CDashboardRecipient *recipient)
{
	m_recipient = recipient;
}

BOOL CDeviceDlg::OnInitDialog()
{
	BOOL val = CDialog::OnInitDialog();
	GetWindowText(m_title);

	requestUpdate();
	SetTimer(DEVICE_TIMER,DEVICE_TIMER_ELAPSE,0);
	
	parseConfig("DeviceDlg.cfg");
	
	m_tailconeDlg = new CTailconeDlg(this);
	m_tailconeDlg->setRecipient(m_recipient);
	m_tailconeDlg->setDeviceDlg(this);
	m_recipient->addDataRecipient(m_tailconeDlg);
	m_tailconeDlg->Create(IDD_TAILCONE_DIALOG);

	m_ahrsDlg = new CAhrsDlg(this);
	m_ahrsDlg->setRecipient(m_recipient);
	m_ahrsDlg->setDeviceDlg(this);
	m_recipient->addDataRecipient(m_ahrsDlg);
	m_ahrsDlg->Create(IDD_AHRS_DIALOG);

	m_depthSensorDlg = new CDepthSensorDlg(this);
	m_depthSensorDlg->setRecipient(m_recipient);
	m_depthSensorDlg->setDeviceDlg(this);
	m_recipient->addDataRecipient(m_depthSensorDlg);
	m_depthSensorDlg->Create(IDD_DEPTHSENSOR_DIALOG);

	m_powerBoardDlg = new CPowerBoardDlg(this);
	m_powerBoardDlg->setRecipient(m_recipient);
	m_powerBoardDlg->setDeviceDlg(this);
	m_recipient->addDataRecipient(m_powerBoardDlg);
	m_powerBoardDlg->Create(IDD_POWER_DIALOG);

	m_batteryDlg = new CBatteryDlg(this);
	m_batteryDlg->setRecipient(m_recipient);
	m_batteryDlg->setDeviceDlg(this);
	m_recipient->addDataRecipient(m_batteryDlg);
	m_batteryDlg->Create(IDD_BATTERY_DIALOG);

	m_dvlDlg = new CDvlDlg(this);
	m_dvlDlg->setRecipient(m_recipient);
	m_dvlDlg->setDeviceDlg(this);
	m_recipient->addDataRecipient(m_dvlDlg);
	m_dvlDlg->Create(IDD_DVL_DIALOG);

	m_homingDlg = new CHomingDlg(this);
	m_homingDlg->setRecipient(m_recipient);
	m_homingDlg->setDeviceDlg(this);
	m_recipient->addDataRecipient(m_homingDlg);
	m_homingDlg->Create(IDD_HOMING_DIALOG);

	m_gpsDlg = new CGpsDlg(this);
	m_gpsDlg->setRecipient(m_recipient);
	m_gpsDlg->setDeviceDlg(this);
	m_recipient->addDataRecipient(m_gpsDlg);
	m_gpsDlg->Create(IDD_GPS_DIALOG);

	m_oasDlg = new COasDlg(this);
	m_oasDlg->setRecipient(m_recipient);
	m_oasDlg->setDeviceDlg(this);
	m_recipient->addDataRecipient(m_oasDlg);
	m_oasDlg->Create(IDD_OAS_DLG);

	m_svsDlg = new CSvsDlg(this);
	m_svsDlg->setRecipient(m_recipient);
	m_svsDlg->setDeviceDlg(this);
	m_recipient->addDataRecipient(m_svsDlg);
	m_svsDlg->Create(IDD_SVS_DLG);

	m_avtrakDlg = new CAvtrakDlg(this);
	m_avtrakDlg->setRecipient(m_recipient);
	m_avtrakDlg->setDeviceDlg(this);
	m_recipient->addDataRecipient(m_avtrakDlg);
	m_avtrakDlg->Create(IDD_AVTRAK_DLG);

	m_ln250Dlg = new CLN250Dlg(this);
	m_ln250Dlg->setRecipient(m_recipient);
	m_ln250Dlg->setDeviceDlg(this);
	m_recipient->addDataRecipient(m_ln250Dlg);
	m_ln250Dlg->Create(IDD_LN250_DLG);

	int i=0;
	m_dialogList[i++] = m_tailconeDlg;
	m_dialogList[i++] = m_ahrsDlg;
	m_dialogList[i++] = m_depthSensorDlg;
	m_dialogList[i++] = m_powerBoardDlg;
	m_dialogList[i++] = m_batteryDlg;
	m_dialogList[i++] = m_dvlDlg;
	m_dialogList[i++] = m_homingDlg;
	m_dialogList[i++] = m_gpsDlg;
	m_dialogList[i++] = m_oasDlg;
	m_dialogList[i++] = m_svsDlg;
	m_dialogList[i++] = m_avtrakDlg;
	m_dialogList[i++] = m_ln250Dlg;

	i=0;
	m_testList[i++] = &m_test1;
	m_testList[i++] = &m_test2;
	m_testList[i++] = &m_test3;
	m_testList[i++] = &m_test4;
	m_testList[i++] = &m_test5;
	m_testList[i++] = &m_test6;
	m_testList[i++] = &m_test7;
	m_testList[i++] = &m_test8;
	m_testList[i++] = &m_test9;
	m_testList[i++] = &m_test10;
	m_testList[i++] = &m_test11;
	m_testList[i++] = &m_test12;
	m_testList[i++] = &m_test13;
	m_testList[i++] = &m_test14;
	m_testList[i++] = &m_test15;
	m_testList[i++] = &m_test16;

	i=0;
	m_startList[i++] = &m_start1;
	m_startList[i++] = &m_start2;
	m_startList[i++] = &m_start3;
	m_startList[i++] = &m_start4;
	m_startList[i++] = &m_start5;
	m_startList[i++] = &m_start6;
	m_startList[i++] = &m_start7;
	m_startList[i++] = &m_start8;
	m_startList[i++] = &m_start9;
	m_startList[i++] = &m_start10;
	m_startList[i++] = &m_start11;
	m_startList[i++] = &m_start12;
	m_startList[i++] = &m_start13;
	m_startList[i++] = &m_start14;
	m_startList[i++] = &m_start15;
	m_startList[i++] = &m_start16;

	i=0;
	m_stopList[i++] = &m_stop1;
	m_stopList[i++] = &m_stop2;
	m_stopList[i++] = &m_stop3;
	m_stopList[i++] = &m_stop4;
	m_stopList[i++] = &m_stop5;
	m_stopList[i++] = &m_stop6;
	m_stopList[i++] = &m_stop7;
	m_stopList[i++] = &m_stop8;
	m_stopList[i++] = &m_stop9;
	m_stopList[i++] = &m_stop10;
	m_stopList[i++] = &m_stop11;
	m_stopList[i++] = &m_stop12;
	m_stopList[i++] = &m_stop13;
	m_stopList[i++] = &m_stop14;
	m_stopList[i++] = &m_stop15;
	m_stopList[i++] = &m_stop16;

	i=0;
	m_nameList[i++] = &m_device1;
	m_nameList[i++] = &m_device2;
	m_nameList[i++] = &m_device3;
	m_nameList[i++] = &m_device4;
	m_nameList[i++] = &m_device5;
	m_nameList[i++] = &m_device6;
	m_nameList[i++] = &m_device7;
	m_nameList[i++] = &m_device8;
	m_nameList[i++] = &m_device9;
	m_nameList[i++] = &m_device10;
	m_nameList[i++] = &m_device11;
	m_nameList[i++] = &m_device12;
	m_nameList[i++] = &m_device13;
	m_nameList[i++] = &m_device14;
	m_nameList[i++] = &m_device15;
	m_nameList[i++] = &m_device16;

	for(i=0; i < NUM_PORTS; i++) {
		CStatusStatic * stat = new CStatusStatic();
		stat->SubclassDlgItem(IDC_FULLSTATUS1 + i, this);
		m_statusList[i] = stat;
		(m_statusList[i])->setDeviceStatus(-1);
	}

	m_active = true;
	return val;
}

void CDeviceDlg::resetDialog()
{
	//UpdateData(TRUE);

	for(int i=0; i < NUM_PORTS; i++) {
		m_nameList[i]->Empty();
		m_testList[i]->EnableWindow(false);
		m_startList[i]->EnableWindow(false);
		m_stopList[i]->EnableWindow(false);
		(m_statusList[i])->setDeviceStatus(-1);
	}
	m_dd->setDevicesOK(False);
	UpdateData(FALSE);
}

void CDeviceDlg::requestUpdate()
{
	Assert(m_recipient);
	m_recipient->requestData(MMAgentRequest::Start,
		ModemMessage::tMMDeviceInfo,5);
	m_recipient->requestData(MMAgentRequest::Start,
		ModemMessage::tMMDriver, 2);
	m_dataRequested = True;
}

void CDeviceDlg::stopRequests()
{
	Assert(m_recipient);
	m_recipient->requestData(MMAgentRequest::Stop,
		ModemMessage::tMMDeviceInfo,-1);
	m_recipient->requestData(MMAgentRequest::Stop,
		ModemMessage::tMMDriver, -1);

	resetDialog();
	m_dataRequested = False;
}

void CDeviceDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDeviceDlg)
	DDX_Control(pDX, IDC_TEST9, m_test9);
	DDX_Control(pDX, IDC_TEST8, m_test8);
	DDX_Control(pDX, IDC_TEST7, m_test7);
	DDX_Control(pDX, IDC_TEST6, m_test6);
	DDX_Control(pDX, IDC_TEST5, m_test5);
	DDX_Control(pDX, IDC_TEST4, m_test4);
	DDX_Control(pDX, IDC_TEST3, m_test3);
	DDX_Control(pDX, IDC_TEST2, m_test2);
	DDX_Control(pDX, IDC_TEST16, m_test16);
	DDX_Control(pDX, IDC_TEST15, m_test15);
	DDX_Control(pDX, IDC_TEST14, m_test14);
	DDX_Control(pDX, IDC_TEST13, m_test13);
	DDX_Control(pDX, IDC_TEST12, m_test12);
	DDX_Control(pDX, IDC_TEST11, m_test11);
	DDX_Control(pDX, IDC_TEST10, m_test10);
	DDX_Control(pDX, IDC_TEST1, m_test1);
	DDX_Control(pDX, IDC_START9, m_start9);
	DDX_Control(pDX, IDC_START8, m_start8);
	DDX_Control(pDX, IDC_START7, m_start7);
	DDX_Control(pDX, IDC_START6, m_start6);
	DDX_Control(pDX, IDC_START5, m_start5);
	DDX_Control(pDX, IDC_START4, m_start4);
	DDX_Control(pDX, IDC_START3, m_start3);
	DDX_Control(pDX, IDC_START2, m_start2);
	DDX_Control(pDX, IDC_START16, m_start16);
	DDX_Control(pDX, IDC_START15, m_start15);
	DDX_Control(pDX, IDC_START14, m_start14);
	DDX_Control(pDX, IDC_START13, m_start13);
	DDX_Control(pDX, IDC_START12, m_start12);
	DDX_Control(pDX, IDC_START11, m_start11);
	DDX_Control(pDX, IDC_START10, m_start10);
	DDX_Control(pDX, IDC_START1, m_start1);
	DDX_Control(pDX, IDC_STOP9, m_stop9);
	DDX_Control(pDX, IDC_STOP8, m_stop8);
	DDX_Control(pDX, IDC_STOP7, m_stop7);
	DDX_Control(pDX, IDC_STOP6, m_stop6);
	DDX_Control(pDX, IDC_STOP5, m_stop5);
	DDX_Control(pDX, IDC_STOP4, m_stop4);
	DDX_Control(pDX, IDC_STOP3, m_stop3);
	DDX_Control(pDX, IDC_STOP2, m_stop2);
	DDX_Control(pDX, IDC_STOP16, m_stop16);
	DDX_Control(pDX, IDC_STOP15, m_stop15);
	DDX_Control(pDX, IDC_STOP14, m_stop14);
	DDX_Control(pDX, IDC_STOP13, m_stop13);
	DDX_Control(pDX, IDC_STOP12, m_stop12);
	DDX_Control(pDX, IDC_STOP11, m_stop11);
	DDX_Control(pDX, IDC_STOP10, m_stop10);
	DDX_Control(pDX, IDC_STOP1, m_stop1);
	DDX_Text(pDX, IDC_DEVICE1, m_device1);
	DDX_Text(pDX, IDC_DEVICE10, m_device10);
	DDX_Text(pDX, IDC_DEVICE11, m_device11);
	DDX_Text(pDX, IDC_DEVICE12, m_device12);
	DDX_Text(pDX, IDC_DEVICE13, m_device13);
	DDX_Text(pDX, IDC_DEVICE14, m_device14);
	DDX_Text(pDX, IDC_DEVICE15, m_device15);
	DDX_Text(pDX, IDC_DEVICE16, m_device16);
	DDX_Text(pDX, IDC_DEVICE2, m_device2);
	DDX_Text(pDX, IDC_DEVICE3, m_device3);
	DDX_Text(pDX, IDC_DEVICE4, m_device4);
	DDX_Text(pDX, IDC_DEVICE5, m_device5);
	DDX_Text(pDX, IDC_DEVICE6, m_device6);
	DDX_Text(pDX, IDC_DEVICE7, m_device7);
	DDX_Text(pDX, IDC_DEVICE8, m_device8);
	DDX_Text(pDX, IDC_DEVICE9, m_device9);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDeviceDlg, CDialog)
	//{{AFX_MSG_MAP(CDeviceDlg)
	ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
	ON_BN_CLICKED(IDC_TEST1, OnTest1)
	ON_BN_CLICKED(IDC_TEST2, OnTest2)
	ON_BN_CLICKED(IDC_TEST3, OnTest3)
	ON_BN_CLICKED(IDC_TEST4, OnTest4)
	ON_BN_CLICKED(IDC_TEST5, OnTest5)
	ON_BN_CLICKED(IDC_TEST6, OnTest6)
	ON_BN_CLICKED(IDC_TEST7, OnTest7)
	ON_BN_CLICKED(IDC_TEST8, OnTest8)
	ON_BN_CLICKED(IDC_TEST9, OnTest9)
	ON_BN_CLICKED(IDC_TEST10, OnTest10)
	ON_BN_CLICKED(IDC_TEST11, OnTest11)
	ON_BN_CLICKED(IDC_TEST12, OnTest12)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_TEST13, OnTest13)
	ON_BN_CLICKED(IDC_TEST14, OnTest14)
	ON_BN_CLICKED(IDC_TEST15, OnTest15)
	ON_BN_CLICKED(IDC_TEST16, OnTest16)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_START1, OnStart1)
	ON_BN_CLICKED(IDC_START2, OnStart2)
	ON_BN_CLICKED(IDC_START3, OnStart3)
	ON_BN_CLICKED(IDC_START4, OnStart4)
	ON_BN_CLICKED(IDC_START5, OnStart5)
	ON_BN_CLICKED(IDC_START6, OnStart6)
	ON_BN_CLICKED(IDC_START7, OnStart7)
	ON_BN_CLICKED(IDC_START8, OnStart8)
	ON_BN_CLICKED(IDC_START9, OnStart9)
	ON_BN_CLICKED(IDC_START10, OnStart10)
	ON_BN_CLICKED(IDC_START11, OnStart11)
	ON_BN_CLICKED(IDC_START12, OnStart12)
	ON_BN_CLICKED(IDC_START13, OnStart13)
	ON_BN_CLICKED(IDC_START14, OnStart14)
	ON_BN_CLICKED(IDC_START15, OnStart15)
	ON_BN_CLICKED(IDC_START16, OnStart16)
	ON_BN_CLICKED(IDC_STOP1, OnStop1)
	ON_BN_CLICKED(IDC_STOP2, OnStop2)
	ON_BN_CLICKED(IDC_STOP3, OnStop3)
	ON_BN_CLICKED(IDC_STOP4, OnStop4)
	ON_BN_CLICKED(IDC_STOP5, OnStop5)
	ON_BN_CLICKED(IDC_STOP6, OnStop6)
	ON_BN_CLICKED(IDC_STOP7, OnStop7)
	ON_BN_CLICKED(IDC_STOP8, OnStop8)
	ON_BN_CLICKED(IDC_STOP9, OnStop9)
	ON_BN_CLICKED(IDC_STOP10, OnStop10)
	ON_BN_CLICKED(IDC_STOP11, OnStop11)
	ON_BN_CLICKED(IDC_STOP12, OnStop12)
	ON_BN_CLICKED(IDC_STOP13, OnStop13)
	ON_BN_CLICKED(IDC_STOP14, OnStop14)
	ON_BN_CLICKED(IDC_STOP15, OnStop15)
	ON_BN_CLICKED(IDC_STOP16, OnStop16)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDeviceDlg message handlers

BEGIN_EVENTSINK_MAP(CDeviceDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CDeviceDlg)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

BOOL CDeviceDlg::OnToolTipNotify(UINT id, NMHDR * pTTTStruct, LRESULT * pResult)
{
	//char str[80];
	UINT nID = ::GetDlgCtrlID((HWND)pTTTStruct->idFrom);
	switch (nID) {
	case (IDC_TEST1):
		break;
	case (IDC_TEST2):	
		break;
	case (IDC_TEST3):	
		break;
	case (IDC_TEST4):
		break;
	case (IDC_TEST5):
		break;
	case (IDC_TEST6):
		break;
	case (IDC_TEST7):
		break;
	case (IDC_TEST8):
		break;
	case (IDC_TEST9):
		break;
	case (IDC_TEST10):
		break;
	case (IDC_TEST11):
		break;
	case (IDC_TEST12):
		break;
	case (IDOK):
		((NMTTDISPINFO *)pTTTStruct)->lpszText = "Return to Dashboard";	
		break;
	case (IDC_UPDATE):
		((NMTTDISPINFO *)pTTTStruct)->lpszText = "Refresh the device list";	
		break;

	}
	return TRUE;
}

void CDeviceDlg::parseConfig(CString fname)
{
	char lineBuffer[256];
	int cur = -1;
	int t;

	CString path = m_homeDir+"\\"+fname;

	fstream configFile;
	configFile.open(path.GetBuffer(0), ios::in);

	while(configFile.eof() != 1) {
		configFile.getline(lineBuffer, 256);
		CString cbuf = lineBuffer;

		//for now, simple: #dialog means dialog
		//better not be anything else on that line
		if(cbuf.IsEmpty()) continue;
		if((t = cbuf.Find("#dialog")) != -1){
			cbuf = cbuf.Right(cbuf.GetLength() - (t+7) -1);
			cbuf.TrimRight();
			cbuf.TrimLeft();
			SingleDia* singleDia = new SingleDia();
			singleDia->name = cbuf;
			diaInfo.Add(singleDia);
			cur++;
		} else {
			cbuf.TrimLeft();
			cbuf.TrimRight();
			if(cbuf.Find('!') != -1) {
				cbuf.Remove('!');
				diaInfo[cur]->exStart = diaInfo[cur]->serverNames.GetSize(); //cause we are about to add it no -1
			}
			diaInfo[cur]->serverNames.Add(cbuf);
		}
	}
}

int CDeviceDlg::serverNameFound(CString dName, CString sName)
{
	int i,j;

	for(i = 0; i < diaInfo.GetSize(); i++){
		if(dName == diaInfo[i]->name){
			for(j = 0; j < diaInfo[i]->serverNames.GetSize(); j++) {
				if(diaInfo[i]->serverNames[j] == sName)
					return i;
			}
		}
	}
	return -1;
}

int CDeviceDlg::diaNameIndex(CString dName)
{
	for(int i = 0; i < diaInfo.GetSize(); i++){
		if(dName == diaInfo[i]->name)
			return i;
	}

	return -1;
}

int CDeviceDlg::findDialogNameFromDeviceName(CString sName, char * dName)
{
	for(int i = 0; i < diaInfo.GetSize(); i++){
		for(int j = 0; j < diaInfo[i]->serverNames.GetSize(); j++) {
			if(diaInfo[i]->serverNames[j] == sName) {
				strcpy(dName, diaInfo[i]->name);
				return i;
			}
		}
	}
	return -1;

}


void CDeviceDlg::setDevice(int port, const char *name, int circuit)
{
	if(!m_dataRequested) return;
	receiveData();
	UpdateData(TRUE);

	char strTemp[64];

	//TRACE("Port: %d Name: %s Circuit: %d\n", port, name, circuit);

	if (strcmp(m_nameList[port-1]->GetBuffer(0), name) != 0) {
		m_circuitList[port-1] = circuit;
		(*(m_nameList[port-1])) = name;
		if (findDialogNameFromDeviceName(name,strTemp) != -1) {
			m_testList[port-1]->EnableWindow(true);
			m_startList[port-1]->EnableWindow(true);
			m_stopList[port-1]->EnableWindow(true);
		} else {
			m_testList[port-1]->EnableWindow(false);
			m_startList[port-1]->EnableWindow(false);
			m_stopList[port-1]->EnableWindow(false);
		}
	}

	UpdateData(FALSE);
}

void CDeviceDlg::setDriverStatus(char *name, int status)
{
	
	//TRACE("DriverStatus: %s %d\n",name, status);
	
	if (m_active) {
		for(int i=0;i<NUM_PORTS;i++){ 
			if (strcmp(m_nameList[i]->GetBuffer(0),name) == 0) {
				(m_statusList[i])->setDeviceStatus(status);
				return;
			}
		}
	}
}

void CDeviceDlg::launchTest(int portNum)
{

	char * device = m_nameList[portNum-1]->GetBuffer(0);
	int circNum = m_circuitList[portNum -1];
	// Check for null string
	if (!device || strlen(device) == 0) {
		MessageBox("No device.");
		return;
	}
	char dTemp[64];
	int i = findDialogNameFromDeviceName(device, dTemp);
	if (i >= 0) {
		m_dialogList[i]->setDriverName(device,portNum);
		if (circNum>=0)  m_recipient->setCircuit(circNum, True);
		m_recipient->setDriverCommand(device, true);
		m_dialogList[i]->ShowWindow(SW_SHOWNORMAL);
		m_dialogList[i]->requestUpdate();
	} else {
		char msg[128];
		sprintf(msg,"No test program for %s",device);
		MessageBox(msg);
	}
}


void CDeviceDlg::OnUpdate() 
{
	// TODO: Add your control notification handler code here
	requestUpdate();
}

void CDeviceDlg::OnTest1() 
{
	launchTest(1);
}

void CDeviceDlg::OnTest2() 
{
	launchTest(2);
}

void CDeviceDlg::OnTest3() 
{
	launchTest(3);
}

void CDeviceDlg::OnTest4() 
{
	launchTest(4);
}

void CDeviceDlg::OnTest5() 
{
	launchTest(5);
}

void CDeviceDlg::OnTest6() 
{
	launchTest(6);
}

void CDeviceDlg::OnTest7() 
{
	launchTest(7);
}

void CDeviceDlg::OnTest8() 
{
	launchTest(8);
}

void CDeviceDlg::OnTest9() 
{
	launchTest(9);
}

void CDeviceDlg::OnTest10() 
{
	launchTest(10);
}

void CDeviceDlg::OnTest11() 
{
	launchTest(11);
}

void CDeviceDlg::OnTest12() 
{
	launchTest(12);
}
void CDeviceDlg::OnTest13() 
{
	launchTest(13);
}

void CDeviceDlg::OnTest14() 
{
	launchTest(14);	
}

void CDeviceDlg::OnTest15() 
{
	launchTest(15);	
}

void CDeviceDlg::OnTest16() 
{
	launchTest(16);
}

void CDeviceDlg::OnOK() 
{
	// TODO: Add extra validation here
	stopRequests();
	CDialog::OnOK();
}

void CDeviceDlg::receiveData()
{
	SetWindowText(m_title + " Received New Data...");
}

void CDeviceDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CDialog::OnTimer(nIDEvent);
	SetWindowText(m_title);
}


void CDeviceDlg::OnClose() 
{
	stopRequests();	
	CDialog::OnClose();
}


void CDeviceDlg::closeAllTests()
{
	//note that none of these actually destroy the window, just reset it
	if(m_tailconeDlg) {
		m_tailconeDlg->stopRequests();
		m_tailconeDlg->EndDialog(0);
	}
	if(m_ahrsDlg) {
		m_ahrsDlg->EndDialog(0);
		m_ahrsDlg->stopRequests();
	}
	if(m_depthSensorDlg) {
		m_depthSensorDlg->EndDialog(0);
		m_depthSensorDlg->stopRequests();
	}
	if(m_powerBoardDlg) {
		m_powerBoardDlg->EndDialog(0);
		m_powerBoardDlg->stopRequests();
	}
	if(m_batteryDlg) {
		m_batteryDlg->EndDialog(0);
		m_batteryDlg->stopRequests();
	}
	if(m_dvlDlg) {
		m_dvlDlg->EndDialog(0);
		m_dvlDlg->stopRequests();
	}
	if(m_homingDlg) {
		m_homingDlg->EndDialog(0);
		m_homingDlg->stopRequests();
	}
	if(m_gpsDlg) {
		m_gpsDlg->EndDialog(0);
		m_gpsDlg->stopRequests();
	}
	if(m_oasDlg) {
		m_oasDlg->EndDialog(0);
		m_oasDlg->stopRequests();
	}
	if(m_svsDlg) {
		m_svsDlg->EndDialog(0);
		m_svsDlg->stopRequests();
	}
	if(m_avtrakDlg) {
		m_avtrakDlg->EndDialog(0);
		//doesn't currently make requests
		//m_avtrakDlg->stopRequests();
	}
	if(m_ln250Dlg) {
		m_ln250Dlg->EndDialog(0);
		m_ln250Dlg->stopRequests();
	}
}


void CDeviceDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	resetDialog();
}

void CDeviceDlg::StartDriver(int portNum)
{
	char * device = m_nameList[portNum-1]->GetBuffer(0);
	int circNum = m_circuitList[portNum -1];
	// Check for null string
	if (!device || strlen(device) == 0) {
		MessageBox("No device.");
		return;
	}
	char dTemp[64];
	int i = findDialogNameFromDeviceName(device, dTemp);
	if (i >= 0) {
		m_dialogList[i]->setDriverName(device,portNum);
		if (circNum>=0)  m_recipient->setCircuit(circNum, True);
		m_recipient->setDriverCommand(device, true);
	} else {
		char msg[128];
		sprintf(msg,"No test program for %s",device);
		MessageBox(msg);
	}
}

void CDeviceDlg::OnStart1() 
{
	StartDriver(1);
}

void CDeviceDlg::OnStart2() 
{
	StartDriver(2);
}

void CDeviceDlg::OnStart3() 
{
	StartDriver(3);
}

void CDeviceDlg::OnStart4() 
{
	StartDriver(4);
}

void CDeviceDlg::OnStart5() 
{
	StartDriver(5);
}

void CDeviceDlg::OnStart6() 
{
	StartDriver(6);
}

void CDeviceDlg::OnStart7() 
{
	StartDriver(7);
}

void CDeviceDlg::OnStart8() 
{
	StartDriver(8);
}

void CDeviceDlg::OnStart9() 
{
	StartDriver(9);
}

void CDeviceDlg::OnStart10() 
{
	StartDriver(10);
}

void CDeviceDlg::OnStart11() 
{
	StartDriver(11);
}

void CDeviceDlg::OnStart12() 
{
	StartDriver(12);
}

void CDeviceDlg::OnStart13() 
{
	StartDriver(13);
}

void CDeviceDlg::OnStart14() 
{
	StartDriver(14);
}

void CDeviceDlg::OnStart15() 
{
	StartDriver(15);
}

void CDeviceDlg::OnStart16() 
{
	StartDriver(16);
}


void CDeviceDlg::StopDriver(int portNum)
{
	char * device = m_nameList[portNum-1]->GetBuffer(0);
	int circNum = m_circuitList[portNum -1];
	// Check for null string
	if (!device || strlen(device) == 0) {
		MessageBox("No device.");
		return;
	}
	char dTemp[64];
	int i = findDialogNameFromDeviceName(device, dTemp);
	if (i >= 0) {
		m_recipient->setDriverCommand(device, false);
		m_dialogList[i]->stopRequests();
	} else {
		char msg[128];
		sprintf(msg,"No test program for %s",device);
		MessageBox(msg);
	}
}

void CDeviceDlg::OnStop1() 
{
	StopDriver(1);
}

void CDeviceDlg::OnStop2() 
{
	StopDriver(2);
}

void CDeviceDlg::OnStop3() 
{
	StopDriver(3);
}

void CDeviceDlg::OnStop4() 
{
	StopDriver(4);
}

void CDeviceDlg::OnStop5() 
{
	StopDriver(5);
}

void CDeviceDlg::OnStop6() 
{
	StopDriver(6);
}

void CDeviceDlg::OnStop7() 
{
	StopDriver(7);
}

void CDeviceDlg::OnStop8() 
{
	StopDriver(8);
}

void CDeviceDlg::OnStop9() 
{
	StopDriver(9);
}

void CDeviceDlg::OnStop10() 
{
	StopDriver(10);
}

void CDeviceDlg::OnStop11() 
{
	StopDriver(11);	
}

void CDeviceDlg::OnStop12() 
{
	StopDriver(12);
}

void CDeviceDlg::OnStop13() 
{
	StopDriver(13);
}

void CDeviceDlg::OnStop14() 
{
	StopDriver(14);
}

void CDeviceDlg::OnStop15() 
{
	StopDriver(15);
}

void CDeviceDlg::OnStop16() 
{
	StopDriver(16);
}
