

#include "stdafx.h"
#include "iMatic16.h"
#include "iMatic16Dlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif




class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();


	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);   


protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()






CiMatic16Dlg::CiMatic16Dlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CiMatic16Dlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void CiMatic16Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CiMatic16Dlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_MESSAGE(MV_MESSAGE_FINSH_CONNECT_SOCK,&CiMatic16Dlg::FinishConnectServer)
	ON_MESSAGE(MV_MESSAGE_FINSH_SEND_COMMAND,&CiMatic16Dlg::FinishSendCommand)
	ON_BN_CLICKED(IDC_CONNECT, &CiMatic16Dlg::OnBnClickedConnect)
	ON_BN_CLICKED(IDC_RELAY1, &CiMatic16Dlg::OnBnClickedRelay1)
	ON_BN_CLICKED(IDC_RELAY2, &CiMatic16Dlg::OnBnClickedRelay2)
	ON_BN_CLICKED(IDC_RELAY3, &CiMatic16Dlg::OnBnClickedRelay3)
	ON_BN_CLICKED(IDC_RELAY4, &CiMatic16Dlg::OnBnClickedRelay4)
	ON_BN_CLICKED(IDC_RELAY5, &CiMatic16Dlg::OnBnClickedRelay5)
	ON_BN_CLICKED(IDC_RELAY15, &CiMatic16Dlg::OnBnClickedRelay15)
	ON_BN_CLICKED(IDC_RELAY6, &CiMatic16Dlg::OnBnClickedRelay6)
	ON_BN_CLICKED(IDC_RELAY7, &CiMatic16Dlg::OnBnClickedRelay7)
	ON_BN_CLICKED(IDC_RELAY8, &CiMatic16Dlg::OnBnClickedRelay8)
	ON_BN_CLICKED(IDC_RELAY9, &CiMatic16Dlg::OnBnClickedRelay9)
	ON_BN_CLICKED(IDC_RELAY10, &CiMatic16Dlg::OnBnClickedRelay10)
	ON_BN_CLICKED(IDC_RELAY11, &CiMatic16Dlg::OnBnClickedRelay11)
	ON_BN_CLICKED(IDC_RELAY12, &CiMatic16Dlg::OnBnClickedRelay12)
	ON_BN_CLICKED(IDC_RELAY13, &CiMatic16Dlg::OnBnClickedRelay13)
	ON_BN_CLICKED(IDC_RELAY14, &CiMatic16Dlg::OnBnClickedRelay14)
	ON_BN_CLICKED(IDC_RELAY16, &CiMatic16Dlg::OnBnClickedRelay16)
	ON_BN_CLICKED(IDC_RELAYALL, &CiMatic16Dlg::OnBnClickedRelayall)
	ON_BN_CLICKED(IDC_REFRESH, &CiMatic16Dlg::OnBnClickedRefresh)
END_MESSAGE_MAP()




BOOL CiMatic16Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}


	SetIcon(m_hIcon, TRUE);			
	SetIcon(m_hIcon, FALSE);		

	ShowWindow(SW_MINIMIZE);


	isConnecting=false;
	isConneced=false;
	test=new iMaticClientSocket();	
	this->GetDlgItem(IDC_PORT)->SetWindowTextW(L"3000");
	this->GetDlgItem(IDC_IP)->SetWindowTextW(L"192.168.1.4");
	return TRUE;  
}

void CiMatic16Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}
void CiMatic16Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); 

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;


		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}


HCURSOR CiMatic16Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CiMatic16Dlg::OnBnClickedConnect()
{
	CString str;
	this->GetDlgItem(IDC_CONNECT)->GetWindowText(str);
	if(str.Compare(L"Connect")==0)
	{
		test->ConnectSocket(16,MV_MESSAGE_FINSH_CONNECT_SOCK,MV_MESSAGE_FINSH_SEND_COMMAND);
	}else
	{
		test->CloseSoket();
		UpdateData(false);
		this->GetDlgItem(IDC_CONNECT)->SetWindowTextW(L"Connect");
		UpdateData(true);
	}
	
}

LRESULT CiMatic16Dlg::FinishConnectServer(WPARAM wParam,LPARAM lparam)
{
	isConnecting=false;
	if(wParam==1)
	{
		UpdateData(false);
		this->GetDlgItem(IDC_CONNECT)->SetWindowTextW(L"Close");
		::MessageBox(NULL, L"Connet sucessful.", L"Message", MB_SYSTEMMODAL);
		UpdateData(true);
		
	}else
	{
		::MessageBox(NULL, L"Connet fail", L"Message", MB_SYSTEMMODAL);
	}
	return 1L;
}

void CiMatic16Dlg::SendCommand(int type,int number,int stuate)
{
	if(!test->SendCommand(type,number,stuate))
		::MessageBox(NULL, L"Send fail", L"Message", MB_SYSTEMMODAL);
}

LRESULT CiMatic16Dlg::FinishSendCommand(WPARAM wParam,LPARAM lparam)
{
	UpdateData(false);
	int relay_ids[16]={IDC_RELAY1,IDC_RELAY2,IDC_RELAY3,IDC_RELAY4,IDC_RELAY5,IDC_RELAY6,IDC_RELAY7
			,IDC_RELAY8,IDC_RELAY9,IDC_RELAY10,IDC_RELAY11,IDC_RELAY12,IDC_RELAY13,IDC_RELAY14,IDC_RELAY15,IDC_RELAY16};
	int *reslut=(int*)lparam;
	if(reslut[0]==16)
	{
		
		int number=0;
		for(int i=0;i<16;i++)
		{
			CString t=L"";
			t.Format(L"%d",i+1);
			if(reslut[i+2]==0)
			{
				number--;
				this->GetDlgItem(relay_ids[i])->SetWindowText(L"Relay-"+t+" Off");
			}else
			{
				number++;
				this->GetDlgItem(relay_ids[i])->SetWindowText(L"Relay-"+t+" On");
			}
		}
		if(number==16)
		{
			this->GetDlgItem(IDC_RELAYALL)->SetWindowText(L"Relay-All On");
		}else
		{
			this->GetDlgItem(IDC_RELAYALL)->SetWindowText(L"Relay-All Off");
		}
	}
	UpdateData(true);
	return 1L;
}

void CiMatic16Dlg::OnBnClickedRelay1()
{
	
	CString str;
	this->GetDlgItem(IDC_RELAY1)->GetWindowText(str);
	if(str.Compare(L"Relay-1 On")==0)
	{
		SendCommand(16,1,0);
	}else
	{
		SendCommand(16,1,1);
	}
	
}


void CiMatic16Dlg::OnBnClickedRelay2()
{
	CString str;
	this->GetDlgItem(IDC_RELAY2)->GetWindowText(str);
	if(str.Compare(L"Relay-2 On")==0)
	{
		SendCommand(16,2,0);
	}else
	{
		SendCommand(16,2,1);
	}
	
}


void CiMatic16Dlg::OnBnClickedRelay3()
{
	CString str;
	this->GetDlgItem(IDC_RELAY3)->GetWindowText(str);
	if(str.Compare(L"Relay-3 On")==0)
	{
		SendCommand(16,3,0);
	}else
	{
		SendCommand(16,3,1);
	}
}


void CiMatic16Dlg::OnBnClickedRelay4()
{
	CString str;
	this->GetDlgItem(IDC_RELAY4)->GetWindowText(str);
	if(str.Compare(L"Relay-4 On")==0)
	{
		SendCommand(16,4,0);
	}else
	{
		SendCommand(16,4,1);
	}
}


void CiMatic16Dlg::OnBnClickedRelay5()
{
	CString str;
	this->GetDlgItem(IDC_RELAY5)->GetWindowText(str);
	if(str.Compare(L"Relay-5 On")==0)
	{
		SendCommand(16,5,0);
	}else
	{
		SendCommand(16,5,1);
	}
}


void CiMatic16Dlg::OnBnClickedRelay6()
{
	CString str;
	this->GetDlgItem(IDC_RELAY6)->GetWindowText(str);
	if(str.Compare(L"Relay-6 On")==0)
	{
		SendCommand(16,6,0);
	}else
	{
		SendCommand(16,6,1);
	}
}


void CiMatic16Dlg::OnBnClickedRelay7()
{
	CString str;
	this->GetDlgItem(IDC_RELAY7)->GetWindowText(str);
	if(str.Compare(L"Relay-7 On")==0)
	{
		SendCommand(16,7,0);
	}else
	{
		SendCommand(16,7,1);
	}
}


void CiMatic16Dlg::OnBnClickedRelay8()
{
	CString str;
	this->GetDlgItem(IDC_RELAY8)->GetWindowText(str);
	if(str.Compare(L"Relay-8 On")==0)
	{
		SendCommand(16,8,0);
	}else
	{
		SendCommand(16,8,1);
	}
}


void CiMatic16Dlg::OnBnClickedRelay9()
{
	CString str;
	this->GetDlgItem(IDC_RELAY9)->GetWindowText(str);
	if(str.Compare(L"Relay-9 On")==0)
	{
		SendCommand(16,9,0);
	}else
	{
		SendCommand(16,9,1);
	}
}


void CiMatic16Dlg::OnBnClickedRelay10()
{
	CString str;
	this->GetDlgItem(IDC_RELAY10)->GetWindowText(str);
	if(str.Compare(L"Relay-10 On")==0)
	{
		SendCommand(16,10,0);
	}else
	{
		SendCommand(16,10,1);
	}
}


void CiMatic16Dlg::OnBnClickedRelay11()
{
	CString str;
	this->GetDlgItem(IDC_RELAY11)->GetWindowText(str);
	if(str.Compare(L"Relay-11 On")==0)
	{
		SendCommand(16,11,0);
	}else
	{
		SendCommand(16,11,1);
	}
}


void CiMatic16Dlg::OnBnClickedRelay12()
{
	CString str;
	this->GetDlgItem(IDC_RELAY12)->GetWindowText(str);
	if(str.Compare(L"Relay-12 On")==0)
	{
		SendCommand(16,12,0);
	}else
	{
		SendCommand(16,12,1);
	}
}


void CiMatic16Dlg::OnBnClickedRelay13()
{
	CString str;
	this->GetDlgItem(IDC_RELAY13)->GetWindowText(str);
	if(str.Compare(L"Relay-13 On")==0)
	{
		SendCommand(16,13,0);
	}else
	{
		SendCommand(16,13,1);
	}
}


void CiMatic16Dlg::OnBnClickedRelay14()
{
	CString str;
	this->GetDlgItem(IDC_RELAY14)->GetWindowText(str);
	if(str.Compare(L"Relay-14 On")==0)
	{
		SendCommand(16,14,0);
	}else
	{
		SendCommand(16,14,1);
	}
}

void CiMatic16Dlg::OnBnClickedRelay15()
{
	CString str;
	this->GetDlgItem(IDC_RELAY15)->GetWindowText(str);
	if(str.Compare(L"Relay-15 On")==0)
	{
		SendCommand(16,15,0);
	}else
	{
		SendCommand(16,15,1);
	}
}

void CiMatic16Dlg::OnBnClickedRelay16()
{
	CString str;
	this->GetDlgItem(IDC_RELAY16)->GetWindowText(str);
	if(str.Compare(L"Relay-16 On")==0)
	{
		SendCommand(16,16,0);
	}else
	{
		SendCommand(16,16,1);
	}
}


void CiMatic16Dlg::OnBnClickedRelayall()
{
	CString str;
	this->GetDlgItem(IDC_RELAYALL)->GetWindowText(str);
	if(str.Compare(L"Relay-All On")==0)
	{
		test->SendCommand(16,0,0);
	}else
	{
		test->SendCommand(16,0,1);
	}
}


void CiMatic16Dlg::OnBnClickedRefresh()
{
	CString str;
	this->GetDlgItem(IDC_RELAYALL)->GetWindowText(str);
	test->SendCommand(16,17,0);

}

