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

📄 subscription.cpp

📁 此为Insight opc client InfoServerExplorer源程序
💻 CPP
字号:
/////////////////////////////////////////////////////////////////////////////
//
//  OPC DataAccess VC++ Client:	subscription.cpp
//								(Source File)
//
/////////////////////////////////////////////////////////////////////////////
//
//          Author: Raphael Imhof
//    Initial Date: 12/10/98
//       $Workfile: subscription.cpp $
//       $Revision: 1 $
//           $Date: 7/27/99 5:23p $
//   Target System: Microsoft Windows NT 4.0
//     Environment: Visual C++ 5.0 / OPC DataAccess 2.0
//         Remarks: 
//
/////////////////////////////////////////////////////////////////////////////
//
//     Description: implementation of the connection point class:
//					COPCConnectionPoint (for OPC 2.0 data notification)
//
/////////////////////////////////////////////////////////////////////////////
//
//  History of Changes     (Please remove very old comments and blank lines!)
//            $Log: /IDK/OPCServer/clients/VC++/InfoServerExplorer/subscription.cpp $
// 
// 1     7/27/99 5:23p Imhof
// 
// 1     7/27/99 5:19p Imhof
// 
// 5     1/15/99 6:43p Imhof
// Updated legal notice.
// 
// 4     1/06/99 10:34a Imhof
// Added a changed attribute to the CItem class which is used to optimize
// the content view update.
// 
// 3     12/14/98 4:46p Imhof
// Modifications for OPC 2.0
// 
// 2     12/11/98 5:58p Imhof
// Made modifications for OPC 2.0.
// 
// 
// 
//  $Nokeywords:$ (To avoid useless search while checking in.)
/////////////////////////////////////////////////////////////////////////////
//  Copyright (C) 1998, Siemens Building Technologies, Inc. Landis Division
//
// SIEMENS BUILDING TECHNOLOGIES, INC. IS PROVIDING THE FOLLOWING
// EXAMPLES OF CODE AS SAMPLE ONLY.
//
// SIEMENS BUILDING TECHNOLOGIES, INC.  MAKES NO REPRESENTATIONS
// OR WARRANTIES OF ANY KIND  WITH RESPECT TO THE VALIDTY OF THE 
// CODES   OR   DESIRED   RESULTS   AND   DISCLAIMS   ALL   SUCH 
// REPRESENTATIONS   AND   WARRANTIES,  INCLUDING  FOR  EXAMPLE, 
// WARRANTIES  OF  MERCHANTABILITY  AND FITNESS FOR A PARTICULAR 
// PURPOSE.    SIEMENS  BUILIDNG  TECHNOLOGIES,  INC.  DOES  NOT 
// REPRESENT  OR  WARRANT  THAT  THE  FOLLOWING CODE SAMPLES ARE 
// ACCURATE, VALID, COMPLETE OR CURRENT.
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "infoserverexplorer.h"
#include "MainFrm.h"
#include "subscription.h"
#include "item.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

COPCConnectionPoint::COPCConnectionPoint()
{

}

COPCConnectionPoint::~COPCConnectionPoint()
{

}

STDMETHODIMP COPCConnectionPoint::OnDataChange(	DWORD		dwTransid,
												OPCHANDLE   hGroup,
												HRESULT     hrMasterpwQualities,
												HRESULT     hrMastererroror,
												DWORD       dwCount,
												OPCHANDLE*	phClientItems,
												VARIANT*	pvValues,
												WORD*		pwQualities,
												FILETIME*	pftTimeStamps,	
												HRESULT*	pErrors)
{
	// If Transid != 0, this is a refresh, otherwise a datachange from an item in 
	// an active group
	BOOL bRefresh = FALSE;
	if(dwTransid != 0) bRefresh = TRUE;

	CGroup* pGroup = NULL;

	for( DWORD index=0; index < dwCount; index++ )
	{
		if(SUCCEEDED(pErrors[index]))
		{
			CItem* pItem = (CItem*) phClientItems[index];
			if(pItem != NULL)
			{
				if(pGroup == NULL) pGroup = pItem->GetGroup();

				pItem->SetTimeStamp(pftTimeStamps[index]);
				pItem->SetValue(pvValues[index]);
				pItem->SetQuality(pwQualities[index]);
				
				//item changed for content view update
				pItem->SetChanged(TRUE);
			}
		}
	}

	if(bRefresh && (pGroup != NULL))
	{
		pGroup->SetCurrentTransactionID(0);
		pGroup->SetCurrentCancelID(0);
	}

	//notify the content view
	CInfoServerExplorerApp* pApp = (CInfoServerExplorerApp*) AfxGetApp();
	CMainFrame* pMainFrame = (CMainFrame*) AfxGetMainWnd();
	if(NULL != pMainFrame)
	{
		CContentView* pView = pMainFrame->GetContentView();
		if(NULL != pView)
			pView->PostMessage(WM_CONTENTVIEW_UPDATE, CONTENT_UPDATE_ONLY, 0);
	}

   return S_OK;
}

STDMETHODIMP COPCConnectionPoint::OnReadComplete(	DWORD       dwTransid,
												OPCHANDLE   hGroup,
												HRESULT     hrMasterpwQualities,
												HRESULT     hrMastererroror,
												DWORD       dwCount,
												OPCHANDLE*	phClientItems,
												VARIANT*	pvValues,
												WORD*		pwQualities,
												FILETIME*	pftTimeStamps,
												HRESULT*	pErrors)
{
	CGroup* pGroup = NULL;

	for(DWORD index = 0; index < dwCount; index++)
	{
		if(SUCCEEDED(pErrors[index]))
		{
			CItem* pItem = (CItem*) phClientItems[index];
			if(pItem != NULL)
			{
				if(pGroup == NULL) pGroup = pItem->GetGroup();

				pItem->SetTimeStamp(pftTimeStamps[index]);
				pItem->SetValue(pvValues[index]);
				pItem->SetQuality(pwQualities[index]);

				//item changed for content view update
				pItem->SetChanged(TRUE);
			}
		}
	}
	
	if(pGroup != NULL)
	{
		pGroup->SetCurrentTransactionID(0);
		pGroup->SetCurrentCancelID(0);
	}

	//notify the content view
	CInfoServerExplorerApp* pApp = (CInfoServerExplorerApp*) AfxGetApp();
	CMainFrame* pMainFrame = (CMainFrame*) AfxGetMainWnd();
	if(NULL != pMainFrame)
	{
		CContentView* pView = pMainFrame->GetContentView();
		if(NULL != pView)
			pView->PostMessage(WM_CONTENTVIEW_UPDATE, CONTENT_UPDATE_ONLY, 0);
	}
   
	return S_OK;
}

STDMETHODIMP COPCConnectionPoint::OnWriteComplete(	DWORD       dwTransid,
												OPCHANDLE   hGroup,
												HRESULT     hrMastererror,
												DWORD       dwCount,
												OPCHANDLE*	phClientItems,
												HRESULT*	pErrors)
{
	CGroup* pGroup = NULL;

	for(DWORD index = 0; index < dwCount; index++)
	{
		if(FAILED(pErrors[index]))
		{
			CItem* pItem = (CItem*) phClientItems[index];

			if(pGroup == NULL) pGroup = pItem->GetGroup();

			((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("Async Write"), pErrors[index]);
		}
	}

	if(pGroup != NULL)
	{
		pGroup->SetCurrentTransactionID(0);
		pGroup->SetCurrentCancelID(0);
	}

	return S_OK;
}

STDMETHODIMP COPCConnectionPoint::OnCancelComplete(	DWORD       dwTransid,
													OPCHANDLE   hGroup)
{
	//TODO: implement COPCConnectionPoint::OnCancelComplete
	((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("IOPCAsyncIO2::Cancel2() completed."));

	return S_OK;
}

BOOL COPCConnectionPoint::Init(CGroup* pGroup)
{
	// OPC 2.0 ConnectionPoints
	CComPtr<IOPCGroupStateMgt>			pGRP = NULL;
	CComPtr<IConnectionPointContainer>	pConPointCont = NULL;
	CComPtr<IConnectionPoint>			pConPoint = NULL;
	DWORD								dwConnection = 0;
	HRESULT								hRes;
	BOOL								bRet = FALSE;

	hRes = pGroup->GetInterface()->QueryInterface(IID_IOPCGroupStateMgt, (void**)&pGRP);
	if(SUCCEEDED(hRes))
	{
		hRes = pGRP->QueryInterface(IID_IConnectionPointContainer, (void**)&pConPointCont);
		if(SUCCEEDED(hRes))
		{
			hRes = pConPointCont->FindConnectionPoint(IID_IOPCDataCallback, &pConPoint);				
			if(FAILED(hRes))
			{
				 ((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("FindConnectionPoint"), hRes);
			}
			else
			{
				hRes = pConPoint->Advise(*pGroup->GetInfoServer()->GetDataAccessConnectionPoint(), &dwConnection);					
				if(FAILED(hRes))
				{
					((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("IConnectionPoint::Advise"), hRes);
				}
				else
				{
					pGroup->SetOPCDataAccessConnectionPoint(dwConnection);
					bRet = TRUE;
				}
				pConPoint.Release();
			}
			pConPointCont.Release();
		}
		else
		{
			//no connection point support
			pGroup->GetInfoServer()->DisplayError(hRes);
		}
	}
	else
	{
		pGroup->GetInfoServer()->DisplayError(hRes);
	}

	return bRet;
}


BOOL COPCConnectionPoint::Term(CGroup* pGroup)
{
	// OPC 2.0 ConnectionPoints
	CComPtr<IOPCGroupStateMgt>			pGRP = NULL;
	CComPtr<IConnectionPointContainer>	pConPointCont = NULL;
	CComPtr<IConnectionPoint>			pConPoint = NULL;
	HRESULT								hRes;
	BOOL								bRet = FALSE;

	if(pGroup->GetInfoServer()->IsConnected())//if server did shutdown it can't succeed)
	{
		hRes = pGroup->GetInterface()->QueryInterface(IID_IOPCGroupStateMgt, (void**)&pGRP);
		if(SUCCEEDED(hRes))
		{
			hRes = pGRP->QueryInterface(IID_IConnectionPointContainer, (void**)&pConPointCont);
			if(SUCCEEDED(hRes) && (pGroup->GetOPCDataAccessConnectionPoint() != 0))
			{
				hRes = pConPointCont->FindConnectionPoint(IID_IOPCDataCallback, &pConPoint);				
				if(FAILED(hRes))
				{
					 ((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("FindConnectionPoint"), hRes);
				}
				else
				{
					hRes = pConPoint->Unadvise(pGroup->GetOPCDataAccessConnectionPoint());
					if(FAILED(hRes))
					{
						((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("IConnectionPoint::Unadvise"), hRes);
					}
					else
					{
						pGroup->SetOPCDataAccessConnectionPoint(0);
						bRet = TRUE;
					}
					pConPoint.Release();
				}
				pConPointCont.Release();
			}
			else
			{
				//no connection point support
				pGroup->GetInfoServer()->DisplayError(hRes);
			}
		}
		else
		{
			pGroup->GetInfoServer()->DisplayError(hRes);
		}
	}
	else
	{
		pGroup->SetOPCDataAccessConnectionPoint(0);
	}

	return bRet;
}

⌨️ 快捷键说明

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