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> GetMonitoringDatabases()
        {
            string listKey = PrefixConstants.GetMonitoringDatabasesListKey();

            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 databases list!");
                }

                List <string> products = JsonSerializer.Deserialize <List <string> >(Encoding.UTF8.GetString(value));
                result.AddRange(products);
            }
            catch (Exception e)
            {
                _logger.Error(e, "Failed to get databases list");
            }

            return(result);
        }
        public List <UserEntity> ReadUsersPage(int page, int pageSize)
        {
            var key                 = PrefixConstants.GetUsersReadKey();
            var keyBytes            = Encoding.UTF8.GetBytes(key);
            List <UserEntity> users = new List <UserEntity>();

            try
            {
                List <byte[]> values = _database.GetPageStartingWith(keyBytes, page, pageSize);
                foreach (var value in values)
                {
                    try
                    {
                        users.Add(JsonSerializer.Deserialize <UserEntity>(Encoding.UTF8.GetString(value)));
                    }
                    catch (Exception e)
                    {
                        _logger.Error(e, $"Failed to deserialize {Encoding.UTF8.GetString(value)} to UserEntity");
                    }
                }
            }
            catch (Exception e)
            {
                _logger.Error(e, "Failed to read users!");
            }

            return(users);
        }
        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);
        }
コード例 #5
0
        public List <SensorDataEntity> GetAllSensorValues(string productName, string path)
        {
            var readKey  = PrefixConstants.GetSensorReadValueKey(productName, path);
            var bytesKey = Encoding.UTF8.GetBytes(readKey);

            return(GetValuesWithKeyEqualOrGreater(bytesKey, path));
        }
        public void RemoveMonitoringDatabaseFromList(string folderName)
        {
            var key = PrefixConstants.GetMonitoringDatabasesListKey();

            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(folderName);

                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 AddMonitoringDatabaseToList(string folderName)
        {
            var key = PrefixConstants.GetMonitoringDatabasesListKey();

            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(folderName))
                {
                    currentList.Add(folderName);
                }

                string stringData = JsonSerializer.Serialize(currentList);
                byte[] bytesValue = Encoding.UTF8.GetBytes(stringData);
                _database.Put(bytesKey, bytesValue);
            }
            catch (Exception e)
            {
                _logger.Error(e, "Failed to add monitoring database to list");
            }
        }
コード例 #8
0
        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 RemoveSensorValues(string productName, string path)
        {
            var key = PrefixConstants.GetSensorInfoKey(productName, path);

            byte[] bytesKey = Encoding.UTF8.GetBytes(key);
            try
            {
                _database.DeleteAllStartingWith(bytesKey);
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to remove sensor values for {productName}/{path}");
            }
        }
        public void RemoveProductInfo(string productName)
        {
            string key = PrefixConstants.GetProductInfoKey(productName);

            byte[] bytesKey = Encoding.UTF8.GetBytes(key);
            try
            {
                _database.Delete(bytesKey);
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to remove info for product {productName}");
            }
        }
コード例 #11
0
        public SensorDataEntity GetLatestSensorValue(string productName, string path)
        {
            var readKey  = PrefixConstants.GetSensorReadValueKey(productName, path);
            var bytesKey = Encoding.UTF8.GetBytes(readKey);
            var values   = GetValuesWithKeyEqualOrGreater(bytesKey, path);

            if (values == null || !values.Any())
            {
                return(null);
            }

            values.Sort((v1, v2) => v2.TimeCollected.CompareTo(v1.TimeCollected));
            return(values.First(v => v.Path == path));
        }
        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}");
            }
        }
        public void RemoveUser(UserEntity user)
        {
            var userKey  = PrefixConstants.GetUniqueUserKey(user.UserName);
            var keyBytes = Encoding.UTF8.GetBytes(userKey);

            try
            {
                _database.Delete(keyBytes);
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to delete user '{user.UserName}'");
            }
        }
        public void RemoveConfigurationObject(string name)
        {
            var key = PrefixConstants.GetUniqueConfigurationObjectKey(name);

            byte[] bytesKey = Encoding.UTF8.GetBytes(key);
            try
            {
                _database.Delete(bytesKey);
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to write configuration object {name}");
            }
        }
        public void RemoveRegistrationTicket(Guid id)
        {
            var key = PrefixConstants.GetRegistrationTicketKey(id);

            byte[] bytesKey = Encoding.UTF8.GetBytes(key);
            try
            {
                _database.Delete(bytesKey);
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to write registration ticket {id}");
            }
        }
コード例 #16
0
        public long GetSensorSize(string productName, string path)
        {
            var stringKey = PrefixConstants.GetSensorReadValueKey(productName, path);
            var bytesKey  = Encoding.UTF8.GetBytes(stringKey);

            try
            {
                return(_database.GetSize(bytesKey));
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to get size of {productName}/{path} sensor");
            }

            return(0);
        }
コード例 #17
0
        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 void AddUser(UserEntity user)
        {
            var userKey     = PrefixConstants.GetUniqueUserKey(user.UserName);
            var keyBytes    = Encoding.UTF8.GetBytes(userKey);
            var stringValue = JsonSerializer.Serialize(user);
            var valueBytes  = Encoding.UTF8.GetBytes(stringValue);

            try
            {
                _database.Put(keyBytes, valueBytes);
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to save user {user.UserName}");
            }
        }
        public void WriteRegistrationTicket(RegisterTicketEntity ticket)
        {
            var key = PrefixConstants.GetRegistrationTicketKey(ticket.Id);

            byte[] bytesKey    = Encoding.UTF8.GetBytes(key);
            string stringValue = JsonSerializer.Serialize(ticket);

            byte[] bytesValue = Encoding.UTF8.GetBytes(stringValue);
            try
            {
                _database.Put(bytesKey, bytesValue);
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to write registration ticket {ticket.Id}");
            }
        }
        public void WriteConfigurationObject(ConfigurationEntity obj)
        {
            var key = PrefixConstants.GetUniqueConfigurationObjectKey(obj.Name);

            byte[] bytesKey    = Encoding.UTF8.GetBytes(key);
            string stringValue = JsonSerializer.Serialize(obj);

            byte[] bytesValue = Encoding.UTF8.GetBytes(stringValue);
            try
            {
                _database.Put(bytesKey, bytesValue);
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to write configuration object {obj.Name}");
            }
        }
        public void AddSensor(SensorEntity info)
        {
            var key = PrefixConstants.GetSensorInfoKey(info.ProductName, info.Path);

            byte[] bytesKey    = Encoding.UTF8.GetBytes(key);
            string stringValue = JsonSerializer.Serialize(info);

            byte[] bytesValue = Encoding.UTF8.GetBytes(stringValue);
            try
            {
                _database.Put(bytesKey, bytesValue);
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to put sensor info for {info.ProductName}/{info.Path}");
            }
        }
        public void PutProductInfo(ProductEntity product)
        {
            string key = PrefixConstants.GetProductInfoKey(product.Name);

            byte[] bytesKey   = Encoding.UTF8.GetBytes(key);
            string stringData = JsonSerializer.Serialize(product);

            byte[] bytesValue = Encoding.UTF8.GetBytes(stringData);
            try
            {
                _database.Put(bytesKey, bytesValue);
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to put product info for {product.Name}");
            }
        }
        public ConfigurationEntity ReadConfigurationObject(string name)
        {
            var key = PrefixConstants.GetUniqueConfigurationObjectKey(name);

            byte[] bytesKey = Encoding.UTF8.GetBytes(key);
            try
            {
                bool isRead = _database.TryRead(bytesKey, out byte[] value);
                if (!isRead)
                {
                    throw new ServerDatabaseException("Failed to read configuration object info");
                }

                return(JsonSerializer.Deserialize <ConfigurationEntity>(Encoding.UTF8.GetString(value)));
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to read configuration object {name}");
            }

            return(null);
        }
        public ProductEntity GetProductInfo(string productName)
        {
            string key = PrefixConstants.GetProductInfoKey(productName);

            byte[] bytesKey = Encoding.UTF8.GetBytes(key);
            try
            {
                bool isRead = _database.TryRead(bytesKey, out byte[] value);
                if (!isRead)
                {
                    throw new ServerDatabaseException("Failed to read product info");
                }

                return(JsonSerializer.Deserialize <ProductEntity>(Encoding.UTF8.GetString(value)));
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to read info for product {productName}");
            }

            return(null);
        }
        public RegisterTicketEntity ReadRegistrationTicket(Guid id)
        {
            var key = PrefixConstants.GetRegistrationTicketKey(id);

            byte[] bytesKey = Encoding.UTF8.GetBytes(key);
            try
            {
                bool isRead = _database.TryRead(bytesKey, out byte[] value);
                if (!isRead)
                {
                    throw new ServerDatabaseException("Failed to read ticket info");
                }

                return(JsonSerializer.Deserialize <RegisterTicketEntity>(Encoding.UTF8.GetString(value)));
            }
            catch (Exception e)
            {
                _logger.Error(e, $"Failed to read registration ticket {id}");
            }

            return(null);
        }
コード例 #26
0
        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);
        }