Skip to content
.NET implementation of a Web Thing server
C#
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode improve May 31, 2019
sample Add WebSockett Jun 1, 2019
src
test
.editorconfig init project May 22, 2019
.gitignore
.whitesource
Directory.Build.props
LICENSE Initial commit May 22, 2019
Mozzila.IoT.WebThing.sln
README.md Update README.md May 27, 2019
azure-pipelines.yml update .net version Jun 14, 2019

README.md

webthing

Implementation of an HTTP Web Thing.

Using

Nuget

Add the following dependency to your project:

dotnet add package Mozilla.IoT.WebThing

Example

In 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.

Dimmable Light

Imagine 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 is required to expose two properties:

  • on: the state of the light, whether it is turned on or off
    • Setting this property via a PUT {"on": true/false} call to the REST API toggles the light.
  • brightness: the brightness level of the light from 0-100%
    • Setting this property via a PUT call to the REST API sets the brightness level of this light.

First we create a new Thing:

var light = new Thing("My Lamp",
                        new JArray("OnOffSwitch", "Light"),
                        "A web connected lamp");

Now we can add the required properties.

The on property reports and sets the on/off state of the light. For this, we need to have a Property 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.

var onDescription = new JObject
{
    {"@type", "OnOffProperty"},
    {"title", "On/Off"},
    {"type", "boolean"},
    {"description", "Whether the lamp is turned on"}
};

var property = new Property<bool>(light, "on", true, onDescription);
property.ValuedChanged += (sender, value) => 
{
   Console.WriteLine($"On-State is now {value}");
};

light.AddProperty(property);

The 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.

var brightnessDescription = new JObject
{
    {"@type", "BrightnessProperty"},
    {"title", "Brightness"},
    {"type", "integer"},
    {"description", "The level of light from 0-100"},
    {"minimum", 0},
    {"maximum", 100},
    {"unit", "percent"}
};

var level = new Property<double>(light, "level", true, onDescription);
level.ValuedChanged += (sender, value) => 
{
   Console.WriteLine($"Brightness is now {value}");
};

light.AddProperty(level);

Now we can add our newly created thing and add Thing middleware to Asp Net Core:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
   services.AddThing();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   app.UseSingleThing(light);
}

This 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.

Sensor

Let's now also connect a humidity sensor to the server we set up for our light.

A 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.

First we create a new Thing:

var sensor = new Thing("My Humidity Sensor",
                         new JArray("MultiLevelSensor"),
                         "A web connected humidity sensor");

Then we create and add the appropriate property:

  • level: tells us what the sensor is actually reading

    • 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.
    var levelDescription = new JObject
    {
       {"@type", "LevelProperty"},
      {"title", "Humidity"},
      {"type", "number"},
      {"description", "The current humidity in %"},
      {"minimum", 0},
      {"maximum", 100},
      {"unit", "percent"},
      {"readOnly", true}
    };
    
    sensor.AddProperty(new Property<double>(sensor, "level", 0, levelDescription));

Now 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 thread that queries the physical sensor every few seconds. For our purposes, it just calls a fake method.

// Start a thread that polls the sensor reading every 3 seconds

Task.Factory.StartNew(async () => {
   await Task.Delay(3_000);
   await level.NotifyOfExternalUpdate(ReadFromGPIO());
});

This 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.

You can’t perform that action at this time.