aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/MachineUpdates/DefaultMachineUpdatesProvider.cs
blob: 7b3555b7d6b179d3a2183219c9ac6862b0208af5 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Core.DI;
using Tango.FSE.Common.Connection;
using Tango.FSE.Common.MachineUpdates;
using Tango.PPC.Shared.Updates;
using Tango.Transport;

namespace Tango.FSE.UI.MachineUpdates
{
    /// <summary>
    /// Represents the <see cref="IMachineUpdatesProvider"/> default implementation.
    /// </summary>
    /// <seealso cref="Tango.FSE.Common.MachineUpdates.IMachineUpdatesProvider" />
    public class DefaultMachineUpdatesProvider : ExtendedObject, IMachineUpdatesProvider
    {
        private MachineUpdatesResult _lastResult;
        private IMachineProvider MachineProvider { get; set; }

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

        /// <summary>
        /// Gets the history of machine updates.
        /// </summary>
        /// <returns></returns>
        public async Task<MachineUpdatesResult> GetUpdates()
        {
            try
            {
                LogManager.Log("Retrieving remote machine software updates and packages history...");

                if (_lastResult != null)
                {
                    return _lastResult;
                }

                var response = await MachineProvider.MachineOperator.SendGenericRequest<GetUpdatesAndPackagesRequest, GetUpdatesAndPackagesResponse>(new GetUpdatesAndPackagesRequest() { }, new TransportRequestConfig()
                {
                    Timeout = TimeSpan.FromSeconds(30)
                });

                MachineUpdatesResult result = new MachineUpdatesResult();
                result.Updates = response.Updates.Select(x => x.ToObservable()).ToList();
                result.Packages = response.Packages;

                _lastResult = result;

                return result;
            }
            catch (Exception ex)
            {
                throw LogManager.Log(ex, "Error retrieving the remote machine updates and packages history.");
            }
        }

        private void MachineProvider_MachineDisconnected(object sender, MachineDisconnectedEventArgs e)
        {
            _lastResult = null;
        }
    }
}