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.Core.Threading;
using Tango.CSV;
using Tango.Integration.ExternalBridge;
using Tango.Integration.Operation;
using Tango.PMR.Common;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.Build;
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; }
}
///
/// Represents the PPC external bridge service capable of exposing a remote API for communicating and controlling the machine through the PPC.
///
///
///
public class PPCExternalBridgeService : ExternalBridgeService, IPPCExternalBridgeService
{
private IBuildProvider _buildProvider;
///
/// Initializes a new instance of the class.
///
/// The application manager.
/// The machine provider.
public PPCExternalBridgeService(IPPCApplicationManager applicationManager, IMachineProvider machineProvider, IBuildProvider buildProvider)
{
_buildProvider = buildProvider;
var csvStream = EmbeddedResourceHelper.GetEmbeddedResourceStream("Tango.PPC.Common.SafetyLevelOperations.csv");
List entries = CsvFile.Read(new CsvSource(csvStream)).ToList();
foreach (var entry in entries)
{
MessageType type;
if (Enum.TryParse(entry.MessageType, out type))
{
ExternalBridgeService.SafetyLevelOperations.Add(type);
}
}
applicationManager.ApplicationReady += (_, __) =>
{
var settings = SettingsManager.Default.GetOrCreate();
//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;
if (_buildProvider.BuildType == BuildType.TS1800)
{
TimeoutTask.StartNew(() =>
{
Enabled = settings.EnableExternalBridge;
}, TimeSpan.FromMinutes(2));
}
else
{
Enabled = settings.EnableExternalBridge;
}
};
}
}
}