Skip to content

Class Device

Namespace: Belay.Core
Assembly: Belay.Core.dll

Main entry point for MicroPython device communication. Provides a high-level interface for connecting to and interacting with MicroPython devices.

csharp
public class Device : IDisposable

Inheritance

objectDevice

Derived

SimpleDevice

Implements

IDisposable

Inherited Members

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Extension Methods

DeviceExtensions.FileSystem(Device), DeviceExtensions.GetFileSystem(Device, ILogger<DeviceFileSystem>?)

Examples

Basic Usage

using var device = Device.FromConnectionString("subprocess:micropython");
await device.ConnectAsync();

// Execute code directly
var result = await device.ExecuteAsync<int>("2 + 3");

// Check device capabilities
Console.WriteLine($"Platform: {device.State.Capabilities?.Platform}");
Console.WriteLine($"Features: {device.State.Capabilities?.SupportedFeatures}");

Remarks

This refactored Device class eliminates complex session management in favor of simple DeviceState tracking, providing direct device communication with improved performance and reliability.

Key improvements over the session-based approach:

  • Eliminates race conditions from concurrent session creation
  • Reduces initialization time from ~2000ms to <100ms
  • Provides direct executor access without session indirection
  • Aligns with single-threaded MicroPython device reality

Constructors

Device(DeviceConnection, ILogger<Device>?)

Initializes a new instance of the class.

Device(DeviceConnection, ILogger<Device>?, ILoggerFactory?)

Initializes a new instance of the class.

Device(DeviceConnection, ILogger<Device>?, ILoggerFactory?, IExecutionContextService?)

Initializes a new instance of the class with dependency injection support.

Properties

ConnectionState

Gets the current connection state of the device.

Executor

Gets the direct executor that handles all attribute types via AttributeHandler.

State

Gets the device state including capabilities, current operations, and connection status.

Methods

ConnectAsync(CancellationToken)

Connect to the MicroPython device and perform capability detection.

DisconnectAsync(CancellationToken)

Disconnect from the MicroPython device.

DiscoverDevicesAsync(CancellationToken)

Discover available MicroPython devices on the system.

DiscoverFirstAsync(ILoggerFactory?, CancellationToken)

Create a Device instance for the first discovered MicroPython device.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

ExecuteAsync(string, CancellationToken)

Execute Python code on the device and return the result. If called from a method with Belay attributes, applies attribute-specific policies.

ExecuteAsync<T>(string, CancellationToken)

Execute Python code on the device and return the result as a typed object. If called from a method with Belay attributes, applies attribute-specific policies.

ExecuteMethodAsync<T>(MethodInfo, object?, object?[]?, CancellationToken)

Executes a method with automatic executor selection based on attributes. This is the main entry point for the attribute-based programming model and uses secure execution context.

ExecuteMethodAsync(MethodInfo, object?, object?[]?, CancellationToken)

Executes a method without returning a value with automatic executor selection based on attributes.

FromConnectionString(string, ILoggerFactory?)

Create a Device instance from a connection string.

GetFileAsync(string, CancellationToken)

Retrieve a file from the device to the local system.

PutFileAsync(string, string, CancellationToken)

Transfer a file from the local system to the device.

OutputReceived

Event raised when output is received from the device.

StateChanged

Event raised when device connection state changes.

Released under the Apache License 2.0.