aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteAssistance/DefaultRemoteAssistanceProvider.cs
blob: 53596544a8a644e859314c3761b2f7ceca027381 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.PPC.Common.Connection;
using Tango.PPC.Common.Scripting;

namespace Tango.PPC.Common.RemoteAssistance
{
    /// <summary>
    /// Represents the default remote assistance provider.
    /// </summary>
    /// <seealso cref="Tango.Core.ExtendedObject" />
    /// <seealso cref="Tango.PPC.Common.RemoteAssistance.IRemoteAssistanceProvider" />
    public class DefaultRemoteAssistanceProvider : ExtendedObject, IRemoteAssistanceProvider
    {
        private IMachineProvider _machineProvider;

        private bool _isEnabled;
        /// <summary>
        /// Gets a value indicating whether the remote assistance service is enabled.
        /// </summary>
        public bool IsEnabled
        {
            get { return _isEnabled; }
            private set { _isEnabled = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultRemoteAssistanceProvider"/> class.
        /// </summary>
        /// <param name="machineProvider">The machine provider.</param>
        public DefaultRemoteAssistanceProvider(IMachineProvider machineProvider)
        {
            _machineProvider = machineProvider;
        }

        /// <summary>
        /// Enables the remote assistance.
        /// </summary>
        /// <returns></returns>
        public async Task EnableRemoteAssistance()
        {
            if (!IsEnabled)
            {
                try
                {
                    CmdCommand command = new CmdCommand("sc.exe", "config TeamViewer start=auto");
                    command.Timeout = TimeSpan.FromSeconds(10);
                    await command.Run();

                    command = new CmdCommand("net", "start TeamViewer");
                    command.Timeout = TimeSpan.FromSeconds(10);
                    await command.Run();

                    IsEnabled = true;
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error enabling remote assistance.");
                    throw;
                }
            }
        }

        /// <summary>
        /// Disables the remote assistance.
        /// </summary>
        /// <returns></returns>
        public async Task DisableRemoteAssistance()
        {
            if (IsEnabled)
            {
                try
                {
                    CmdCommand command = new CmdCommand("sc.exe", "config TeamViewer start=disabled");
                    command.Timeout = TimeSpan.FromSeconds(10);
                    await command.Run();

                    command = new CmdCommand("net", "stop TeamViewer");
                    command.Timeout = TimeSpan.FromSeconds(10);
                    await command.Run();
                    IsEnabled = false;
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error disabling remote assistance.");
                    throw;
                }
            }
        }

        /// <summary>
        /// Installs the remote assistance.
        /// </summary>
        /// <param name="groupName">Name of the group.</param>
        /// <param name="computerName">Name of the computer.</param>
        /// <returns></returns>
        public async Task InstallRemoteAssistance(string groupName, string computerName)
        {
            try
            {
                CmdCommand command = new CmdCommand("msiexec.exe", $"/i \"C:\\Program Files(x86)\\TeamViewer\\TeamViewer_Host.msi\" /qn CUSTOMCONFIGID=ke43ann APITOKEN=4765529-gon1LwO1N1TTrlLI21ji ASSIGNMENTOPTIONS=\" --reassign --alias {"TANGO-" + _machineProvider.Machine.SerialNumber} --grant-easy-access\"");
                await command.Run();
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error installing remote assistance.");
                throw;
            }
        }
    }
}