aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.MachineSettings/ViewModels/MainViewVM.cs
blob: 1565cda5af749ae778d5833be832298028ec204b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using RealTimeGraphEx;
using RealTimeGraphEx.Controllers;
using RealTimeGraphX;
using RealTimeGraphX.WPF.DataSeries;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.MachineStudio.Common.Controls
{
    public interface IRealTimeGraph
    {
        /// <summary>
        /// Gets or sets the name of the sensor.
        /// </summary>
        String DisplayName { get; set; }

        /// <summary>
        /// Gets or sets the tag.
        /// </summary>
        Object Tag { get; set; }

        /// <summary>
        /// Gets or sets the sensor units.
        /// </summary>
        String DisplayUnits { get; set; }

        /// <summary>
        /// Gets or sets the inner graph controller.
        /// </summary>
        IGraphController<WpfDataSeries> Controller { get; set; }
    }
}
href='#n326'>326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using Tango.BL;
using Tango.BL.Builders;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.ColorConversion;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.Core.ExtensionMethods;
using Tango.PPC.Common;
using Tango.PPC.Common.Connection;
using Tango.PPC.Common.ExternalBridge;
using Tango.PPC.Common.Lubrication;
using Tango.PPC.Common.Messages;
using Tango.PPC.Common.OS;
using Tango.PPC.Common.UWF;
using Tango.Settings;
using Tango.SharedUI.Components;
using Tango.WiFi;

namespace Tango.PPC.MachineSettings.ViewModels
{
    /// <summary>
    /// Represents the main view VM and entry point for <see cref="Synchronization.MyModule"/>.
    /// </summary>
    /// <seealso cref="Tango.PPC.Common.PPCViewModel" />
    public class MainViewVM : PPCViewModel
    {
        private TimeZoneInfo _previousTimeZone;
        private bool _previousEnableUWF;

        #region Classes

        public class RmlLubricationLevelSettings : RmlLubricationLevel
        {
            public String Name { get; set; }

            public RmlLubricationLevelSettings()
            {

            }

            public RmlLubricationLevelSettings(RmlLubricationLevel rmlLevel, String name)
            {
                RmlGuid = rmlLevel.RmlGuid;
                LubricationLevel = rmlLevel.LubricationLevel;
                Name = name;
            }

            public RmlLubricationLevel ToRmlLubricationLevel()
            {
                return new RmlLubricationLevel()
                {
                    RmlGuid = RmlGuid,
                    LubricationLevel = LubricationLevel,
                };
            }
        }

        #endregion

        #region Properties

        [TangoInject]
        private IOperationSystemManager OperationSystemManager { get; set; }

        [TangoInject]
        private IUnifiedWriteFilterManager UnifiedWriteFilterManager { get; set; }

        private Machine _machine;
        public Machine Machine
        {
            get { return _machine; }
            set { _machine = value; RaisePropertyChangedAuto(); }
        }

        private SelectedObjectCollection<JobTypes> _selectedJobTypes;
        public SelectedObjectCollection<JobTypes> SelectedJobTypes
        {
            get { return _selectedJobTypes; }
            set { _selectedJobTypes = value; RaisePropertyChangedAuto(); }
        }

        private SelectedObjectCollection<ColorSpaces> _selectedColorSpaces;
        public SelectedObjectCollection<ColorSpaces> SelectedColorSpaces
        {
            get { return _selectedColorSpaces; }
            set { _selectedColorSpaces = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<Rml> _rmls;
        public ObservableCollection<Rml> Rmls
        {
            get { return _rmls; }
            set { _rmls = value; RaisePropertyChangedAuto(); }
        }

        private bool _enableHotSpot;
        public bool EnableHotSpot
        {
            get { return _enableHotSpot; }
            set { _enableHotSpot = value; RaisePropertyChangedAuto(); OnEnableHotSpotChanged(); }
        }

        private String _hotSpotPassword;
        public String HotSpotPassword
        {
            get { return _hotSpotPassword; }
            set { _hotSpotPassword = value; RaisePropertyChangedAuto(); }
        }

        private bool _enableExternalBridge;
        public bool EnableExternalBridge
        {
            get { return _enableExternalBridge; }
            set { _enableExternalBridge = value; RaisePropertyChangedAuto(); OnEnableExternalBridgeChanged(); }
        }

        private String _externalBridgePassword;
        public String ExternalBridgePassword
        {
            get { return _externalBridgePassword; }
            set { _externalBridgePassword = value; RaisePropertyChangedAuto(); }
        }

        private bool _enableRemoteAssistance;
        public bool EnableRemoteAssistance
        {
            get { return _enableRemoteAssistance; }
            set { _enableRemoteAssistance = value; RaisePropertyChangedAuto(); OnEnableRemoteAssistanceChanged(); }
        }

        private bool _enableLockScreen;
        public bool EnableLockScreen
        {
            get { return _enableLockScreen; }
            set { _enableLockScreen = value; RaisePropertyChangedAuto(); }
        }

        private int _lockScreenTimeoutMinutes;
        public int LockScreenTimeoutMinutes
        {
            get { return _lockScreenTimeoutMinutes; }
            set { _lockScreenTimeoutMinutes = value; RaisePropertyChangedAuto(); }
        }

        private String _lockScreenPassword;
        public String LockScreenPassword
        {
            get { return _lockScreenPassword; }
            set { _lockScreenPassword = value; RaisePropertyChangedAuto(); }
        }

        private Rml _defaultRML;
        public Rml DefaultRML
        {
            get { return _defaultRML; }
            set { _defaultRML = value; RaisePropertyChangedAuto(); }
        }

        private SpoolType _defaultSpoolType;
        public SpoolType DefaultSpoolType
        {
            get { return _defaultSpoolType; }
            set { _defaultSpoolType = value; RaisePropertyChangedAuto(); }
        }

        private bool _synchronizeJobs;
        public bool SynchronizeJobs
        {
            get { return _synchronizeJobs; }
            set { _synchronizeJobs = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private bool _synchronizeDiagnostics;
        public bool SynchronizeDiagnostics
        {
            get { return _synchronizeDiagnostics; }
            set { _synchronizeDiagnostics = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private bool _autoCheckForUpdates;
        public bool AutoCheckForUpdates
        {
            get { return _autoCheckForUpdates; }
            set { _autoCheckForUpdates = value; RaisePropertyChangedAuto(); }
        }

        private List<TimeZoneInfo> _timeZones;
        /// <summary>
        /// Gets or sets the available time zones.
        /// </summary>
        public List<TimeZoneInfo> TimeZones
        {
            get { return _timeZones; }
            set { _timeZones = value; RaisePropertyChangedAuto(); }
        }

        private TimeZoneInfo _selectedTimeZone;
        /// <summary>
        /// Gets or sets the selected time zone.
        /// </summary>
        public TimeZoneInfo SelectedTimeZone
        {
            get { return _selectedTimeZone; }
            set { _selectedTimeZone = value; RaisePropertyChangedAuto(); }
        }

        private bool _enableUWF;
        public bool EnableUWF
        {
            get { return _enableUWF; }
            set { _enableUWF = value; RaisePropertyChangedAuto(); }
        }

        private List<RmlLubricationLevelSettings> _lubricationLevels;
        public List<RmlLubricationLevelSettings> LubricationLevels
        {
            get { return _lubricationLevels; }
            set { _lubricationLevels = value; RaisePropertyChangedAuto(); }
        }

        private RmlLubricationLevelSettings _selectedLubricationLevel;
        public RmlLubricationLevelSettings SelectedLubricationLevel
        {
            get { return _selectedLubricationLevel; }
            set { _selectedLubricationLevel = value; RaisePropertyChangedAuto(); }
        }

        private bool _useLightInks;
        public bool UseLightInks
        {
            get { return _useLightInks; }
            set { _useLightInks = value; RaisePropertyChangedAuto(); }
        }


        #endregion

        #region Commands

        /// <summary>
        /// Gets or sets the save command.
        /// </summary>
        public RelayCommand SaveCommand { get; set; }

        /// <summary>
        /// Gets or sets the discard command.
        /// </summary>
        public RelayCommand DiscardCommand { get; set; }

        /// <summary>
        /// Gets or sets the synchronize command.
        /// </summary>
        public RelayCommand SynchronizeCommand { get; set; }

        #endregion

        public MainViewVM()
        {
            LubricationLevels = new List<RmlLubricationLevelSettings>();
            SaveCommand = new RelayCommand(Save);
            DiscardCommand = new RelayCommand(Discard);
            SynchronizeCommand = new RelayCommand(Synchronize, () => !MachineDataSynchronizer.IsSynchronizing && IsFree);
        }

        private void Discard()
        {
            NavigationManager.NavigateBack();
        }

        private async void Save()
        {
            if (Validate())
            {
                Settings.SupportedJobTypes = SelectedJobTypes.SynchedSource.ToList();
                Settings.SupportedColorSpaces = SelectedColorSpaces.SynchedSource.ToList();
                Machine.MapPropertiesTo(MachineProvider.Machine, MappingFlags.NoReferenceTypes);

                Settings.EnableHotSpot = EnableHotSpot;
                Settings.HotSpotPassword = HotSpotPassword;
                Settings.EnableExternalBridge = EnableExternalBridge;
                Settings.ExternalBridgePassword = ExternalBridgePassword;
                Settings.EnableLockScreen = EnableLockScreen;
                Settings.LockScreenTimeout = TimeSpan.FromMinutes(LockScreenTimeoutMinutes);
                Settings.LockScreenPassword = LockScreenPassword;
                Settings.DefaultRmlGuid = DefaultRML?.Guid;
                Settings.DefaultSpoolTypeGuid = DefaultSpoolType?.Guid;
                Settings.SynchronizeJobs = SynchronizeJobs;
                Settings.SynchronizeDiagnostics = SynchronizeDiagnostics;
                Settings.AutoCheckForUpdates = AutoCheckForUpdates;
                Settings.LubricationLevels = LubricationLevels.Where(x => x.LubricationLevel != LubricationLevel.Standard).Select(x => x.ToRmlLubricationLevel()).ToList();

                MachineDataSynchronizer.IsEnabled = SynchronizeJobs || SynchronizeDiagnostics;

                var colorConversionSettings = SettingsManager.Default.GetOrCreate<ColorConversionSettings>();
                colorConversionSettings.UseLightInks = UseLightInks;

                Settings.Save();

                await MachineProvider.SaveMachine();

                bool isRestarting = false;

                if (_previousTimeZone.ToStringSafe() != SelectedTimeZone.ToStringSafe())
                {
                    if (await NotificationProvider.ShowQuestion("Changing the time zone requires the application to restart. Do you wish to restart the application?"))
                    {
                        try
                        {
                            LogManager.Log($"Setting new time zone to '{SelectedTimeZone.ToString()}'.");
                            NotificationProvider.SetGlobalBusyMessage("Setting new time zone...");
                            await OperationSystemManager.ChangeTimeZone(SelectedTimeZone);
                            NotificationProvider.ReleaseGlobalBusyMessage();
                            isRestarting = true;
                            ApplicationManager.Restart();
                        }
                        catch (Exception ex)
                        {
                            LogManager.Log(ex, "Error changing the time zone.");
                            NotificationProvider.ReleaseGlobalBusyMessage();
                            await NotificationProvider.ShowError($"Error setting timezone.\n{ex.FlattenMessage()}");
                        }
                    }
                }

                if (_previousEnableUWF != EnableUWF)
                {
                    await NotificationProvider.ShowWarning("Changes to disk protection (UWF) will take effect only after a full system restart.");

                    try
                    {
                        LogManager.Log($"Changing UWF mode to '{EnableUWF}'.");
                        if (EnableUWF)
                        {
                            NotificationProvider.SetGlobalBusyMessage("Enabling disk protection (UWF)...");
                            await UnifiedWriteFilterManager.Enable();
                        }
                        else
                        {
                            NotificationProvider.SetGlobalBusyMessage("Disabling disk protection (UWF)...");
                            await UnifiedWriteFilterManager.Disable();
                        }
                        NotificationProvider.ReleaseGlobalBusyMessage();
                    }
                    catch (Exception ex)
                    {
                        NotificationProvider.ReleaseGlobalBusyMessage();
                        LogManager.Log(ex, "Error setting UWF mode.");
                        await NotificationProvider.ShowError($"Could not change the disk protection mode\n{ex.FlattenMessage()}");
                    }
                }

                if (!isRestarting)
                {
                    await NavigationManager.NavigateBack();
                }
            }
        }

        protected override void OnValidating()
        {
            base.OnValidating();
        }

        /// <summary>
        /// Called when the application has been started
        /// </summary>
        public override void OnApplicationStarted()
        {
            try
            {
                TimeZones = OperationSystemManager.GetAvailableTimeZones().ToList();
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error retrieving available time zones.");
            }
        }

        public async override void OnApplicationReady()
        {
            base.OnApplicationReady();
            MachineDataSynchronizer.SynchronizationStarted += (_, __) => InvalidateRelayCommands();
            MachineDataSynchronizer.SynchronizationEnded += (_, __) => InvalidateRelayCommands();

            using (ObservablesContext db = ObservablesContext.CreateDefault())
            {
                Rmls = await new RmlsCollectionBuilder(db).SetAll().ForHeadType(MachineProvider.Machine.MachineHeadType).ForSite(MachineProvider.Machine.SiteGuid).BuildAsync();
            }
        }

        public async override void OnNavigatedTo()
        {
            base.OnNavigatedTo();

            Machine = MachineProvider.Machine.ShallowClone();

            RaisePropertyChanged(nameof(Machine));

            _enableHotSpot = HotSpotProvider.IsEnabled;
            RaisePropertyChanged(nameof(EnableHotSpot));

            HotSpotPassword = Settings.HotSpotPassword;

            _enableExternalBridge = ExternalBridgeService.Enabled;
            RaisePropertyChanged(nameof(EnableExternalBridge));

            ExternalBridgePassword = Settings.ExternalBridgePassword;

            _enableRemoteAssistance = RemoteAssistanceProvider.IsEnabled;
            RaisePropertyChanged(nameof(EnableRemoteAssistance));

            EnableLockScreen = Settings.EnableLockScreen;
            LockScreenTimeoutMinutes = (int)Settings.LockScreenTimeout.TotalMinutes;
            LockScreenPassword = Settings.LockScreenPassword;

            SelectedJobTypes = new SelectedObjectCollection<JobTypes>(Enum.GetValues(typeof(JobTypes)).Cast<JobTypes>().ToObservableCollection(), Settings.SupportedJobTypes.ToObservableCollection());
            SelectedColorSpaces = new SelectedObjectCollection<ColorSpaces>(Enum.GetValues(typeof(ColorSpaces)).Cast<ColorSpaces>().Where(x => x.IsUserSpace()).ToObservableCollection(), Settings.SupportedColorSpaces.ToObservableCollection());

            DefaultRML = Rmls.SingleOrDefault(x => x.Guid == Settings.DefaultRmlGuid);
            DefaultSpoolType = Adapter.SpoolTypes.SingleOrDefault(x => x.Guid == Settings.DefaultSpoolTypeGuid);

            SynchronizeJobs = Settings.SynchronizeJobs;
            SynchronizeDiagnostics = Settings.SynchronizeDiagnostics;

            AutoCheckForUpdates = Settings.AutoCheckForUpdates;

            var colorConversionSettings = SettingsManager.Default.GetOrCreate<ColorConversionSettings>();
            UseLightInks = colorConversionSettings.UseLightInks;

            SelectedTimeZone = TimeZones.SingleOrDefault(x => x.StandardName == TimeZone.CurrentTimeZone.StandardName);
            _previousTimeZone = SelectedTimeZone;

            try
            {
                EnableUWF = await UnifiedWriteFilterManager.IsEnabled();
                _previousEnableUWF = EnableUWF;
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error getting UWF status.");
            }

            try
            {
                List<RmlLubricationLevelSettings> levels = new List<RmlLubricationLevelSettings>();

                foreach (var rml in Rmls)
                {
                    RmlLubricationLevel userLevel = Settings.LubricationLevels.FirstOrDefault(x => x.RmlGuid == rml.Guid);

                    RmlLubricationLevelSettings rmlLevel = new RmlLubricationLevelSettings();
                    rmlLevel.RmlGuid = rml.Guid;
                    rmlLevel.Name = rml.Name;
                    rmlLevel.LubricationLevel = userLevel != null ? userLevel.LubricationLevel : LubricationLevel.Standard;

                    levels.Add(rmlLevel);
                }

                LubricationLevels = levels;

                if (SelectedLubricationLevel != null)
                {
                    SelectedLubricationLevel = LubricationLevels.FirstOrDefault(x => x.RmlGuid == SelectedLubricationLevel.RmlGuid);
                }
                else
                {
                    SelectedLubricationLevel = LubricationLevels.FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error loading lubrication levels.");
            }
        }

        private async void OnEnableRemoteAssistanceChanged()
        {
            if (EnableRemoteAssistance)
            {
                try
                {
                    await RemoteAssistanceProvider.EnableRemoteAssistance();
                }
                catch
                {
                    await NotificationProvider.ShowError("An error occurred while trying to activate the remote assistance service. Please check your device settings and try again.");
                    _enableRemoteAssistance = false;
                }
            }
            else
            {
                try
                {
                    await RemoteAssistanceProvider.DisableRemoteAssistance();
                }
                catch
                {
                    await NotificationProvider.ShowError("An error occurred while trying to deactivate the remote assistance service. Please check your device settings and try again.");
                    _enableRemoteAssistance = true;
                }
            }

            RaisePropertyChanged(nameof(EnableRemoteAssistance));
        }

        private async void OnEnableHotSpotChanged()
        {
            if (EnableHotSpot)
            {
                if (HotSpotPassword == null || HotSpotPassword.Length < 8 || HotSpotPassword.Length > 16)
                {
                    await NotificationProvider.ShowError("Hot spot requires a password of 8 to 16 characters.");
                    _enableHotSpot = false;
                    RaisePropertyChanged(nameof(EnableHotSpot));
                    return;
                }

                try
                {
                    await HotSpotProvider.EnableHotSpot(HotSpotPassword);
                }
                catch
                {
                    await NotificationProvider.ShowError("An error occurred while trying to activate the hot spot network. Please check your device settings and try again.");
                    _enableHotSpot = false;
                }
            }
            else
            {
                try
                {
                    await HotSpotProvider.DisableHotSpot();
                }
                catch
                {
                    await NotificationProvider.ShowError("An error occurred while trying to deactivate the hot spot network.");
                    _enableHotSpot = true;
                }
            }

            RaisePropertyChanged(nameof(EnableHotSpot));
        }

        private void OnEnableExternalBridgeChanged()
        {
            ExternalBridgeService.Enabled = EnableExternalBridge;
        }

        private async void Synchronize()
        {
            try
            {
                IsFree = false;
                NotificationProvider.SetGlobalBusyMessage("Synchronizing...");

                await MachineDataSynchronizer.Synchronize();

                NotificationProvider.ReleaseGlobalBusyMessage();
                await NotificationProvider.ShowSuccess("Synchronization completed successfully.");
            }
            catch (Exception ex)
            {
                NotificationProvider.ReleaseGlobalBusyMessage();
                await NotificationProvider.ShowError($"Error occurred while trying to synchronize.\n{ex.FlattenMessage()}");
            }
            finally
            {
                NotificationProvider.ReleaseGlobalBusyMessage();
                IsFree = true;
            }
        }
    }
}