/// <summary>
            /// Convert to DomainObject.
            /// </summary>
            public T ConvertToEntity <T>(FluxRecord fluxRecord)
            {
                if (typeof(T) != typeof(DomainEntity))
                {
                    throw new NotSupportedException($"This converter doesn't supports: {typeof(DomainEntity)}");
                }

                var customEntity = new DomainEntity
                {
                    SeriesId   = Guid.Parse(Convert.ToString(fluxRecord.GetValueByKey("series_id")) !),
                    Value      = Convert.ToDouble(fluxRecord.GetValueByKey("data")),
                    Timestamp  = fluxRecord.GetTime().GetValueOrDefault().ToDateTimeUtc(),
                    Properties = new List <DomainEntityAttribute>()
                };

                foreach (var(key, value) in fluxRecord.Values)
                {
                    if (key.StartsWith("property_"))
                    {
                        var attribute = new DomainEntityAttribute
                        {
                            Name = key.Replace("property_", string.Empty), Value = Convert.ToInt32(value)
                        };

                        customEntity.Properties.Add(attribute);
                    }
                }

                return((T)Convert.ChangeType(customEntity, typeof(T)));
            }
        /// <summary>
        /// Maps FluxRecord into custom POCO class.
        /// </summary>
        /// <param name="record">the Flux record</param>
        /// <typeparam name="T">the POCO type</typeparam>
        /// <returns></returns>
        /// <exception cref="InfluxException"></exception>
        internal T ToPoco <T>(FluxRecord record)
        {
            Arguments.CheckNotNull(record, "Record is required");

            try
            {
                var type = typeof(T);
                var poco = (T)Activator.CreateInstance(type);

                // copy record to case insensitive dictionary (do this once)
                var recordValues =
                    new Dictionary <string, object>(record.Values, StringComparer.InvariantCultureIgnoreCase);

                var properties = _attributesCache.GetProperties(type);

                foreach (var property in properties)
                {
                    var attribute = _attributesCache.GetAttribute(property);

                    if (attribute != null && attribute.IsTimestamp)
                    {
                        SetFieldValue(poco, property, record.GetTime());
                    }
                    else
                    {
                        var columnName = _attributesCache.GetColumnName(attribute, property);

                        string col = null;

                        if (recordValues.ContainsKey(columnName))
                        {
                            col = columnName;
                        }
                        else if (recordValues.ContainsKey("_" + columnName))
                        {
                            col = "_" + columnName;
                        }

                        if (!string.IsNullOrEmpty(col))
                        {
                            // No need to set field value when column does not exist (default poco field value will be the same)
                            if (recordValues.TryGetValue(col, out var value))
                            {
                                SetFieldValue(poco, property, value);
                            }
                        }
                    }
                }

                return(poco);
            }
            catch (Exception e)
            {
                throw new InfluxException(e);
            }
        }
Пример #3
0
            /// <summary>
            /// Convert to DomainObject.
            /// </summary>
            public T ConvertToEntity <T>(FluxRecord fluxRecord)
            {
                if (typeof(T) != typeof(Sensor))
                {
                    throw new NotSupportedException($"This converter doesn't supports: {typeof(T)}");
                }

                var customEntity = new Sensor
                {
                    Type      = Convert.ToString(fluxRecord.GetValueByKey("type")),
                    Version   = Convert.ToString(fluxRecord.GetValueByKey("version")),
                    Value     = Convert.ToDouble(fluxRecord.GetValueByKey("data")),
                    Timestamp = fluxRecord.GetTime().GetValueOrDefault().ToDateTimeUtc(),
                };

                return((T)Convert.ChangeType(customEntity, typeof(T)));
            }