Skip to content

Commit

Permalink
Add API access with OAuth2 including an example web application
Browse files Browse the repository at this point in the history
  • Loading branch information
sineichen committed Aug 16, 2019
1 parent 8e18bb0 commit c0f66d5
Show file tree
Hide file tree
Showing 168 changed files with 30,141 additions and 142 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ Src/packages/
.vs/
Src/SmartMeApiClient/bin/
Src/SmartMeApiClient/obj/
Src/OAuthWebExample/bin/
Src/OAuthWebExample/obj/
*.user
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ The smart-me Api Client Library enables you to integrate smart-me API functional

## Examples

See the Example project in [`Src/Example`](Src/Example). You need a smart-me login which you can create [here](https://web.smart-me.com/login/). Set your username and password in [`Credentials.json`](Src/Example/Credentials.json). Please keep your username and password private.
First you need a smart-me login which you can create [here](https://web.smart-me.com/login/).

### Console Application using Basic Authentication

See the Console Application Example project in [`Src/Example`](Src/Example). Set your username and password in [`Credentials.json`](Src/Example/Credentials.json). Please keep your username and password private.

### Web Application using OAuth2 Authentication

See the Web Application Example project in [`Src/OAuthWebExample`](Src/Example). Set your username and password in [`appsettings.json`](Src/OAuthWebExample/appsettings.json). Please keep your username and password private.

## Requirements

.Net Framework 4.7.2+
- .Net Framework 4.7.2+
- .Net Core 2.1+
8 changes: 4 additions & 4 deletions Src/Example/ActionsExamples.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#region License
// Copyright (c) 2019 smart-me AG https://web.smart-me.com/
// Copyright (c) 2019 smart-me AG https://www.smart-me.com/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -41,7 +41,7 @@ public static async Task ActionsAsync(UserPassword credentials)
{
Helpers.WriteConsoleTitle("Get Actions");

List<SmartMeApiClient.Containers.Action> actions = await ActionsApi.GetActionsAsync(credentials, new Guid("c04db633-3c35-4e88-b9ee-11ab04ee7331"));
List<SmartMeApiClient.Containers.Action> actions = await ActionsApi.GetActionsAsync(credentials, new Guid("00315ffa-a6b6-4538-84f5-b50b685b0e83"));

foreach (var action in actions)
{
Expand All @@ -55,7 +55,7 @@ public static async Task ActionsAsync(UserPassword credentials)

await ActionsApi.RunActionsAsync(credentials, new SmartMeApiClient.Containers.ActionToRun
{
DeviceID = new Guid("c04db633-3c35-4e88-b9ee-11ab04ee7331"),
DeviceID = new Guid("00315ffa-a6b6-4538-84f5-b50b685b0e83"),
Actions = new List<SmartMeApiClient.Containers.ActionToRunItem>
{
new SmartMeApiClient.Containers.ActionToRunItem(HexStringHelper.ByteArrayToString(ObisCodes.SmartMeSpecificPhaseSwitchL1), 1.0)
Expand All @@ -68,7 +68,7 @@ public static async Task ActionsAsync(UserPassword credentials)

await ActionsApi.RunActionsAsync(credentials, new SmartMeApiClient.Containers.ActionToRun
{
DeviceID = new Guid("c04db633-3c35-4e88-b9ee-11ab04ee7331"),
DeviceID = new Guid("00315ffa-a6b6-4538-84f5-b50b685b0e83"),
Actions = new List<SmartMeApiClient.Containers.ActionToRunItem>
{
new SmartMeApiClient.Containers.ActionToRunItem(HexStringHelper.ByteArrayToString(ObisCodes.SmartMeSpecificPhaseSwitchL1), 0.0)
Expand Down
22 changes: 21 additions & 1 deletion Src/Example/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.2.1.0" newVersion="1.2.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
60 changes: 47 additions & 13 deletions Src/Example/DevicesExamples.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#region License
// Copyright (c) 2019 smart-me AG https://web.smart-me.com/
// Copyright (c) 2019 smart-me AG https://www.smart-me.com/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -50,9 +50,9 @@ public static async Task DevicesAsync(UserPassword credentials)

// Get all devices with a certain energy type
{
Helpers.WriteConsoleTitle("Get all devices with energy type 'MeterTypeAllMeters'");
Helpers.WriteConsoleTitle("Get all devices with energy type 'MeterTypeElectricity'");

List<Device> devices = await DevicesApi.GetDevicesAsync(credentials, MeterEnergyType.MeterTypeAllMeters);
List<Device> devices = await DevicesApi.GetDevicesAsync(credentials, MeterEnergyType.MeterTypeElectricity);

foreach (var device in devices)
{
Expand All @@ -62,9 +62,9 @@ public static async Task DevicesAsync(UserPassword credentials)

// Get all devices with a certain meter sub type
{
Helpers.WriteConsoleTitle("Get all devices with meter sub type 'MeterSubTypeElectricity'");
Helpers.WriteConsoleTitle("Get all devices with meter sub type 'MeterSubTypeHeat'");

List<Device> devices = await DevicesApi.GetDevicesAsync(credentials, MeterSubType.MeterSubTypeElectricity);
List<Device> devices = await DevicesApi.GetDevicesAsync(credentials, MeterSubType.MeterSubTypeHeat);

foreach (var device in devices)
{
Expand All @@ -83,7 +83,7 @@ public static async Task DevicesAsync(UserPassword credentials)
{
Helpers.WriteConsoleTitle("Get device by Id");

Device device = await DevicesApi.GetDeviceAsync(credentials, new Guid("c04db633-3c35-4e88-b9ee-11ab04ee7331"));
Device device = await DevicesApi.GetDeviceAsync(credentials, new Guid("00315ffa-a6b6-4538-84f5-b50b685b0e83"));
Console.WriteLine($"Name: {device.Name}, Id: {device.Id}");
}

Expand All @@ -99,7 +99,7 @@ public static async Task DevicesAsync(UserPassword credentials)
{
Helpers.WriteConsoleTitle("Get additional device information");

var info = await DevicesApi.GetAdditionalDeviceInformationAsync(credentials, new Guid("c04db633-3c35-4e88-b9ee-11ab04ee7331"));
var info = await DevicesApi.GetAdditionalDeviceInformationAsync(credentials, new Guid("00315ffa-a6b6-4538-84f5-b50b685b0e83"));
Console.WriteLine($"ID: {info.ID}, HardwareVersion: {info.HardwareVersion}, FirmwareVersion: {info.FirmwareVersion}, AdditionalMeterSerialNumber: {info.AdditionalMeterSerialNumber}");
}

Expand Down Expand Up @@ -147,29 +147,43 @@ public static async Task DeviceConfigurationAsync(UserPassword credentials)
{
Helpers.WriteConsoleTitle("Get smart-me device configuration");

var configuration = await DevicesApi.GetSmartMeDeviceConfigurationAsync(credentials, new Guid("c04db633-3c35-4e88-b9ee-11ab04ee7331"));
var configuration = await DevicesApi.GetSmartMeDeviceConfigurationAsync(credentials, new Guid("00315ffa-a6b6-4538-84f5-b50b685b0e83"));
Console.WriteLine($"Id: {configuration.Id}, UploadInterval: {configuration.UploadInterval}");
}

// Set smart-me Device Configuration
{
Helpers.WriteConsoleTitle("Set smart-me device configuration");

var configuration = await DevicesApi.GetSmartMeDeviceConfigurationAsync(credentials, new Guid("c04db633-3c35-4e88-b9ee-11ab04ee7331"));
var configuration = await DevicesApi.GetSmartMeDeviceConfigurationAsync(credentials, new Guid("00315ffa-a6b6-4538-84f5-b50b685b0e83"));

configuration.SwitchConfiguration[0].CanSwitchOff = false;

await DevicesApi.SetSmartMeDeviceConfigurationAsync(credentials, configuration);
try
{
await DevicesApi.SetSmartMeDeviceConfigurationAsync(credentials, configuration);
}
catch(Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}

Console.WriteLine("Device is not allowed to switch off");

Thread.Sleep(5000);

configuration = await DevicesApi.GetSmartMeDeviceConfigurationAsync(credentials, new Guid("c04db633-3c35-4e88-b9ee-11ab04ee7331"));
configuration = await DevicesApi.GetSmartMeDeviceConfigurationAsync(credentials, new Guid("00315ffa-a6b6-4538-84f5-b50b685b0e83"));

configuration.SwitchConfiguration[0].CanSwitchOff = true;

await DevicesApi.SetSmartMeDeviceConfigurationAsync(credentials, configuration);
try
{
await DevicesApi.SetSmartMeDeviceConfigurationAsync(credentials, configuration);
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}

Console.WriteLine("Device is allowed to switch off");

Expand Down Expand Up @@ -205,7 +219,27 @@ public static async Task CustomDevicesAsync(UserPassword credentials)

foreach (CustomDevice customDevice in customDevices)
{
Console.WriteLine($"Id: {customDevice.Id}, Name: {customDevice.Name}");
if (customDevice != null)
{
Console.WriteLine($"Id: {customDevice.Id}, Name: {customDevice.Name}");
}
}
}

// Update Custom Device
{
Helpers.WriteConsoleTitle("Update a custom device");

List<CustomDevice> customDevices = await DevicesApi.GetCustomDevicesAsync(credentials);

foreach (CustomDevice customDevice in customDevices)
{
if (customDevice != null)
{
customDevice.Name = "UpdatedCustomDeviceTest";
await DevicesApi.UpdateCustomDeviceAsync(credentials, customDevice);
break;
}
}
}

Expand Down
Loading

0 comments on commit c0f66d5

Please sign in to comment.