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;
        }
Пример #2
0
 public CustomParameterFunction(CustomParameter customParameter)
     : base(customParameter)
 {}
Пример #3
0
 public CustomSensor(Guid id, PlantsArea plantsArea, TimeSpan measuringTimeout, CustomParameter customParameter)
     : base(id, plantsArea, measuringTimeout, customParameter)
 {
     IsCustom = true;
     Function = new CustomParameterFunction(customParameter);
 }