aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/ExternalBridge/PPCExternalBridgeService.cs
blob: 99951d812a5da48dd03828e5c54feeada89f3ac1 (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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.Core.DI;
using Tango.Core.Helpers;
using Tango.CSV;
using Tango.Integration.ExternalBridge;
using Tango.Integration.Operation;
using Tango.PMR.Common;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.Connection;
using Tango.PPC.Common.Messages;
using Tango.Settings;

namespace Tango.PPC.Common.ExternalBridge
{
    public class CsvEntry
    {
        public String MessageType { get; set; }
    }

    /// <summary>
    /// Represents the PPC external bridge service capable of exposing a remote API for communicating and controlling the machine through the PPC.
    /// </summary>
    /// <seealso cref="Tango.Integration.ExternalBridge.ExternalBridgeService" />
    /// <seealso cref="Tango.PPC.Common.ExternalBridge.IPPCExternalBridgeService" />
    public class PPCExternalBridgeService : ExternalBridgeService, IPPCExternalBridgeService
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PPCExternalBridgeService"/> class.
        /// </summary>
        /// <param name="applicationManager">The application manager.</param>
        /// <param name="machineProvider">The machine provider.</param>
        public PPCExternalBridgeService(IPPCApplicationManager applicationManager, IMachineProvider machineProvider)
        {
            var csvStream = EmbeddedResourceHelper.GetEmbeddedResourceStream("Tango.PPC.Common.SafetyLevelOperations.csv");

            List<CsvEntry> entries = CsvFile.Read<CsvEntry>(new CsvSource(csvStream)).ToList();

            foreach (var entry in entries)
            {
                MessageType type;
                if (Enum.TryParse<MessageType>(entry.MessageType, out type))
                {
                    ExternalBridgeService.SafetyLevelOperations.Add(type);
                }
            }

            applicationManager.ApplicationReady += (_, __) =>
            {
                var settings = SettingsManager.Default.GetOrCreate<PPCSettings>();
                //TODO: Configure external bridge for configure protocol enforce...
                MachineOperator = machineProvider.MachineOperator;
                Machine = machineProvider.Machine;
                SignalRConfiguration.Enabled = settings.EnableExternalBridgeSignalR;
                TcpTransportAdapterWriteMode = settings.TcpTransportAdapterWriteMode;
                if (Environment.CommandLine.Contains("-webDebug"))
                {
                    SignalRConfiguration.Address = "http://localhost:1111/"; //settings.DeploymentSlot.ToAddress();
                }
                else
                {
                    SignalRConfiguration.Address = settings.DeploymentSlot.ToAddress();
                }
                SignalRConfiguration.Hub = settings.ExternalBridgeSignalRHub;
                Enabled = settings.EnableExternalBridge;
            };
        }
    }
}