Ejemplo n.º 1
0
 public bool RemoveServiceState(ServiceState serviceState)
 {
     if (ServicesStates != null)
     {
         if (ServicesStates.All(s => s.ServiceName != serviceState.ServiceName))
         {
             return false;
         }
         return ServicesStates.Remove(serviceState);
     }
     return false;
 }
Ejemplo n.º 2
0
 public bool AddServiceState(ServiceState serviceState)
 {
     if (ServicesStates == null)
     {
         ServicesStates = new BindingList<ServiceState>();
     }
     if (ServicesStates.Any(s => s.ServiceName == serviceState.ServiceName))
     {
         return false;
     }
     ServicesStates.Add(serviceState);
     return true;
 }
Ejemplo n.º 3
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());
        }
        public FrameworkElementFactory CreateSensorSaveButtonTemplate(PlantsArea area,
            BindingList<DataGridSensorView> dataGridSensorViews,
            BindingList<DataGridServiceScheduleView> dataGridServiceScheduleViews,
            Func<PlantsArea, Sensor, ServiceSchedule, bool> saveSensor)
        {
            FrameworkElementFactory buttonTemplate = new FrameworkElementFactory(typeof (Button));
            buttonTemplate.SetValue(ContentControl.ContentProperty, "Save");
            buttonTemplate.AddHandler(
                ButtonBase.ClickEvent,
                new RoutedEventHandler((o, e) =>
                {
                    DataGridSensorView dataGridSensorView = ((FrameworkElement) o).DataContext as DataGridSensorView;

                    if (dataGridSensorView != null)
                    {
                        try
                        {
                            ServiceState serviceState;
                            ServiceSchedule serviceSchedule;

                            if (dataGridSensorView.Sensor != null)
                            {
                                if (!dataGridSensorView.Sensor.MeasurableParameter.HasValidParameters())
                                {
                                    throw new Exception();
                                }

                                if (dataGridSensorView.MeasurableIsModified)
                                {

                                    string oldMeasurable = dataGridSensorView.Sensor.MeasurableType;

                                    dataGridSensorView.Sensor.MeasurableParameter.MeasurableType =
                                        dataGridSensorView.Measurable;

                                    serviceState = dataGridSensorView.Sensor.PlantsArea
                                        .PlantServicesStates.GetServiceState(state => state.IsFor(oldMeasurable));

                                    if (serviceState != null)
                                    {
                                        serviceSchedule = dataGridSensorView.Sensor.PlantsArea
                                            .ServicesSchedulesStates.GetServiceSchedule(
                                                schedule => schedule.IsFor(serviceState.ServiceName));

                                        serviceState.ServiceName = dataGridSensorView.Measurable;

                                        if (serviceSchedule != null)
                                        {
                                            serviceSchedule.ServiceName = serviceState.ServiceName;
                                        }

                                        saveSensor(area, dataGridSensorView.Sensor, serviceSchedule);

                                        dataGridSensorView.MeasurableIsModified = false;
                                        dataGridSensorView.IsModified = false.ToString();

                                        MessageBox.Show(
                                            $"Sensor with measurable '{dataGridSensorView.Measurable}' updated");

                                        OnRefreshControls();

                                        return;
                                    }
                                }
                                saveSensor(area, dataGridSensorView.Sensor, null);

                                dataGridSensorView.IsModified = false.ToString();

                                MessageBox.Show($"'{dataGridSensorView.Measurable}': sensor data saved");

                                return;
                            }

                            if (dataGridSensorViews.Count(s => s.Measurable == dataGridSensorView.Measurable) != 1)
                            {
                                MessageBox.Show(
                                    $"Sensor with measurable '{dataGridSensorView.Measurable}' already exists");
                                return;
                            }

                            CustomParameter customParameter =
                                new CustomParameter(Guid.NewGuid(), Convert.ToInt32(dataGridSensorView.Optimal),
                                    Convert.ToInt32(dataGridSensorView.Min),
                                    Convert.ToInt32(dataGridSensorView.Max),
                                    dataGridSensorView.Measurable);

                            CustomSensor sensor =
                                new CustomSensor(Guid.NewGuid(), area,
                                    TimeSpan.Parse(dataGridSensorView.Timeout), customParameter);

                            dataGridSensorView.Sensor = sensor;

                            serviceState = new ServiceState(sensor.MeasurableType, true);

                            area.PlantServicesStates.AddServiceState(serviceState);

                            serviceSchedule = new ServiceSchedule(Guid.NewGuid(), area.Id,
                                serviceState.ServiceName, new TimeSpan(0, 0, 10), new TimeSpan(0, 1, 0),
                                new List<MeasurableParameter> {sensor.MeasurableParameter});

                            area.ServicesSchedulesStates.AddServiceSchedule(serviceSchedule);

                            dataGridServiceScheduleViews.Add(new DataGridServiceScheduleView(serviceSchedule));

                            saveSensor(area, sensor, serviceSchedule);

                            dataGridSensorView.IsModified = false.ToString();

                            MessageBox.Show($"'{dataGridSensorView.Measurable}': sensor data saved");

                            OnRefreshControls();
                        }
                        catch (Exception)
                        {
                            MessageBox.Show($"'{dataGridSensorView.Measurable}': wrong sensor data");
                        }
                    }
                })
                );
            return buttonTemplate;
        }