{"id":13499813,"url":"https://github.com/WebThingsIO/webthing-java","last_synced_at":"2025-03-29T05:32:28.268Z","repository":{"id":54578868,"uuid":"96545893","full_name":"WebThingsIO/webthing-java","owner":"WebThingsIO","description":"Java implementation of a Web Thing server","archived":false,"fork":false,"pushed_at":"2023-12-17T19:56:25.000Z","size":265,"stargazers_count":51,"open_issues_count":3,"forks_count":30,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-01-25T04:53:23.346Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WebThingsIO.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2017-07-07T14:21:15.000Z","updated_at":"2024-04-15T06:30:08.697Z","dependencies_parsed_at":"2023-12-17T20:46:23.885Z","dependency_job_id":"35758337-0fe4-4423-b4fa-1ab6a9ff302f","html_url":"https://github.com/WebThingsIO/webthing-java","commit_stats":null,"previous_names":["mozilla-iot/webthing-java"],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebThingsIO%2Fwebthing-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebThingsIO%2Fwebthing-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebThingsIO%2Fwebthing-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebThingsIO%2Fwebthing-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WebThingsIO","download_url":"https://codeload.github.com/WebThingsIO/webthing-java/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222465673,"owners_count":16989057,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T22:00:43.311Z","updated_at":"2024-10-31T18:31:15.080Z","avatar_url":"https://github.com/WebThingsIO.png","language":"Java","funding_links":[],"categories":["Section"],"sub_categories":["Libraries"],"readme":"# webthing\n\n[![Build Status](https://github.com/WebThingsIO/webthing-java/workflows/Java%20package/badge.svg)](https://github.com/WebThingsIO/webthing-java/workflows/Java%20package)\n[![Maven](https://img.shields.io/maven-central/v/io.webthings/webthing.svg)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22io.webthings%22%20AND%20a%3A%22webthing%22)\n[![license](https://img.shields.io/badge/license-MPL--2.0-blue.svg)](LICENSE)\n\nImplementation of an HTTP [Web Thing](https://webthings.io/api).\n\n# Using\n\n## Maven\n\nAdd the following dependency to your project:\n\n```xml\n\u003cdependencies\u003e\n    \u003cdependency\u003e\n        \u003cgroupId\u003eio.webthings\u003c/groupId\u003e\n        \u003cartifactId\u003ewebthing\u003c/artifactId\u003e\n        \u003cversion\u003e0.15.0\u003c/version\u003e\n    \u003c/dependency\u003e\n\u003c/dependencies\u003e\n```\n\n## Gradle\n\nAdd the following dependency to your project:\n\n```gradle\ndependencies {\n    runtime(\n        [group: 'io.webthings', name: 'webthing', version: '0.15.0'],\n    )\n}\n```\n\n## Android Studio\n\n- Open File → Project Structure\n- Select the module you want to add this as a dependency to\n- Go to the \"Dependencies\" tab\n- Click green \"+\" button\n- Select \"Library dependency\"\n- Enter `io.webthings:webthing` in the search bar and search\n- Select the package in the result and confirm with \"OK\"\n- Click \"OK\" in the Project Structure dialog\n\n# Example\n\nIn this example we will set up a dimmable light and a humidity sensor (both using fake data, of course). Both working examples can be found in [here](https://github.com/WebThingsIO/webthing-java/tree/master/src/main/java/io/webthings/webthing/example).\n\n## Dimmable Light\n\nImagine you have a dimmable light that you want to expose via the web of things API. The light can be turned on/off and the brightness can be set from 0% to 100%. Besides the name, description, and type, a [`Light`](https://webthings.io/schemas/#Light) is required to expose two properties:\n* `on`: the state of the light, whether it is turned on or off\n    * Setting this property via a `PUT {\"on\": true/false}` call to the REST API toggles the light.\n* `brightness`: the brightness level of the light from 0-100%\n    * Setting this property via a PUT call to the REST API sets the brightness level of this light.\n\nFirst we create a new Thing:\n\n```java\nThing light = new Thing(\"urn:dev:ops:my-lamp-1234\",\n                        \"My Lamp\",\n                        new JSONArray(Arrays.asList(\"OnOffSwitch\", \"Light\")),\n                        \"A web connected lamp\");\n```\n\nNow we can add the required properties.\n\nThe **`on`** property reports and sets the on/off state of the light. For this, we need to have a `Value` object which holds the actual state and also a method to turn the light on/off. For our purposes, we just want to log the new state if the light is switched on/off.\n\n```java\nJSONObject onDescription = new JSONObject();\nonDescription.put(\"@type\", \"OnOffProperty\");\nonDescription.put(\"title\", \"On/Off\");\nonDescription.put(\"type\", \"boolean\");\nonDescription.put(\"description\", \"Whether the lamp is turned on\");\n\nValue\u003cBoolean\u003e on = new Value\u003c\u003e(true,\n                                // Here, you could send a signal to\n                                // the GPIO that switches the lamp\n                                // off\n                                v -\u003e System.out.printf(\n                                        \"On-State is now %s\\n\",\n                                        v));\n\nlight.addProperty(new Property(light, \"on\", on, onDescription));\n```\n\nThe **`brightness`** property reports the brightness level of the light and sets the level. Like before, instead of actually setting the level of a light, we just log the level.\n\n```java\nJSONObject brightnessDescription = new JSONObject();\nbrightnessDescription.put(\"@type\", \"BrightnessProperty\");\nbrightnessDescription.put(\"title\", \"Brightness\");\nbrightnessDescription.put(\"type\", \"number\");\nbrightnessDescription.put(\"description\",\n                          \"The level of light from 0-100\");\nbrightnessDescription.put(\"minimum\", 0);\nbrightnessDescription.put(\"maximum\", 100);\nbrightnessDescription.put(\"unit\", \"percent\");\n\nValue\u003cDouble\u003e level = new Value\u003c\u003e(0.0,\n                                  // Here, you could send a signal\n                                  // to the GPIO that controls the\n                                  // brightness\n                                  l -\u003e System.out.printf(\n                                          \"Brightness is now %s\\n\",\n                                          l));\n\nlight.addProperty(new Property(light, \"level\", level, brightnessDescription));\n```\n\nNow we can add our newly created thing to the server and start it:\n\n```java\ntry {\n    // If adding more than one thing, use MultipleThings() with a name.\n    // In the single thing case, the thing's name will be broadcast.\n    WebThingServer server = new WebThingServer(new SingleThing(light), 8888);\n\n    Runtime.getRuntime().addShutdownHook(new Thread() {\n        public void run() {\n            server.stop();\n        }\n    });\n\n    server.start(false);\n} catch (IOException e) {\n    System.out.println(e);\n    System.exit(1);\n}\n```\n\nThis will start the server, making the light available via the WoT REST API and announcing it as a discoverable resource on your local network via mDNS.\n\n## Sensor\n\nLet's now also connect a humidity sensor to the server we set up for our light.\n\nA [`MultiLevelSensor`](https://webthings.io/schemas/#MultiLevelSensor) (a sensor that returns a level instead of just on/off) has one required property (besides the name, type, and optional description): **`level`**. We want to monitor this property and get notified if the value changes.\n\nFirst we create a new Thing:\n\n```java\nThing sensor = new Thing(\"urn:dev:ops:my-humidity-sensor-1234\",\n                         \"My Humidity Sensor\",\n                         new JSONArray(Arrays.asList(\"MultiLevelSensor\")),\n                         \"A web connected humidity sensor\");\n```\n\nThen we create and add the appropriate property:\n* `level`: tells us what the sensor is actually reading\n    * Contrary to the light, the value cannot be set via an API call, as it wouldn't make much sense, to SET what a sensor is reading. Therefore, we are creating a *readOnly* property.\n\n    ```java\n    JSONObject levelDescription = new JSONObject();\n    levelDescription.put(\"@type\", \"LevelProperty\");\n    levelDescription.put(\"title\", \"Humidity\");\n    levelDescription.put(\"type\", \"number\");\n    levelDescription.put(\"description\", \"The current humidity in %\");\n    levelDescription.put(\"minimum\", 0);\n    levelDescription.put(\"maximum\", 100);\n    levelDescription.put(\"unit\", \"percent\");\n    levelDescription.put(\"readOnly\", true);\n\n    this.level = new Value\u003c\u003e(0.0);\n\n    sensor.addProperty(new Property(sensor, \"level\", level, levelDescription));\n    ```\n\nNow we have a sensor that constantly reports 0%. To make it usable, we need a thread or some kind of input when the sensor has a new reading available. For this purpose we start a thread that queries the physical sensor every few seconds. For our purposes, it just calls a fake method.\n\n```java\n// Start a thread that polls the sensor reading every 3 seconds\nnew Thread(()-\u003e{\n    while(true){\n        try {\n            Thread.sleep(3000);\n            // Updates the underlying value, which in turn notifies all\n            // listeners\n            this.level.notifyOfExternalUpdate(readFromGPIO());\n        } catch (InterruptedException e) {\n            throw new IllegalStateException(e);\n        }\n    }\n}).start();\n```\n\nThis will update our `Value` object with the sensor readings via the `this.level.notifyOfExternalUpdate(readFromGPIO());` call. The `Value` object now notifies the property and the thing that the value has changed, which in turn notifies all websocket listeners.\n\n# Adding to Gateway\n\nTo add your web thing to the WebThings Gateway, install the \"Web Thing\" add-on and follow the instructions [here](https://github.com/WebThingsIO/thing-url-adapter#readme).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWebThingsIO%2Fwebthing-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FWebThingsIO%2Fwebthing-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWebThingsIO%2Fwebthing-java/lists"}