public List <SensorDataEntity> GetSensorValuesBetween(string productName, string path, DateTime from, DateTime to) { string fromKey = PrefixConstants.GetSensorWriteValueKey(productName, path, from); string toKey = PrefixConstants.GetSensorWriteValueKey(productName, path, to); string startWithKey = PrefixConstants.GetSensorReadValueKey(productName, path); byte[] fromBytes = Encoding.UTF8.GetBytes(fromKey); byte[] toBytes = Encoding.UTF8.GetBytes(toKey); byte[] startWithBytes = Encoding.UTF8.GetBytes(startWithKey); List <SensorDataEntity> result = new List <SensorDataEntity>(); try { var values = _database.GetStartingWithRange(fromBytes, toBytes, startWithBytes); foreach (var value in values) { try { var currentEl = JsonSerializer.Deserialize <SensorDataEntity>(Encoding.UTF8.GetString(value)); if (currentEl.Path == path && (currentEl.TimeCollected < to && currentEl.TimeCollected > from)) { result.Add(currentEl); } } catch (Exception e) { _logger.Error(e, $"Failed to deserialize {Encoding.UTF8.GetString(value)} to SensorDataEntity"); } } } catch (Exception) { } return(result); }
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}"); } }
public List <SensorDataEntity> GetSensorValuesFrom(string productName, string path, DateTime from) { var readKey = PrefixConstants.GetSensorWriteValueKey(productName, path, from); byte[] bytesKey = Encoding.UTF8.GetBytes(readKey); var startWithKey = PrefixConstants.GetSensorReadValueKey(productName, path); byte[] startWithBytes = Encoding.UTF8.GetBytes(startWithKey); List <SensorDataEntity> result = new List <SensorDataEntity>(); try { var values = _database.GetAllStartingWithAndSeek(startWithBytes, bytesKey); foreach (var value in values) { try { var currentEl = JsonSerializer.Deserialize <SensorDataEntity>(Encoding.UTF8.GetString(value)); if (currentEl.Path == path && currentEl.TimeCollected > from) { result.Add(currentEl); } } catch (Exception e) { _logger.Error(e, $"Failed to deserialize {Encoding.UTF8.GetString(value)} to SensorDataEntity"); } } } catch (Exception e) { _logger.Error(e, $"Failed to read all sensors values for {Encoding.UTF8.GetString(bytesKey)}"); } return(result); }