public SymbolNavigator(IInstanceCollection <T> symbols) { this._pathSeparator = '.'; if (symbols == null) { throw new ArgumentNullException("symbols"); } this._symbols = symbols; }
public SymbolIterator(IInstanceCollection <T> coll, Func <T, bool> predicate) : this(coll, false, predicate) { switch (coll.Mode) { case InstanceCollectionMode.Names: this._recurse = false; return; case InstanceCollectionMode.Path: this._recurse = false; return; case InstanceCollectionMode.PathHierarchy: this._recurse = true; return; } throw new NotSupportedException(); }
public SymbolIterator(IInstanceCollection <ISymbol> coll) : this(coll, null) { }
public SymbolIterator(IInstanceCollection <ISymbol> coll, Func <ISymbol, bool> predicate) : base(coll, predicate) { }
public async Task <JObject> ReadPersistentVariables(TcAdsClient client, IInstanceCollection <ISymbol> symbols) { var jobj = new JObject(); try { if (client.IsConnected) { var iterator = new SymbolIterator(symbols, s => s.IsPersistent && s.InstancePath.Split('.').Length == 2 && !s.InstancePath.Contains("[")); var variables = new Dictionary <string, List <JObject> >(); foreach (var symbol in iterator) { var splitPath = symbol.InstancePath.Split('.'); var globalName = splitPath.First(); var localName = splitPath.Last(); if (!variables.ContainsKey(globalName)) { variables.Add(globalName, new List <JObject>()); } try { logger.Debug($"reading symbol '{symbol.InstancePath}' in json format..."); currentTaskSubject.OnNext($"Reading {symbol.InstancePath}..."); var json = await client.ReadJson(symbol.InstancePath, force : true); if (json.ContainsKey(localName)) { variables[globalName].Add(json); } else { var innerObject = new JObject(); innerObject.Add(localName, json); variables[globalName].Add(innerObject); } } catch (Exception e) { logger.Error(string.Format(Resources.ErrorDuringReadingVariable0InJsonFormat, symbol.InstancePath), e); } } foreach (var element in variables) { var uo = new JObject(); foreach (var p in element.Value) { foreach (var up in p.Properties()) { uo.Add(up); } } jobj.Add(element.Key, uo); } } } catch (Exception e) { logger.Error(Resources.ErrorWhileReadingPersistentVariables, e); } currentTaskSubject.OnNext(string.Empty); logger.Debug($"Persistent variable successfully downloaded!"); return(jobj); }
public ReadOnlySymbolCollection(IInstanceCollection <T> coll) : base(coll) { }
internal static bool TryGetSubItem(IInstanceCollection <T> coll, string[] pathSplit, int splitIndex, out T symbol) { SymbolParser.ArrayIndexType type; T local = default(T); symbol = default(T); string nameWithIndices = pathSplit[splitIndex]; string instanceName = null; IList <int[]> jaggedIndices = null; string indicesStr = null; bool flag = SymbolParser.TryParseArrayElement(nameWithIndices, out instanceName, out indicesStr, out jaggedIndices, out type); if (flag) { nameWithIndices = instanceName; } bool flag2 = nameWithIndices[nameWithIndices.Length - 1] == '^'; if (flag2) { nameWithIndices = nameWithIndices.Substring(0, nameWithIndices.Length - 1); } IList <T> list2 = null; if (coll.TryGetInstanceByName(nameWithIndices, out list2)) { local = list2[0]; if (flag) { ISymbol symbol2 = null; IArrayInstance instance = local as IArrayInstance; IReferenceInstanceAccess access = local as IReferenceInstanceAccess; if (instance != null) { instance.TryGetElement(jaggedIndices, out symbol2); } else if (access != null) { access.TryGetElement(jaggedIndices, out symbol2); } local = (T)symbol2; } if ((local == null) || (splitIndex >= (pathSplit.Length - 1))) { symbol = !((splitIndex == (pathSplit.Length - 1)) & flag2) ? local : ((ISymbolInternal)local).SubSymbolsInternal[0]; } else { ISymbol symbol3 = local as ISymbol; if (symbol3.IsContainerType) { ISymbolCollection subSymbolsInternal = ((ISymbolInternal)symbol3).SubSymbolsInternal; if (flag2) { subSymbolsInternal = ((ISymbolInternal)subSymbolsInternal[0]).SubSymbolsInternal; } if ((subSymbolsInternal != null) && (subSymbolsInternal.Count > 0)) { T local2 = default(T); splitIndex++; if (InstanceCollection <T> .TryGetSubItem((IInstanceCollection <T>)subSymbolsInternal, pathSplit, splitIndex, out local2)) { symbol = local2; } else { symbol = default(T); } } } } } return(((T)symbol) != null); }
public ReadOnlyInstanceCollection(IInstanceCollection <T> coll) : base(coll) { this.mode = coll.Mode; }
public async Task <JObject> ReadGlobalPersistentVariables(AdsClient client, IInstanceCollection <ISymbol> symbols) { var jobj = new JObject(); try { if (client.IsConnected) { var iterator = new SymbolIterator(symbols, s => s.IsPersistent && s.InstancePath.Split('.').Length >= 2 && !s.InstancePath.Contains("[")) ; var persistentSymbols = iterator.Where(s => s.Parent != null ? !iterator.Contains(s.Parent) : true); var variables = new Dictionary <string, List <JObject> >(); foreach (var symbol in persistentSymbols) { var splitPath = symbol.InstancePath.Split('.'); var localName = splitPath.Last(); var globalName = symbol.InstancePath.Replace($".{localName}", string.Empty); if (!variables.ContainsKey(globalName)) { variables.Add(globalName, new List <JObject>()); } try { logger.Debug($"reading symbol '{symbol.InstancePath}' in json format..."); currentTaskSubject.OnNext($"Reading {symbol.InstancePath}..."); var json = await client.ReadJson(symbol.InstancePath, force : true); if (json.ContainsKey(localName)) { variables[globalName].Add(json); } else { var innerObject = new JObject(); innerObject.Add(localName, json); variables[globalName].Add(innerObject); } } catch (Exception e) { logger.Error(string.Format(Resources.ErrorDuringReadingVariable0InJsonFormat, symbol.InstancePath), e); } } foreach (var element in variables.OrderBy(pair => pair.Key.Length)) { var uo = new JObject(); foreach (var p in element.Value) { foreach (var up in p.Properties()) { uo.Add(up); } } var path = element.Key.Split("."); if (path.Count() == 1) { jobj.Add(element.Key, uo); } else { for (var i = 0; i < path.Length - 1; i++) { if ((jobj.SelectToken(string.Join(".", path.Take(i + 1))) as JObject) == null) { if (i > 0) { (jobj.SelectToken(string.Join(".", path.Take(i))) as JObject).Add(path[i], new JObject()); } else { jobj.Add(path[i], new JObject()); } } } try { (jobj.SelectToken(string.Join(".", path.Take(path.Length - 1))) as JObject).Add(path.Last(), uo); } catch (ArgumentException) { var token = (jobj.SelectToken(string.Join(".", path)) as JObject); if (token != null) { foreach (var prop in uo.Properties()) { token.Add(prop.Name, prop.Value); } } } } } } } catch (Exception e) { logger.Error(Resources.ErrorWhileReadingPersistentVariables, e); } currentTaskSubject.OnNext(string.Empty); logger.Debug($"Persistent variable successfully downloaded!"); return(jobj); }
public ReadOnlySymbolCollection(IInstanceCollection <ISymbol> symbols) : base(symbols) { }