aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Gateway/DefaultGetwayService.cs
blob: 47d50547ec17768a8e914b19d7150010bb259111 (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
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.Core;
using Tango.MachineService.Gateway;
using Tango.Settings;
using Tango.Web;

namespace Tango.MachineStudio.Common.Gateway
{
    public class DefaultGetwayService : ExtendedObject, IGatewayService
    {
        private ReadOnlyObservableCollection<EnvironmentConfiguration> _environments;

        public ReadOnlyObservableCollection<EnvironmentConfiguration> Environments
        {
            get { return _environments; }
            private set { _environments = value; RaisePropertyChangedAuto(); }
        }

        public List<EnvironmentConfiguration> GetEnvironments()
        {
            if (SettingsManager.Default.GetOrCreate<MachineStudioSettings>().GatewayMode == GatewayMode.Gateway)
            {
                using (HttpClient http = new HttpClient())
                {
                    GatewayClient client = new GatewayClient(ConfigurationManager.AppSettings.Get("GatewayUrl"), http);
                    var response = client.GetEnvironments(new EnvironmentsRequest());
                    var list = response.Environments.ToList();
                    Environments = new ReadOnlyObservableCollection<EnvironmentConfiguration>(new ObservableCollection<EnvironmentConfiguration>(list));
                    return list;
                }
            }
            else
            {
                List<EnvironmentConfiguration> environments = new List<EnvironmentConfiguration>();

                foreach (var e in Enum.GetValues(typeof(DeploymentSlot)))
                {
                    environments.Add(new EnvironmentConfiguration()
                    {
                        ID = ((int)e).ToString(),
                        Description = ((DeploymentSlot)e).ToDescription(),
                        Name = e.ToString(),
                        MachineServiceAddress = ((DeploymentSlot)e).ToAddress()
                    });
                }

                return environments;
            }
        }
    }
}