public SensorDataEntity GetOneValueSensorValue(string productName, string path) { try { string value = string.Empty; var key = Encoding.UTF8.GetBytes(GetSensorReadValueKey(productName, path)); lock (_accessLock) { using (var iterator = _database.CreateIterator()) { iterator.Seek(key); if (iterator.IsValid()) { if (iterator.Key().StartsWith(key)) { value = iterator.ValueAsString(); } } } } SensorDataEntity entity = JsonSerializer.Deserialize <SensorDataEntity>(value); return(entity); } catch (Exception e) { _logger.Error(e, $"Failed to get one value sensor value for {productName}/{path}"); } return(null); }
private void AddUnitedValue(UnitedSensorValue value) { try { string productName = _productManager.GetProductNameByKey(value.Key); if (!_productManager.IsSensorRegistered(productName, value.Path)) { _productManager.AddSensor(productName, value); } DateTime timeCollected = DateTime.Now; SensorData updateMessage = _converter.ConvertUnitedValue(value, productName, timeCollected); _queueManager.AddSensorData(updateMessage); _valuesCache.AddValue(productName, updateMessage); bool isToDB = true; if (value.IsBarSensor()) { isToDB = ProcessBarSensor(value, timeCollected, productName); } if (!isToDB) { return; } SensorDataEntity dataObject = _converter.ConvertUnitedValueToDatabase(value, timeCollected); Task.Run(() => SaveSensorValue(dataObject, productName)); } catch (Exception e) { _logger.LogError(e, $"Failed to add value for sensor {value?.Path}"); } }
public void AddSensorValue(DoubleBarSensorValue value) { try { string productName = _productManager.GetProductNameByKey(value.Key); bool isNew = false; if (!_productManager.IsSensorRegistered(productName, value.Path)) { isNew = true; _productManager.AddSensor(productName, value); } DateTime timeCollected = DateTime.Now; SensorData updateMessage = _converter.Convert(value, productName, timeCollected, isNew ? TransactionType.Add : TransactionType.Update); _queueManager.AddSensorData(updateMessage); _valuesCache.AddValue(productName, updateMessage); if (value.EndTime == DateTime.MinValue) { _barsStorage.Add(value, updateMessage.Product, timeCollected); return; } _barsStorage.Remove(productName, value.Path); SensorDataEntity dataObject = _converter.ConvertToDatabase(value, timeCollected); Task.Run(() => SaveSensorValue(dataObject, productName)); } catch (Exception e) { _logger.LogError(e, $"Failed to add value for sensor '{value?.Path}'"); } }
public static SensorData Convert(this SensorDataEntity dataEntity, SensorInfo sensorInfo, string productName) { var data = Convert(dataEntity, productName); data.Description = sensorInfo.Description; return(data); }
private static void TestSensorDataStringValues(SensorDataEntity expected, SensorData actual, SensorType sensorType) { switch (sensorType) { case SensorType.BooleanSensor: BoolSensorData boolData = JsonSerializer.Deserialize <BoolSensorData>(expected.TypedData); Assert.Equal(SensorDataStringValuesFactory.GetSimpleSensorsString(expected.TimeCollected, boolData.Comment, boolData.BoolValue), actual.StringValue); Assert.Equal(boolData.BoolValue.ToString(), actual.ShortStringValue); break; case SensorType.IntSensor: IntSensorData intData = JsonSerializer.Deserialize <IntSensorData>(expected.TypedData); Assert.Equal(SensorDataStringValuesFactory.GetSimpleSensorsString(expected.TimeCollected, intData.Comment, intData.IntValue), actual.StringValue); Assert.Equal(intData.IntValue.ToString(), actual.ShortStringValue); break; case SensorType.DoubleSensor: DoubleSensorData doubleData = JsonSerializer.Deserialize <DoubleSensorData>(expected.TypedData); Assert.Equal(SensorDataStringValuesFactory.GetSimpleSensorsString(expected.TimeCollected, doubleData.Comment, doubleData.DoubleValue), actual.StringValue); Assert.Equal(doubleData.DoubleValue.ToString(), actual.ShortStringValue); break; case SensorType.StringSensor: StringSensorData stringData = JsonSerializer.Deserialize <StringSensorData>(expected.TypedData); Assert.Equal(SensorDataStringValuesFactory.GetSimpleSensorsString(expected.TimeCollected, stringData.Comment, stringData.StringValue), actual.StringValue); Assert.Equal(stringData.StringValue, actual.ShortStringValue); break; case SensorType.IntegerBarSensor: IntBarSensorData intBarData = JsonSerializer.Deserialize <IntBarSensorData>(expected.TypedData); Assert.Equal(SensorDataStringValuesFactory.GetBarSensorsString(expected.TimeCollected, intBarData.Comment, intBarData.Min, intBarData.Mean, intBarData.Max, intBarData.Count, intBarData.LastValue), actual.StringValue); Assert.Equal(SensorDataStringValuesFactory.GetBarSensorsShortString(intBarData.Min, intBarData.Mean, intBarData.Max, intBarData.Count, intBarData.LastValue), actual.ShortStringValue); break; case SensorType.DoubleBarSensor: DoubleBarSensorData doubleBarData = JsonSerializer.Deserialize <DoubleBarSensorData>(expected.TypedData); Assert.Equal(SensorDataStringValuesFactory.GetBarSensorsString(expected.TimeCollected, doubleBarData.Comment, doubleBarData.Min, doubleBarData.Mean, doubleBarData.Max, doubleBarData.Count, doubleBarData.LastValue), actual.StringValue); Assert.Equal(SensorDataStringValuesFactory.GetBarSensorsShortString(doubleBarData.Min, doubleBarData.Mean, doubleBarData.Max, doubleBarData.Count, doubleBarData.LastValue), actual.ShortStringValue); break; case SensorType.FileSensorBytes: FileSensorBytesData fileSensorBytesData = JsonSerializer.Deserialize <FileSensorBytesData>(expected.TypedData); Assert.Equal(SensorDataStringValuesFactory.GetFileSensorsString(expected.TimeCollected, fileSensorBytesData.Comment, fileSensorBytesData.FileName, fileSensorBytesData.Extension, fileSensorBytesData.FileContent.Length), actual.StringValue); Assert.Equal(SensorDataStringValuesFactory.GetFileSensorsShortString(fileSensorBytesData.FileName, fileSensorBytesData.Extension, fileSensorBytesData.FileContent.Length), actual.ShortStringValue); break; } }
private SensorHistoryData Convert(SensorDataEntity entity) { SensorHistoryData result = new SensorHistoryData(); result.SensorType = (SensorType)entity.DataType; result.TypedData = entity.TypedData; result.Time = entity.Time; return(result); }
private SensorDataEntity CreateDataEntity(string path, string value) { SensorDataEntity entity = new SensorDataEntity(); entity.Path = path; entity.Time = DateTime.Now; entity.TimeCollected = DateTime.Now; entity.Status = (byte)SensorStatus.Ok; entity.DataType = (byte)SensorType.IntSensor; entity.TypedData = value; entity.Timestamp = DateTime.Now.Ticks; return(entity); }
private static void TestSensorDataCommonProperties(SensorDataEntity expected, SensorData actual, string productName, SensorType sensorType) { Assert.NotNull(actual); Assert.Equal(expected.Path, actual.Path); Assert.Equal(productName, actual.Product); Assert.Equal(expected.TimeCollected, actual.Time); Assert.Equal((SensorType)expected.DataType, actual.SensorType); Assert.Equal((SensorStatus)expected.Status, actual.Status); Assert.Equal(TransactionType.Unknown, actual.TransactionType); Assert.True(string.IsNullOrEmpty(actual.Key)); Assert.True(string.IsNullOrEmpty(actual.ValidationError)); TestSensorDataStringValues(expected, actual, sensorType); }
public void WriteOneValueSensorData(SensorDataEntity dataObject, string productName) { try { var key = GetOneValueSensorWriteKey(productName, dataObject.Path); string value = JsonSerializer.Serialize(dataObject); lock (_accessLock) { _database.Put(key, value); } } catch (Exception e) { _logger.Error(e, $"Failed to add data for sensor {dataObject.Path}"); } }
public void PutSensorData(SensorDataEntity sensorData, string productName) { var writeKey = PrefixConstants.GetSensorWriteValueKey(productName, sensorData.Path, sensorData.TimeCollected); var bytesKey = Encoding.UTF8.GetBytes(writeKey); try { var serializedValue = JsonSerializer.Serialize(sensorData); var bytesValue = Encoding.UTF8.GetBytes(serializedValue); _database.Put(bytesKey, bytesValue); } catch (Exception e) { _logger.Error(e, $"Failed to write data for {productName}/{sensorData.Path}"); } }
internal static SensorDataEntity ConvertToFileSensorBytes(this SensorDataEntity dataEntity) { if (dataEntity.DataType != (byte)SensorType.FileSensor) { return(dataEntity); } return(new() { Path = dataEntity.Path, Status = dataEntity.Status, Time = dataEntity.Time, Timestamp = dataEntity.Timestamp, TimeCollected = dataEntity.TimeCollected, DataType = (byte)SensorType.FileSensorBytes, TypedData = GetTypedDataForFileSensorBytes(dataEntity.TypedData), }); }
public static SensorData Convert(this SensorDataEntity dataEntity, string productName) { if (dataEntity.DataType == (byte)SensorType.FileSensor) { dataEntity = dataEntity.ConvertToFileSensorBytes(); } return(new() { Path = dataEntity.Path, SensorType = (SensorType)dataEntity.DataType, Product = productName, Time = dataEntity.TimeCollected, StringValue = SensorDataPropertiesBuilder.GetStringValue(dataEntity), ShortStringValue = SensorDataPropertiesBuilder.GetShortStringValue(dataEntity), Status = (SensorStatus)dataEntity.Status }); }
public SensorDataEntity GetLastSensorValue(string productName, string path) { SensorDataEntity sensorDataObject = default(SensorDataEntity); try { byte[] searchKey = Encoding.UTF8.GetBytes(GetSensorReadValueKey(productName, path)); DateTime lastDateTime = DateTime.MinValue; byte[] bytesValue = new byte[0]; lock (_accessLock) { using (var iterator = _database.CreateIterator()) { for (iterator.Seek(searchKey); iterator.IsValid() && iterator.Key().StartsWith(searchKey); iterator.Next()) { try { DateTime currentDateTime = GetTimeFromSensorWriteKey(iterator.Key()); if (currentDateTime > lastDateTime) { lastDateTime = currentDateTime; bytesValue = iterator.Value(); } } catch (Exception e) { _logger.Error(e, "Failed to read SensorDataEntity"); } } } } string stringValue = Encoding.UTF8.GetString(bytesValue); sensorDataObject = JsonSerializer.Deserialize <SensorDataEntity>(stringValue); } catch (Exception e) { _logger.Error(e, $"Failed to get last value for sensor = {path}, product = {productName}"); } return(sensorDataObject); }
private void BarsStorage_IncompleteBarOutdated(object sender, ExtendedBarSensorData e) { switch (e.ValueType) { case SensorType.IntegerBarSensor: { var typedValue = e.Value as IntBarSensorValue; typedValue.EndTime = DateTime.Now.ToUniversalTime(); SensorDataEntity obj = _converter.ConvertToDatabase(typedValue, e.TimeCollected); SaveSensorValue(obj, e.ProductName); break; } case SensorType.DoubleBarSensor: { var typedValue = e.Value as DoubleBarSensorValue; typedValue.EndTime = DateTime.Now.ToUniversalTime(); SensorDataEntity obj = _converter.ConvertToDatabase(typedValue, e.TimeCollected); SaveSensorValue(obj, e.ProductName); break; } } }
public SensorHistoryData GetOneValueSensorValue(string productName, string path) { SensorDataEntity entity = _database.GetLatestSensorValue(productName, path); return(entity != null?ConvertSensorDataEntityToHistoryData(entity) : null); }
public void PutOneValueSensorData(SensorDataEntity data, string productName) { _databaseWorker.WriteOneValueSensorData(data, productName); }
public void PutSensorData(SensorDataEntity data, string productName) => _database.AddSensorValue(data, productName);
/// <summary> /// Use this method for sensors, for which only last value is stored /// </summary> /// <param name="dataObject"></param> /// <param name="productName"></param> private void SaveOneValueSensorValue(SensorDataEntity dataObject, string productName) { Task.Run(() => _databaseAdapter.PutOneValueSensorData(dataObject, productName)); }
/// <summary> /// Simply save the given sensorValue /// </summary> /// <param name="dataObject"></param> /// <param name="productName"></param> private void SaveSensorValue(SensorDataEntity dataObject, string productName) { //_productManager.AddSensorIfNotRegistered(productName, dataObject.Path); Task.Run(() => _databaseAdapter.PutSensorData(dataObject, productName)); }
public void PutOneValueSensorData(SensorDataEntity data, string productName) { _adapter.PutOneValueSensorData(data, productName); }
private static void TestConvertSensorDataEntityToSensorData(SensorDataEntity sensorDataEntity, SensorInfo sensorInfo, SensorData data) { Assert.Equal(sensorInfo.Description, data.Description); TestSensorDataCommonProperties(sensorDataEntity, data, sensorInfo.ProductName, (SensorType)sensorDataEntity.DataType); }
public void AddSensorValue(SensorDataEntity entity, string productName) { var database = _sensorsDatabases.GetDatabase(entity.TimeCollected); database.PutSensorData(entity, productName); }
private static SensorDataEntity GetExpectedFileSensorBytesDataEntity(SensorDataEntity dataEntity) =>
private SensorHistoryData ConvertSensorDataEntityToHistoryData(SensorDataEntity entity) =>