Delete TestClient
This commit is contained in:
parent
adfdfdd747
commit
0555d2f1d3
6 changed files with 0 additions and 384 deletions
|
@ -1,20 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommandLineParser" Version="2.6.0" />
|
||||
<PackageReference Include="Grpc.Net.Client" Version="2.25.0" />
|
||||
<PackageReference Include="Microsoft.Azure.EventHubs" Version="4.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\NucuCar.Domain\NucuCar.Domain.csproj" />
|
||||
<ProjectReference Include="..\NucuCar.Telemetry\NucuCar.Telemetry.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -1,34 +0,0 @@
|
|||
using CommandLine;
|
||||
using NucuCar.TestClient.Sensors;
|
||||
using NucuCar.TestClient.Telemetry;
|
||||
|
||||
namespace NucuCar.TestClient
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
// ReSharper disable once ArrangeTypeMemberModifiers
|
||||
static int Main(string[] args)
|
||||
{
|
||||
return Parser.Default
|
||||
.ParseArguments<SensorsCmd.SensorsCmdOptions, AzureTelemetryPublishCmd.AzureTelemetryPublishOptions,
|
||||
AzureTelemetryReaderCmd.AzureTelemetryReaderOpts>(args)
|
||||
.MapResult(
|
||||
(SensorsCmd.SensorsCmdOptions opts) =>
|
||||
{
|
||||
SensorsCmd.RunSensorsTestCommand(opts).GetAwaiter().GetResult();
|
||||
return 0;
|
||||
},
|
||||
(AzureTelemetryPublishCmd.AzureTelemetryPublishOptions opts) =>
|
||||
{
|
||||
AzureTelemetryPublishCmd.RunAzurePublisherTelemetryTest(opts).GetAwaiter().GetResult();
|
||||
return 0;
|
||||
},
|
||||
(AzureTelemetryReaderCmd.AzureTelemetryReaderOpts opts) =>
|
||||
{
|
||||
AzureTelemetryReaderCmd.RunAzureTelemetryReaderTest(opts).GetAwaiter().GetResult();
|
||||
return 0;
|
||||
},
|
||||
errs => 1);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
# NucuCar.TestClient
|
||||
|
||||
Command line utility that allows you to play with different car functionality.
|
||||
|
||||
````bash
|
||||
NucuCar.TestClient 1.0.0
|
||||
Copyright (C) 2019 NucuCar.TestClient
|
||||
|
||||
ERROR(S):
|
||||
No verb selected.
|
||||
|
||||
sensors Test the gRPC sensors services.
|
||||
|
||||
azure-telemetry-publish Test the publishing telemetry using Microsoft Azure IoT Hub.
|
||||
|
||||
azure-telemetry-read Test reading the telemetry using Microsoft Azure's IoT Hub.
|
||||
|
||||
help Display more information on a specific command.
|
||||
|
||||
version Display version information.
|
||||
````
|
|
@ -1,132 +0,0 @@
|
|||
// ReSharper disable UnusedAutoPropertyAccessor.Global
|
||||
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CommandLine;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
using Grpc.Net.Client;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NucuCarSensorsProto;
|
||||
|
||||
namespace NucuCar.TestClient.Sensors
|
||||
{
|
||||
public class SensorsCmd
|
||||
{
|
||||
[Verb("sensors", HelpText = "Test the gRPC sensors services.")]
|
||||
public class SensorsCmdOptions
|
||||
{
|
||||
[Option('u', "url", Required = false, HelpText = "The url and port of the gRPC server.",
|
||||
Default = "http://localhost:8000")]
|
||||
public string GrpcServiceAddress { get; set; }
|
||||
|
||||
[Option('s', "sensor", Required = false, HelpText = "The sensor name you'd like to test.",
|
||||
Default = "environment")]
|
||||
public string SensorName { get; set; }
|
||||
}
|
||||
|
||||
public string GrpcServiceAddress { get; set; }
|
||||
private static ILogger _logger;
|
||||
|
||||
public static async Task RunSensorsTestCommand(SensorsCmdOptions options)
|
||||
{
|
||||
_logger = LoggerFactory.Create(builder => { builder.AddConsole(); }).CreateLogger<SensorsCmd>();
|
||||
var sensorsCommandLine = new SensorsCmd();
|
||||
sensorsCommandLine.GrpcServiceAddress = options.GrpcServiceAddress;
|
||||
|
||||
switch (options.SensorName)
|
||||
{
|
||||
case "environment":
|
||||
{
|
||||
await sensorsCommandLine.EnvironmentSensorGrpcServiceTest();
|
||||
break;
|
||||
}
|
||||
case "health":
|
||||
{
|
||||
await sensorsCommandLine.HealthSensorGrpcServiceTest();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new ArgumentException($"Invalid sensor name: ${options.SensorName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HealthSensorGrpcServiceTest()
|
||||
{
|
||||
var cts = SetupCancellation();
|
||||
var channel = SetupGrpc();
|
||||
var client = new HealthSensorGrpcService.HealthSensorGrpcServiceClient(channel);
|
||||
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (cts.Token.IsCancellationRequested)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
await Task.Delay(1000, cts.Token);
|
||||
|
||||
var measurementJson = await client.GetCpuTemperatureAsync(new Empty());
|
||||
_logger.LogInformation("State: " + measurementJson.State);
|
||||
_logger.LogInformation("CpuTemperature: " + measurementJson.JsonData);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task EnvironmentSensorGrpcServiceTest()
|
||||
{
|
||||
var cts = SetupCancellation();
|
||||
var channel = SetupGrpc();
|
||||
var client = new EnvironmentSensorGrpcService.EnvironmentSensorGrpcServiceClient(channel);
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (cts.Token.IsCancellationRequested)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
await Task.Delay(1000, cts.Token);
|
||||
|
||||
var measurementJson = await client.GetMeasurementAsync(new Empty());
|
||||
_logger.LogInformation("State " + measurementJson.State);
|
||||
_logger.LogInformation(measurementJson.JsonData);
|
||||
}
|
||||
}
|
||||
|
||||
private static CancellationTokenSource SetupCancellation()
|
||||
{
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
Console.CancelKeyPress += (s, e) =>
|
||||
{
|
||||
e.Cancel = true;
|
||||
cts.Cancel();
|
||||
Console.WriteLine("Shutting down...");
|
||||
};
|
||||
return cts;
|
||||
}
|
||||
|
||||
private GrpcChannel SetupGrpc()
|
||||
{
|
||||
// Used to allow gRPC calls over unsecured HTTP.
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
||||
|
||||
// Allow untrusted certificates.
|
||||
var httpClientHandler = new HttpClientHandler
|
||||
{
|
||||
ServerCertificateCustomValidationCallback =
|
||||
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
|
||||
};
|
||||
var httpClient = new HttpClient(httpClientHandler);
|
||||
|
||||
|
||||
var channel = GrpcChannel.ForAddress(GrpcServiceAddress,
|
||||
new GrpcChannelOptions {HttpClient = httpClient});
|
||||
return channel;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
// ReSharper disable UnusedAutoPropertyAccessor.Global
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CommandLine;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using NucuCar.Telemetry;
|
||||
using NucuCar.Telemetry.Abstractions;
|
||||
|
||||
namespace NucuCar.TestClient.Telemetry
|
||||
{
|
||||
public class AzureTelemetryPublishCmd
|
||||
{
|
||||
[Verb("azure-telemetry-publish", HelpText = "Test the publishing telemetry using Microsoft Azure IoT Hub.")]
|
||||
public class AzureTelemetryPublishOptions
|
||||
{
|
||||
[Option('c', "connectionString", Required = true,
|
||||
HelpText = "The publisher's connection string. Get it from the Device.")]
|
||||
public string PublisherConnectionString { get; set; }
|
||||
|
||||
[Option('m', "message", Required = true, HelpText = "The message to publish")]
|
||||
public string PublisherJsonMessage { get; set; }
|
||||
}
|
||||
|
||||
private class DummyTelemeter : ITelemeter
|
||||
{
|
||||
private readonly Dictionary<string, object> _dummyTelemeterData;
|
||||
|
||||
public DummyTelemeter(Dictionary<string, object> dummyData)
|
||||
{
|
||||
_dummyTelemeterData = dummyData;
|
||||
}
|
||||
|
||||
public string GetIdentifier()
|
||||
{
|
||||
return "DummyTelemeter";
|
||||
}
|
||||
|
||||
public Dictionary<string, object> GetTelemetryData()
|
||||
{
|
||||
return _dummyTelemeterData;
|
||||
}
|
||||
|
||||
public bool IsTelemetryEnabled()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task RunAzurePublisherTelemetryTest(AzureTelemetryPublishOptions opts)
|
||||
{
|
||||
var logger = LoggerFactory.Create(builder => { builder.AddConsole(); })
|
||||
.CreateLogger<AzureTelemetryPublishCmd>();
|
||||
|
||||
var telemetryPublisher = TelemetryPublisherFactory.Create(TelemetryPublisherType.Azure,
|
||||
opts.PublisherConnectionString, "NucuCar.TestClient", logger);
|
||||
|
||||
var anonymousTelemeter =
|
||||
new DummyTelemeter(
|
||||
JsonConvert.DeserializeObject<Dictionary<string, object>>(opts.PublisherJsonMessage));
|
||||
|
||||
|
||||
logger.LogInformation($"Publishing message: {opts.PublisherJsonMessage}");
|
||||
telemetryPublisher.RegisterTelemeter(anonymousTelemeter);
|
||||
await telemetryPublisher.PublishAsync(CancellationToken.None);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,107 +0,0 @@
|
|||
// ReSharper disable UnusedAutoPropertyAccessor.Global
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CommandLine;
|
||||
using Microsoft.Azure.EventHubs;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
||||
|
||||
namespace NucuCar.TestClient.Telemetry
|
||||
{
|
||||
public class AzureTelemetryReaderCmd
|
||||
{
|
||||
private static ILogger _logger;
|
||||
|
||||
[Verb("azure-telemetry-read", HelpText = "Test reading the telemetry using Microsoft Azure's IoT Hub.")]
|
||||
public class AzureTelemetryReaderOpts
|
||||
{
|
||||
[Option('c', "connectionString", Required = true,
|
||||
HelpText = "The connection string for the event hub. Get it from 'Build-in endpoints'")]
|
||||
public string EventHubConnectionString { get; set; }
|
||||
}
|
||||
|
||||
private static EventHubClient _eventHubClient;
|
||||
|
||||
public static async Task RunAzureTelemetryReaderTest(AzureTelemetryReaderOpts opts)
|
||||
{
|
||||
_logger = LoggerFactory.Create(builder => { builder.AddConsole(); })
|
||||
.CreateLogger<AzureTelemetryReaderCmd>();
|
||||
|
||||
_eventHubClient = EventHubClient.CreateFromConnectionString(opts.EventHubConnectionString);
|
||||
|
||||
var runtimeInfo = await _eventHubClient.GetRuntimeInformationAsync();
|
||||
var d2CPartitions = runtimeInfo.PartitionIds;
|
||||
|
||||
_logger.LogInformation("Starting reading messages from the Azure IoT Hub... Press Ctrl-C to cancel");
|
||||
var cts = new CancellationTokenSource();
|
||||
Console.CancelKeyPress += (s, e) =>
|
||||
{
|
||||
e.Cancel = true;
|
||||
cts.Cancel();
|
||||
_logger.LogInformation("Exiting...");
|
||||
};
|
||||
|
||||
var tasks = new List<Task>();
|
||||
foreach (string partition in d2CPartitions)
|
||||
{
|
||||
tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
|
||||
}
|
||||
|
||||
// Wait for all the PartitionReceivers to finsih.
|
||||
Task.WaitAll(tasks.ToArray());
|
||||
}
|
||||
|
||||
// Asynchronously create a PartitionReceiver for a partition and then start
|
||||
// reading any messages sent from the simulated client.
|
||||
private static async Task ReceiveMessagesFromDeviceAsync(string partition, CancellationToken ct)
|
||||
{
|
||||
// Create the receiver using the default consumer group.
|
||||
// For the purposes of this sample, read only messages sent since
|
||||
// the time the receiver is created. Typically, you don't want to skip any messages.
|
||||
var eventHubReceiver =
|
||||
_eventHubClient.CreateReceiver("$Default", partition, EventPosition.FromEnqueuedTime(DateTime.Now));
|
||||
_logger.LogInformation("Create receiver on partition: " + partition);
|
||||
while (true)
|
||||
{
|
||||
if (ct.IsCancellationRequested)
|
||||
{
|
||||
break;
|
||||
}
|
||||
_logger.LogInformation("Listening for messages on: " + partition);
|
||||
// Check for EventData - this methods times out if there is nothing to retrieve.
|
||||
var events = await eventHubReceiver.ReceiveAsync(100);
|
||||
|
||||
// If there is data in the batch, process it.
|
||||
if (events == null) continue;
|
||||
|
||||
foreach (var eventData in events)
|
||||
{
|
||||
var data = Encoding.UTF8.GetString(eventData.Body.Array);
|
||||
_logger.LogInformation($"Message received on partition {partition}:");
|
||||
_logger.LogInformation($"Data: {data}:");
|
||||
_logger.LogInformation("Application properties (set by device):");
|
||||
|
||||
var sb = new StringBuilder();
|
||||
foreach (var (key, value) in eventData.Properties)
|
||||
{
|
||||
sb.Append($"{key}: {value} \r\n");
|
||||
_logger.LogInformation(sb.ToString());
|
||||
}
|
||||
|
||||
sb.Clear();
|
||||
_logger.LogInformation("System properties (set by IoT Hub):");
|
||||
foreach (var (key, value) in eventData.SystemProperties)
|
||||
{
|
||||
sb.Append($"{key}: {value},");
|
||||
}
|
||||
|
||||
_logger.LogInformation(sb.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue