Пример #1
0
void CShdrEchoInstance::Init(std::string devicesxmlfilename, 
									 std::string shdrfilename, 
									 int ipport, 
									 double dOverride,
									 std::string devicename)
{
	DevicesXmlFilename()=devicesxmlfilename;
	ShdrFilename() =shdrfilename;
	IpAddress() ="127.0.0.1"; // really cant be anything else
	IpPort()=ipport;
	DeviceName() = devicename;
	Override()=dOverride;
	_bFaultThread=false;

	if(DevicesXmlFilename().empty())
		DevicesXmlFilename()=ExeDirectory() + "devices.xml";

	_bRepeat=true;

	_backend = new CoComMTCShdrBackEnd();
	_backend->Init(_bstr_t(IpAddress().c_str()), IpPort(), _bstr_t(DeviceName().c_str()));
	_backend->MyLogger().AddListener(boost::bind(&CShdrEchoInstance::EchoShdr, this,_1));

	_dict = _deviceparser.ReadDeviceDescription(DevicesXmlFilename());
	std::string storemap,itemlist, typelist;
	int i=0;
	for(DataDictionary::iterator it = _dict.begin(); it!=_dict.end(); it++, i++)
	{
		if((*it).first.empty())
		{
			i--;
			continue;
		}
		if(i>0) itemlist+=",";
		if(i>0) typelist+=",";
		storemap+=(*it).first+"="+(*it).first+"\n";  // isomorphic: alias = name
		itemlist+=(*it).first;
		typelist+=_dict[(*it).first];
	}
	_backend->StoreTagMap(_bstr_t(storemap.c_str()));
	_backend->StoreTypeMap(_bstr_t(itemlist.c_str()),bstr_t(typelist.c_str()));

	Open();
	_state=Ready;
}
	int GetExecutableDirectory( char **p_ppBuffer, ssize_t p_BufferSize )
	{
		char LinkName[ 64 ];
		pid_t PID;

		PID = getpid( );

		if( snprintf( LinkName, sizeof( LinkName ), "/proc/%i/exe", PID ) < 0 )
		{
			std::cout << "<ERROR> Failed to get executable process" <<
				std::endl;
			return 1;
		}

		char FullPath[ p_BufferSize ];
		int Return = readlink( LinkName, FullPath, p_BufferSize );

		if( Return == -1 )
		{
			std::cout << "<ERROR> Failed to get executable directory" <<
				std::endl;
			return 1;
		}

		if( Return >= p_BufferSize )
		{
			std::cout << "<ERROR> Directory path longer than expected" <<
				std::endl;
			errno = ERANGE;
			return 1;
		}

		FullPath[ Return ] = '\0';

		std::string ExeDirectory( FullPath );
		size_t LastSlash = ExeDirectory.find_last_of( "/" );
		ExeDirectory.resize( LastSlash );
		ExeDirectory.append( "/" );

		( *p_ppBuffer ) = new char[ ExeDirectory.size( ) + 1 ];
		strncpy( *p_ppBuffer, ExeDirectory.c_str( ), ExeDirectory.size( ) );
		( *p_ppBuffer )[ ExeDirectory.size( ) ] = '\0';

		return 0;
	}
CShdrFileEchoView::CShdrFileEchoView()
{
	_bRepeat=true;
	_dOverride=1.0;

	_backend = new CoComMTCShdrBackEnd();
	_backend->Init(_bstr_t("127.0.0.1"), 7878, _bstr_t(""));
	_backend->MyLogger().AddListener(boost::bind(&CShdrFileEchoView::EchoShdr, this,_1));

	_dict = _deviceparser.ReadDeviceDescription(ExeDirectory() + "devices.xml");
	std::string storemap,itemlist,typelist;
	int i=0;
	for(DataDictionary::iterator it = _dict.begin(); it!=_dict.end(); it++, i++)
	{
		if(i>0) itemlist+=",";
		if(i>0) typelist+=",";
		storemap+=(*it).first+"="+(*it).first+"\n";  // isomorphic: alias = name
		itemlist+=(*it).first;
		typelist+=_dict[(*it).second];
	}
	_backend->StoreTagMap(_bstr_t(storemap.c_str()));
	_backend->StoreTypeMap(_bstr_t(itemlist.c_str()),bstr_t(typelist.c_str()));
}
int COpcAdapter::ReadOPCSettings()
{
	try 
	{
		// Read OPC Server details
		COPCBaseClass::ReadOPCSettings("");
		_nFakeSpindle= _config.GetSymbolValue("OPCSERVER.FakeSpindle", "0").toNumber<long>();
		_nAlarmsEnabled= _config.GetSymbolValue("OPCSERVER.AlarmsEnabled", "0").toNumber<long>();
		_nMultiSpindle= _config.GetSymbolValue("OPCSERVER.MultiSpindle", "1").toNumber<long>();


		if(_sTagSectionName.empty())
			throw std::exception(_T("Error: OPCSERVER Tag name Undefined\n"));
		if(_opcservermachine.empty())
			throw std::exception(_T("Error: [OPCSERVER] Opc Server Machine (IP) Undefined\n"));

		_sClsid= GetSymbolValue(_sTagSectionName, "CLSID", "");
		_sClsid = "{" +_sClsid + "}";
		if(FAILED(CLSIDFromString(_bstr_t(_sClsid.c_str()) , &_gOpcServerClsid)))
			throw std::exception("Misformed or mistyped OPC Server Class ID\n");
		_gszOPCServerName= GetSymbolValue(_sTagSectionName, "SERVERNAME", "");
		_gszCncControllerProcessName= GetSymbolValue(_sTagSectionName, "CNCProcessName", "");
		_CNCTaskListCmd	= GetSymbolValue(_sTagSectionName, "CNCTaskListCmd", ""); // assume no remote task list command


		_gServerProgId= GetSymbolValue(_sTagSectionName, "ProgId", ""); // assume no remote task list command

		GLogger.LogMessage(StdStringFormat("CONFIGURATION\n================================\n"));
		GLogger.LogMessage(StdStringFormat("Device                   = %s           \n", _sTagSectionName.c_str()));
		GLogger.LogMessage(StdStringFormat("Fake Spindle             = %d   \n", _nFakeSpindle));
		GLogger.LogMessage(StdStringFormat("Alarms Enabled           = %d  NO EFFECT\n", _nAlarmsEnabled));
		GLogger.LogMessage(StdStringFormat("Mutliple Spindles        = %d NO EFFECT\n", _nMultiSpindle));
		GLogger.LogMessage(StdStringFormat("CNCProcessName           = %s\n", _gszCncControllerProcessName.c_str()));
		GLogger.LogMessage(StdStringFormat("OPCServerName            = %s\n", _gszOPCServerName.c_str()));
		GLogger.LogMessage(StdStringFormat("CLSID                    = %s\n", _sClsid.c_str()));
		GLogger.LogMessage(StdStringFormat("AutoConnect              = %d\n", _bAutoConnect));
		GLogger.LogMessage(StdStringFormat("AutoReconnectMaxAttempts = %d\n", _nMaxAutoReconnects));
		GLogger.LogMessage(StdStringFormat("QueryServerPeriod        = %d ms (to connect)\n", _nQueryServerPeriod));
		GLogger.LogMessage(StdStringFormat("ProcessPriority          = %x\n", _nProcessPriority));
		GLogger.LogMessage(StdStringFormat("SynchronousUpdate        = %d\n", _bSynchronousUpdate));
		GLogger.LogMessage(StdStringFormat("ServerRate               = %d ms (when connected)\n", _nOPCServerRate));
		GLogger.LogMessage(StdStringFormat("PingTimeout              = %d ms\n", _nPingTimeout));
		GLogger.LogMessage(StdStringFormat("Agent Http Port          = %d ms\n", Globals.HttpPort));

		GLogger.LogMessage(Globals.Dump().c_str());
		GLogger.LogMessage(StdStringFormat("================================\n"));
				

		// Now parse the tag information into MTConnect information
		std::vector<std::string> tags = GetSectionKeys(_sTagSectionName);
		if(tags.size() < 1)
			throw std::exception(StdStringFormat("Error: No [%s] Section\n", _sTagSectionName.c_str()).c_str());
		for(int i=0; i<tags.size(); i++)
		{
			VarTypes vt;

			// make sure its is an OPC Tag
			if(tags[i].find(_T("Tag.")) == 0)
			{
				OpcItem * item = new OpcItem();
				item->_alias=tags[i];
				item->_alias=ReplaceOnce(item->_alias, _T("Tag."), _T(""));
				item->vt=VT_BSTR;
				if(item->_alias.find(_T("Enum."))==0)
				{
					item->_alias=ReplaceOnce(item->_alias, _T("Enum."), _T(""));
					item->_subtype="enumeration";
				}
				else if(item->_alias.find(_T("Const."))==0)
				{
					item->_alias=ReplaceOnce(item->_alias,_T("Const."), _T(""));
					item->vt= VT_USERDEFINED;
				}
				else if(item->_alias.find(_T("Prog."))==0)
				{
					item->_alias=ReplaceOnce(item->_alias,_T("Prog."), _T(""));
					item->vt= VT_USERDEFINED;
					item->_subtype="program";
				}

				if(item->_alias.find(_T("Sample."))==0)
				{
					item->_alias=ReplaceOnce(item->_alias,_T("Sample."), _T(""));
					item->_type=_T("Sample");
					//_mtcagent->AddSample(this->_device, item->_alias);
				}
				else if(item->_alias.find(_T("Event."))==0)
				{
					item->_alias=ReplaceOnce(item->_alias,_T("Event."), _T(""));
					item->_type=_T("Event");
					//_mtcagent->AddEvent(this->_device, item->_alias);
				}
				else if(item->_alias.find(_T("OPC."))==0)  // reads opc values does not send to MT Connect
				{
					item->_alias=ReplaceOnce(item->_alias, _T("OPC."), _T(""));  // OPC value for scripting
					item->_type=_T("OPC");
				}
				else if(item->_alias.find(_T("Condition."))==0)
				{
					item->_alias=ReplaceOnce(item->_alias,_T("Condition."), _T(""));
					item->_type="Condition";
					//_mtcagent->AddCondition(this->_device, item->_alias);
				}
				std::string tmp= GetSymbolValue(_sTagSectionName, tags[i],"");
				item->_tagname=tmp;
				opcitems.push_back(item);
			}

		}
		// Test to see if we have a power, if not add one.
		//if(opcitems.FindItem("power")==NULL)
		//	_mtcagent->AddEvent(this->_device, "power");

#ifdef ALARMS
		//Alarm Management
		_alarmconfig.load(ExeDirectory() + "English\\Alarms.cfg");
#endif
	}
	catch(std::string errmsg)
	{
		_gszStartupErrors+=errmsg;
		ErrorMessage( errmsg + "\n");
		return E_FAIL;

	}
	catch(std::exception e)
	{
		ErrorMessage( e.what());
		return E_FAIL;
	}
	catch(...)
	{
		ErrorMessage( "ReadOPCSettings() OPC Items Ini error \n");
		return E_FAIL;

	}
	GLogger.LogMessage(StdStringFormat("COpcAdapter::ReadOPCSettings() Done for Device = %s \n",_device.c_str()));

	return S_OK;
}