aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service
diff options
context:
space:
mode:
authorRoy Ben Shabat <Roy.mail.net@gmail.com>2020-12-18 05:26:29 +0200
committerRoy Ben Shabat <Roy.mail.net@gmail.com>2020-12-18 05:26:29 +0200
commit86bbaee1a545f08dc7bfb4efe5f4696d6e4dccdd (patch)
tree0b46af20d6bb4568e44148b5e89d31da298c43fc /Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service
parentd9ee0b8e11f15c2b3bae068767516bc84a5ca4ed (diff)
downloadTango-86bbaee1a545f08dc7bfb4efe5f4696d6e4dccdd.tar.gz
Tango-86bbaee1a545f08dc7bfb4efe5f4696d6e4dccdd.zip
StubUtils
Diffstat (limited to 'Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service')
-rw-r--r--Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service/StubsService.cs137
-rw-r--r--Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service/StubsServiceSettings.cs5
2 files changed, 62 insertions, 80 deletions
diff --git a/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service/StubsService.cs b/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service/StubsService.cs
index 05b2d518c..58af2d8ff 100644
--- a/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service/StubsService.cs
+++ b/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service/StubsService.cs
@@ -26,12 +26,12 @@ namespace Tango.StubsUtils.Service
{
public class StubsService : ExtendedObject
{
+ private static int transporterCount = 1;
private const string PIPE_NAME = "Tango_Stubs_Server";
private NamedPipeServerStream _server;
private StreamReader _reader;
private StreamWriter _writer;
private BinaryFormatter _formatter;
- private StubsServiceSettings _settings;
private bool _initialized;
private List<Type> _stubsTypes;
private Dictionary<String, StubReflection> _stubsLookup;
@@ -73,8 +73,7 @@ namespace Tango.StubsUtils.Service
public StubsService()
{
- _settings = SettingsManager.Default.GetOrCreate<StubsServiceSettings>();
- _settings.Save();
+
}
#endregion
@@ -141,13 +140,14 @@ namespace Tango.StubsUtils.Service
#region Connect/Disconnect
- public async Task Connect()
+ public async Task Connect(String comPort)
{
if (!IsStarted) throw new InvalidOperationException("Cannot connect the transporter before the service has started.");
if (!IsConnected)
{
- Transporter = new BasicTransporter(new UsbTransportAdapter(_settings.USBPort));
+ Transporter = new BasicTransporter(new UsbTransportAdapter(comPort));
+ Transporter.ComponentName = $"Transporter {transporterCount++}";
Transporter.UseKeepAlive = false;
await Transporter.Connect();
IsConnected = true;
@@ -159,6 +159,7 @@ namespace Tango.StubsUtils.Service
if (IsConnected)
{
await Transporter.Disconnect();
+ IsConnected = false;
}
}
@@ -180,30 +181,27 @@ namespace Tango.StubsUtils.Service
var package = new StubPackageRequestDTO();
package.Arguments = request.Split(' ');
- if (EnableLogs) LogManager.Log($"Stub package constructed: '{package.ToJsonString()}'...");
-
var response = ProcessStubPackage(package);
- if (EnableLogs) LogManager.Log($"Stub package response: '{response.ToJsonString()}'...");
+ //if (EnableLogs) LogManager.Log($"Stub package response:\n'{response.ToJsonString()}'...");
_writer.Write(response.ToString());
_writer.Flush();
}
catch (Exception ex)
{
- if (EnableLogs) LogManager.Log(ex, "Error processing stub package.");
+ LogManager.Log(ex, "Error processing stub package.");
try
{
var response = new StubPackageResponseDTO()
{
Status = StubPackageResponseStatus.Error,
- Message = $"Error processing stub package\n{ex.FlattenMessage()}"
+ Message = $"Error: {ex.GetFirstIfAggregate().Message}"
};
_writer.WriteLine(response.ToString());
_writer.Flush();
- _server.Disconnect();
}
catch { }
}
@@ -224,92 +222,81 @@ namespace Tango.StubsUtils.Service
if (Transporter == null || Transporter.State != TransportComponentState.Connected)
{
- response.Status = StubPackageResponseStatus.NoConnection;
- response.Message = "Machine is disconnected.";
- return response;
+ throw new InvalidOperationException("Machine is disconnected");
}
- try
- {
- if (EnableLogs) LogManager.Log("Processing package...");
+ if (EnableLogs) LogManager.Log("Processing package...");
- String stubName = package.Arguments[0];
- List<String> arguments = package.Arguments.Skip(1).ToList();
+ String stubName = package.Arguments[0];
+ List<String> arguments = package.Arguments.Skip(1).ToList();
- StubReflection stubReflection = GetStubReflection(stubName);
+ StubReflection stubReflection = GetStubReflection(stubName);
- MessageContainer requestContainer = new MessageContainer();
- requestContainer.Token = Guid.NewGuid().ToString();
- requestContainer.Type = stubReflection.MessageType;
+ MessageContainer requestContainer = new MessageContainer();
+ requestContainer.Token = Guid.NewGuid().ToString();
+ requestContainer.Type = stubReflection.MessageType;
- IMessage request = Activator.CreateInstance(stubReflection.Type) as IMessage;
+ IMessage request = Activator.CreateInstance(stubReflection.Type) as IMessage;
- for (int i = 0; i < arguments.Count; i++)
- {
- String argument = arguments[i];
+ for (int i = 0; i < arguments.Count; i++)
+ {
+ String argument = arguments[i];
- if (i >= stubReflection.Properties.Count)
- {
- throw new ArgumentOutOfRangeException($"Argument '{argument}' index is out of range for stub '{stubReflection.Type.Name}'.");
- }
+ if (i >= stubReflection.Properties.Count)
+ {
+ throw new ArgumentException($"Argument '{argument}' index is out of range for stub '{stubReflection.Type.Name}'.");
+ }
- PropertyInfo prop = stubReflection.Properties[i];
+ PropertyInfo prop = stubReflection.Properties[i];
- if (prop.PropertyType == typeof(UInt32))
- {
- prop.SetValue(request, UInt32.Parse(argument));
- }
- else if (prop.PropertyType == typeof(bool))
- {
- prop.SetValue(request, bool.Parse(argument));
- }
- else if (typeof(IList).IsAssignableFrom(prop.PropertyType))
- {
- IList arr = prop.GetValue(request) as IList;
- foreach (var item in argument.Split(','))
- {
- object converted = Convert.ChangeType(item, prop.PropertyType.GetGenericArguments()[0]);
- arr.Add(converted);
- }
- }
- else
+ if (prop.PropertyType == typeof(UInt32))
+ {
+ prop.SetValue(request, UInt32.Parse(argument));
+ }
+ else if (prop.PropertyType == typeof(bool))
+ {
+ prop.SetValue(request, bool.Parse(argument));
+ }
+ else if (typeof(IList).IsAssignableFrom(prop.PropertyType))
+ {
+ IList arr = prop.GetValue(request) as IList;
+ foreach (var item in argument.Split(','))
{
- object converted = Convert.ChangeType(argument, prop.PropertyType);
- prop.SetValue(request, converted);
+ object converted = Convert.ChangeType(item, prop.PropertyType.GetGenericArguments()[0]);
+ arr.Add(converted);
}
}
+ else
+ {
+ object converted = Convert.ChangeType(argument, prop.PropertyType);
+ prop.SetValue(request, converted);
+ }
+ }
- if (EnableLogs) LogManager.Log($"Request stub constructed:\n{request.ToJsonString()}");
-
- requestContainer.Data = request.ToByteString();
- var responseContainer = Transporter.SendRequest(requestContainer, new TransportRequestConfig() { ThreadingMode = TransportThreadingMode.ThreadPool }).Result;
+ if (EnableLogs) LogManager.Log($"Request stub constructed:\n{request.ToJsonString()}");
- var stubResponseReflection = GetStubReflection(responseContainer.Type.ToOriginalName());
- IMessage stubResponse = stubResponseReflection.Parser.ParseFrom(responseContainer.Data);
+ requestContainer.Data = request.ToByteString();
+ var responseContainer = Transporter.SendRequest(requestContainer, new TransportRequestConfig() { ThreadingMode = TransportThreadingMode.ThreadPool }).Result;
- String responseMessage = String.Empty;
+ var stubResponseReflection = GetStubReflection(responseContainer.Type.ToOriginalName());
+ IMessage stubResponse = stubResponseReflection.Parser.ParseFrom(responseContainer.Data);
- foreach (var prop in stubResponseReflection.Properties)
- {
- responseMessage += $"{prop.Name}: {prop.GetValue(stubResponse).ToStringSafe()}\n";
- }
-
- if (EnableLogs)
- {
- String responseJson = stubResponse.ToJsonString();
- LogManager.Log($"Response:\n{stubResponse}");
- }
+ String responseMessage = String.Empty;
- response.Status = StubPackageResponseStatus.OK;
- response.Message = responseMessage;
+ foreach (var prop in stubResponseReflection.Properties)
+ {
+ responseMessage += $"{prop.Name}: {prop.GetValue(stubResponse).ToStringSafe()}\n";
}
- catch (Exception ex)
+
+ if (EnableLogs)
{
- LogManager.Log(ex, "Error processing stub package.");
- response.Status = StubPackageResponseStatus.Error;
- response.Message = ex.FlattenMessage();
+ String responseJson = stubResponse.ToJsonString();
+ LogManager.Log($"Stub package response:\n{responseJson}");
}
+ response.Status = StubPackageResponseStatus.OK;
+ response.Message = responseMessage;
+
return response;
}
diff --git a/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service/StubsServiceSettings.cs b/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service/StubsServiceSettings.cs
index 8741065cf..e71fd2138 100644
--- a/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service/StubsServiceSettings.cs
+++ b/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service/StubsServiceSettings.cs
@@ -9,11 +9,6 @@ namespace Tango.StubsUtils.Service
{
public class StubsServiceSettings : SettingsBase
{
- public String USBPort { get; set; }
- public StubsServiceSettings()
- {
- USBPort = "COM1";
- }
}
}