// Login.cpp : implementation file
//

#include "stdafx.h"
#include "ex-1.h"
#include "Login.h"
#include "afxdialogex.h"


// CLogin dialog

// ===================Slamtec example code start===================
#include <rpos/robot_platforms/slamware_core_platform.h>

using namespace rpos::robot_platforms;

extern SlamwareCorePlatform robot;
// ===================Slamtec example code end  ===================


IMPLEMENT_DYNAMIC(CLogin, CDialogEx)

CLogin::CLogin(CWnd* pParent /*=NULL*/)
	: CDialogEx(CLogin::IDD, pParent)
{

}

CLogin::~CLogin()
{
}

void CLogin::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_IPADDRESS1, m_robotIP);
}


BEGIN_MESSAGE_MAP(CLogin, CDialogEx)
	ON_BN_CLICKED(IDOK, &CLogin::OnBnClickedOk)
//	ON_WM_CREATE()
END_MESSAGE_MAP()

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

    // TODO:  Add extra initialization here
	// ===================Slamtec example code start===================
    CString strIp = _T("192.168.11.1");
    m_robotIP.SetWindowText(strIp);  
	// ===================Slamtec example code end  ===================

    return TRUE;  // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
}

// CLogin message handlers
void CLogin::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	// ===================Slamtec example code start===================
	CString strIP;
    BYTE IP0,IP1,IP2,IP3;

	// get IP address
    m_robotIP.GetAddress(IP0,IP1,IP2,IP3);
	// construct CString IP address
    strIP.Format(_T("%d.%d.%d.%d"),IP0,IP1,IP2,IP3);
	// convert it to string format
	std::string platformHost(strIP.GetBuffer());

    try 
    {
		// connect to robot with default port# 1445
        robot = rpos::robot_platforms::SlamwareCorePlatform::connect(platformHost, 1445);
        CDialogEx::OnOK();
    } 
    catch (const rpos::robot_platforms::ConnectionFailException &e)
    {
         MessageBox(_T("连接机器人失败，请检查网络连接或是IP地址是否正确"), _T("错误"), MB_OK);
         CDialogEx::OnCancel();
 
    }
    catch(const std::exception &e)
    {
         MessageBox(_T("连接机器人失败，请检查网络连接或是IP地址是否正确"), _T("错误"), MB_OK);
         CDialogEx::OnCancel();
    }
	// ===================Slamtec example code end  ===================
}

