Switch API

The switch API allows you to give commands to your switch components for reading the state of a physical switch that has multiple discrete positions. A simple switch has two positions, and a knob could have any number of positions.

The switch component supports the following methods:

Method NameDescriptionviam-micro-server Support
SetPositionSet the position of the switch.
GetPositionReturn the current position of the switch.
GetNumberOfPositionsReturn the number of valid positions for this switch.
DoCommandExecute model-specific commands that are not otherwise defined by the component API.
CloseSafely shut down the resource and prevent further use.

Establish a connection

To get started using Viam’s SDKs to connect to and control your switch and the rest of your machine, go to your machine’s page on the Viam app, Navigate to the CONNECT tab’s Code sample page, select your preferred programming language, and copy the sample code.

To show your machine’s API key in the sample code, toggle Include API key.

When executed, this sample code creates a connection to your machine as a client.

The following examples assume you have an switch called "my_switch" configured as a component of your machine. If your switch has a different name, change the name in the code.

Import the switch package for the SDK you are using:

import (
  "go.viam.com/rdk/components/switch"
)

API

SetPosition

Set the position of the switch. Position must be within the valid range for the switch type.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • position (uint32)
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

GetPosition

Return the current position of the switch.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

For more information, see the Go SDK Docs.

GetNumberOfPositions

Return the number of valid positions for this switch.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

For more information, see the Go SDK Docs.

DoCommand

Execute model-specific commands that are not otherwise defined by the component API. For built-in models, model-specific commands are covered with each model’s documentation. If you are implementing your own switch and add features that have no built-in API method, you can access them with DoCommand.

Parameters:

Returns:

Example:

mySwitch, err := switch.FromRobot(machine, "my_switch")

command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := mySwitch.DoCommand(context.Background(), command)

For more information, see the Go SDK Docs.

Close

Safely shut down the resource and prevent further use.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.

Returns:

  • (error): An error, if one occurred.

Example:

mySwitch, err := switch.FromRobot(machine, "my_switch")

err = mySwitch.Close(context.Background())

For more information, see the Go SDK Docs.