public DbDataReaderSimulator(int datasourceId, DatasourceRecord.DataTypeEnum dataType)
        {
            _datasourceId = datasourceId;
            _dataType = dataType;

            _random = new Random();
        }
Esempio n. 2
0
        public virtual bool Equivalent(DatasourceRecord compare)
        {
            if (compare == null)
            {
                return(false);
            }
            if (this == compare)
            {
                return(true);
            }

            if (DatasourceId != compare.DatasourceId)
            {
                return(false);
            }
            if (Timestamp != compare.Timestamp)
            {
                return(false);
            }
            if (IntervalSeconds != compare.IntervalSeconds)
            {
                return(false);
            }
            if (Value == null ^ compare.Value == null || (Value != null && !Value.SequenceEqual(compare.Value)))
            {
                return(false);
            }
            if (EncodedDataType != compare.EncodedDataType)
            {
                return(false);
            }

            return(true);
        }
        public static void SetStringValue(this DatasourceRecord record, string value)
        {
            ThrowExceptionOnInvalidConversion(record.DataType, DatasourceRecord.DataTypeEnum.String);

            record.Value    = Encoding.UTF8.GetBytes(value);
            record.DataType = DatasourceRecord.DataTypeEnum.String;
        }
        public static void SetDoubleValue(this DatasourceRecord record, double value)
        {
            ThrowExceptionOnInvalidConversion(record.DataType, DatasourceRecord.DataTypeEnum.Double);

            record.Value    = BitConverter.GetBytes(value);
            record.DataType = DatasourceRecord.DataTypeEnum.Double;
        }
        public static void SetIntValue(this DatasourceRecord record, int value)
        {
            ThrowExceptionOnInvalidConversion(record.DataType, DatasourceRecord.DataTypeEnum.Integer);

            record.Value    = BitConverter.GetBytes(value);
            record.DataType = DatasourceRecord.DataTypeEnum.Integer;
        }
        public static decimal GetDecimalValue(this DatasourceRecord record)
        {
            ThrowExceptionOnInvalidConversion(record.DataType, DatasourceRecord.DataTypeEnum.Decimal);

            using (var memoryStream = new MemoryStream(record.Value))
            {
                using (var binaryReader = new BinaryReader(memoryStream))
                {
                    return(binaryReader.ReadDecimal());
                }
            }
        }
        public static void SetDecimalValue(this DatasourceRecord record, decimal value)
        {
            ThrowExceptionOnInvalidConversion(record.DataType, DatasourceRecord.DataTypeEnum.Decimal);

            using (var memoryStream = new MemoryStream())
            {
                using (var binaryWriter = new BinaryWriter(memoryStream))
                {
                    binaryWriter.Write(value);
                    record.Value = memoryStream.ToArray();
                }
            }
            record.DataType = DatasourceRecord.DataTypeEnum.Decimal;
        }
        public virtual bool Equivalent(DatasourceRecord compare)
        {
            if (compare == null)
                return false;
            if (this == compare)
                return true;

            if (DatasourceId != compare.DatasourceId)
                return false;
            if (Timestamp != compare.Timestamp)
                return false;
            if (IntervalSeconds != compare.IntervalSeconds)
                return false;
            if (Value == null ^ compare.Value == null || (Value != null && !Value.SequenceEqual(compare.Value)))
                return false;
            if (EncodedDataType != compare.EncodedDataType)
                return false;

            return true;
        }
Esempio n. 9
0
        public override bool Read()
        {
            _lastTime = _lastTime.AddSeconds(1);

            _currentRecord              = new DatasourceRecord();
            _currentRecord.Timestamp    = _lastTime;
            _currentRecord.DatasourceId = _datasourceId;
            _currentRecord.DataType     = _dataType;

            if (_dataType == DatasourceRecord.DataTypeEnum.Double)
            {
                _currentRecord.SetDoubleValue(_random.NextDouble());
            }
            else if (_dataType == DatasourceRecord.DataTypeEnum.String)
            {
                _currentRecord.SetStringValue(_random.NextDouble().ToString());
            }
            else
            {
                throw new NotSupportedException("Simulator doesn't support " + _dataType.ToString());
            }

            return(true);
        }
     private static void ThrowExceptionOnInvalidConversion(DatasourceRecord.DataTypeEnum from,
 DatasourceRecord.DataTypeEnum to)
     {
         if (!(from == DatasourceRecord.DataTypeEnum.Undefined || from == to))
             throw new NotSupportedException();
     }
        public override bool Read()
        {
            _lastTime = _lastTime.AddSeconds(1);

            _currentRecord = new DatasourceRecord();
            _currentRecord.Timestamp = _lastTime;
            _currentRecord.DatasourceId = _datasourceId;
            _currentRecord.DataType = _dataType;

            if(_dataType == DatasourceRecord.DataTypeEnum.Double)
            {
                _currentRecord.SetDoubleValue(_random.NextDouble());
            }
            else if(_dataType == DatasourceRecord.DataTypeEnum.String)
            {
                _currentRecord.SetStringValue(_random.NextDouble().ToString());
            }
            else
            {
                throw new NotSupportedException("Simulator doesn't support " + _dataType.ToString());
            }

            return true;
        }
        public static string GetStringValue(this DatasourceRecord record)
        {
            ThrowExceptionOnInvalidConversion(record.DataType, DatasourceRecord.DataTypeEnum.String);

            return(Encoding.UTF8.GetString(record.Value));
        }
        public static double GetDoubleValue(this DatasourceRecord record)
        {
            ThrowExceptionOnInvalidConversion(record.DataType, DatasourceRecord.DataTypeEnum.Double);

            return(BitConverter.ToDouble(record.Value, 0));
        }
        public static int GetIntValue(this DatasourceRecord record)
        {
            ThrowExceptionOnInvalidConversion(record.DataType, DatasourceRecord.DataTypeEnum.Integer);

            return(BitConverter.ToInt32(record.Value, 0));
        }