-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
125 lines (103 loc) · 5.67 KB
/
Program.cs
File metadata and controls
125 lines (103 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Kepware.Api;
using Kepware.Api.Model;
namespace Kepware.Api.Sample
{
internal static class Program
{
static async Task Main(string[] args)
{
// 1. Create a host builder
using var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
// Add application services
services.AddKepwareApiClient(
name: "sample",
baseUrl: "https://localhost:57512",
apiUserName: "Administrator",
apiPassword: "ReallyStrongPassword400!",
disableCertificateValidation: true
);
})
.ConfigureLogging(logging =>
{
// Configure logging to use the console
logging.ClearProviders();
logging.AddConsole();
logging.SetMinimumLevel(LogLevel.Information);
logging.AddFilter("Microsoft", LogLevel.Warning);
logging.AddFilter("System", LogLevel.Warning);
})
.Build();
// 2. Get the KepwareApiClient
var api = host.Services.GetRequiredService<KepwareApiClient>();
if (await api.TestConnectionAsync())
{
//connection is established
var channel1 = await api.Project.Channels.GetOrCreateChannelAsync("Channel by Api", "Simulator");
var device = await api.Project.Devices.GetOrCreateDeviceAsync(channel1, "Device by Api");
device.Description = "Test";
await api.Project.Devices.UpdateDeviceAsync(device);
device.Tags = new DeviceTagCollection([
new Tag { Name = "RampByApi", TagAddress = "RAMP (120, 35, 100, 4)", Description ="A ramp created by the C# Api Client" },
new Tag { Name = "SineByApi", TagAddress = "SINE (10, -40.000000, 40.000000, 0.050000, 0)" },
new Tag { Name = "BooleanByApi", TagAddress = "B0001" },
]);
await api.Project.Devices.UpdateDeviceAsync(device, true);
// --- IoT Gateway: MQTT Client Agent ---
var mqttAgent = await api.Project.IotGateway.GetOrCreateMqttClientAgentAsync("MQTT Agent by Api");
mqttAgent.Url = "tcp://broker.example.com:1883";
mqttAgent.Topic = "kepware/data";
await api.Project.IotGateway.UpdateMqttClientAgentAsync(mqttAgent);
// Add an IoT Item referencing a tag on the device created above
var mqttItem = await api.Project.IotGateway.GetOrCreateIotItemAsync(
$"{channel1.Name}.{device.Name}.BooleanByApi", mqttAgent);
mqttItem.ScanRateMs = 500;
await api.Project.IotGateway.UpdateIotItemAsync(mqttItem);
// Clean up MQTT IoT Item and agent
await api.Project.IotGateway.DeleteIotItemAsync(mqttItem);
await api.Project.IotGateway.DeleteMqttClientAgentAsync(mqttAgent);
// --- IoT Gateway: REST Client Agent ---
var restClientAgent = await api.Project.IotGateway.GetOrCreateRestClientAgentAsync("REST Client by Api");
restClientAgent.Url = "https://api.example.com/data";
restClientAgent.HttpMethod = RestClientHttpMethod.Post;
await api.Project.IotGateway.UpdateRestClientAgentAsync(restClientAgent);
// Add an IoT Item to the REST Client agent
var restClientItem = await api.Project.IotGateway.GetOrCreateIotItemAsync(
$"{channel1.Name}.{device.Name}.SineByApi", restClientAgent);
await api.Project.IotGateway.DeleteIotItemAsync(restClientItem);
await api.Project.IotGateway.DeleteRestClientAgentAsync(restClientAgent);
// --- IoT Gateway: REST Server Agent ---
var restServerAgent = await api.Project.IotGateway.GetOrCreateRestServerAgentAsync("REST Server by Api");
restServerAgent.PortNumber = 39321;
restServerAgent.EnableWriteEndpoint = true;
await api.Project.IotGateway.UpdateRestServerAgentAsync(restServerAgent);
// Add an IoT Item to the REST Server agent
var restServerItem = await api.Project.IotGateway.GetOrCreateIotItemAsync(
$"{channel1.Name}.{device.Name}.RampByApi", restServerAgent);
await api.Project.IotGateway.DeleteIotItemAsync(restServerItem);
await api.Project.IotGateway.DeleteRestServerAgentAsync(restServerAgent);
// Clean up channel and device
await api.Project.Devices.DeleteDeviceAsync(device);
await api.Project.Channels.DeleteChannelAsync(channel1);
var reinitJob = await api.ApiServices.ReinitializeRuntimeAsync();
var result = await reinitJob.AwaitCompletionAsync();
if (result)
{
Console.WriteLine("ReinitializeRuntimeAsync completed successfully.");
}
else
{
Console.WriteLine("ReinitializeRuntimeAsync failed.");
}
}
Console.WriteLine();
Console.WriteLine("-------------------");
Console.WriteLine("Press <Enter> to exit...");
Console.ReadLine();
}
}
}