Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions docs/beginners-guide/articles/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ Only methods decorated with `action()` are exposed to clients.

## Payload Validation

If arguments are loosely typed, the action will be invoked with given payload without any validation. One may validate them manually inside the method. However, one can also specify the expected argument schema using either `JSON Schema` or `pydantic` models:
If arguments are loosely typed, the action will be invoked with the given payload without any validation. One may validate them manually inside the method. However, one can also specify the expected argument schema using either `JSON Schema` or `pydantic` models:

<a id="actions-argument-schema"></a>
=== "JSON Schema"

=== "Single Argument"

Specify the expected type of the argument (with or without name)
Specify the expected type of the argument (with or without a name):

```py title="Input Schema" linenums="1" hl_lines="8"
--8<-- "docs/beginners-guide/code/thing_example_2.py:195:198"
Expand All @@ -47,7 +47,7 @@ If arguments are loosely typed, the action will be invoked with given payload wi
=== "Multiple Arguments"

Specify the argument names under the `properties` field with `type` as `object`.
Names not found in the `properties` field can be subsumed under python spread operator `**kwargs` if necessary (dont set `additionalProperties` to `False` in that case).
Names not found in the `properties` field can be subsumed under the Python unpacking operator `**kwargs` if necessary (don't set `additionalProperties` to `False` in that case).

```py title="Input Schema with Multiple Arguments" linenums="1" hl_lines="32"
--8<-- "docs/beginners-guide/code/thing_example_3.py:57:85"
Expand Down Expand Up @@ -98,7 +98,7 @@ If arguments are loosely typed, the action will be invoked with given payload wi

=== "Return Type"

Specify return type under `output_schema` field:
Specify the return type under the `output_schema` field:

```py title="With Return Type" linenums="1" hl_lines="38 39"
--8<-- "docs/beginners-guide/code/thing_example_3.py:22:55"
Expand Down Expand Up @@ -289,7 +289,7 @@ If arguments are loosely typed, the action will be invoked with given payload wi

=== "Return Type"

type annotate the return type:
Type annotate the return type:

```py
from typing import Annotated
Expand Down Expand Up @@ -368,7 +368,7 @@ If arguments are loosely typed, the action will be invoked with given payload wi
--8<-- "docs/beginners-guide/code/thing_example_2.py:558:559"
``` -->

It is always possible to custom validate the arguments after invoking the action:
It is always possible to perform custom validation of the arguments after invoking the action:

```py title="Custom Validation" linenums="1"
--8<-- "docs/beginners-guide/code/actions/parameterized_function.py:3:3"
Expand All @@ -381,7 +381,7 @@ It is always possible to custom validate the arguments after invoking the action
--8<-- "docs/beginners-guide/code/actions/parameterized_function.py:52:"
```

`ParameterizedFunction`(s) are classes that implement the `__call__` method and whose arguments are type defined using the same objects as properties. However, this type definition using `Property` object do not make these properties of the `Thing`. The implementation follows convention used by `param` where the
`ParameterizedFunction`(s) are classes that implement the `__call__` method and whose arguments are type defined using the same objects as properties. However, this type definition using a `Property` object does not make these properties of the `Thing`. The implementation follows the convention used by `param` where the
properties are termed as "parameters" (also hence the word "ParameterizedFunction").

The `__call__` method signature accepts its own self as the first argument,
Expand Down Expand Up @@ -420,7 +420,7 @@ class ServoMotor(Thing):
...
```

The return value is fetched and returned to the client. One could also start long running actions without fetching a return value,
The return value is fetched and returned to the client. One could also start long running actions without fetching a return value
(although it would be better in many cases to manually thread out a long running action):

```py title="Threaded Actions" linenums="3"
Expand Down Expand Up @@ -469,7 +469,7 @@ class DCPowerSupply(Thing):
# The suitability of this example in a realistic use case is untested
```

For long running actions that do not return, call them with `oneway` flag on the client, otherwise expect a `TimeoutError`:
For long running actions that do not return, call them with the `oneway` flag on the client, otherwise expect a `TimeoutError`:

```py
client.invoke_action("monitor_over_voltage", period=10, oneway=True)
Expand All @@ -481,6 +481,6 @@ client.invoke_action("monitor_over_voltage", period=10, oneway=True)
| ----------- | --------- | ------------------------------------------------------------------------------------------------------------------------------- |
| input | ✓ | schema of the input payload (validation carried out) |
| output | ✓ | schema of the output payload (validation not carried out) |
| safe | ✓ | whether the action is safe to execute, only treated as a metadata |
| safe | ✓ | whether the action is safe to execute, only treated as metadata |
| idempotent | ✓ | whether the action is idempotent, `True` when the action is executable in all states of a state machine, otherwise `False` |
| synchronous | ✓ | whether the action is synchronous, `False` for threaded actions and async actions which are scheduled in the running event loop |
12 changes: 6 additions & 6 deletions docs/beginners-guide/articles/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[API Reference](../../api-reference/events/index.md)

Events are pushed to the client through a publish-subscribe mechanism through all the protocols. Call `push()` method on the event to publish data to clients:
Events are pushed to the client through a publish-subscribe mechanism through all the protocols. Call the `push()` method on the event to publish data to clients:

```py title="Definition" linenums="1" hl_lines="21-23"
--8<-- "docs/beginners-guide/code/events/definition.py:3:25"
Expand All @@ -16,7 +16,7 @@ One can subscribe to the event using the attribute name:

=== "threaded"

In the default synchronous mode, the `subscribe_event` method spawns a thread that listens to the event in backgound:
In the default synchronous mode, the `subscribe_event` method spawns a thread that listens to the event in the background:

```py title="Subscription" linenums="1" hl_lines="9"
from hololinked.client import ClientFactory
Expand All @@ -32,7 +32,7 @@ One can subscribe to the event using the attribute name:

=== "async"

In the asynchronous mode, the `subscribe_event` method creates an event listening task in the running async loop. This requires the client to be running in an async loop, otherwise no events will be received although the server will be publishing it:
In the asynchronous mode, the `subscribe_event` method creates an event listening task in the running async loop. This requires the client to be running in an async loop, otherwise no events will be received although the server will be publishing them:

```py title="Subscription" linenums="1" hl_lines="9"
from hololinked.client import ClientFactory
Expand All @@ -52,7 +52,7 @@ One can subscribe to the event using the attribute name:

---

The callback function(s) must accept a single argument - an instance of `SSE` object. The payload can be accessed as using the `data` attribute:
The callback function(s) must accept a single argument - an instance of the `SSE` object. The payload can be accessed using the `data` attribute:

```py title="Event Data" linenums="1" hl_lines="9"
def event_cb(event):
Expand Down Expand Up @@ -103,7 +103,7 @@ Each subscription creates a new event stream. One can also supply multiple callb

=== "async"

The event listening task creates newer tasks in the running event loop:
The event listening task creates new tasks in the running event loop:

```py title="Thread Callbacks" linenums="1" hl_lines="12-13"
async def event_cb1(event):
Expand Down Expand Up @@ -136,7 +136,7 @@ All subscriptions to the same event are removed.

## Payload Schema

Schema for the payload may be supplied using pydantic or JSON schema:
A schema for the payload may be supplied using pydantic or JSON schema:

```py title="Payload Schema" linenums="1" hl_lines="13"
class GentecMaestroEnergyMeter(Thing):
Expand Down
36 changes: 18 additions & 18 deletions docs/beginners-guide/articles/object-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ One would require a [Thing Description](https://www.w3.org/TR/wot-thing-descript
[Spectrometer](https://examples.hololinked.dev/simulations/spectrometer/resources/wot-td) |
[Oscilloscope](https://examples.hololinked.dev/simulations/oscilloscope/resources/wot-td) <br/>

The purpose of this JSON metadata is to provide both a human and machine readable description of the `Thing` and its capabilities, so that clients can automatically discover and interact with it without prior knowledge. In `hololinked`, this JSON document is automatically generated and served by the server protocols. There is lesser requirement for manually creating said `ThingDescription`, unless one wants to highly customize it.
The purpose of this JSON metadata is to provide both a human and machine readable description of the `Thing` and its capabilities, so that clients can automatically discover and interact with it without prior knowledge. In `hololinked`, this JSON document is automatically generated and served by the server protocols. There is less need to manually create said `ThingDescription`, unless one wants to highly customize it.

To instantiate an `ObjectProxy`, use the `ClientFactory` for one protocol at a time:

Expand Down Expand Up @@ -79,7 +79,7 @@ To instantiate an `ObjectProxy`, use the `ClientFactory` for one protocol at a t
```

The `Thing Description` is published to the MQTT Broker under the topic `<thing_id>/thing-description` by the server,
and the `ClientFactory` subsribes to the `Thing Description` and constructs the `ObjectProxy`.
and the `ClientFactory` subscribes to the `Thing Description` and constructs the `ObjectProxy`.

MQTT currently supports only events and properties that publish change events.

Expand All @@ -99,29 +99,29 @@ To read and write multiple properties:

### invoke actions

One can also access actions with dot operator and supply positional and keyword arguments:
One can also access actions with the dot operator and supply positional and keyword arguments:

```py title="invoke actions with dot operator" linenums="1"
--8<-- "docs/beginners-guide/code/object_proxy/sync.py:31:40"
```

One can also use `invoke_action` to invoke an action by name
One can also use `invoke_action` to invoke an action by name:

```py title="invoke_action()" linenums="1"
--8<-- "docs/beginners-guide/code/object_proxy/sync.py:44:55"
```

### oneway scheduling

`oneway` scheduling do not fetch return value and exceptions that might occur while executing a property or an action. The server schedules the operation and returns an empty response to the client, allowing it to process further logic. It is possible to set a property, set multiple or all properties or invoke an action in oneway. Other operations are not supported.
`oneway` scheduling does not fetch return values or exceptions that might occur while executing a property or an action. The server schedules the operation and returns an empty response to the client, allowing it to process further logic. It is possible to set a property, set multiple or all properties or invoke an action in oneway. Other operations are not supported.

```py title="oneway=True" linenums="1"
--8<-- "docs/beginners-guide/code/object_proxy/sync.py:79:103"
```

Simply provide the keyword argument `oneway=True` to the operation method.

`oneway` must be always specified as a keyword argument. Due to this reason, one cannot have an action argument or a property on the server named `oneway` as it is a reserved keyword argument to such methods on the client. At least they become inaccessible on the `ObjectProxy`.
`oneway` must always be specified as a keyword argument. Due to this reason, one cannot have an action argument or a property on the server named `oneway` as it is a reserved keyword argument to such methods on the client. At least they become inaccessible on the `ObjectProxy`.

### no-block scheduling

Expand All @@ -131,9 +131,9 @@ Simply provide the keyword argument `oneway=True` to the operation method.
--8<-- "docs/beginners-guide/code/object_proxy/sync.py:107:139"
```

When using `read_reply()`, `noblock` calls raise exception on the client if the server raised its own exception or fetch the return value .
When using `read_reply()`, `noblock` calls raise an exception on the client if the server raised its own exception or fetch the return value.

<!-- We supported this before, but not now, TODO renable -->
<!-- We supported this before, but not now, TODO re-enable -->
<!-- Timeout exceptions are raised when there is no reply within timeout specified.

.. literalinclude:: code/object_proxy/sync.py
Expand All @@ -154,19 +154,19 @@ Simply prefix `async_` to the method name, like `async_read_property`, `async_wr
--8<-- "docs/beginners-guide/code/object_proxy/async.py:15:44"
```

There is no support for dot operator based access for asyncio. One may also note that `async` operations
There is no support for dot operator based access for asyncio. One may also note that `async` operations
do not change the nature of the execution on the server side.
`asyncio` on `ObjectProxy` is purely a client-side non-blocking network call, so that one can
simultaneously perform other async operations while the client is waiting for said network operation to complete.

!!! Note

`oneway` and `noblock` are not supported for async calls due to the asynchronous nature of the
operation themselves.
operations themselves.

### subscribe and unsubscribe events

To subscribe to an event, use `subscribe_event` method and pass a callback function that accepts a single argument, the event data:
To subscribe to an event, use the `subscribe_event` method and pass a callback function that accepts a single argument, the event data:

```py title="subscribe_event()" linenums="1"
from hololinked.client.abstractions import SSE
Expand All @@ -180,7 +180,7 @@ def update_plot(event: SSE):
spectrometer.subscribe_event("intensity_measurement_event", callbacks=update_plot)
```

It is possible to also use bound methods as callbacks. To unsubscribe from an event, use `unsubscribe_event` method:
It is possible to also use bound methods as callbacks. To unsubscribe from an event, use the `unsubscribe_event` method:

```py title="unsubscribe_event()" linenums="1"
spectrometer.unsubscribe_event("intensity_measurement_event")
Expand All @@ -190,7 +190,7 @@ One can also supply multiple callbacks to be executed in series or concurrently,

### observe and unobserve properties

Observing properties work similar to event subcriptions, provided the property was specified as [`observable`](properties/arguments.md#observable) on the server side.
Observing properties works similarly to event subscriptions, provided the property was specified as [`observable`](properties/arguments.md#observable) on the server side.

To observe a property:

Expand Down Expand Up @@ -221,7 +221,7 @@ Normally, there cannot be user defined attributes on the `ObjectProxy` as the at
--8<-- "docs/beginners-guide/code/object_proxy/customizations.py:3:7"
```

The requirement for this behaviour is due to python's duck typing. If one intends to set a property named `foo`, and instead types it as `fooo` (misspelt), it is better to raise an error instead of silently setting a new attribute `fooo` on the client.
The requirement for this behaviour is due to Python's duck typing. If one intends to set a property named `foo`, and instead types it as `fooo` (misspelt), it is better to raise an error instead of silently setting a new attribute `fooo` on the client.

One can overcome this by setting `allow_foreign_attributes` to `True`:

Expand All @@ -242,12 +242,12 @@ When the `invokation_timeout` expires, the operation is guaranteed to be never e
--8<-- "docs/beginners-guide/code/object_proxy/customizations.py:29:36"
```

> Currently only a global customization of these values are supported. In future, one may be able to specify timeouts per operation.
> Currently only a global customization of these values is supported. In future, one may be able to specify timeouts per operation.

<!-- #### change handshake timeout

Before sending the first message to the server, a handshake is always done explicitly to not loose messages on the socket.
This is an artificat of ZMQ (which also does its own handshake). `handshake_timeout` controls how long to look for the server,
Before sending the first message to the server, a handshake is always done explicitly to not lose messages on the socket.
This is an artifact of ZMQ (which also does its own handshake). `handshake_timeout` controls how long to look for the server,
in case the server takes a while to boot.

```py title="timeout" linenums="1"
Expand All @@ -256,7 +256,7 @@ in case the server takes a while to boot.

Default value is 1 minute. A `ConnectionError` is raised if the server cannot be contacted.

One can also delay contacting the server by setting `load_thing` to False. But one has to manually performing the `handshake` later
One can also delay contacting the server by setting `load_thing` to False. But one has to manually perform the `handshake` later
before loading the server resources:

```py title="timeout" linenums="1"
Expand Down
Loading
Loading