public bool RemoveServiceState(ServiceSchedule serviceSchedule)
 {
     if (ServicesSchedules != null)
     {
         return ServicesSchedules.Remove(serviceSchedule);
     }
     return false;
 }
 public bool AddServiceSchedule(ServiceSchedule serviceSchedule)
 {
     if (ServicesSchedules == null)
     {
         ServicesSchedules = new BindingList<ServiceSchedule>();
     }
     ServicesSchedules.Add(serviceSchedule);
     return true;
 }
Beispiel #3
0
        public bool SaveSensor(PlantsArea area, Sensor sensor, ServiceSchedule serviceSchedule)
        {
            try
            {
                MeasurableParameterMapping measurableParameterMapping =
                    _dbMapper.GetMeasurableParameterMapping(sensor.MeasurableParameter);

                if (!(_sqlMeasurableParameterMappingRepository.Save(measurableParameterMapping,
                          measurableParameterMapping.Id) &&
                      area.Plant.AddMeasurableParameter(sensor.MeasurableParameter)))
                {
                    return false;
                }

                if (!area.AddSensor(sensor))
                {
                    return false;
                }

                SensorMapping sensorMapping = _dbMapper.GetSensorMapping(sensor);
                if (!(_sqlSensorMappingRepository.Save(sensorMapping, sensorMapping.Id) &&
                      _sensorsCollection.AddSensor(sensor)))
                {
                    return false;
                }

                if (serviceSchedule != null)
                {
                    if (!SaveServiceSchedule(area, serviceSchedule))
                    {
                        return false;
                    }
                }

                //if custom sensor
                if (sensor.IsCustom)
                {
                    PlantMapping plantMapping = _dbMapper.GetPlantMapping(area.Plant);
                    if (!_sqlPlantMappingRepository.Save(plantMapping, plantMapping.Id))
                    {
                        return false;
                    }
                }

                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.StackTrace);
                return false;
            }
        }
Beispiel #4
0
        private bool RemoveSensor(PlantsArea area, Sensor sensor, ServiceSchedule serviceSchedule)
        {
            _mongoDbAccessor.DeleteMongoSensor(new MongoSensor(sensor));
            _mongoDbAccessor.AddMongoNotification(new MongoNotification(sensor.PlantsAreaId.ToString(),
                $"{sensor.MeasurableType} sensor removed", _user.Id.ToString()));

            if (_mySqlDbDataModifier.RemoveSensor(area, sensor, serviceSchedule))
            {
                MessageBox.Show($"'{sensor.MeasurableType}': sensor removed");

                _mongoDbAccessor.SaveMongoPlantsArea(new MongoPlantsArea(area));

                return true;
            }
            return false;
        }
Beispiel #5
0
        private bool SaveSensor(PlantsArea area, Sensor sensor, ServiceSchedule serviceSchedule)
        {
            if (_mySqlDbDataModifier.SaveSensor(area, sensor, serviceSchedule))
            {
                _mongoDbAccessor.SaveMongoSensor(new MongoSensor(sensor));
                _mongoDbAccessor.SaveMongoPlantsArea(new MongoPlantsArea(sensor.PlantsArea));
                _mongoDbAccessor.AddMongoNotification(new MongoNotification(sensor.PlantsAreaId.ToString(),
                    $"{sensor.MeasurableType} sensor added/updated", _user.Id.ToString()));

                return true;
            }
            return false;
        }
Beispiel #6
0
 private bool SaveServiceSchedule(PlantsArea area, ServiceSchedule serviceSchedule)
 {
     return _mySqlDbDataModifier.SaveServiceSchedule(area, serviceSchedule);
 }
        public DataGridServiceScheduleView(ServiceSchedule serviceSchedule)
        {
            _serviceSchedule = serviceSchedule;
            _serviceSchedule.NewServiceName += GetNewServiceName;

            ServiceName = _serviceSchedule.ServiceName;

            Parameters = MeasurableParametersToStringView();

            ServicingSpan = _serviceSchedule.ServicingSpan.ToString();
            ServicingPauseSpan = _serviceSchedule.ServicingPauseSpan.ToString();

            IsOn = true.ToString();

            IsModified = false.ToString();
        }
Beispiel #8
0
        public bool RemoveSensor(PlantsArea area, Sensor sensor, ServiceSchedule serviceSchedule)
        {
            try
            {
                if (_sqlSensorMappingRepository.Delete(sensor.Id) &&
                    _sensorsCollection.RemoveSensor(sensor) &&
                    area.RemoveSensor(sensor))
                {
                    //if custom sensor
                    ServiceState serviceState =
                        area.PlantServicesStates.ServicesStates.FirstOrDefault(
                            s => s.IsFor(sensor.MeasurableType));

                    if (serviceState != null)
                    {
                        if (area.PlantServicesStates.RemoveServiceState(serviceState))
                        {
                            if (area.Plant.RemoveMeasurableParameter(sensor.MeasurableParameter))
                            {
                                PlantMapping plantMapping = _dbMapper.GetPlantMapping(area.Plant);
                                if (_sqlPlantMappingRepository.Save(plantMapping, plantMapping.Id))
                                {
                                    if (_sqlMeasurableParameterMappingRepository.Delete(sensor.MeasurableParameter.Id))
                                    {
                                        if (serviceSchedule != null)
                                        {
                                            return _sqlServiceScheduleMappingRepository.Delete(serviceSchedule.Id);
                                        }

                                        serviceSchedule = area.ServicesSchedulesStates.ServicesSchedules.SingleOrDefault
                                        (
                                            s => s.ServiceName == serviceState.ServiceName);

                                        if (serviceSchedule != null)
                                        {
                                            return _sqlServiceScheduleMappingRepository.Delete(serviceSchedule.Id);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                return false;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.StackTrace);
                return false;
            }
        }
Beispiel #9
0
 public bool SaveServiceSchedule(PlantsArea area, ServiceSchedule serviceSchedule)
 {
     try
     {
         ServiceScheduleMapping serviceScheduleMapping = _dbMapper.GetServiceScheduleMapping(serviceSchedule);
         if (serviceScheduleMapping != null)
         {
             if (_sqlServiceScheduleMappingRepository.Save(serviceScheduleMapping, serviceScheduleMapping.Id))
             {
                 return true;
             }
         }
         return false;
     }
     catch (Exception e)
     {
         MessageBox.Show(e.StackTrace);
         return false;
     }
 }
Beispiel #10
0
        public ServiceScheduleMapping GetServiceScheduleMapping(ServiceSchedule serviceSchedule)
        {
            StringBuilder builder = new StringBuilder();

            if (serviceSchedule.MeasurableParameters.Count != 0)
            {
                serviceSchedule.MeasurableParameters.ToList().ForEach(c => builder.Append(c.Id.ToString() + ','));
                builder.Remove(builder.Length - 1, 1);
            }
            return new ServiceScheduleMapping(serviceSchedule.Id, serviceSchedule.PlantsAreaId,
                serviceSchedule.ServiceName,
                (int)serviceSchedule.ServicingSpan.TotalSeconds,
                (int)serviceSchedule.ServicingPauseSpan.TotalSeconds, builder.ToString());
        }
        public FrameworkElementFactory CreateServiceScheduleSaveButtonTemplate(PlantsArea area,
            BindingList<DataGridServiceScheduleView> dataGridServiceScheduleViews, Func<PlantsArea,
                ServiceSchedule, bool> saveServiceSchedule)
        {
            FrameworkElementFactory buttonTemplate = new FrameworkElementFactory(typeof (Button));
            buttonTemplate.SetValue(ContentControl.ContentProperty, "Save");
            buttonTemplate.AddHandler(
                ButtonBase.ClickEvent,
                new RoutedEventHandler((o, e) =>
                {
                    DataGridServiceScheduleView dataGridServiceScheduleView =
                        ((FrameworkElement) o).DataContext as DataGridServiceScheduleView;

                    if (dataGridServiceScheduleView != null)
                    {
                        ServiceSchedule serviceSchedule =
                            area.ServicesSchedulesStates.ServicesSchedules.FirstOrDefault(
                                s => s.ServiceName.ToString() == dataGridServiceScheduleView.ServiceName);

                        TimeSpan servicingSpan;
                        TimeSpan servicingPauseSpan;

                        try
                        {
                            servicingSpan = TimeSpan.Parse(dataGridServiceScheduleView.ServicingSpan);
                            servicingPauseSpan = TimeSpan.Parse(dataGridServiceScheduleView.ServicingPauseSpan);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show($"'{dataGridServiceScheduleView.ServiceName}': wrong schedule data");
                            return;
                        }

                        if (serviceSchedule != null)
                        {
                            serviceSchedule.ServicingSpan = servicingSpan;
                            serviceSchedule.ServicingPauseSpan = servicingPauseSpan;
                        }
                        else
                        {
                            MeasurableParameter measurableParameter =
                                area.Plant.MeasurableParameters.SingleOrDefault(
                                    p => p.MeasurableType == dataGridServiceScheduleView.Parameters);

                            serviceSchedule = new ServiceSchedule(Guid.NewGuid(), area.Id,
                                dataGridServiceScheduleView.ServiceName,
                                servicingSpan, servicingPauseSpan, new List<MeasurableParameter> {measurableParameter});
                        }
                        saveServiceSchedule(area, serviceSchedule);

                        MessageBox.Show($"'{dataGridServiceScheduleView.ServiceName}': schedule data saved");
                        dataGridServiceScheduleView.IsModified = false.ToString();
                    }
                })
                );
            return buttonTemplate;
        }
        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;
        }