Skip to content

Commit c0f66d5

Browse files
committed
Add API access with OAuth2 including an example web application
1 parent 8e18bb0 commit c0f66d5

File tree

168 files changed

+30141
-142
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+30141
-142
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ Src/packages/
66
.vs/
77
Src/SmartMeApiClient/bin/
88
Src/SmartMeApiClient/obj/
9+
Src/OAuthWebExample/bin/
10+
Src/OAuthWebExample/obj/
11+
*.user

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@ The smart-me Api Client Library enables you to integrate smart-me API functional
44

55
## Examples
66

7-
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.
7+
First you need a smart-me login which you can create [here](https://web.smart-me.com/login/).
8+
9+
### Console Application using Basic Authentication
10+
11+
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.
12+
13+
### Web Application using OAuth2 Authentication
14+
15+
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.
816

917
## Requirements
1018

11-
.Net Framework 4.7.2+
19+
- .Net Framework 4.7.2+
20+
- .Net Core 2.1+

Src/Example/ActionsExamples.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#region License
2-
// Copyright (c) 2019 smart-me AG https://web.smart-me.com/
2+
// Copyright (c) 2019 smart-me AG https://www.smart-me.com/
33
//
44
// Permission is hereby granted, free of charge, to any person obtaining a copy
55
// of this software and associated documentation files (the "Software"), to deal
@@ -41,7 +41,7 @@ public static async Task ActionsAsync(UserPassword credentials)
4141
{
4242
Helpers.WriteConsoleTitle("Get Actions");
4343

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

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

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

6969
await ActionsApi.RunActionsAsync(credentials, new SmartMeApiClient.Containers.ActionToRun
7070
{
71-
DeviceID = new Guid("c04db633-3c35-4e88-b9ee-11ab04ee7331"),
71+
DeviceID = new Guid("00315ffa-a6b6-4538-84f5-b50b685b0e83"),
7272
Actions = new List<SmartMeApiClient.Containers.ActionToRunItem>
7373
{
7474
new SmartMeApiClient.Containers.ActionToRunItem(HexStringHelper.ByteArrayToString(ObisCodes.SmartMeSpecificPhaseSwitchL1), 0.0)

Src/Example/App.config

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1-
<?xml version="1.0" encoding="utf-8" ?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<startup>
44
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
55
</startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
11+
</dependentAssembly>
12+
<dependentAssembly>
13+
<assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
14+
<bindingRedirect oldVersion="0.0.0.0-1.2.1.0" newVersion="1.2.1.0" />
15+
</dependentAssembly>
16+
<dependentAssembly>
17+
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
18+
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
19+
</dependentAssembly>
20+
<dependentAssembly>
21+
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
22+
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
23+
</dependentAssembly>
24+
</assemblyBinding>
25+
</runtime>
626
</configuration>

Src/Example/DevicesExamples.cs

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#region License
2-
// Copyright (c) 2019 smart-me AG https://web.smart-me.com/
2+
// Copyright (c) 2019 smart-me AG https://www.smart-me.com/
33
//
44
// Permission is hereby granted, free of charge, to any person obtaining a copy
55
// of this software and associated documentation files (the "Software"), to deal
@@ -50,9 +50,9 @@ public static async Task DevicesAsync(UserPassword credentials)
5050

5151
// Get all devices with a certain energy type
5252
{
53-
Helpers.WriteConsoleTitle("Get all devices with energy type 'MeterTypeAllMeters'");
53+
Helpers.WriteConsoleTitle("Get all devices with energy type 'MeterTypeElectricity'");
5454

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

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

6363
// Get all devices with a certain meter sub type
6464
{
65-
Helpers.WriteConsoleTitle("Get all devices with meter sub type 'MeterSubTypeElectricity'");
65+
Helpers.WriteConsoleTitle("Get all devices with meter sub type 'MeterSubTypeHeat'");
6666

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

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

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

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

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

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

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

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

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

160160
configuration.SwitchConfiguration[0].CanSwitchOff = false;
161161

162-
await DevicesApi.SetSmartMeDeviceConfigurationAsync(credentials, configuration);
162+
try
163+
{
164+
await DevicesApi.SetSmartMeDeviceConfigurationAsync(credentials, configuration);
165+
}
166+
catch(Exception e)
167+
{
168+
Console.WriteLine($"Error: {e.Message}");
169+
}
163170

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

166173
Thread.Sleep(5000);
167174

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

170177
configuration.SwitchConfiguration[0].CanSwitchOff = true;
171178

172-
await DevicesApi.SetSmartMeDeviceConfigurationAsync(credentials, configuration);
179+
try
180+
{
181+
await DevicesApi.SetSmartMeDeviceConfigurationAsync(credentials, configuration);
182+
}
183+
catch (Exception e)
184+
{
185+
Console.WriteLine($"Error: {e.Message}");
186+
}
173187

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

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

206220
foreach (CustomDevice customDevice in customDevices)
207221
{
208-
Console.WriteLine($"Id: {customDevice.Id}, Name: {customDevice.Name}");
222+
if (customDevice != null)
223+
{
224+
Console.WriteLine($"Id: {customDevice.Id}, Name: {customDevice.Name}");
225+
}
226+
}
227+
}
228+
229+
// Update Custom Device
230+
{
231+
Helpers.WriteConsoleTitle("Update a custom device");
232+
233+
List<CustomDevice> customDevices = await DevicesApi.GetCustomDevicesAsync(credentials);
234+
235+
foreach (CustomDevice customDevice in customDevices)
236+
{
237+
if (customDevice != null)
238+
{
239+
customDevice.Name = "UpdatedCustomDeviceTest";
240+
await DevicesApi.UpdateCustomDeviceAsync(credentials, customDevice);
241+
break;
242+
}
209243
}
210244
}
211245

0 commit comments

Comments
 (0)