// ExceptView.cpp : implementation file
//

#include "stdafx.h"
#include "ctms.h"
#include "ExceptView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CExceptView

IMPLEMENT_DYNCREATE(CExceptView, CFormView)

CExceptView::CExceptView()
	: CFormView(CExceptView::IDD)
{
	//{{AFX_DATA_INIT(CExceptView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CExceptView::~CExceptView()
{
}

void CExceptView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExceptView)
	DDX_Control(pDX, IDC_YLED, m_LEDyellow);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CExceptView, CFormView)
	//{{AFX_MSG_MAP(CExceptView)
	ON_BN_CLICKED(IDC_SEND1, OnSend)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExceptView diagnostics

#ifdef _DEBUG
void CExceptView::AssertValid() const
{
	CFormView::AssertValid();
}

void CExceptView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CExceptView message handlers

void CExceptView::OnInitialUpdate() 
{
  CFormView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
  ((CComboBox *)GetDlgItem(IDC_LEVEL))->SetCurSel( 0);

  CWnd *pWndLED = (CWnd *)GetDlgItem(IDC_YLED);
  m_LEDyellow.Blinking( 0);
  m_LEDyellow.SetLED(pWndLED,ID_LED_YELLOW, ID_SHAPE_ROUND, 0);

  GetDlgItem(IDC_SEND1)->EnableWindow( FALSE);

  UpdateView();

    //create 500 msec timer event
  m_iTimerId = SetTimer( TIMER_TDI, TIME_TDI, NULL);
}

void CExceptView::UpdateView()
{

  int idx = ((CCTMSApp *)AfxGetApp())->GetTMIndex();
  CTM *tm = ((CCTMSApp *)AfxGetApp())->GetTM(idx);

  //we just need a tm
  if (!tm) return;

  //lifesign registration
  if (tm->IsConnected())
  {

	GetDlgItem(IDC_SEND1)->EnableWindow();
    m_LEDyellow.SwitchOn();
  }
  else
  {

	GetDlgItem(IDC_SEND1)->EnableWindow( FALSE);
    m_LEDyellow.SwitchOff();
  }
} //UpdateView


void CExceptView::OnSend() 
{
	// TODO: Add your control notification handler code here
  int idx = ((CCTMSApp *)AfxGetApp())->GetTMIndex();
  CTM *tm = ((CCTMSApp *)AfxGetApp())->GetTM(idx);
  char code[80];
  char desc[80];
  int level = 1;

  if (!tm) return;
  if (!tm->IsConnected()) return;
  
  memset( (void *)code, (int)' ', sizeof(code));
  memset( (void *)desc, (int)' ', sizeof(desc));
  //strcpy( 

  level = ((CComboBox *)GetDlgItem(IDC_LEVEL))->GetCurSel() + 1;

  GetDlgItem(IDC_EQUIP)->GetWindowText( code, 17);
  GetDlgItem(IDC_DESC)->GetWindowText( desc, 51);
  code[ strlen(code)] = ' ';
  desc[ strlen(desc)] = ' ';

  tm->SendException( code, level, desc);

  tm->GetDateTime( code);
  GetDlgItem(IDC_DATETIME)->SetWindowText( code);
  //GetDlgItem( IDC_STATIC)->SetWindowText( code);
}

void CExceptView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
    if (nIDEvent == TIMER_TDI) 
      UpdateView();	
	
	CFormView::OnTimer(nIDEvent);
}
