public void AddSensorIfNotRegistered(string productName, SensorValueBase sensorValue)
        {
            bool needToAdd = false;
            var  newObject = _converter.Convert(productName, sensorValue);

            lock (_dictionaryLock)
            {
                if (!_productSensorsDictionary.ContainsKey(productName))
                {
                    _productSensorsDictionary[productName] = new List <SensorInfo>();
                    needToAdd = true;
                }

                var existingSensor = _productSensorsDictionary[productName]
                                     .FirstOrDefault(s => s.Path == sensorValue.Path);

                if (existingSensor == null || !string.IsNullOrEmpty(sensorValue.Description))
                {
                    _productSensorsDictionary[productName].Add(newObject);
                    needToAdd = true;
                }
            }

            if (needToAdd)
            {
                Task.Run(() => _databaseAdapter.AddSensor(newObject));
            }
        }
 private static void TestRemovedSensorData(SensorValueBase expected, string expectedProductName, SensorData actual)
 {
     Assert.Equal(expectedProductName, actual.Product);
     Assert.Equal(expected.Key, actual.Key);
     Assert.Equal(expected.Path, actual.Path);
     Assert.Equal(TransactionType.Delete, actual.TransactionType);
     Assert.True(DateTime.UtcNow > actual.Time);
 }
예제 #3
0
        private static ValidationResult ValidateSensorPath(this SensorValueBase value)
        {
            var pathLength = value.Path.Split(CommonConstants.SensorPathSeparator).Length;

            return(pathLength > _maxPathLength
                ? PredefinedValidationResults.TooLongPathValidationResult
                : new ValidationResult(value));
        }
        public ValidationResult(SensorValueBase data) : this()
        {
            _warnings = new HashSet <string>();
            _errors   = new HashSet <string>();

            Data = data;

            ResultType = ResultType.Ok;
        }
예제 #5
0
        private async Task FullSensorValueTestAsync(SensorValueBase sensorValue, GetValuesFromCache getCachedValues,
                                                    GetSensorHistoryData getSensorHistoryData, GetSensorInfo getSensorInfo)
        {
            await Task.Delay(100);

            TestSensorDataFromCache(sensorValue, getCachedValues);
            TestSensorHistoryDataFromDB(sensorValue, getSensorHistoryData);
            TestSensorInfoFromDB(sensorValue, getSensorInfo);
        }
예제 #6
0
        public static ValidationResult Validate(this SensorValueBase value)
        {
            if (value == null)
            {
                return(PredefinedValidationResults.NullObjectValidationResult);
            }

            return(value.TypedValidate() + value.ValidateSensorPath());
        }
        private static void FullTestSensorInfo(string productName, SensorValueBase sensorValue, IsSensorRegistered isSensorRegistered,
                                               GetSensorInfo getSensorInfo, GetProductSensors getProductSensors, GetSensorInfoFromDB getSensorFromDB, SensorValuesTester tester)
        {
            var sensorValuePath = sensorValue.Path;

            Assert.True(isSensorRegistered(productName, sensorValuePath));
            tester.TestSensorInfoFromDB(sensorValue, getSensorInfo(productName, sensorValuePath));
            tester.TestSensorInfoFromDB(sensorValue, getProductSensors(productName).FirstOrDefault(s => s.Path == sensorValuePath));
            tester.TestSensorInfoFromDB(sensorValue, getSensorFromDB(productName, sensorValuePath));
        }
예제 #8
0
        private void TestSensorDataFromCache(SensorValueBase sensorValue, GetValuesFromCache getCachedValues)
        {
            var sensorDataFromCache = getCachedValues?.Invoke(new List <string>(1)
            {
                _testProductName
            })
                                      ?.FirstOrDefault(s => s.Path == sensorValue.Path);

            _sensorValuesTester.TestSensorDataFromCache(sensorValue, sensorDataFromCache);
        }
        private ValidationResult(SensorValueBase data, HashSet <string> warnings, HashSet <string> errors) : this(data)
        {
            _warnings = warnings;
            _errors   = errors;

            if (_errors.Count > 0)
            {
                ResultType = ResultType.Error;
            }
            else if (_warnings.Count > 0)
            {
                ResultType = ResultType.Warning;
            }
        }
 protected override string GetStringData(SensorValueBase data)
 {
     try
     {
         StringSensorValue typedData = (StringSensorValue)data;
         return(JsonConvert.SerializeObject(typedData));
         //return Serializer.Serialize(typedData);
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         return(string.Empty);
     }
 }
        public void AddSensor(string productName, SensorValueBase sensorValue)
        {
            var newObject = _converter.Convert(productName, sensorValue);

            lock (_dictionaryLock)
            {
                if (!_productSensorsDictionary.ContainsKey(productName))
                {
                    _productSensorsDictionary[productName] = new List <SensorInfo>();
                }
                _productSensorsDictionary[productName].Add(newObject);
            }

            Task.Run(() => _databaseAdapter.AddSensor(newObject));
        }
예제 #12
0
        internal static string GetTypedData(SensorValueBase sensorValue)
        {
            object sensorData = sensorValue switch
            {
                BoolSensorValue boolSensorValue => GetBoolSensorTypedData(boolSensorValue),
                IntSensorValue intSensorValue => GetIntSensorTypedData(intSensorValue),
                DoubleSensorValue doubleSensorValue => GetDoubleSensorTypedData(doubleSensorValue),
                StringSensorValue stringSensorValue => GetStringSensorTypedData(stringSensorValue),
                IntBarSensorValue intBarSensorValue => GetIntBarSensorTypedData(intBarSensorValue),
                DoubleBarSensorValue doubleBarSensorValue => GetDoubleBarSensorTypedData(doubleBarSensorValue),
                FileSensorBytesValue fileSensorBytesValue => GetFileSensorBytesTypedData(fileSensorBytesValue),
                UnitedSensorValue unitedSensorValue => GetUnitedSensorTypedData(unitedSensorValue),
                _ => null,
            };

            return(sensorData != null?JsonSerializer.Serialize(sensorData) : string.Empty);
        }
        private static void FullTestRemovedSensor(string productName, SensorValueBase sensorValue,
                                                  IsSensorRegistered isSensorRegistered, GetSensorInfo getSensorInfo, GetProductSensors getProductSensors,
                                                  GetQueueValues getQueueValues, GetAllSensorHistory getAllSensorHistory,
                                                  GetSensorInfoFromDB getSensorFromDB, SensorValuesTester tester)
        {
            string sensorValuePath = sensorValue.Path;

            Assert.False(isSensorRegistered(productName, sensorValuePath));
            Assert.Null(getSensorInfo(productName, sensorValuePath));
            Assert.Null(getProductSensors(productName).FirstOrDefault(s => s.Path == sensorValuePath));
            Assert.Empty(getQueueValues(new List <string>()
            {
                productName
            }));
            Assert.Empty(getAllSensorHistory(productName, sensorValuePath));
            tester.TestSensorInfoFromDB(sensorValue, getSensorFromDB(productName, sensorValuePath));
        }
예제 #14
0
 internal static string GetStringValue(SensorValueBase sensorValue, DateTime timeCollected) =>
 sensorValue switch
 {
예제 #15
0
        private void TestSensorInfoFromDB(SensorValueBase sensorValue, GetSensorInfo getSensorInfo)
        {
            var sensorInfoFromDB = getSensorInfo?.Invoke(_testProductName, sensorValue.Path);

            _sensorValuesTester.TestSensorInfoFromDB(sensorValue, sensorInfoFromDB);
        }
예제 #16
0
        private void TestSensorHistoryDataFromDB(SensorValueBase sensorValue, GetSensorHistoryData getSensorHistoryData)
        {
            var sensorHistoryDataFromDB = getSensorHistoryData?.Invoke(_testProductName, sensorValue.Path);

            SensorValuesTester.TestSensorHistoryDataFromDB(sensorValue, sensorHistoryDataFromDB);
        }
예제 #17
0
 private static ValidationResult TypedValidate(this SensorValueBase value) =>
 value switch
 {
예제 #18
0
 private string GetSensorDataStringValue(SensorValueBase sensorValue) =>
 sensorValue switch
 {
 protected override string GetStringData(SensorValueBase data)
 {
     throw new NotImplementedException();
 }
 public static SensorType GetSensorType(SensorValueBase sensorValue) =>
 sensorValue switch
 {
 public ValidationResult(SensorValueBase data, string warning) : this(data)
 {
     _warnings.Add(warning);
     ResultType = ResultType.Warning;
 }
 private static void FullTestUpdatedSensorInfo(SensorInfo expected, SensorInfo actual, SensorValueBase sensorValue)
 {
     Assert.NotNull(actual);
     Assert.Equal(expected.Description, actual.Description);
     Assert.Equal(expected.Path, actual.Path);
     Assert.Equal(expected.ProductName, actual.ProductName);
     Assert.Equal(expected.ExpectedUpdateInterval, actual.ExpectedUpdateInterval);
     Assert.Equal(expected.Unit, actual.Unit);
     Assert.Equal(sensorValue.Path, actual.SensorName);
     Assert.Equal(SensorValuesTester.GetSensorValueType(sensorValue), actual.SensorType);
     Assert.NotEqual(expected.SensorName, actual.SensorName);
     Assert.NotEqual(expected.SensorType, actual.SensorType);
     Assert.Empty(actual.ValidationParameters);
 }
 public static SensorData Convert(this SensorValueBase sensorValue, string productName,
                                  DateTime timeCollected, TransactionType transactionType) =>
예제 #24
0
 protected abstract string GetStringData(SensorValueBase data);
 public static SensorDataEntity Convert(this SensorValueBase sensorValue, DateTime timeCollected,
                                        SensorStatus validationStatus = SensorStatus.Unknown) =>
 public static SensorInfo Convert(this SensorValueBase sensorValue, string productName) =>