|
| 1 | +import { Meta, Description } from '@storybook/addon-docs' |
| 2 | + |
| 3 | +<Meta |
| 4 | + title="Mobile app/TDM discovery" |
| 5 | + parameters={{ |
| 6 | + viewMode: 'docs', |
| 7 | + previewTabs: { |
| 8 | + canvas: { hidden: true } |
| 9 | + }, |
| 10 | + }} |
| 11 | +/> |
| 12 | + |
| 13 | +# Documentation for the TDM Discovery Module |
| 14 | + |
| 15 | +This documentation provides a detailed explanation of the TDM Discovery module, which is utilized in the discovery and communication of Thymio devices with iOS and Android tablets via network services. |
| 16 | + |
| 17 | +## Overview |
| 18 | + |
| 19 | +The TDM Discovery module leverages Zeroconf (also known as Bonjour) network technology to discover, resolve, and manage network services advertised by Thymio devices. This enables seamless communication between Thymio robots and controlling tablets without requiring manual network configuration. |
| 20 | + |
| 21 | +## Key Objects and Methods |
| 22 | + |
| 23 | +### `TdmDiscovery` |
| 24 | + |
| 25 | +This class encapsulates the logic for discovering Thymio devices using Zeroconf technology. It provides methods to start and stop discovery, manage service updates, and handle network statuses. |
| 26 | + |
| 27 | +#### Methods |
| 28 | + |
| 29 | +| Method | Parameters | Description | Return Type | |
| 30 | +|-------------------|-------------------------------------------|--------------------------------------------------------------|--------------------| |
| 31 | +| `constructor` | `nativeZeroConf: Zeroconf \| any` | Initializes a new TdmDiscovery instance with Zeroconf. | None | |
| 32 | +| `onChange` | `fun: (services: {[key: string]: MobsyaService}) => void` | Sets a callback to receive updates when new services are found or updated. | None | |
| 33 | +| `onStatusChange` | `fun: (status: StatusZeroConf) => void` | Sets a callback to receive updates on the discovery status. | None | |
| 34 | +| `close` | None | Stops the ongoing discovery process and cleans up resources. | None | |
| 35 | +| `scan` | None | Initiates the discovery process to find Thymio devices. | None | |
| 36 | + |
| 37 | +### `Zeroconf` |
| 38 | + |
| 39 | +This class provides the foundational network capabilities required for the discovery and management of network services in a local area network. |
| 40 | + |
| 41 | +#### Methods |
| 42 | + |
| 43 | +| Method | Parameters | Description | Return Type | |
| 44 | +|-------------------|---------------------------------------------------------------|--------------------------------------------------------------|--------------------| |
| 45 | +| `constructor` | `props: any` | Initializes a new Zeroconf instance. | None | |
| 46 | +| `addDeviceListeners` | None | Registers event listeners for Zeroconf network events. | None | |
| 47 | +| `removeDeviceListeners` | None | Removes all registered event listeners. | None | |
| 48 | +| `scan` | `type: string, protocol: string, domain: string, implType: string` | Starts scanning for Zeroconf services based on specified parameters. | None | |
| 49 | +| `stop` | `implType: string` | Stops the current scanning operation. | None | |
| 50 | +| `publishService` | `type, protocol, domain, name, port, txt, implType` | Publishes a service that can be discovered by others. | None | |
| 51 | +| `unpublishService`| `name, implType` | Stops publishing a previously registered service. | None | |
| 52 | +| `getServices` | None | Retrieves a list of all resolved services. | `Array of services`| |
| 53 | + |
| 54 | +## Integration in the Context of Thymio and Tablet Communication |
| 55 | + |
| 56 | +In the context of Thymio robots and tablet communication, the TDM Discovery module serves as a crucial component for auto-discovery and connectivity. Here’s how it integrates: |
| 57 | + |
| 58 | +1. **Device Discovery:** When the TdmDiscovery's `scan` method is initiated from a tablet, it uses the underlying Zeroconf technology to listen for Thymio devices broadcasting their availability on the network. |
| 59 | + |
| 60 | +2. **Service Handling:** As Thymio devices are found and their services resolved, the `TdmDiscovery` class updates its internal state with these services, accessible via callbacks set through `onChange`. |
| 61 | + |
| 62 | +3. **Status Monitoring:** The discovery process's status is monitored through `onStatusChange`, enabling the tablet application to provide user feedback (e.g., scanning, connected, or errors). |
| 63 | + |
| 64 | +4. **Service Resolution:** The `Zeroconf` class processes and resolves the details of each Thymio service, ensuring that the tablet can establish a robust communication link with correct network parameters (IP address, port, etc.). |
| 65 | + |
| 66 | +## Event-Driven Communication and State Management in Zeroconf |
| 67 | + |
| 68 | +Zeroconf, a protocol designed to enable automatic discovery of computers, devices, and services on IP networks, employs an event-driven architecture. This approach simplifies the process of integrating devices into networks without requiring manual network configuration. Here’s a detailed exploration of how Zeroconf manages its state and data through event-oriented communication, particularly in the context of the TDM Discovery module which uses Zeroconf for discovering Thymio devices. |
| 69 | + |
| 70 | +### Core Concepts |
| 71 | + |
| 72 | +**Event-Driven Architecture**: In Zeroconf, operations are primarily reactive; they respond to network events such as the appearance of new services, changes in the network status, or errors. This model is highly effective for network communication where state changes are frequent and unpredictable. |
| 73 | + |
| 74 | +**State Management**: Zeroconf maintains a dynamic representation of the network state, tracking available services and their statuses. This state is updated based on the events triggered by the network environment. |
| 75 | + |
| 76 | +### Key Events and States in Zeroconf |
| 77 | + |
| 78 | +Zeroconf revolves around several key events that trigger state changes: |
| 79 | + |
| 80 | +1. **Start**: The discovery process begins, indicating that the system is actively scanning the network for services. |
| 81 | +2. **Found**: A service is detected on the network. This event triggers an update in the internal state to include the new service. |
| 82 | +3. **Resolved**: Additional details of a found service are resolved, such as its network address and port. This step is crucial for enabling actual communication with the service. |
| 83 | +4. **Update**: The state or details of a service change, necessitating an update in the internal representation. |
| 84 | +5. **Stop**: The discovery process is halted, either due to manual intervention or network conditions. |
| 85 | +6. **Error**: An error occurs during the discovery process, affecting the current operation or the overall network state. |
| 86 | + |
| 87 | +### Mechanism of Event Handling and State Updates |
| 88 | + |
| 89 | +**Event Listeners**: Zeroconf employs listeners for each event type. These listeners are functions that execute in response to their respective events. For example: |
| 90 | +- When the `found` event is triggered, the listener function updates the internal service registry with the new service. |
| 91 | +- The `resolved` event's listener adds resolved details to the already registered service, such as IP addresses and ports. |
| 92 | + |
| 93 | +**State Transitions**: The transition between different states in Zeroconf is governed by the outcomes of these event responses. For instance: |
| 94 | +- On receiving the `start` event, the state transitions to "scanning". |
| 95 | +- On a `stop` event, the state changes to "stopped". |
| 96 | +- An `error` event may move the system into an "error" state, prompting either a retry or a halt, depending on the error's nature. |
| 97 | + |
| 98 | +**Callback Functions**: Developers can hook into these events by registering callbacks. These callbacks allow external modules, like the TDM Discovery module, to react appropriately to changes in the network state. For example, updating a UI element to reflect the discovery status or handling the connectivity logic to communicate with a Thymio device. |
| 99 | + |
| 100 | +### Practical Implementation in TDM Discovery |
| 101 | + |
| 102 | +In the TDM Discovery module, the Zeroconf mechanism is encapsulated within a class that manages both the discovery and the communication setup with Thymio devices. Here’s how it integrates Zeroconf: |
| 103 | +- **Initialization**: Upon instantiation, the `TdmDiscovery` class configures Zeroconf with necessary listeners for all relevant events. |
| 104 | +- **Discovery Process**: When the `scan` method is called, it triggers Zeroconf to start the network scan, handling the `start`, `found`, and `resolved` events as they occur. |
| 105 | +- **State Management**: The internal state of `TdmDiscovery` is updated based on these events, maintaining a current view of all Thymio devices available and their connectivity details. |
0 commit comments