
// iMaticDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "iMatic.h"
#include "iMaticDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CiMaticDlg 对话框



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

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

BEGIN_MESSAGE_MAP(CiMaticDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CTLCOLOR()
	//ON_BN_CLICKED(MV_MESSAGE_RELAY_ALL_ONCLICK, &CiMaticDlg::OnRelayAllClick)
	ON_MESSAGE(MV_MESSAGE_FINSH_CONNECT_SOCK,&CiMaticDlg::FinishConnectServer)
	ON_BN_CLICKED(IDC_CONNECT_SOCKET, &CiMaticDlg::OnBnClickedConnect)
	ON_BN_CLICKED(IDC_RELAY1, &CiMaticDlg::OnBnClickedRelay1)
	ON_BN_CLICKED(IDC_RELAY2, &CiMaticDlg::OnBnClickedRelay2)
	ON_BN_CLICKED(IDC_RELAY3, &CiMaticDlg::OnBnClickedRelay3)
	ON_BN_CLICKED(IDC_RELAY4, &CiMaticDlg::OnBnClickedRelay4)
	ON_BN_CLICKED(IDC_RELAY5, &CiMaticDlg::OnBnClickedRelay5)
	ON_BN_CLICKED(IDC_RELAY6, &CiMaticDlg::OnBnClickedRelay6)
	ON_BN_CLICKED(IDC_RELAY7, &CiMaticDlg::OnBnClickedRelay7)
	ON_BN_CLICKED(IDC_RELAY8, &CiMaticDlg::OnBnClickedRelay8)
	ON_BN_CLICKED(IDC_RELAY_All, &CiMaticDlg::OnBnClickedRelayAll)
END_MESSAGE_MAP()


// CiMaticDlg 消息处理程序

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

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标
	
	isConnect=false;
	connecting=false;
	this->GetDlgItem(IDC_SOCKET_IP)->SetWindowText(L"192.168.1.4");
	this->GetDlgItem(IDC_SOCKET_PORT)->SetWindowText(L"30000");
	//AfxBeginThread(ListenThread,NULL);
	
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}
UINT ListenThread(LPVOID lpParam)
{

	return 0;
}

void CiMaticDlg::ConnectServer(void *ptr)
{
	AfxSocketInit();
	//SockInfo *sockInfo=(SockInfo*)ptr;
	CSocket *m_sock=new CSocket;
	HWND hWnd=(HWND)ptr;
	//::SendMessage(hWnd,MV_MESSAGE_FINSH_CONNECT_SOCK,1,NULL);
	if(!m_sock->Create()){
		::SendMessage(hWnd,MV_MESSAGE_FINSH_CONNECT_SOCK,-1,NULL);
	}
	else
	{
		if(m_sock->Connect(L"192.168.1.4", 30000))
		{
			::SendMessage(hWnd,MV_MESSAGE_FINSH_CONNECT_SOCK,1,(LPARAM)m_sock);
		}else
		{
			::SendMessage(hWnd,MV_MESSAGE_FINSH_CONNECT_SOCK,-1,NULL);
		}
	}
	_endthread();
}


LRESULT CiMaticDlg::FinishConnectServer(WPARAM wParam,LPARAM lparam)
{
	connecting=false;
	CString t_string;
	if(wParam==-1)
	{
		::MessageBox(NULL, L"Connet fail", L"Message", MB_SYSTEMMODAL);
	}else
	{
				m_socket=(CSocket*)lparam;
		isConnect=true;
		::MessageBox(NULL, L"Connet sucessful.", L"Message", MB_SYSTEMMODAL);
	}
	return 1L;
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CiMaticDlg::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
	{
		CRect rect;
        CPaintDC dc(this);
        GetClientRect(rect);
        dc.FillSolidRect(rect,RGB(255,255,255)); //设置为绿色背景
        CDialog::OnPaint();
		CDialogEx::OnPaint();
	}

}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CiMaticDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



HBRUSH CiMaticDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
	return hbr;
}


void CiMaticDlg::OnRelayAllClick()
{

}


void CiMaticDlg::OnBnClickedConnect()
{
	

	if(!connecting && !isConnect){
		try{
			_beginthread(&CiMaticDlg::ConnectServer,0,this->GetSafeHwnd());
		}catch(...)
		{
			::MessageBox(NULL, L"Connet fail", L"Message", MB_SYSTEMMODAL);
		}
	}
	
}


void CiMaticDlg::OnBnClickedRelay1()
{

	CString str;
	this->GetDlgItem(IDC_RELAY1)->GetWindowText(str);
	if(str=="Relay-1 Off")
	{
		Send_data(1,1);
	}
	else
	{
		Send_data(1,0);
	}
}


void CiMaticDlg::OnBnClickedRelay2()
{
	CString str;
	this->GetDlgItem(IDC_RELAY2)->GetWindowText(str);
	if(str=="Relay-2 Off")
	{
		Send_data(2,1);
	}
	else
	{
		Send_data(2,0);
	}
}


void CiMaticDlg::OnBnClickedRelay3()
{
	CString str;
	this->GetDlgItem(IDC_RELAY3)->GetWindowText(str);
	if(str=="Relay-3 Off")
	{
		Send_data(3,1);
	}
	else
	{
		Send_data(3,0);
	}
}


void CiMaticDlg::OnBnClickedRelay4()
{
	CString str;
	this->GetDlgItem(IDC_RELAY4)->GetWindowText(str);
	if(str=="Relay-4 Off")
	{
		Send_data(4,1);
	}
	else
	{
		Send_data(4,0);
	}
}


void CiMaticDlg::OnBnClickedRelay5()
{
	CString str;
	this->GetDlgItem(IDC_RELAY5)->GetWindowText(str);
	if(str=="Relay-5 Off")
	{
		Send_data(5,1);
	}
	else
	{
		Send_data(5,0);
	}
}


void CiMaticDlg::OnBnClickedRelay6()
{
	CString str;
	this->GetDlgItem(IDC_RELAY6)->GetWindowText(str);
	if(str=="Relay-6 Off")
	{
		Send_data(6,1);
	}
	else
	{
		Send_data(6,0);
	}
}


void CiMaticDlg::OnBnClickedRelay7()
{
	CString str;
	this->GetDlgItem(IDC_RELAY7)->GetWindowText(str);
	if(str=="Relay-7 Off")
	{
		Send_data(7,1);
	}
	else
	{
		Send_data(7,0);
	}
}


void CiMaticDlg::OnBnClickedRelay8()
{
	CString str;
	this->GetDlgItem(IDC_RELAY8)->GetWindowText(str);
	if(str=="Relay-8 Off")
	{
		Send_data(8,1);
	}
	else
	{
		Send_data(8,0);
	}
}

void CiMaticDlg::OnBnClickedRelayAll()
{
	CString str;
	this->GetDlgItem(IDC_RELAY_All)->GetWindowText(str);
	if(str=="Relay-All Off")
	{
		Send_data(0xF8,0x88);
	}
	else
	{
		Send_data(0xF8,0x80);
	}
}

void CiMaticDlg::Send_data(unsigned char relay_number, unsigned char isOn)
{
	if(isConnect)
	{
		unsigned char databuffer[6],datareceive[1024];
		databuffer[0]=0xFD;
		databuffer[1]=0x2;
		databuffer[2]=0x20;
		databuffer[3]=relay_number;
		databuffer[4]=isOn;
		databuffer[5]=0x5D;
		m_socket->Send(databuffer, 6);
		datareceive[0] = 0;
		if(m_socket->Receive((void *)datareceive, 8))
		{
			UpdateData(false);

			switch (datareceive[1])
			{
			case 1:
				if(datareceive[2]==0)
				{
					this->GetDlgItem(IDC_RELAY1)->SetWindowText(L"Relay-1 Off");
				}
				else
				{
					this->GetDlgItem(IDC_RELAY1)->SetWindowText(L"Relay-1 On");
				}
				break;
			case 2:
				if(datareceive[2]==0)
				{
					this->GetDlgItem(IDC_RELAY2)->SetWindowText(L"Relay-2 Off");
				}
				else
				{
					this->GetDlgItem(IDC_RELAY2)->SetWindowText(L"Relay-2 On");
				}
				break;
			case 3:
				if(datareceive[2]==0)
				{
					this->GetDlgItem(IDC_RELAY3)->SetWindowText(L"Relay-3 Off");
				}
				else
				{
					this->GetDlgItem(IDC_RELAY3)->SetWindowText(L"Relay-3 On");
				}
				break;
			case 4:
				if(datareceive[2]==0)
				{
					this->GetDlgItem(IDC_RELAY4)->SetWindowText(L"Relay-4 Off");
				}
				else
				{
					this->GetDlgItem(IDC_RELAY4)->SetWindowText(L"Relay-4 On");
				}
				break;
			case 5:
				if(datareceive[2]==0)
				{
					this->GetDlgItem(IDC_RELAY5)->SetWindowText(L"Relay-5 Off");
				}
				else
				{
					this->GetDlgItem(IDC_RELAY5)->SetWindowText(L"Relay-5 On");
				}
				break;
			case 6:
				if(datareceive[2]==0)
				{
					this->GetDlgItem(IDC_RELAY6)->SetWindowText(L"Relay-6 Off");
				}
				else
				{
					this->GetDlgItem(IDC_RELAY6)->SetWindowText(L"Relay-6 On");
				}
				break;
			case 7:
				if(datareceive[2]==0)
				{
					this->GetDlgItem(IDC_RELAY7)->SetWindowText(L"Relay-7 Off");
				}
				else
				{
					this->GetDlgItem(IDC_RELAY7)->SetWindowText(L"Relay-7 On");
				}
				break;
			case 8:
				if(datareceive[2]==0)
				{
					this->GetDlgItem(IDC_RELAY8)->SetWindowText(L"Relay-8 Off");
				}
				else
				{
					this->GetDlgItem(IDC_RELAY8)->SetWindowText(L"Relay-8 On");
				}
				break;
			case 0xF8:
				if(datareceive[2]==0x88)
				{
					this->GetDlgItem(IDC_RELAY_All)->SetWindowText(L"Relay-All On");
					this->GetDlgItem(IDC_RELAY1)->SetWindowText(L"Relay-1 On");
					this->GetDlgItem(IDC_RELAY2)->SetWindowText(L"Relay-2 On");
					this->GetDlgItem(IDC_RELAY3)->SetWindowText(L"Relay-3 On");
					this->GetDlgItem(IDC_RELAY4)->SetWindowText(L"Relay-4 On");
					this->GetDlgItem(IDC_RELAY5)->SetWindowText(L"Relay-5 On");
					this->GetDlgItem(IDC_RELAY6)->SetWindowText(L"Relay-6 On");
					this->GetDlgItem(IDC_RELAY7)->SetWindowText(L"Relay-7 On");
					this->GetDlgItem(IDC_RELAY8)->SetWindowText(L"Relay-8 On");
					
				}
				else
				{
					this->GetDlgItem(IDC_RELAY_All)->SetWindowText(L"Relay-All Off");
					this->GetDlgItem(IDC_RELAY1)->SetWindowText(L"Relay-1 Off");
					this->GetDlgItem(IDC_RELAY2)->SetWindowText(L"Relay-2 Off");
					this->GetDlgItem(IDC_RELAY3)->SetWindowText(L"Relay-3 Off");
					this->GetDlgItem(IDC_RELAY4)->SetWindowText(L"Relay-4 Off");
					this->GetDlgItem(IDC_RELAY5)->SetWindowText(L"Relay-5 Off");
					this->GetDlgItem(IDC_RELAY6)->SetWindowText(L"Relay-6 Off");
					this->GetDlgItem(IDC_RELAY7)->SetWindowText(L"Relay-7 Off");
					this->GetDlgItem(IDC_RELAY8)->SetWindowText(L"Relay-8 Off");
				}
				break;

			}
			int bt_ids[8]={IDC_RELAY1,IDC_RELAY2,IDC_RELAY3,IDC_RELAY4,IDC_RELAY5,IDC_RELAY6,IDC_RELAY7,IDC_RELAY8};
			CString str;
			CString c_str=L"Relay-";
			CString t_str;
			int relay_on_number=0;
			for(int i=0;i<8;i++)
			{
				c_str="";
				c_str.Append(L"Relay-");
				t_str.Format(L"%d",i+1);
				c_str.Append(t_str);
				c_str.Append(L" Off");
				//::MessageBox(NULL,c_str, L"Message", MB_OK);
				this->GetDlgItem(bt_ids[i])->GetWindowText(str);
				if(str==c_str)
				{
					//::MessageBox(NULL,c_str, L"Message", MB_OK);
					relay_on_number--;
				}
				else
				{
					relay_on_number++;
				}
			}

			if(relay_on_number==8)
			{
				this->GetDlgItem(IDC_RELAY_All)->SetWindowText(L"Relay-All On");
			}
			if(relay_on_number==-8)
			{
				this->GetDlgItem(IDC_RELAY_All)->SetWindowText(L"Relay-All Off");
			}
			UpdateData(true);
		}
	}
	else
	{
		::MessageBox(NULL, L"No connection.", L"Message", MB_SYSTEMMODAL);
	}
}


