Pattern 5: Custom Functions¶
Define in adapter-registration.yaml¶
custom_functions:
ElevatorControl:
description: "Elevator floor access control"
states:
- pq.state.elevator.idle
- pq.state.elevator.moving
- pq.state.elevator.fault
properties:
current_floor:
type: int
default: 1
target_floor:
type: int
default: 1
actions:
FloorRequested:
description: "User requested floor access"
target_state: "pq.state.elevator.moving"
events:
- pq.event.elevator.floor.requested
FloorReached:
description: "Elevator arrived at floor"
target_state: "pq.state.elevator.idle"
events:
- pq.event.elevator.floor.reached
Fault:
description: "Elevator malfunction"
target_state: "pq.state.elevator.fault"
events:
- pq.event.elevator.fault
Generated Interface¶
// .GeneratedFiles/.../IElevatorControlFunction.g.cs
public interface IElevatorControlFunction : IFunction
{
int CurrentFloor { get; }
int TargetFloor { get; }
Task SetIdle(CancellationToken cancellationToken);
Task SetMoving(CancellationToken cancellationToken);
Task SetFault(CancellationToken cancellationToken);
}
Usage¶
// In ElevatorDevice.cs
internal sealed partial class ElevatorDevice
{
internal async Task OnFloorRequested(uint floor)
{
ElevatorControl.CurrentFloor = (int)floor;
await ElevatorControl.SetMoving(CancellationToken.None);
}
internal async Task OnFloorReached(uint floor)
{
ElevatorControl.TargetFloor = (int)floor;
await ElevatorControl.SetIdle(CancellationToken.None);
}
}
When to Use Custom Functions¶
Use custom functions for device-specific capabilities not covered by standard functions:
- Special access modes (elevator floors, parking levels)
- Device-specific states (patrol mode, test mode)
- Proprietary features unique to vendor
Standard functions (Door, Reader, Power, etc.) should be used whenever possible.