{"id":13499826,"url":"https://github.com/lillo42/webthing-csharp","last_synced_at":"2025-08-04T03:37:25.477Z","repository":{"id":40871730,"uuid":"188098991","full_name":"lillo42/webthing-csharp","owner":"lillo42","description":".NET implementation of a Web Thing server ","archived":false,"fork":false,"pushed_at":"2022-12-08T10:35:23.000Z","size":1154,"stargazers_count":23,"open_issues_count":1,"forks_count":6,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-30T21:05:23.781Z","etag":null,"topics":["csharp","iot","mozilla-iot"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lillo42.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-22T19:13:13.000Z","updated_at":"2023-03-23T19:45:27.000Z","dependencies_parsed_at":"2023-01-25T09:45:57.390Z","dependency_job_id":null,"html_url":"https://github.com/lillo42/webthing-csharp","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/lillo42/webthing-csharp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lillo42%2Fwebthing-csharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lillo42%2Fwebthing-csharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lillo42%2Fwebthing-csharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lillo42%2Fwebthing-csharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lillo42","download_url":"https://codeload.github.com/lillo42/webthing-csharp/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lillo42%2Fwebthing-csharp/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268171959,"owners_count":24207437,"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","status":"online","status_checked_at":"2025-08-01T02:00:08.611Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["csharp","iot","mozilla-iot"],"created_at":"2024-07-31T22:00:43.611Z","updated_at":"2025-08-04T03:37:25.450Z","avatar_url":"https://github.com/lillo42.png","language":"C#","funding_links":[],"categories":["Section"],"sub_categories":["Libraries"],"readme":"# webthing\n\n[![Build Status](https://lillo42.visualstudio.com/Moziila%20%20IoT%20-%20Web%20Thing/_apis/build/status/lillo42.webthing-csharp?branchName=master)](https://lillo42.visualstudio.com/Moziila%20%20IoT%20-%20Web%20Thing/_build/latest?definitionId=3\u0026branchName=master)\n[![NuGet](http://img.shields.io/nuget/v/Mozilla.IoT.WebThing.svg)](https://www.nuget.org/packages/Mozilla.IoT.WebThing/)\n\n\n\nImplementation of an HTTP [Web Thing](https://iot.mozilla.org/wot/).\n\n# Using\n\n## Nuget\n\nAdd the following dependency to your project:\n\n```bash\ndotnet add package Mozilla.IoT.WebThing\n```\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/lillo42/webthing-csharp/tree/master/sample).\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://iot.mozilla.org/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```csharp\npublic class LampThing : Thing\n{\n    public override string Name =\u003e \"my-lamp-123\";\n    public override string? Title =\u003e \"My Lamp\";\n    public override string? Description =\u003e \"A web connected lamp\";\n    public override string[]? Type { get; } = new[] { \"Light\", \"OnOffSwitch\" };\n\n}\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 create a new property in Thing and add ```ThingPropertyAttribute```. For our purposes, we just want to log the new state if the light is switched on/off.\n\n```csharp\npublic class LampThing : Thing\n{\n    ...\n    private bool _on;\n\n    [ThingProperty(Type = new []{ \"OnOffProperty\" }, Title = \"On/Off\", Description = \"Whether the lamp is turned on\")]\n    public bool On \n    { \n        get =\u003e _on;\n        set \n        {\n            _on = value;\n            Console.WriteLine($\"On is now {value}\");\n            OnPropertyChanged();\n        }\n    }\n}\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```csharp\npublic class LampThing : Thing\n{\n    ...\n    private int _brightness;\n    [ThingProperty(Type = new []{ \"BrightnessProperty\" },Title = \"Brightness\",\n        Description = \"The level of light from 0-100\", Minimum = 0, Maximum = 100,\n        Unit = \"percent\")]\n    public int Brightness \n    { \n        get =\u003e _brightness; \n        set\n        { \n            _brightness = value;\n            Console.WriteLine($\"Brightness is now {value}\");\n            OnPropertyChanged();\n        } \n    }\n}\n```\n\nNow we can add our newly created thing and add Thing middleware to Asp Net Core:\n\n```csharp\n// This method gets called by the runtime. Use this method to add services to the container.\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddThings()\n        .AddThing\u003cLampThing\u003e();\n\n    // If you want use Web Sockets.\n    services.AddWebSockets(opt =\u003e { });\n}\n\npublic void Configure(IApplicationBuilder app, IWebHostEnvironment env)\n{\n    // If you want use Web Sockets.\n    app.UseWebSockets();\n\n    app.UseEndpoints(endpoints =\u003e\n    {\n        endpoints.MapThings();\n    });\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://iot.mozilla.org/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```csharp\npublic class Humidity : Thing\n{\n    public override string? Title =\u003e \"My Humidity Sensor\";\n\n    public override string[]? Type { get; } = new[] {\"MultiLevelSensor\"};\n\n    public override string? Description =\u003e \"A web connected humidity sensor\";\n}\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```csharp\npublic class Humidity : Thing\n{\n    ...\n    [ThingProperty(Type = new []{\"LevelProperty\"}, Title = \"Humidity\", Description = \"The current humidity in %\",\n        Minimum = 0, Maximum = 100, Unit = \"percent\")]\n    public double Level { get; private set; }\n}\n```\n\nNow we have a sensor that constantly reports 0%. To make it usable, we need a thread or some kind of inAdd when the sensor has a new reading available. For this purpose we start a task that queries the physical sensor every few seconds. For our purposes, it just calls a fake method.\n\n```csharp\n// Start a task that polls the sensor reading every 3 seconds\n\nTask.Factory.StartNew(async () =\u003e {\n   await Task.Delay(3_000);\n   await Level = ReadFromGPIO();\n});\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flillo42%2Fwebthing-csharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flillo42%2Fwebthing-csharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flillo42%2Fwebthing-csharp/lists"}