Пример #1
0
        public Measurement CreateMeasurement(string payload, string device_token, string username)
        {
            Measurement measurement = new Measurement
            {
                payload = payload,
                device  = _db.devices.FirstOrDefault(m => m.token == device_token),
                owner   = _db.users.FirstOrDefault(m => m.username == username)
            };

            _db.measurements.Add(measurement);
            _db.SaveChanges();
            _logger.LogInformation($"Created measurement of device {device_token} for user {username}");
            return(measurement);
        }
Пример #2
0
        public Device CreateDevice(string name, string token, string username)
        {
            if (_db.devices.Any(m => m.name == name))
            {
                return(null);
            }

            Device device = new Device
            {
                name  = name,
                token = token,
                owner = _db.users.FirstOrDefault(m => m.username == username)
            };

            _db.devices.Add(device);
            _db.SaveChanges();
            _logger.LogInformation($"Created Device with id {device.id}");
            return(device);
        }
Пример #3
0
        public User CreateUser(string username, string password)
        {
            if (_db.users.Any(m => m.username == username))
            {
                return(null);
            }

            User user = new User
            {
                username = username,
                password = GenerateHash(password)
            };

            _db.users.Add(user);
            _db.SaveChanges();
            _logger.LogInformation($"Created user with username {username}");
            return(user);
        }
Пример #4
0
        public static void EnsureSeedData(this OwnThingsContext _db)
        {
            User user1 = new User
            {
                username     = "******",
                token        = Guid.NewGuid().ToString(),
                devices      = new List <Device>(),
                measurements = new List <Measurement>()
            };

            using (MD5 md5 = MD5.Create())
            {
                byte[]        hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes("12345678"));
                StringBuilder builder   = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    builder.Append(hashBytes[i].ToString("x2"));
                }
                user1.password = builder.ToString();
            }


            Device device1 = new Device
            {
                name         = "Device_1",
                token        = Guid.NewGuid().ToString(),
                owner        = user1,
                measurements = new List <Measurement>()
            };

            Device device2 = new Device
            {
                name         = "Device_2",
                token        = Guid.NewGuid().ToString(),
                owner        = user1,
                measurements = new List <Measurement>()
            };

            Measurement measurement1 = new Measurement
            {
                payload = "{ \"temperature\": \"25\" }",
                owner   = user1,
                device  = device1
            };

            Measurement measurement2 = new Measurement
            {
                payload = "{ \"Humidity\": \"54\" }",
                owner   = user1,
                device  = device2
            };

            device1.measurements.Add(measurement1);
            device1.measurements.Add(measurement2);

            user1.devices.Add(device1);
            user1.devices.Add(device2);

            user1.measurements.Add(measurement1);
            user1.measurements.Add(measurement2);

            if (!_db.users.Any() && !_db.devices.Any() && !_db.measurements.Any())
            {
                _db._logger.LogInformation("Seeding database");

                _db.users.Add(user1);
                _db.devices.Add(device1);
                _db.devices.Add(device2);
                _db.measurements.Add(measurement1);
                _db.measurements.Add(measurement2);

                _db.SaveChanges();

                _db._logger.LogInformation("Database seeded");
            }
        }