Skip to content

Commit

Permalink
Merge pull request #382 from WildernessLabs/feature/local-connection
Browse files Browse the repository at this point in the history
adding local connection support for Mac and Windows
  • Loading branch information
ctacke authored Oct 21, 2023
2 parents b55c525 + ac8aa55 commit 19eb5b9
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 96 deletions.
108 changes: 51 additions & 57 deletions Source/v2/Meadow.Cli/Commands/Current/Device/DeviceProvisionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class DeviceProvisionCommand : BaseDeviceCommand<DeviceProvisionCommand>
{
private DeviceService _deviceService;

public const string DefaultHost = "https://www.meadowcloud.co";

[CommandOption("orgId", 'o', Description = "The target org for device registration", IsRequired = false)]
public string? OrgId { get; set; }

Expand All @@ -30,46 +32,37 @@ public DeviceProvisionCommand(DeviceService deviceService, MeadowConnectionManag

protected override async ValueTask ExecuteCommand()
{
await base.ExecuteCommand();

UserOrg? org = null;

UserOrg? org;
try
{
if (Host == null) Host = BaseCloudCommand<DeviceProvisionCommand>.DefaultHost;
if (Host == null) Host = DefaultHost;

var identityManager = new IdentityManager(Logger);
var _userService = new UserService(identityManager);

Logger?.LogInformation("Retrieving your user and organization information...");

var userOrgs = await _userService.GetUserOrgs(Host, CancellationToken).ConfigureAwait(false);
if (userOrgs != null)
if (!userOrgs.Any())
{
Logger?.LogInformation($"Please visit {Host} to register your account.");
return;
}
else if (userOrgs.Count() > 1 && string.IsNullOrEmpty(OrgId))
{
Logger?.LogInformation($"You are a member of more than 1 organization. Please specify the desired orgId for this device provisioning.");
return;
}
else if (userOrgs.Count() == 1 && string.IsNullOrEmpty(OrgId))
{
OrgId = userOrgs.First().Id;
}

org = userOrgs.FirstOrDefault(o => o.Id == OrgId || o.Name == OrgId);
if (org == null)
{
if (!userOrgs.Any())
{
Logger?.LogInformation($"Please visit {Host} to register your account.");
return;
}
else if (userOrgs.Count() > 1 && string.IsNullOrEmpty(OrgId))
{
Logger?.LogInformation($"You are a member of more than 1 organization. Please specify the desired orgId for this device provisioning.");
return;
}
else if (userOrgs.Count() == 1 && string.IsNullOrEmpty(OrgId))
{
OrgId = userOrgs.First().Id;
}

if (!string.IsNullOrEmpty(OrgId))
{
org = userOrgs.FirstOrDefault(o => o.Id == OrgId || o.Name == OrgId);
if (org == null)
{
Logger?.LogInformation($"Unable to find an organization with a Name or ID matching '{OrgId}'");
return;
}
}
Logger?.LogInformation($"Unable to find an organization with a Name or ID matching '{OrgId}'");
return;
}
}
catch (MeadowCloudAuthException)
Expand All @@ -79,38 +72,39 @@ protected override async ValueTask ExecuteCommand()
return;
}

if (Connection != null && Connection.Device != null)
var connection = await GetCurrentConnection();

if (connection == null)
{
var info = await Connection.Device.GetDeviceInfo(CancellationToken);
Logger?.LogError($"No connection path is defined");
return;
}

Logger?.LogInformation("Requesting device public key (this will take a minute)...");
var publicKey = await Connection.Device.GetPublicKey(CancellationToken);
var info = await connection.Device!.GetDeviceInfo(CancellationToken);

var delim = "-----END PUBLIC KEY-----\n";
publicKey = publicKey.Substring(0, publicKey.IndexOf(delim) + delim.Length);
Logger?.LogInformation("Requesting device public key (this will take a minute)...");
var publicKey = await connection.Device.GetPublicKey(CancellationToken);

if (org != null
&& info != null
&& !string.IsNullOrEmpty(info.ProcessorId))
{
Logger?.LogInformation("Provisioning device with Meadow.Cloud...");

if (!string.IsNullOrEmpty(org.Id))
{
var result = await _deviceService.AddDevice(org.Id, info.ProcessorId, publicKey, CollectionId, Name, Host, CancellationToken);

if (result.isSuccess)
{
Logger?.LogInformation("Device provisioned successfully");
}
else
{
Logger?.LogError($"Failed to provision device: {result.message}");
}
}
}
var delim = "-----END PUBLIC KEY-----\n";
publicKey = publicKey.Substring(0, publicKey.IndexOf(delim) + delim.Length);


Logger?.LogInformation("Provisioning device with Meadow.Cloud...");

var provisioningID = !string.IsNullOrWhiteSpace(info.ProcessorId) ? info.ProcessorId : info.SerialNumber;
var provisioningName = !string.IsNullOrWhiteSpace(Name) ? Name : info.DeviceName;

var result = await _deviceService.AddDevice(org.Id, provisioningID, publicKey, CollectionId, provisioningName, Host, CancellationToken);

if (result.isSuccess)
{
Logger?.LogInformation("Device provisioned successfully");
}
else
{
Logger?.LogError($"Failed to provision device: {result.message}");
}

return;
}
}
}
59 changes: 33 additions & 26 deletions Source/v2/Meadow.Cli/MeadowConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,49 @@ public MeadowConnectionManager(ISettingsManager settingsManager)
if (_currentConnection != null) return _currentConnection;

// try to determine what the route is
string? uri = null;
if (route.StartsWith("http"))
if (route == "local")
{
uri = route;
}
else if (IPAddress.TryParse(route, out var ipAddress))
{
uri = $"http://{route}:5000";
}
else if (IPEndPoint.TryParse(route, out var endpoint))
{
uri = $"http://{route}";
}

if (uri != null)
{
_currentConnection = new TcpConnection(uri);
_currentConnection = new LocalConnection();
}
else
{
var retryCount = 0;
string? uri = null;
if (route.StartsWith("http"))
{
uri = route;
}
else if (IPAddress.TryParse(route, out var ipAddress))
{
uri = $"http://{route}:5000";
}
else if (IPEndPoint.TryParse(route, out var endpoint))
{
uri = $"http://{route}";
}

get_serial_connection:
try
if (uri != null)
{
_currentConnection = new SerialConnection(route);
_currentConnection = new TcpConnection(uri);
}
catch
else
{
retryCount++;
if (retryCount > 10)
var retryCount = 0;

get_serial_connection:
try
{
throw new Exception($"Cannot find port {route}");
_currentConnection = new SerialConnection(route);
}
catch
{
retryCount++;
if (retryCount > 10)
{
throw new Exception($"Cannot find port {route}");
}
Thread.Sleep(500);
goto get_serial_connection;
}
Thread.Sleep(500);
goto get_serial_connection;
}
}

Expand Down
Loading

0 comments on commit 19eb5b9

Please sign in to comment.