Skip to content

Commit 7f115c2

Browse files
author
Robert Winkler
committed
Modified a readme
1 parent 0f912aa commit 7f115c2

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,83 @@ servient.expose("agent")
153153

154154
With Kotlin-WoT, developers can focus on implementing business logic while the framework handles the complexities of protocol management and Thing Description generation.
155155

156+
## Step-by-Step Guide to Consuming a Thing and Interacting with It
157+
158+
Here’s how to consume a Thing and interact with its properties and actions in a Web of Things (WoT) setup. Below are the main steps extracted from the provided code:
159+
160+
### Create a WoT Object
161+
162+
Example:
163+
```kotlin
164+
// Create the WoT object which can make use of HTTP. You can also add other protocols.
165+
val wot = Wot.create(Servient(clientFactories = listOf(HttpProtocolClientFactory())))
166+
167+
```
168+
169+
### **Obtain the Thing Description**
170+
- The first step in interacting with a Thing is to obtain its **Thing Description (TD)**, which describes the capabilities of the Thing (such as properties, actions, and events).
171+
- Use the `wot.requestThingDescription` function to fetch the TD of a Thing by its URL.
172+
173+
Example:
174+
```kotlin
175+
val thingDescription = wot.requestThingDescription("http://localhost:8080/${thingId}")
176+
```
177+
178+
### **Consume the Thing**
179+
- Once you have the TD, you can consume the Thing using the `wot.consume` method. This will allow you to interact with its properties and actions.
180+
- The `consume` function returns a `ConsumedThing` object, which represents the Thing in your code and provides methods to interact with it.
181+
182+
Example:
183+
```kotlin
184+
val consumedThing = wot.consume(thingDescription)
185+
```
186+
187+
### **Read a Property**
188+
- To interact with a property of the Thing, you can call the `readProperty` method on the `ConsumedThing` object.
189+
- This will return the current value of the property, which can be cast to the appropriate data type.
190+
191+
Example:
192+
```kotlin
193+
val readProperty = consumedThing.readProperty("property_name")
194+
```
195+
196+
### **Write to a Property**
197+
- To modify the value of a property, you can use the `writeProperty` method on the `ConsumedThing` object.
198+
- You need to pass the updated value to this method. Ensure that the value is wrapped in the correct `InteractionInput.Value` type.
199+
200+
Example:
201+
```kotlin
202+
consumedThing.writeProperty("property_name", 20.toInteractionInputValue()) // Update the property value
203+
```
204+
205+
### **Invoke an Action**
206+
- If the Thing exposes any actions, you can invoke them by calling the `invokeAction` method on the `ConsumedThing` object.
207+
- You need to provide the action name and any necessary input parameters.
208+
209+
Example:
210+
```kotlin
211+
val output = consumedThing.invokeAction(ACTION_NAME, "actionInput".toInteractionInputValue(), null)
212+
```
213+
214+
### **Read All Properties**
215+
- You can read all the properties of the Thing at once using the `readAllProperties` method on the `ConsumedThing` object.
216+
- This method returns a map of property names to their respective values.
217+
218+
Example:
219+
```kotlin
220+
val responseMap = consumedThing.readAllProperties()
221+
```
222+
223+
### **Observe Property Changes**
224+
- If the Thing supports property observation, you can use the `observeProperty` method to listen for updates to a property.
225+
- When a property changes, the listener will be triggered with the new value.
226+
227+
Example:
228+
```kotlin
229+
consumedThing.observeProperty(PROPERTY_NAME, listener = { println("Property observed: $it") })
230+
```
231+
232+
156233
For more details, refer to the official [W3C Web of Things](https://www.w3.org/WoT/) website.
157234

158235

0 commit comments

Comments
 (0)