You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/operate/get-started/other-hardware/dependencies.md
+59-46
Original file line number
Diff line number
Diff line change
@@ -7,38 +7,27 @@ type: "docs"
7
7
description: "Handle dependencies in your custom modular resource."
8
8
---
9
9
10
-
Dependencies are other {{< glossary_tooltip term_id="resource" text="resources" >}} that your modular resource needs to access in order to function.
11
-
For example, a vision service might depend on a camera component, meaning that the camera is a dependency of that vision service.
12
-
13
-
When [`viam-server` builds all the resources on a machine](/operate/get-started/other-hardware/#how-and-where-do-modules-run), it builds the dependencies first.
14
-
15
-
## Implicit versus explicit dependencies
10
+
## What are dependencies?
16
11
17
-
-**Implicit dependencies** require users to configure a named attribute (for example `"left-motor": "motor1"`).
12
+
Dependencies are other {{< glossary_tooltip term_id="resource" text="resources" >}} that your modular resource needs to access in order to function.
18
13
19
-
- Recommended when dependencies are required, because implicit dependencies:
20
-
- Make it more clear what needs to be configured.
21
-
- Eliminate the need for users to configure the same resource name twice.
22
-
- Make debugging easier.
23
-
- Your module code must access the dependency using its attribute name and return it in the list of dependencies from the `validate` function.
14
+
For example, you could write a sensor component that requires a camera component, meaning that the camera is a dependency of that sensor.
24
15
25
-
-**Explicit dependencies** require that a user list the names of dependencies in the `depends_on` field of the resource's configuration.
- Depending on how you write your module, especially if your resources use multiple explicit dependencies, you may need users to configure the dependency both in the `depends_on` field and as an attribute so that your code can determine which dependency is which.
29
-
For example:
27
+
Dependencies are configured just like any other resource attribute.
28
+
The difference is that dependencies represent other resources that must be built before the resource that depends on them.
When [`viam-server` builds all the resources on a machine](/operate/get-started/other-hardware/#how-and-where-do-modules-run), it builds the dependencies first.
42
31
43
32
## Use dependencies
44
33
@@ -47,8 +36,7 @@ For example, you cannot call `Camera.from_robot()` to get a camera resource.
47
36
48
37
Instead, you must access dependencies by writing your module code as follows:
49
38
50
-
{{< tabs >}}
51
-
{{% tab name="Use implicit dependencies" %}}
39
+
### Required dependencies
52
40
53
41
{{< tabs >}}
54
42
{{% tab name="Python" %}}
@@ -185,54 +173,79 @@ If you need to maintain the state of your resource, see [(Optional) Create and e
185
173
{{% /tab %}}
186
174
{{< /tabs >}}
187
175
188
-
{{% /tab %}}
189
-
{{% tab name="Use explicit dependencies" %}}
176
+
### Optional dependencies
190
177
191
178
{{< tabs >}}
192
179
{{% tab name="Python" %}}
193
180
194
-
If you prefer to use explicit dependencies (for example, for an optional dependency), the steps are the same as for implicit dependencies, except that you do not need to return the dependency from the `validate_config` method and can instead return an empty list:
181
+
If your module has optional dependencies, the steps are the same as for required dependencies, except that your `validate_config` method can treat the dependency as optional by returning an empty list if the dependency is not configured:
Be sure to handle the case where the dependency is not configured in your API implementation as well.
199
+
209
200
{{% /tab %}}
210
201
{{% tab name="Go" %}}
211
202
212
-
If you prefer to use explicit dependencies (for example, for an optional dependency), the steps are the same as for implicit dependencies, except that you do not need to return the dependency from the `Validate` method and can instead return `nil`:
203
+
If your module has optional dependencies, the steps are the same as for required dependencies, except that your `Validate` method can treat the dependency as optional by returning an empty list if the dependency is not configured:
if cfg.CameraName != "" && reflect.TypeOf(cfg.CameraName).Kind() != reflect.String {
217
-
returnnil, errors.New("camera_name must be a string")
218
-
}
207
+
vardeps []string
219
208
if cfg.CameraName == "" {
220
-
logger.Info("camera_name not configured, using empty string and no camera")
209
+
logger.Info("camera_name not configured, using no camera")
210
+
returnnil, nil
211
+
}
212
+
if reflect.TypeOf(cfg.CameraName).Kind() != reflect.String {
213
+
returnnil, errors.New("camera_name must be a string")
221
214
}
222
-
returnnil, nil
215
+
deps = append(deps, cfg.CameraName)
216
+
return deps, nil
223
217
}
224
218
```
225
219
226
-
{{% /tab %}}
227
-
{{< /tabs >}}
220
+
Be sure to handle the case where the dependency is not configured in your API implementation as well.
228
221
229
222
{{% /tab %}}
230
223
{{< /tabs >}}
231
224
232
225
{{% hiddencontent %}}
233
-
There is not currently an SDK method to access configuration attributes of dependencies in Python or Go, but in Python it is possible to use `get_robot_part` to return information including the whole configuration of a machine part, and then access the configuration attributes of the dependency from there.
226
+
There is not currently an SDK method to directly access configuration attributes of dependencies in Python or Go, but in Python it is possible to use `get_robot_part` to return information including the whole configuration of a machine part, and then access the configuration attributes of the dependency from there.
227
+
You must access the API key module environment variables to establish the app client connection.
234
228
{{% /hiddencontent %}}
235
229
230
+
### Explicit dependencies (deprecated)
231
+
232
+
Some older modules use explicit dependencies, which require users to list the names of dependencies in the `depends_on` field of the resource's configuration, for example:
This is deprecated and not recommended when writing new modules.
246
+
247
+
Instead, we recommend using implicit dependencies (as shown in the examples above), which do not require users to list the names of dependencies in the `depends_on` field.
248
+
236
249
## Configure your module's dependencies more easily with a discovery service
237
250
238
251
If your module requires dependencies, you can make it easier for users to configure them by writing a [discovery service](/operate/reference/services/discovery/) as one model within your module.
0 commit comments