aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.BL/Services/GatewayService.cs
blob: 3d315f45d2111c7a63a25dd907d7ae73a8376bbc (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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Tango.MachineService.Gateway;

namespace Tango.FSE.BL.Services
{
    /// <summary>
    /// Represents the gateway service used to retrieve the available Tango system environments/deployment slots.
    /// </summary>
    /// <seealso cref="Tango.FSE.BL.FSEServiceBase" />
    public class GatewayService : FSEServiceBase
    {
        private const string ENVIRONMENTS_COLLECTION = "Environments";
        private const string WEB_DEBUG_ADDRESS = "http://localhost:1111";

        private ReadOnlyObservableCollection<EnvironmentConfiguration> _environments;
        /// <summary>
        /// Gets the environments that were last retrieved using the <see cref="GetEnvironments"/> method.
        /// </summary>
        public ReadOnlyObservableCollection<EnvironmentConfiguration> Environments
        {
            get { return _environments; }
            private set { _environments = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Gets the available environments.
        /// Environments are cached on disk.
        /// </summary>
        /// <returns></returns>
        public Task<List<EnvironmentConfiguration>> GetEnvironments()
        {
            return DataResolver<List<EnvironmentConfiguration>>.Builder.New()
                   .ConfigureCascade(DataResolverNode.Online, DataResolverNode.DiskCache)
                   .Online((context) =>
                   {

                       using (HttpClient http = new HttpClient())
                       {
                           http.DefaultRequestHeaders.TryAddWithoutValidation("Cookie", "x-ms-routing-name=self");
                           GatewayClient client = new GatewayClient(ConfigurationManager.AppSettings.Get("GatewayUrl"), http);
                           var list = client.GetEnvironments(new EnvironmentsRequest()).Environments.OrderBy(x => x.DisplayIndex).ToList();

                           LogManager.Log("Online environments retrieved successfully. Caching environments...");

                           try
                           {

                               using (var cache = DiskCache.CreateContext())
                               {
                                   var collection = cache.Database.GetCollection<EnvironmentConfiguration>(ENVIRONMENTS_COLLECTION);

                                   foreach (var environment in list)
                                   {
                                       collection.Upsert(environment);
                                   }
                               }
                           }
                           catch (Exception ex)
                           {
                               LogManager.Log(ex, "Error caching online environments.");
                           }

                           bool isWebDebug = Environment.GetCommandLineArgs().Contains("-webDebug");

                           if (isWebDebug)
                           {
                               foreach (var item in list)
                               {
                                   item.MachineServiceAddress = WEB_DEBUG_ADDRESS;
                               }
                           }

                           return list;
                       }

                   })
                   .DiskCache((context) =>
                   {

                       using (var cache = DiskCache.CreateContext())
                       {
                           var collection = cache.Database.GetCollection<EnvironmentConfiguration>(ENVIRONMENTS_COLLECTION);
                           var environments = collection.FindAll().OrderBy(x => x.DisplayIndex).ToList();

                           if (environments.Count == 0)
                           {
                               //Online failed and no cache found so there must be an Internet connection at least once.
                               throw new InternetConnectionException();
                           }

                           return environments;
                       }

                   })
                   .OnComplete(environments =>
                   {

                       Environments = new ReadOnlyObservableCollection<EnvironmentConfiguration>(new ObservableCollection<EnvironmentConfiguration>(environments));

                   }).BuildExecuteAsync();
        }
    }
}