//------------------------------------------------------------------------------- private CColumnDefinitionSNMP GetSourceColumn(int nIdChampCustom) { if (TypeEntiteSnmp == null) { return(null); } CChampEntiteFromQueryToChampCustom field = TypeEntiteSnmp.ChampsFromQuery.FirstOrDefault(c => c.IdChampCustom != null && c.IdChampCustom == nIdChampCustom); if (field != null) { return(GetSourceColumn(TypeEntiteSnmp.FindSourceObjetInQuery().DataType, field)); } return(null); }
public bool SetSnmpValueForField(int nIdChampCustom, object value) { ITableDefinition table = null; IColumnDefinition col = null; CResultAErreurType <IObjetDeEasyQuery> resObj = TypeEntiteSnmp.FindSourceObjetInQuery(); if (!resObj || resObj.DataType == null) { return(false); } IObjetDeEasyQuery objetSource = resObj.DataType; CEasyQuery query = objetSource.Query; IEnumerable <CChampEntiteFromQueryToChampCustom> lstChamps = TypeEntiteSnmp.ChampsFromQuery; CChampEntiteFromQueryToChampCustom champ = null; foreach (CChampEntiteFromQueryToChampCustom champTest in lstChamps) { if (champTest.IdChampCustom != null && champTest.IdChampCustom == nIdChampCustom) { champ = champTest; break; } } if (champ == null) { return(false); } if (query.FindSource(champ.Champ.ColonneSource, objetSource, out table, out col)) { CEntiteSnmpPourSupervision ettPourSup = null; CColumnDefinitionSNMP colSnmp = col as CColumnDefinitionSNMP; if (colSnmp != null && !colSnmp.IsReadOnly) { ISnmpData snmpVal = AbstractTypeAssignment.GetSnmpFromDotNet(colSnmp.SnmpType, value); if (snmpVal == null && value != null) { snmpVal = new OctetString(value.ToString()); } if (snmpVal != null) { string strIndex = Index; if (champ.Champ.FormuleIndex != null) { if (ettPourSup == null) { ettPourSup = GetEntitePourSupervision(TypeEntiteSnmp.GetTypeEntitePourSupervision(null, false)); } CContexteEvaluationExpression ctx = new CContexteEvaluationExpression(ettPourSup); CResultAErreur restTmp = champ.Champ.FormuleIndex.Eval(ctx); if (restTmp && restTmp.Data != null) { strIndex = restTmp.Data.ToString(); } } if (!strIndex.StartsWith(".")) { strIndex = "." + strIndex; } try { CInterrogateurSnmp agent = new CInterrogateurSnmp(); agent.Connexion.Version = AgentSnmp.SnmpVersion; agent.ConnexionIp = AgentSnmp.SnmpIp; agent.ConnexionPort = AgentSnmp.SnmpPort; agent.ConnexionCommunity = AgentSnmp.SnmpCommunaute; CResultAErreur resTmp = agent.Set(colSnmp.OIDString + strIndex, snmpVal); if (resTmp) { return(true); } } catch { } } } } return(false); }
//----------------------------------------------------------------------- //Si idsChampsAMettreAJour est vide, il faut tout mettre à jour public CResultAErreur SendToSnmp(bool bOnlyModifiés, params int[] idsChampsAMettreAJour) { CResultAErreur result = CResultAErreur.True; if (TypeEntiteSnmp == null) { return(result); } if (TypeEntiteSnmp.ReadOnly) { result.EmpileErreur(I.T("This is a readonly entity|20127")); return(result); } IEnumerable <CChampEntiteFromQueryToChampCustom> lstChamps = TypeEntiteSnmp.ChampsFromQuery; Dictionary <string, object> lstValeurs = new Dictionary <string, object>(); CResultAErreurType <IObjetDeEasyQuery> resObj = TypeEntiteSnmp.FindSourceObjetInQuery( ); if (!resObj || resObj.DataType == null) { result.EmpileErreur(resObj.Erreur); return(result); } IObjetDeEasyQuery objetSource = resObj.DataType; CEasyQuery query = objetSource.Query; Dictionary <string, string> dicOIDStringToChamp = new Dictionary <string, string>(); CEntiteSnmpPourSupervision ettPourSup = null; foreach (CChampEntiteFromQueryToChampCustom champ in lstChamps) { if (champ.IdChampCustom != null && (idsChampsAMettreAJour.Length == 0 || idsChampsAMettreAJour.Contains(champ.IdChampCustom.Value))) { CRelationElementAChamp_ChampCustom rel = CUtilElementAChamps.GetRelationToChamp(this, champ.IdChampCustom.Value); if (rel != null && (bOnlyModifiés && (rel.Row.RowState == DataRowState.Added || rel.Row.RowState == DataRowState.Modified) || !bOnlyModifiés)) { object valeur = rel.Valeur; ITableDefinition table = null; IColumnDefinition col = null; if (query.FindSource(champ.Champ.ColonneSource, objetSource, out table, out col)) { CColumnDefinitionSNMP colSnmp = col as CColumnDefinitionSNMP; if (colSnmp != null && !champ.Champ.IsReadOnly) { ISnmpData snmpVal = AbstractTypeAssignment.GetSnmpFromDotNet(colSnmp.SnmpType, valeur); if (snmpVal == null && valeur != null) { snmpVal = new OctetString(valeur.ToString()); } if (snmpVal != null) { string strIndex = Index; if (champ.Champ.FormuleIndex != null) { if (ettPourSup == null) { ettPourSup = GetEntitePourSupervision(TypeEntiteSnmp.GetTypeEntitePourSupervision(null, false)); } CContexteEvaluationExpression ctx = new CContexteEvaluationExpression(ettPourSup); CResultAErreur restTmp = champ.Champ.FormuleIndex.Eval(ctx); if (restTmp && restTmp.Data != null) { strIndex = restTmp.Data.ToString(); } } if (!strIndex.StartsWith(".")) { strIndex = "." + strIndex; } lstValeurs[colSnmp.OIDString + strIndex] = snmpVal; dicOIDStringToChamp[colSnmp.OIDString + strIndex] = champ.ChampCustom.Nom; } } } } } } if (lstValeurs.Count > 0) { CInterrogateurSnmp agent = new CInterrogateurSnmp(); agent.Connexion.Version = AgentSnmp.SnmpVersion; agent.ConnexionIp = AgentSnmp.SnmpIp; agent.ConnexionPort = AgentSnmp.SnmpPort; agent.ConnexionCommunity = AgentSnmp.SnmpCommunaute; foreach (KeyValuePair <string, object> kv in lstValeurs) { try { CResultAErreur resTmp = agent.Set(kv.Key, kv.Value); if (!resTmp) { string strChamp = null; if (dicOIDStringToChamp.TryGetValue(kv.Key, out strChamp)) { result.EmpileErreur(new CErreurValidation(I.T("Entity @1 (@2), Field @3 : |20105", Libelle, Index, strChamp) + resTmp.Erreur.ToString(), resTmp.IsAvertissement)); } else { result.EmpileErreur(resTmp.Erreur); } if (!result.IsAvertissement) { return(result); } } } catch (Exception e) { result.EmpileErreur(new CErreurException(e)); } } } return(result); }