Skip to content

Class TaskAttribute

Namespace: Belay.Attributes
Assembly: Belay.Attributes.dll

Marks a method as a remote task to be executed on a MicroPython device. Methods decorated with this attribute will have their code deployed to the connected device and executed remotely when called from the host application.

csharp
[AttributeUsage(AttributeTargets.Method)]
public sealed class TaskAttribute : Attribute

Inheritance

objectAttributeTaskAttribute

Inherited Members

Attribute.Equals(object?), Attribute.GetCustomAttribute(Assembly, Type), Attribute.GetCustomAttribute(Assembly, Type, bool), Attribute.GetCustomAttribute(MemberInfo, Type), Attribute.GetCustomAttribute(MemberInfo, Type, bool), Attribute.GetCustomAttribute(Module, Type), Attribute.GetCustomAttribute(Module, Type, bool), Attribute.GetCustomAttribute(ParameterInfo, Type), Attribute.GetCustomAttribute(ParameterInfo, Type, bool), Attribute.GetCustomAttributes(Assembly), Attribute.GetCustomAttributes(Assembly, bool), Attribute.GetCustomAttributes(Assembly, Type), Attribute.GetCustomAttributes(Assembly, Type, bool), Attribute.GetCustomAttributes(MemberInfo), Attribute.GetCustomAttributes(MemberInfo, bool), Attribute.GetCustomAttributes(MemberInfo, Type), Attribute.GetCustomAttributes(MemberInfo, Type, bool), Attribute.GetCustomAttributes(Module), Attribute.GetCustomAttributes(Module, bool), Attribute.GetCustomAttributes(Module, Type), Attribute.GetCustomAttributes(Module, Type, bool), Attribute.GetCustomAttributes(ParameterInfo), Attribute.GetCustomAttributes(ParameterInfo, bool), Attribute.GetCustomAttributes(ParameterInfo, Type), Attribute.GetCustomAttributes(ParameterInfo, Type, bool), Attribute.GetHashCode(), Attribute.IsDefaultAttribute(), Attribute.IsDefined(Assembly, Type), Attribute.IsDefined(Assembly, Type, bool), Attribute.IsDefined(MemberInfo, Type), Attribute.IsDefined(MemberInfo, Type, bool), Attribute.IsDefined(Module, Type), Attribute.IsDefined(Module, Type, bool), Attribute.IsDefined(ParameterInfo, Type), Attribute.IsDefined(ParameterInfo, Type, bool), Attribute.Match(object?), Attribute.TypeId, object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Examples

Basic Task Method

public class TemperatureSensor : Device
{
    [Task]
    public async Task<float> ReadTemperatureAsync()
    {
        // This code executes on the MicroPython device
        return await ExecuteAsync<float>(@"
            import machine
            import time

            # Read from analog temperature sensor
            adc = machine.ADC(machine.Pin(26))
            reading = adc.read_u16()
            voltage = reading * 3.3 / 65535
            temperature = 27 - (voltage - 0.706) / 0.001721
            temperature
        ");
    }
}

Task with Parameters

public class LEDController : Device
{
    [Task]
    public async Task SetBrightnessAsync(int pin, float brightness)
    {
        // Parameters are automatically marshaled to the device
        await ExecuteAsync($@"
            import machine
            led = machine.PWM(machine.Pin({pin}))
            led.freq(1000)
            led.duty_u16(int({brightness} * 65535))
        ");
    }
}

Complex Return Types

public class EnvironmentSensor : Device
{
    [Task]
    public async Task<SensorReading> GetReadingsAsync()
    {
        return await ExecuteAsync<SensorReading>(@"
            import json
            # Read multiple sensors
            temp = read_temperature()
            humidity = read_humidity()
            pressure = read_pressure()

            # Return as JSON for automatic deserialization
            json.dumps({
                'temperature': temp,
                'humidity': humidity,
                'pressure': pressure,
                'timestamp': time.ticks_ms()
            })
        ");
    }
}

public class SensorReading
{
    public float Temperature { get; set; }
    public float Humidity { get; set; }
    public float Pressure { get; set; }
    public long Timestamp { get; set; }
}

Remarks

The is the core attribute of the Belay.NET programming model. It enables seamless execution of C# methods on remote MicroPython devices by automatically handling code deployment, parameter marshaling, and result retrieval.

When a method decorated with [Task] is called, the Belay.NET runtime:

  1. Converts the method body to Python code
  2. Deploys the code to the connected MicroPython device
  3. Marshals parameters to Python-compatible types
  4. Executes the code on the device
  5. Retrieves and deserializes the result back to .NET types

Constructors

TaskAttribute()

Initializes a new instance of the class.

Properties

Cache

Gets or sets a value indicating whether gets or sets whether the method should be cached on the device. When true, the method code is deployed once and reused for subsequent calls. When false, the code is sent for each invocation.

Exclusive

Gets or sets a value indicating whether gets or sets whether this task method requires exclusive access to the device. When true, ensures no other methods execute concurrently on the device.

Name

Gets or sets the name of the method on the device. If not specified, the C# method name will be used.

TimeoutMs

Gets or sets the timeout for method execution in milliseconds. If not specified (or set to -1), uses the default timeout configured for the device.

Methods

ToString()

Returns a string that represents the current .

Released under the Apache License 2.0.