aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Scripting/Tango.Scripting.IDE/Images/Redo_16x.png
blob: 331e15706010115fc35ed09873524ae04ad293e2 (plain)
ofshex dumpascii
0000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 10 00 00 00 10 08 06 00 00 00 1f f3 ff .PNG........IHDR................
0020 61 00 00 00 04 67 41 4d 41 00 00 b1 8f 0b fc 61 05 00 00 00 09 70 48 59 73 00 00 0e c2 00 00 0e a....gAMA......a.....pHYs.......
0040 c2 01 15 28 4a 80 00 00 00 18 74 45 58 74 53 6f 66 74 77 61 72 65 00 70 61 69 6e 74 2e 6e 65 74 ...(J.....tEXtSoftware.paint.net
0060 20 34 2e 31 2e 36 fd 4e 09 e8 00 00 00 63 49 44 41 54 38 4f b5 8c 41 0e c0 20 08 04 7d 74 7f d7 .4.1.6.N.....cIDAT8O..A.....}t..
0080 97 f5 b4 0d 87 6d 28 ae 2d 68 3c 4c a2 03 4c 03 b0 44 27 8e f3 82 22 ce f9 4f 1d 13 bf c3 9b e1 .....m(.-h<L..L..D'..."..O......
00a0 f1 c8 13 ce 65 80 4e cd e2 8e 5c a2 8b de c3 f9 fe c0 1f cf c3 07 2a 91 d7 67 26 d2 89 6a 44 ca ....e.N...\...........*..g&..jD.
00c0 4a 44 4a 23 1b 91 92 2c 07 8c af 63 43 ca 3c 68 37 1a 17 0a 78 97 d0 db c7 00 00 00 00 49 45 4e JDJ#...,...cC.<h7...x........IEN
00e0 44 ae 42 60 82 D.B`.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.Core.ExtensionMethods;
using Tango.Integration.ExternalBridge;
using Tango.PMR.Integration;
using Tango.PPC.Common;
using Tango.PPC.Common.ExternalBridge;
using Tango.PPC.Common.Navigation;

namespace Tango.PPC.UI.ViewModels
{
    /// <summary>
    /// Represents the external bridge view ViewModel.
    /// </summary>
    /// <seealso cref="Tango.PPC.Common.PPCViewModel" />
    [TangoCreateWhenRegistered]
    public class ExternalBridgeViewVM : PPCViewModel
    {
        private bool _disconnecting;

        #region Properties

        private ExternalBridgeClientConnectedEventArgs _connection;
        /// <summary>
        /// Gets or sets the last client connection event arguments.
        /// </summary>
        public ExternalBridgeClientConnectedEventArgs Connection
        {
            get { return _connection; }
            set { _connection = value; RaisePropertyChangedAuto(); }
        }

        private User _user;
        /// <summary>
        /// Gets or sets the user connected user.
        /// </summary>
        public User User
        {
            get { return _user; }
            set { _user = value; RaisePropertyChangedAuto(); }
        }

        #endregion

        #region Commands

        /// <summary>
        /// Gets or sets the close session command.
        /// </summary>
        public RelayCommand CloseSessionCommand { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ExternalBridgeViewVM"/> class.
        /// </summary>
        public ExternalBridgeViewVM()
        {
            CloseSessionCommand = new RelayCommand(CloseSession, () => !_disconnecting);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Closes the current session.
        /// </summary>
        private void CloseSession()
        {
            LogManager.Log("Disconnecting current external bridge session.");
            _disconnecting = true;
            InvalidateRelayCommands();
            ExternalBridgeService.DisconnectSession();
        }

        #endregion

        #region Override Methods

        /// <summary>
        /// Called when the navigation system has navigated to this VM view.
        /// </summary>
        public override void OnNavigatedTo()
        {
            base.OnNavigatedTo();

            _disconnecting = false;
            InvalidateRelayCommands();
        }

        /// <summary>
        /// Called when the application has been started.
        /// </summary>
        public override void OnApplicationStarted()
        {
            ExternalBridgeService.ConnectionRequest += ExternalBridgeService_ConnectionRequest;
            ExternalBridgeService.ClientDisconnected += ExternalBridgeService_ClientDisconnected;
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the <see cref="ExternalBridgeService.ClientDisconnected"/> event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ExternalBridgeService_ClientDisconnected(object sender, EventArgs e)
        {
            if (IsVisible)
            {
                LogManager.Log("External bridge client disconnected. Navigating to home module...");

                InvokeUI(() =>
                {
                    NavigationManager.NavigateTo(NavigationView.HomeModule);
                });
            }
        }

        /// <summary>
        /// Handles the <see cref="ExternalBridgeService.ConnectionRequest"/> event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="ExternalBridgeClientConnectedEventArgs"/> instance containing the event data.</param>
        private void ExternalBridgeService_ConnectionRequest(object sender, ExternalBridgeClientConnectedEventArgs e)
        {
            LogManager.Log($"External bridge connection request received.\n{e.ToJsonString()}");

            if (!e.Request.Intent.RequiresPassword() || e.Request.Password == Settings.ExternalBridgePassword)
            {
                e.Confirmed = true;

                Connection = e;

                User = Adapter.Users.SingleOrDefault(x => x.Guid == e.Request.UserGuid);

                if (User != null)
                {
                    LogManager.Log($"External bridge connection user has been identified as {User.Contact.FullName}");
                }

                if (e.Request.Intent == ExternalBridgeLoginIntent.FullControl)
                {
                    LogManager.Log("Navigating to external bridge view...");
                    InvokeUI(() =>
                    {
                        NavigationManager.NavigateTo(NavigationView.ExternalBridgeView, false);
                    });
                }
            }
            else
            {
                LogManager.Log("Connection password did not match the machine external bridge password.");
            }
        }

        #endregion
    }
}