#include "StdAfx.h"
|
#include "CHLightControls/LightControl_Shinhan_TCP.h"
|
|
|
CLightControl_Shinhan_TCP::CLightControl_Shinhan_TCP(int nIndex) : CLightControl(nIndex)
|
{
|
AfxSocketInit() ;
|
m_bConnected = FALSE;
|
|
memset(m_dCurrentValue,0,MAX_CHANNEL);
|
memset(m_nCurrentStatus,0,MAX_CHANNEL);
|
}
|
|
|
CLightControl_Shinhan_TCP::~CLightControl_Shinhan_TCP(void)
|
{
|
Disconnect();
|
}
|
|
|
BOOL CLightControl_Shinhan_TCP::Connect( const CLightControlInfo& controlInfo )
|
{
|
CString strMessage;
|
|
if(TRUE == m_bConnected)
|
{
|
Disconnect();
|
}
|
|
if(controlInfo.GetConnectionPort() == _T(""))
|
{
|
strMessage.Format(_T("Port / IP is Null! [%s]"), controlInfo.GetConnectionPort());
|
if(m_pLC2P)
|
{
|
m_pLC2P->ILC2P_DisplayMessage(m_nIndex,strMessage);
|
}
|
m_bConnected = FALSE;
|
return m_bConnected;
|
}
|
|
//Create Socket
|
if(FALSE == Create())
|
{
|
if(m_pLC2P)
|
{
|
strMessage.Format(_T("Create Socket Fail! [%s]"), controlInfo.GetConnectionPort());
|
m_pLC2P->ILC2P_DisplayMessage(m_nIndex,strMessage);
|
}
|
|
m_bConnected = FALSE;
|
return m_bConnected;
|
}
|
|
if(CSocket::Connect(controlInfo.GetConnectionPort() , 88) == FALSE)
|
{
|
if(m_pLC2P)
|
{
|
strMessage.Format(_T("Connect Socket Fail! [%s]"), controlInfo.GetConnectionPort());
|
m_pLC2P->ILC2P_DisplayMessage(m_nIndex,strMessage);
|
}
|
|
Close();
|
m_bConnected = FALSE;
|
return m_bConnected;
|
}
|
|
if(m_pLC2P)
|
{
|
strMessage.Format(_T("Connect Socket Success! [%s]"), controlInfo.GetConnectionPort());
|
m_pLC2P->ILC2P_DisplayMessage(m_nIndex,strMessage);
|
}
|
|
m_bConnected = TRUE;
|
|
// Last ¹à±â·Î Turn On
|
strMessage.Format(_T("*LC 0\r\n"));
|
|
USES_CONVERSION;
|
CSocket::Send(strMessage,strMessage.GetLength());
|
|
return m_bConnected;
|
}
|
|
void CLightControl_Shinhan_TCP::Disconnect()
|
{
|
if(TRUE == m_bConnected)
|
{
|
Close();
|
m_bConnected = FALSE;
|
}
|
}
|
|
LONG CLightControl_Shinhan_TCP::ConnectEx( const CLightControlInfo& controlInfo )
|
{
|
return TRUE;
|
}
|
|
void CLightControl_Shinhan_TCP::DisconnectEx()
|
{
|
|
}
|
|
BOOL CLightControl_Shinhan_TCP::GetLightLevel( int &nValue, int nChannel /*= 0*/ ) const
|
{
|
if(MAX_CHANNEL >= nChannel) return FALSE;
|
|
// Send Message : "*SV\r\n"
|
// Receive Message : "LAMP 15.2, 23.4, 34.5, 34.5, 55.0, 23.5, 89.8, 77.0,OK + CrLf"
|
|
double dvalue = 0.0;
|
|
if(GetLightLevel(dvalue,nChannel))
|
{
|
nValue = (int)dvalue;
|
return TRUE;
|
}
|
|
return FALSE;
|
}
|
|
BOOL CLightControl_Shinhan_TCP::GetLightLevel( double &dValue, int nChannel /*= 0*/ ) const
|
{
|
if(nChannel >= MAX_CHANNEL) return FALSE;
|
|
dValue = m_dCurrentValue[nChannel];
|
|
return TRUE;
|
}
|
|
BOOL CLightControl_Shinhan_TCP::GetLightStatus( int &nValue, int nChannel /*= 0*/ ) const
|
{
|
if(nChannel >= MAX_CHANNEL) return FALSE;
|
|
nValue = m_nCurrentStatus[nChannel];
|
|
return TRUE;
|
}
|
|
BOOL CLightControl_Shinhan_TCP::SetLightLevel( int nValue, int nChannel /*= 0*/ )
|
{
|
return SetLightLevel((double)nValue, nChannel);
|
}
|
|
BOOL CLightControl_Shinhan_TCP::SetLightLevel( double dValue, int nChannel /*= 0*/ )
|
{
|
if(nChannel >= MAX_CHANNEL) return FALSE;
|
//COMMANDÇü½Ä : *CH ch,perc
|
|
CString strMessage;
|
|
strMessage.Format(_T("*CH %d,%.1f\r\n"),nChannel,dValue);
|
|
USES_CONVERSION;
|
CSocket::Send(strMessage, strMessage.GetLength());
|
m_dCurrentValue[nChannel] = dValue;
|
|
return TRUE;
|
}
|
|
BOOL CLightControl_Shinhan_TCP::SetLightStatus( int nValue, int nChannel /*= 0*/ )
|
{
|
if(nChannel >= MAX_CHANNEL) return FALSE;
|
|
m_nCurrentStatus[nChannel] = nValue;
|
|
return TRUE;
|
}
|
void CLightControl_Shinhan_TCP::OnReceive(int nErrorCode)
|
{
|
// Receive Message : "LAMP 15.2, 23.4, 34.5, 34.5, 55.0, 23.5, 89.8, 77.0,OK + CrLf"
|
|
CString strData = _T(""), strTemp = _T("");
|
char buf[200] = {0,};
|
int len = Receive(buf,200);
|
|
CString strBuffer(buf);
|
strData = strBuffer.Left(len);
|
|
strTemp = strData.Left(4);
|
|
if(strTemp.CompareNoCase(_T("LAMP")) == 0)
|
{
|
|
int i = 0;
|
strData = strData.Mid(5,strData.GetLength());
|
|
while (FALSE != AfxExtractSubString(strTemp, strData, i++, ','))
|
{
|
if(i > MAX_CHANNEL) return;
|
m_dCurrentValue[i+1] = _ttof(strTemp);
|
}
|
|
if(m_pLC2P)
|
{
|
m_pLC2P->ILC2P_DisplayMessage(m_nIndex, _T("[LightControl] Update Current Light Value!"));
|
}
|
|
}
|
}
|
|
BOOL CLightControl_Shinhan_TCP::SetLightOn()
|
{
|
return SetLightLevel(100.0,0);
|
}
|
|
BOOL CLightControl_Shinhan_TCP::SetLightOff()
|
{
|
return SetLightLevel(0.0,0);
|
}
|
|
BOOL CLightControl_Shinhan_TCP::SetLightOn(int Channel)
|
{
|
if (!m_bConnected) return FALSE;
|
|
return TRUE;
|
}
|
|
BOOL CLightControl_Shinhan_TCP::SetLightOff(int Channel)
|
{
|
if (!m_bConnected) return FALSE;
|
|
return TRUE;
|
}
|
|
BOOL CLightControl_Shinhan_TCP::SetLightAllOn()
|
{
|
if (!m_bConnected) return FALSE;
|
|
return TRUE;
|
}
|
|
BOOL CLightControl_Shinhan_TCP::SetLightAllOff()
|
{
|
if (!m_bConnected) return FALSE;
|
|
return TRUE;
|
}
|
|
int CLightControl_Shinhan_TCP::GetStatus( int& nStatusCode, CString& strStatusMessage )
|
{
|
CLightControlInfo controlInfo;
|
controlInfo.SetIndex(0);
|
controlInfo.SetName(_T("TestLightControl"));
|
controlInfo.SetControllerType(-1);
|
controlInfo.SetConnectionPort(_T("192.168.3.100"));
|
controlInfo.SetBaudRate(-1);
|
controlInfo.SetDefaultValue(100);
|
controlInfo.SetCurrentValue(100);
|
controlInfo.SetMaxValue(255);
|
controlInfo.SetMinValue(0);
|
controlInfo.SetChannel(0);
|
controlInfo.SetMaster(1);
|
|
if (Connect(controlInfo) == FALSE)
|
{
|
nStatusCode = LightStatus_NotConnected;
|
strStatusMessage = _T("Not_Connected");
|
}
|
else
|
{
|
nStatusCode = LightStatus_Connected;
|
strStatusMessage = _T("Connected");
|
|
CString strMessage;
|
strMessage.Format(_T("*SV\r\n"));
|
|
USES_CONVERSION;
|
CSocket::Send(strMessage, strMessage.GetLength());
|
|
char buf[200] = {0,};
|
int len = Receive(buf,200);
|
|
if (len > 0)
|
{
|
if (buf[len-4]=='O' && buf[len-3]=='K') return 1;
|
else if (buf[len-4]=='N' && buf[len-3]=='G') return 2;
|
else return 3;
|
}
|
else return 0;
|
}
|
return 0;
|
}
|