Code Monkey home page Code Monkey logo

lightwaverflinkplussharp's Introduction

LightwaveRFLinkPlusSharp

A C# binding for the LightwaveRF LinkPlus API

This is also available as a NuGet package: https://www.nuget.org/packages/LightwaveRFLinkPlusSharp

In order to connect to the Lightwave API you must provide a bearer ID and an initial refresh token. You can get these from https://my.lightwaverf.com > Settings > API. (The bearer ID is the long string labelled "Basic" for some reason.) During use of the API, further refresh tokens will be provided which will be handled for you automatically. If you stop being able to access the API at any point, however, you will have to request a new refresh token from the Lightwave site and provide it in this constructor.

To start using the API, first create an instance of it:

LightwaveAPI api = new LightwaveAPI("INSERT BEARER TOKEN HERE", "INSERT INITIAL REFRESH TOKEN HERE");

You can then get a list of devices present in the LinkPlus' first "structure" using the method below. This is a helper method for if your LinkPlus ecosystem only has a single structure; if you have more than one, use GetStructuresAsync and GetDevicesAsync(string) instead. See https://linkpluspublicapi.docs.apiary.io/#introduction/structure for more details about Structures.

Device[] devices = await api.GetDevicesInFirstStructureAsync();

This provides a list of devices and their features: the "switch" feature, for example, is used to control whether a device is currently switched on or not. At this stage, however, we only know what features each device supports, not the current value of each feature. We can get all the values for a device's features using PopulateFeatureValuesAsync:

Console.WriteLine($"{devices.Count()} devices discovered:");
foreach (var discoveredDevice in devices)
{
	Console.WriteLine($"\t{discoveredDevice.Name}");

	await api.PopulateFeatureValuesAsync(discoveredDevice);

	foreach (var deviceFeature in discoveredDevice.Features)
	{
		Console.WriteLine($"\t\t{deviceFeature}");
	}
}

We can also get the details for a particular device by name:

Device device = devices.First(x => x.Name == "INSERT DEVICE NAME HERE");

And then read the current value of a device's particular feature by getting the ID of the feature and then querying its value. For example, to get the value of a device's "switch" feature:

string featureId = device.GetFeatureId("switch");
int featureValue = await api.GetFeatureValueAsync(featureId);

For a lot of the feature types, however, there are helper properties that get the feature ID for you. The above query can therefore be simplified to just:

int featureValue = await api.GetFeatureValueAsync(device.SwitchFeatureId);

Note that for the older Connect devices, the LinkPlus may not know the current state if the last state change was not triggered by the app or the API, as Connect devices do not support 2-way communication. Therefore, if the device is currently switched on it may still return a value of 0

In order to change the value of a device's feature, use SetFeatureValue. For example, to turn the device on:

await api.SetFeatureValueAsync(device.SwitchFeatureId, toggledValue);

Note for some features, there are also typed helper methods for getting and setting states, for example:

bool on = await api.GetSwitchStateAsync(device);
await api.SetSwitchStateAsync(device, !on);

lightwaverflinkplussharp's People

Contributors

jimmyjudas avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.