Beispiel #1
0
        //private User GetUser(string fn, string ln, string pass)
        //{
        //    IUserRepository userRepository = new UserRepository();
        //    return userRepository.GetUser(fn, ln, Encrypt(pass));
        //}

        //private bool CreateUserAccount()
        //{
        //    IUserRepository userRepository = new UserRepository();
        //    if (userRepository.GetUser(_user.FirstName, _user.LastName, _user.PasswordHash) != null)
        //    {
        //        return false;
        //    }
        //    userRepository.Save(_user, _user.Id);
        //    return true;
        //}

        //private string Encrypt(string password)
        //{
        //    SHA256 sha256 = SHA256.Create();
        //    byte[] data = Encoding.UTF8.GetBytes(password);
        //    byte[] result = sha256.ComputeHash(data);

        //    string hashString = result.Aggregate(string.Empty, (current, x) => current + $"{x:x2}");
        //    return hashString;
        //}

        private void LoginButton_OnClick(object sender, RoutedEventArgs e)
        {
            _mySqlDbDataModifier = new MySqlDbDataModifier();
            //new MySqlMeasurableParameterMappingRepository(),
            //    new MySqlPlantMappingRepository(), new MySqlPlantsAreaMappingRepository(),
            //    new MySqlSensorMappingRepository(), new MySqlServiceScheduleMappingRepository(), new MySqlMeasuringMessageMappingRepository(), new SensorsCollection(), new PlantsAreas());

            //MeasurableParameterMapping mpm = new MeasurableParameterMapping(Guid.NewGuid(), 10, 9, 11,
            //    ParameterEnum.Temperature.ToString());
            //_mySqlDbDataModifier.SqlMeasurableParameterMappingRepository.Save(mpm, Guid.Empty);
            //var mpm1 = _mySqlDbDataModifier.SqlMeasurableParameterMappingRepository.Get(mpm.Id);
            //mpm1.Optimal = 23;
            //_mySqlDbDataModifier.SqlMeasurableParameterMappingRepository.Save(mpm1, Guid.Empty);
            //_mySqlDbDataModifier.SqlMeasurableParameterMappingRepository.Delete(mpm1.Id);

            //PlantMapping plantMapping = new PlantMapping(Guid.NewGuid(), new Guid("a2948b2a-921f-4c7c-806d-a5724e7302cb"),
            //    new Guid("3555C74B-E887-4685-A627-005017305139"), new Guid("FDCF0612-1077-45D6-B294-272504C4E51E"),
            //    new Guid("F8867AAB-1B2C-4629-BB51-04E61B714222"),
            //    "Apple", null);

            //_mySqlDbDataModifier.SqlPlantMappingRepository.Save(plantMapping, Guid.Empty);
            //var mpm1 = _mySqlDbDataModifier.SqlPlantMappingRepository.Get(plantMapping.Id);
            //mpm1.Name = "Pear";
            //_mySqlDbDataModifier.SqlPlantMappingRepository.Save(mpm1, Guid.Empty);
            //_mySqlDbDataModifier.SqlPlantMappingRepository.Delete(mpm1.Id);

            //PlantsAreaMapping mpm = new PlantsAreaMapping(Guid.NewGuid(), new Guid("aafdb697-fd67-48e8-9ea8-7f2afe5989d0"), 10, new Guid("9adc5dca-94bc-4c35-afb5-42b06eedf989"));
            //_mySqlDbDataModifier.SqlPlantsAreaMappingRepository.Save(mpm, Guid.Empty);
            //var mpm1 = _mySqlDbDataModifier.SqlPlantsAreaMappingRepository.Get(mpm.Id);
            //mpm1.Number = 23;
            //_mySqlDbDataModifier.SqlPlantsAreaMappingRepository.Save(mpm1, Guid.Empty);

            //SensorMapping mpm = new SensorMapping(Guid.NewGuid(), new Guid("48664c4e-8300-4a2c-a7d5-3829005b1d7e"), 3,
            //    new Guid("a2948b2a-921f-4c7c-806d-a5724e7302cb"), ParameterEnum.Temperature.ToString());
            //_mySqlDbDataModifier.SqlSensorMappingRepository.Save(mpm, Guid.Empty);
            //mpm = new SensorMapping(Guid.NewGuid(), new Guid("48664c4e-8300-4a2c-a7d5-3829005b1d7e"), 3,
            //    new Guid("3555C74B-E887-4685-A627-005017305139"), ParameterEnum.Humidity.ToString());
            //_mySqlDbDataModifier.SqlSensorMappingRepository.Save(mpm, Guid.Empty);
            //mpm = new SensorMapping(Guid.NewGuid(), new Guid("48664c4e-8300-4a2c-a7d5-3829005b1d7e"), 3,
            //    new Guid("FDCF0612-1077-45D6-B294-272504C4E51E"), ParameterEnum.Nutrient.ToString());
            //_mySqlDbDataModifier.SqlSensorMappingRepository.Save(mpm, Guid.Empty);
            //mpm = new SensorMapping(Guid.NewGuid(), new Guid("48664c4e-8300-4a2c-a7d5-3829005b1d7e"), 3,
            //    new Guid("F8867AAB-1B2C-4629-BB51-04E61B714222"), ParameterEnum.SoilPh.ToString());
            //_mySqlDbDataModifier.SqlSensorMappingRepository.Save(mpm, Guid.Empty);
            //var mpm1 = _mySqlDbDataModifier.SqlSensorMappingRepository.Get(new Guid("74e4da76-a799-48b4-b75e-65e5f3fc2cd8"));
            //mpm1.MeasuringTimeout = 25;
            //_mySqlDbDataModifier.SqlSensorMappingRepository.Save(mpm1, Guid.Empty);

            //ServiceScheduleMapping mpm = new ServiceScheduleMapping(Guid.NewGuid(),
            //    new Guid("48664c4e-8300-4a2c-a7d5-3829005b1d7e"), ServiceStateEnum.Watering.ToString(), 3, 2,
            //        "a2948b2a-921f-4c7c-806d-a5724e7302cb,3555C74B-E887-4685-A627-005017305139");
            //_mySqlDbDataModifier.SqlServiceScheduleMappingRepository.Save(mpm, Guid.Empty);

            //mpm = new ServiceScheduleMapping(Guid.NewGuid(), new Guid("2e664c4e-8300-4a2c-a7d5-3829005b1d7e"), ServiceStateEnum.Nutrienting.ToString(), 3, 2,
            //        "F8867AAB-1B2C-4629-BB51-04E61B714222,FDCF0612-1077-45D6-B294-272504C4E51E");
            //_mySqlDbDataModifier.SqlServiceScheduleMappingRepository.Save(mpm, Guid.Empty);

            //mpm = new ServiceScheduleMapping(Guid.NewGuid(), new Guid("7be64c4e-8300-4a2c-a7d5-3829005b1d7e"), ServiceStateEnum.Warming.ToString(), 3, 2,
            //        "F8867AAB-1B2C-4629-BB51-04E61B714222");
            //_mySqlDbDataModifier.SqlServiceScheduleMappingRepository.Save(mpm, Guid.Empty);

            //mpm = new ServiceScheduleMapping(Guid.NewGuid(), new Guid("9ce64c4e-8300-4a2c-a7d5-3829005b1d7e"), ServiceStateEnum.Cooling.ToString(), 3, 2,
            //        "F8867AAB-1B2C-4629-BB51-04E61B714222");
            //_mySqlDbDataModifier.SqlServiceScheduleMappingRepository.Save(mpm, Guid.Empty);

            //var mpm1 = _mySqlDbDataModifier.SqlServiceScheduleMappingRepository.Get(new Guid("0824a75a-f91e-4f57-84a8-1f9bf45a995f"));
            //mpm1.ServicingPauseSpan = 5;
            //_mySqlDbDataModifier.SqlServiceScheduleMappingRepository.Save(mpm1, Guid.Empty);

            //var mm = new MeasuringMessageMapping(Guid.NewGuid(), DateTime.Now, MessageTypeEnum.UsualInfo.ToString(),
            //    ParameterEnum.Nutrient.ToString(), new Guid("48664c4e-8300-4a2c-a7d5-3829005b1d7e"), 12.66);
            //_mySqlDbDataModifier.SqlMeasuringMessageMappingRepository.Save(mm, Guid.Empty);

            //_mySqlDbDataModifier.SqlMeasuringMessageMappingRepository.Delete(mm.Id);

            //IUserRepository userRepository = new UserRepository();
            //foreach (var user in userRepository.GetAll())
            //{
            //    _mongoDbAccessor = new MongoDbAccessor();
            //    _mongoDbAccessor.AddMongoUser(new MongoUser(user));
            //}

            Logginglabel.Content = @"You are being logged in. Please, wait...";
            LoginButton.IsEnabled = false;

            string firstName = FirstName.Text;
            string lastName = LastName.Text;
            string email = Email.Text;
            string password = Password.Password;

            HttpResponseMessage response;
            if (CreateAccount.IsChecked != null && !(bool)CreateAccount.IsChecked)
            {
                //_user = GetUser(firstName, lastName, password);
                //if (_user == null)
                //{
                //    Logginglabel.Content = @"User with such credentials does not exist";
                //    LoginButton.IsEnabled = true;
                //    return;
                //}
                LoginViewModel loginViewModel = new LoginViewModel
                {
                    Email = email,
                    Password = password,
                    RememberMe = true
                };

                response = _mySqlDbDataModifier.LoginUser(loginViewModel);

                if (!response.IsSuccessStatusCode)
                {
                    Logginglabel.Content = response.ReasonPhrase;
                    LoginButton.IsEnabled = true;

                    //_user = (response.Content as ObjectContent)?.Value as ApplicationUser;
                    return;
                }
                _user = response.Content.ReadAsAsync<ApplicationUser>().Result;
            }
            else
            {
                string confirmPassword = ConfirmPassword.Password;
                //EmailAddressAttribute addressAttribute = new EmailAddressAttribute();
                //if (!addressAttribute.IsValid(Email.Text))
                //{
                //    Logginglabel.Content = @"Email is wrong";
                //    LoginButton.IsEnabled = true;
                //    return;
                //}
                //if (password != confirmPassword)
                //{
                //    Logginglabel.Content = @"Passwords do not match";
                //    LoginButton.IsEnabled = true;
                //    return;
                //}

                string username = $"{firstName} {lastName}";

                RegisterViewModel registerViewModel = new RegisterViewModel
                {
                    Name = username,
                    Email = email,
                    Password = password,
                    ConfirmPassword = confirmPassword
                };

                response = _mySqlDbDataModifier.RegisterUser(registerViewModel);

                if (!response.IsSuccessStatusCode)
                {
                    Logginglabel.Content = response.ReasonPhrase;
                    LoginButton.IsEnabled = true;

                    //_user = (response.Content as ObjectContent)?.Value as ApplicationUser;
                    return;
                }
                _user = response.Content.ReadAsAsync<ApplicationUser>().Result;
            }

            //for MS Sql and Mongo DBs
            //_user = new User(firstName, lastName, email, Encrypt(password));
            //if (CreateUserAccount())
            //{
            //    _mongoDbAccessor = new MongoDbAccessor();
            //    _mongoDbAccessor.AddMongoUser(new MongoUser(_user));
            //}
            //else
            //{
            //    Logginglabel.Content = @"User with such credentials already exists";
            //    LoginButton.IsEnabled = true;
            //    return;
            //}


            if (_user != null)
            {
                StartMainProcess();
                LoginNameLabel.Content = $"You are logged in as {_user.UserName}";
                LoginNameLabel.Background = Brushes.Wheat;
            }
        }
Beispiel #2
0
        public void Initialize()
        {
            //IPlantMappingRepository plantRepository = new PlantMappingRepository();
            //IPlantsAreaMappingRepository plantsAreaRepository = new PlantsAreaMappingRepository();
            //IMeasurableParameterMappingRepository measurableParameterRepository =
            //    new MeasurableParameterMappingRepository();
            //ISensorMappingRepository sensorRepository = new SensorMappingRepository();
            //IServiceScheduleMappingRepository serviceScheduleMappingRepository = new ServiceScheduleMappingRepository();

            MySqlMeasurableParameterMappingRepository sqlMeasurableParameterMappingRepository = new MySqlMeasurableParameterMappingRepository();
            MySqlPlantMappingRepository sqlPlantMappingRepository = new MySqlPlantMappingRepository();
            MySqlPlantsAreaMappingRepository sqlPlantsAreaMappingRepository = new MySqlPlantsAreaMappingRepository();
            MySqlSensorMappingRepository sqlSensorMappingRepository = new MySqlSensorMappingRepository();
            MySqlServiceScheduleMappingRepository sqlServiceScheduleMappingRepository = new MySqlServiceScheduleMappingRepository();

            _dbMapper = new DbMapper(sqlPlantMappingRepository,
                sqlMeasurableParameterMappingRepository, sqlServiceScheduleMappingRepository);

            _mongoDbAccessor = new MongoDbAccessor();

            List<PlantsAreaMapping> plantsAreaMappings = new List<PlantsAreaMapping>();

            if (_user != null)
            {
                plantsAreaMappings = sqlPlantsAreaMappingRepository.GetAll(mapping => mapping.UserId == new Guid(_user.Id));
            }

            _plantsAreas = new PlantsAreas();
                
            plantsAreaMappings.ForEach(p => _plantsAreas.AddPlantsArea(_dbMapper.RestorePlantArea(p)));

            _sensorsCollection = new SensorsCollection();

            foreach (PlantsArea area in _plantsAreas.Areas)
            {
                foreach (SensorMapping sensorMapping in sqlSensorMappingRepository.GetAll(sm => sm.PlantsAreaId == area.Id))
                {
                    Sensor sensor = _dbMapper.RestoreSensor(sensorMapping, area);
                    if (sensor != null)
                    {
                        _sensorsCollection.AddSensor(sensor);
                        sensor.IsOn = true;
                    }
                }
            }

            foreach (PlantsArea area in _plantsAreas.Areas)
            {
                //if custom sensor
                foreach (Sensor source in area.Sensors.Where(s => s.IsCustom))
                {
                    ServiceState serviceState = new ServiceState(source.MeasurableType, true);
                    area.PlantServicesStates.AddServiceState(serviceState);
                }
            }

            _sensorsMeasuringsProvider = new SensorsMeasuringsProvider(_sensorsCollection);

            _observer = new Observer(_sensorsMeasuringsProvider, _plantsAreas);

            _mongoMessagesListener = new MongoMessagesListener(_observer);

            _serviceProvider = new ServiceProvider(_observer, _plantsAreas);

            //_dbDataModifier = new DbDataModifier(_plantsAreas, _sensorsCollection, measurableParameterRepository,
            //    plantRepository, sensorRepository, plantsAreaRepository, serviceScheduleMappingRepository);

            _mySqlDbDataModifier = new MySqlDbDataModifier(sqlMeasurableParameterMappingRepository,
                sqlPlantMappingRepository, sqlPlantsAreaMappingRepository, sqlSensorMappingRepository,
                sqlServiceScheduleMappingRepository, new MySqlMeasuringMessageMappingRepository(), _sensorsCollection,
                _plantsAreas);
            //new MySqlMeasurableParameterMappingRepository(),
            //               new MySqlPlantMappingRepository(), new MySqlPlantsAreaMappingRepository(),
            //               new MySqlSensorMappingRepository(), new MySqlServiceScheduleMappingRepository(), new MySqlMeasuringMessageMappingRepository(), new SensorsCollection(), new PlantsAreas());
        }