⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 impiopcgroupstatemgt.cpp

📁 基于Intellution开发包的开发的OPC服务器
💻 CPP
字号:
// ImpIOPCGroupStateMgt.cpp
//
//  This file contains the implementation of 
//  the IOPCGroupStateMgt interface for groups in the XXX server.
//
//
//	(c) COPYRIGHT 1996-1998, INTELLUTION INC.
// ALL RIGHTS RESERVED
//
//
//	Functions defined in this module:
//
//			CImpIOPCGroupStateMgt::CImpIOPCGroupStateMgt()
//			CImpIOPCGroupStateMgt::~CImpIOPCGroupStateMgt()
//			CImpIOPCGroupStateMgt::AddRef()
//			CImpIOPCGroupStateMgt::Release()
//			CImpIOPCGroupStateMgt::QueryInterface()
//			CImpIOPCGroupStateMgt::GetState()
//			CImpIOPCGroupStateMgt::SetState()
//			CImpIOPCGroupStateMgt::SetName()
//			CImpIOPCGroupStateMgt::CloneGroup()
//
//
//
// Modification Log:
//	Vers	Date     By		Notes
//	----	-------- ---	-----
//	1.0		08/26/97 jra	Created
//	1.3		03/10/98 jra	Modified to be wizard generated and driver specific.
//
//

#define WIN32_LEAN_AND_MEAN

#include "OpcStdAfx.h"
#include <afxconv.h>	// used for ATL conversion functions
#include "OPC.h"


////////////////////////////////////////////////////////////////
// CImpIOPCGroupStateMgt()
//
// Constructor for this Interface
//
////////////////////////////////////////////////////////////////
CImpIOPCGroupStateMgt::CImpIOPCGroupStateMgt(LPUNKNOWN pUnkOuter)
{
	m_pUnkOuter		= pUnkOuter;
	m_pParentGroup	= (COPCDrvGroup *)pUnkOuter;
}


////////////////////////////////////////////////////////////////
// ~CImpIOPCGroupStateMgt()
//
// Destructor for this Interface
//
////////////////////////////////////////////////////////////////
CImpIOPCGroupStateMgt::~CImpIOPCGroupStateMgt(void)
{
	m_pParentGroup->m_pCImpIGroupMgt = NULL;
}


/////////////////////////////////////////////////////////////////////////////
// IUnknown functions Delegate to Parent
//


////////////////////////////////////////////////////////////////
// AddRef()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP_(ULONG) CImpIOPCGroupStateMgt::AddRef(void)
{
	return m_pUnkOuter->AddRef();
}


////////////////////////////////////////////////////////////////
// Release()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP_(ULONG) CImpIOPCGroupStateMgt::Release(void)
{
	return m_pUnkOuter->Release();
}


////////////////////////////////////////////////////////////////
// QueryInterface()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCGroupStateMgt::QueryInterface(REFIID	iid, 
												   LPVOID	*ppInterface)
{
	return m_pUnkOuter->QueryInterface(iid, ppInterface);
}


/////////////////////////////////////////////////////////////////////////////
// CImpIOPCGroupStateMgt (IOPCGroupStateMgt) interface functions
//


////////////////////////////////////////////////////////////////
// GetState()
//
// Returns the various members of a group.
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCGroupStateMgt::GetState(DWORD		*pUpdateRate, 
											 BOOL		*pActive, 
											 LPWSTR		*ppName, 
											 LONG		*pTimeBias,
											 FLOAT		*pPercentDeadband, 
											 DWORD		*pLCID,
											 OPCHANDLE	*phClientGroup,
											 OPCHANDLE	*phServerGroup)
{
	if (pUpdateRate)
	{
		*pUpdateRate = m_pParentGroup->GetUpdateRate();
	}
	if (pActive)
	{
		*pActive = m_pParentGroup->GetActive();
	}
	if (pTimeBias)
	{
		*pTimeBias = m_pParentGroup->m_TimeBias;
	}
	if (pPercentDeadband)
	{
		*pPercentDeadband = m_pParentGroup->m_Deadband;
	}
	if (pLCID)
	{
		*pLCID = m_pParentGroup->m_LCID;
	}
	if (phClientGroup)
	{
		*phClientGroup = m_pParentGroup->m_ClientGroupHandle;
	}
	if (phServerGroup)
	{
		*phServerGroup = m_pParentGroup->m_ServerGroupHandle;
	}
	if (ppName)
	{
		*ppName	= WSTRClone(m_pParentGroup->m_szName, pIMalloc);
		if (NULL == *ppName)
		{
			return E_FAIL;
		}
	}

	return S_OK;
}


////////////////////////////////////////////////////////////////
// SetState()
//
// Sets the various members of a group object.
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCGroupStateMgt::SetState(DWORD		*pRequestedUpdateRate, 
											 DWORD		*pRevisedUpdateRate, 
											 BOOL		*pActive, 
											 LONG		*pTimeBias,
											 FLOAT		*pPercentDeadband,
											 DWORD		*pLCID,
											 OPCHANDLE	*phClientGroup)
{
	// Lock the parent group
	//
	m_pParentGroup->Lock();

	if(pRequestedUpdateRate) 
	{
		m_pParentGroup->SetUpdateRate(*pRequestedUpdateRate);
	}
	if(pActive)
	{
		m_pParentGroup->SetActive(*pActive);
	}
	if(phClientGroup)
	{
		m_pParentGroup->m_ClientGroupHandle = *phClientGroup;
	}
	if(pPercentDeadband)
	{
		m_pParentGroup->m_Deadband = *pPercentDeadband;
	}
	if(pLCID)
	{
		m_pParentGroup->m_LCID = *pLCID;
	}
	if(pTimeBias)
	{
		m_pParentGroup->m_TimeBias = *pTimeBias;
	}
	if (pRevisedUpdateRate)
	{
		*pRevisedUpdateRate = m_pParentGroup->GetUpdateRate();
	}

	// Unlock the parent group
	//
	m_pParentGroup->UnLock();

	return S_OK;
}


////////////////////////////////////////////////////////////////
// SetName()
//
// This function sets the name of the group object. If the name
// already exists in the server, then it is not changed.
//
// Returns:
//	HRESULT	-	S_OK if the name was changed
//			-	E_FAIL if the name already exists in the server.
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCGroupStateMgt::SetName(LPCWSTR szName)
{
	// Verify that the name provided is Unique
	//
	if (m_pParentGroup->m_pParentServer->m_pCImpIOPCServer)
	{
		LPUNKNOWN	lpTest;

		m_pParentGroup->m_pParentServer->m_pCImpIOPCServer->GetGroupByName(szName, 
																		   IID_IUnknown, 
																		   &lpTest);
		if(lpTest)
		{
			// The name already exists.
			//
			lpTest->Release();
			return E_FAIL;
		}
	}

	WSTRFree(m_pParentGroup->m_szName, NULL);
	m_pParentGroup->m_szName = WSTRClone(szName, NULL);

	return S_OK;
}


////////////////////////////////////////////////////////////////
// CloneGroup()
//
// This function creates a new group and copies all members of
// the calling group to it. It works similiar to a copy 
// constructor.
//
// Returns:
//	HRESULT	-	S_OK if the function succeeded
//				E_FAIL is the function failed.
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCGroupStateMgt::CloneGroup(LPCWSTR		szName,
											   REFIID		riid,
											   LPUNKNOWN	*ppUnk)
{
	COPCDrvGroup	*pOldGroup = m_pParentGroup,
					*pNewGroup;
	COPCDrvServer	*pParentServer = m_pParentGroup->m_pParentServer;
	HRESULT			hr;
	OPCHANDLE		OPCHandle;


	// Make sure that we didn't get any bad pointers
	//
	if (NULL == szName)
	{
		if (NULL == ppUnk)
		{
			return E_FAIL;
		}
		*ppUnk = NULL;
		return E_FAIL;
	}
	if (NULL == ppUnk)
	{
		return E_FAIL;
	}

	// If the caller didn't give us a name, then generate one. I don't think
	// this is required by the spec, but it is a nice feature.
	//
	if ((NULL == *szName) && (pParentServer->m_pCImpIOPCServer))
	{
		CString		strGeneratedName;
		DWORD		dwNumGroups			= pParentServer->GetNumGroupHandles(),
					i					= 0;
		char		szNumGroups[15],
					*pszGeneratedName	= NULL;
		int			nLength;
		LPUNKNOWN	lpTest;

		// Macro required for ATL string conversions
		USES_CONVERSION;

		// We will try 1000 different names. We SHOULD find one that isn't
		// already configured.
		//
		while(i < 1000)
		{
			strGeneratedName = "Group";
			ltoa((dwNumGroups + i), szNumGroups, 10);
			strGeneratedName += szNumGroups;
			nLength	= strGeneratedName.GetLength();

			pszGeneratedName = strGeneratedName.GetBuffer(nLength+1);
			szName = A2W(pszGeneratedName);
			strGeneratedName.ReleaseBuffer();

			pParentServer->m_pCImpIOPCServer->GetGroupByName(szName, 
															 IID_IUnknown, 
															 &lpTest);
			if(NULL == lpTest)
			{
				break;
			}

			i++;
		}
	}
	else if (pParentServer->m_pCImpIOPCServer)
	{
		// Make sure the name is not already defined in the server
		//
		LPUNKNOWN	lpTest;
		pParentServer->m_pCImpIOPCServer->GetGroupByName(szName, 
														 IID_IUnknown, 
														 &lpTest);
		if(lpTest)
		{
			*ppUnk = NULL;
			return E_FAIL;
		}
	}

	// Allocate the memory for the new group and get a handle for it.
	//
	hr = pParentServer->GroupAlloc(&OPCHandle, &pNewGroup, pParentServer);
	if (FAILED(hr))
	{
		return E_FAIL;
	}

	// And request a 2nd interface for the caller
	//
	hr = pNewGroup->QueryInterface(riid, (LPVOID *)ppUnk);
	if(FAILED(hr))
	{
		// If error - delete group and return
		delete pNewGroup;
		return E_FAIL;
	}
	pNewGroup->AddRef();

	// Lock the old group so we don't have things changing midstream.
	//
	pOldGroup->Lock();

	// If OK then Copy info from the old group to the new group
	// (except name, server handle and active state)
	//
	pNewGroup->m_ServerGroupHandle	= OPCHandle;
	pNewGroup->m_bActive			= FALSE;
	pNewGroup->m_szName				= WSTRClone(szName, NULL);
	*pNewGroup						= *pOldGroup;

	// And then copy the ITEMS from the old group to the new group
	// Note that the Item Handles are preserved (per the OPC specification).
	//
	POSITION		ItemPosition	= pOldGroup->GetFirstItemPosition();
	DWORD			dwNumItems		= pOldGroup->GetNumItemHandles();
	OPCHANDLE		tempOPCHandle;
	COPCDrvItem		*pOldItem,
					*pNewItem;
	for(DWORD i = 0; i < dwNumItems; i++)
	{
		// Loop through all of the items in the old group, clone it,
		// then insert it in the new group.
		//
		pOldGroup->GetNextItem(ItemPosition, tempOPCHandle, pOldItem);
		pNewItem = pOldItem->Clone(pNewGroup);
		if (NULL == pNewItem)
		{
			delete pNewGroup;
			return E_FAIL;
		}
		pNewGroup->ItemSet(pNewItem);
	}

	// Unlock the group
	//
	pOldGroup->UnLock();

	// Now add the new group to the map.
	//
	pParentServer->GroupSet(pNewGroup);

	return S_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -