Hooks
useTelemetry
The useTelemetry
hook makes it very easy to just listen to telemetry on a single device.
Arguments
Argument | Description | Type | Required? |
---|---|---|---|
deviceId | The device ID of the device from which to receive telemetry data |
| |
telemetryKey | The key of the telemetry item |
| |
options | Configuration Options |
|
HookOptions
key | Description | value type | required |
---|---|---|---|
onData | Callback, executed when new telemetry of |
| |
onGrantError | Callback, executed when the |
| |
onSubscriptionError | Callback, executed when an error is returned from the subscription endpoints of ux4iot |
|
Return Value
This hook returns a single value. Every time the device sends new telemetry with key telemetryKey
this hook will update this value.
This hook relies on the assumption that your Device-to-Cloud messages are JSON documents where the key is the telemetry key and the value is the current telemetry value. We plan to support more complex payloads in the future (selecting using JSON Path, Avro, etc). If you have other message payloads, you can use the useD2CMessage hook.
Example
A component subscribing to the temperature
telemetry.
The D2C messages are expected to look like this:
useMultiTelemetry
The useMultiTelemetry
hook is a more sophisticated variant of useTelemetry
. It is designed to cover use-cases where a several streams of telemetry of multiple devices need to be subscribed to.
Minimal Hook Example
Full Hook Example
Full Code Example
Arguments
Argument | Description | Type | Required? |
---|---|---|---|
options | Configuration Options |
|
HookOptions
Argument | Description | Type | Required? |
---|---|---|---|
initialSubscribers | Object of key-value pairs, with keys: the device IDs of your IoTHub devices, and value: a list of strings, defining the telemetryKeys to subscribe to |
| |
onData | Callback, executed when a new |
| |
onGrantError | Callback, executed when the |
| |
onSubscriptionError | Callback, executed when an error is returned from the subscription endpoints of ux4iot |
|
Do not try to perform subscription updates over the initialSubscribers
object. This object is meant solely as an option for use cases where you always have an initial set of subscribers. Updates to initialSubscribers
will not trigger updates in the hook.
Return Value
This hook returns an object containing other objects and functions to interact with telemetry subscriptions.
Key | Value | Type |
---|---|---|
telemetry | Object holding the current values of all your telemetry subscriptions |
|
toggleTelemetry | Toggles a telemetry subscription for a |
|
addTelemetry | Adds a telemetry subscription for a |
|
removeTelemetry | Removes a telemetry subscription for a |
|
isSubscribed | Checks whether a telemetry subscription for a |
|
currentSubscribers | Object containing all current subscribers with key being the |
|
useDirectMethod
The useDirectMethod
hook returns a function that, when invoked, calls a direct method on the target device. It returns a Promise that resolves to the direct method result that the device returns (or an error when the direct method could not be executed, e.g. if the device is offline).
Arguments
Argument | Description | Type | Required? |
---|---|---|---|
deviceId | The device ID of the device to execute the direct method on |
| |
methodName | The name of the method to execute on the device |
| |
options | Configuration Options |
|
Hook Options
Argument | Description | Type | Required? |
---|---|---|---|
onGrantError | Callback, executed when the |
|
Return Value
This hook returns a function: (params: Record<string, unknown>) => Promise<IotHubResponse | void>
IoTHubResponse
You pass the method payload to this function and it outputs the result of the direct method invocation on the device.
Do not confuse the onGrantError
handler of useDirectMethod
with the catch block of the direct method itself. onGrantError
will only be executed when this hook is rendered by react and the security backend fails to grant the direct method. The error that reboot
may throw is an HTTP error that could occur when the function is executed.
Example
useDeviceTwin
The useDeviceTwin
hook subscribes to device twin updates.
Arguments
Argument | Description | Type | Required? |
---|---|---|---|
deviceId | The device id of the device you want to subscribe to. |
| |
options | Configuration Options |
|
Hook Options
Argument | Description | Type | Required? |
---|---|---|---|
onData | Callback, executed when a new twin updated is received. |
| |
onGrantError | Callback, executed when the |
| |
onSubscriptionError | Callback, executed when an error is returned from the subscription endpoints of ux4iot |
|
Return Value
This hook returns a value: Twin
This hook returns the device twin whenever twin updates are made on the IoTHub device. This hook uses the Twin
typescript type provided from the azure-iothub library.
Here is an example of a returned device twin:
useConnectionState
The useConnectionState
hook subscribes to the connection state of a device. This state can change, i.e as soon as a device connects or disconnects.
Arguments
Argument | Description | Type | Required? |
---|---|---|---|
deviceId | The device id of the device you want to subscribe to. |
| |
options | Configuration Options |
|
Hook Options
Argument | Description | Type | Required? |
---|---|---|---|
onData | Callback, executed when a new connectionState update is received. |
| |
onGrantError | Callback, executed when the |
| |
onSubscriptionError | Callback, executed when an error is returned from the subscription endpoints of ux4iot |
|
Return Value
This hook returns a value: boolean
This value changed as soon as a device connects or disconnects.
useMultiConnectionState
Minimal Hook Example
Full Hook Example
Arguments
Argument | Description | Type | Required? |
---|---|---|---|
options | Configuration Options |
|
HookOptions
Argument | Description | Type | Required? |
---|---|---|---|
initialSubscribers | Object of key-value pairs, with keys: the device IDs of your IoTHub devices, and value: a list of strings, defining the telemetryKeys to subscribe to |
| |
onData | Callback, executed when a new |
| |
onGrantError | Callback, executed when the |
| |
onSubscriptionError | Callback, executed when an error is returned from the subscription endpoints of ux4iot |
|
Do not try to perform subscription updates over the initialSubscribers
object. This object is meant solely as an option for use cases where you always have an initial set of subscribers. Updates to initialSubscribers
will not trigger updates in the hook.
Return Value
This hook returns an object containing other objects and functions to interact with telemetry subscriptions.
Key | Value | Type |
---|---|---|
connectionStates | Object holding the current values of all your connectionState subscriptions |
|
toggleConnectionState | Toggles a connectionState subscription for a |
|
addConnectionState | Adds a connectionState subscription for a |
|
removeConnectionState | Removes a connectionState subscription for a |
|
isSubscribed | Checks whether a telemetry subscription for a |
|
currentSubscribers | List of all current deviceIds that are subscribed |
|
usePatchDesiredProperties
The usePatchDesiredProperties
hook is used to perform desired property updates on devices.
Arguments
Argument | Description | Type | Required? |
---|---|---|---|
deviceId | The device id of the device onto which to patch the desired properties |
| |
options | Configuration Options |
|
Hook Options
Argument | Description | Type | Required? |
---|---|---|---|
onGrantError | Callback, executed when the |
|
Return Value
This hook returns a function: (desiredProperties: Record<string, unknown>) => Promise<IoTHubResponse | void>
IoTHubResponse
The hook takes in an object of desired properties to send to the device with the specified device ID.
When you call the function returned by this hook you will inevitably perform a device twin update. This means you will receive an update of the output of useDeviceTwin
Example
useD2CMessages<T>
Arguments
Argument | Description | Type | Required? |
---|---|---|---|
deviceId | The device ID of the device you want to subscribe to. |
| |
options | Configuration Options |
|
Hook Options
Argument | Description | Type | Required? |
---|---|---|---|
onData | Callback, executed when the device sends a new message. |
| |
onGrantError | Callback, executed when the |
| |
onSubscriptionError | Callback, executed when an error is returned from the subscription endpoints of ux4iot |
|
Return Value
This hook returns the generic type you specified: T
Messages received over this hook have the type unknown
first and are then casted to your type T
. Omitting this generic type will leave the type unknown
.
We assume that every message a device sends will be an object. The return value of this hook will always be the last message sent by the device.
Common Options
The hooks provided by ux4iot-react are using a specific authorization mechanism. They are designed to provide the easiest API to cover your use-case and hide the most complexity possible. There are two callbacks that are available on (almost) every hook.
onData
onData
A convenient callback to have a custom callback whenever data is received. You will receive updates of subscription hooks mostly over the return value. However, if you want to use custom logic whenever an update is received you would need to use custom hooks to listen for these changes like this:
Therefore onData
as function in subscription hooks, removes the burden of you needing to decide when to notice value changes.
onGrantError
onGrantError
This callback exists on every hook. The purpose of this callback is to inform you about errors that the custom grantRequestFunction
returns. The grantRequestFunction
is something that you need to implement yourself when you want to use ux4iot in production. The purpose of this function is to determine whether you as a user of the react application have the permission to subscribe to telemetry / device twin / connection state or perform a direct method / desired property patch.
Read more about this here.
Last updated