public void RemoveSensorFromList(string productName, string path) { var key = PrefixConstants.GetSensorsListKey(productName); byte[] bytesKey = Encoding.UTF8.GetBytes(key); try { bool result = _database.TryRead(bytesKey, out byte[] value); if (!result) { throw new ServerDatabaseException("Failed to read products list!"); } string stringValue = Encoding.UTF8.GetString(value); List <string> currentList = string.IsNullOrEmpty(stringValue) ? new List <string>() : JsonSerializer.Deserialize <List <string> >(stringValue); currentList.Remove(path); string stringData = JsonSerializer.Serialize(currentList); byte[] bytesValue = Encoding.UTF8.GetBytes(stringData); _database.Put(bytesKey, bytesValue); } catch (Exception e) { _logger.Error(e, "Failed to add prodct to list"); } }
public void AddNewSensorToList(string productName, string path) { var key = PrefixConstants.GetSensorsListKey(productName); byte[] bytesKey = Encoding.UTF8.GetBytes(key); try { byte[] value = _database.Read(bytesKey); List <string> currentList = value == null ? new List <string>() : JsonSerializer.Deserialize <List <string> >(Encoding.UTF8.GetString(value)); if (!currentList.Contains(path)) { currentList.Add(path); } string stringData = JsonSerializer.Serialize(currentList); byte[] bytesValue = Encoding.UTF8.GetBytes(stringData); _database.Put(bytesKey, bytesValue); } catch (Exception e) { _logger.Error(e, "Failed to add prodct to list"); } }
public List <string> GetSensorsList(string productName) { string listKey = PrefixConstants.GetSensorsListKey(productName); byte[] bytesKey = Encoding.UTF8.GetBytes(listKey); List <string> result = new List <string>(); try { bool isRead = _database.TryRead(bytesKey, out byte[] value); if (!isRead) { throw new ServerDatabaseException($"Failed to read sensors list for {productName}!"); } string stringValue = Encoding.UTF8.GetString(value); List <string> products = string.IsNullOrEmpty(stringValue) ? new List <string>() : JsonSerializer.Deserialize <List <string> >(stringValue); result.AddRange(products); } catch (Exception e) { _logger.Error(e, $"Failed to get products list for {productName}"); } return(result); }
public void RemoveSensorsList(string productName) { var key = PrefixConstants.GetSensorsListKey(productName); byte[] bytesKey = Encoding.UTF8.GetBytes(key); try { _database.Delete(bytesKey); } catch (Exception e) { _logger.Error(e, $"Failed to remove sensors list for {productName}"); } }