public MappingEntry(PlcObject plcObject) { PlcObject = plcObject ?? ExceptionThrowHelper.ThrowArgumentNullException <PlcObject>(nameof(plcObject)); BaseBinding = new PlcObjectBinding(new PlcRawData(plcObject.ByteSize), plcObject, 0, 0, true); Variables = new Dictionary <string, Tuple <int, PlcObject> >(); Bindings = new Dictionary <string, PlcObjectBinding>(); }
public object Deserialize(Type t, byte[] data) { var binding = _mappingEntryProvider.GetMappingEntryForType(t).BaseBinding; if (data.Length < binding.Size) { ExceptionThrowHelper.ThrowArgumentOutOfRangeException(nameof(data)); } return(binding.ConvertFromRaw(data)); }
/// <summary> /// Return all variable names of an mapping /// </summary> /// <param name="mapping"></param> /// <returns></returns> public IEnumerable <string> GetVariablesOf(string mapping) { var result = new List <string>(); if (EntriesByName.TryGetValue(mapping, out IEntry entry)) { return(PlcObjectResolver.GetLeafs(entry.PlcObject, result)); } ExceptionThrowHelper.ThrowMappingNotFoundException(mapping); return(null); }
/// <summary> /// Sequential Equal with offset and length specification /// </summary> /// <typeparam name="TSource"></typeparam> /// <param name="first"></param> /// <param name="firstStartIndex"></param> /// <param name="second"></param> /// <param name="secondStartIndex"></param> /// <param name="length"></param> /// <param name="comparer"></param> /// <returns></returns> public static bool SequenceEqual <TSource>(this IEnumerable <TSource> first, int firstStartIndex, IEnumerable <TSource> second, int secondStartIndex, int length = -1, IEqualityComparer <TSource> comparer = null) { if (comparer == null) { comparer = EqualityComparer <TSource> .Default; } ; if (first == null) { ExceptionThrowHelper.ThrowArgumentNullException(nameof(first)); } if (second == null) { ExceptionThrowHelper.ThrowArgumentNullException(nameof(second)); } using (var e1 = first.GetEnumerator()) using (var e2 = second.GetEnumerator()) { var skip = Math.Max(firstStartIndex, secondStartIndex); for (int i = 0; i < skip; i++) { if (i < firstStartIndex) { e1.MoveNext(); } if (i < secondStartIndex) { e2.MoveNext(); } } var index = 0; while (e1.MoveNext()) { if (!(e2.MoveNext() && comparer.Equals(e1.Current, e2.Current))) { return(false); } index++; if (length > 0 && index >= length) { return(true); } } if (e2.MoveNext()) { return(false); } } return(true); }
/// <summary> /// Add a type with and a list og MappingAttributes to register this type as an mapping for read and write operations /// </summary> /// <param name="type">Could be any type</param> /// <param name="mappingAttributes">mappings to add</param> /// <returns></returns> public bool AddMapping(Type type, params MappingAttribute[] mappingAttributes) { if (type == null) { ExceptionThrowHelper.ThrowArgumentNullException <Type>(nameof(type)); } if (!mappingAttributes.Any()) { ExceptionThrowHelper.ThrowMappingAttributeNotFoundForTypeException(type); } return(AddMappingsInternal(type, mappingAttributes)); }
/// <summary> /// Add a type with an MappingAttribute to register this type as an mapping for read and write operations /// </summary> /// <param name="type">Has to be a type with at least one MappingAttribute</param> /// <returns></returns> public bool AddMapping(Type type) { if (type == null) { ExceptionThrowHelper.ThrowArgumentNullException <Type>(nameof(type)); } var mappingAttributes = type.GetTypeInfo().GetCustomAttributes <MappingAttribute>().ToList(); if (!mappingAttributes.Any()) { ExceptionThrowHelper.ThrowMappingAttributeNotFoundForTypeException(type); } return(AddMappingsInternal(type, mappingAttributes)); }
public PlcDataMapper(int pduSize, ReadOperation readEventHandler, WriteOperation writeEventHandler, UpdateMonitoring updateHandler, ReadBlockInfo blockInfoHandler, OptimizerType optimizer = OptimizerType.Block) { PduSize = pduSize; _readEventHandler = readEventHandler; _writeEventHandler = writeEventHandler; _updateHandler = updateHandler; _blockInfoHandler = blockInfoHandler; Optimizer = OptimizerFactory.CreateOptimizer(optimizer); ReadDataBlockSize = pduSize - _readDataHeaderLength; if (ReadDataBlockSize <= 0) { ExceptionThrowHelper.ThrowInvalidPduSizeException(_readDataHeaderLength); } PlcMetaDataTreePath.CreateAbsolutePath(PlcObjectResolver.RootNodeName); }