-
Notifications
You must be signed in to change notification settings - Fork 10
/
Startup.cs
172 lines (144 loc) · 6.14 KB
/
Startup.cs
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
namespace Opc.Ua.Cloud.Publisher
{
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading.Tasks;
using Opc.Ua.Cloud.Publisher.Configuration;
using Opc.Ua.Cloud.Publisher.Interfaces;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(5);
});
services.AddControllersWithViews();
services.AddSignalR();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddLogging(logging =>
{
logging.AddFile("./logs/UACloudPublisher.log");
});
// add our singletons
services.AddSingleton<IUAApplication, UAApplication>();
services.AddSingleton<IUAClient, UAClient>();
services.AddSingleton<KafkaClient>();
services.AddSingleton<MQTTClient>();
services.AddSingleton<Settings.BrokerResolver>(serviceProvider => key =>
{
switch (key)
{
case "MQTT":
return serviceProvider.GetService<MQTTClient>();
case "Kafka":
return serviceProvider.GetService<KafkaClient>();
default:
return null;
}
});
services.AddSingleton<IPublishedNodesFileHandler, PublishedNodesFileHandler>();
services.AddSingleton<ICommandProcessor, CommandProcessor>();
// add our message processing engine
services.AddSingleton<IMessageProcessor, MessageProcessor>();
services.AddSingleton<IMessageSource, MonitoredItemNotification>();
services.AddSingleton<IMessageEncoder, PubSubTelemetryEncoder>();
services.AddSingleton<IMessagePublisher, StoreForwardPublisher>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,
IWebHostEnvironment env,
ILoggerFactory loggerFactory,
IUAApplication uaApp,
IMessageProcessor engine,
Settings.BrokerResolver brokerResolver,
IPublishedNodesFileHandler publishedNodesFileHandler)
{
ILogger logger = loggerFactory.CreateLogger("Statup");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Browser/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSession();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Diag}/{action=Index}/{id?}");
endpoints.MapHub<StatusHub>("/statushub");
endpoints.MapBlazorHub();
});
// do all further initialization on a background thread to load the webserver independently
_ = Task.Run(() =>
{
// kick off the task to show periodic diagnostic info
_ = Task.Run(() => Diagnostics.Singleton.RunAsync());
// create our app
uaApp.CreateAsync().GetAwaiter().GetResult();
IBrokerClient broker;
IBrokerClient altBroker;
if (Settings.Instance.UseKafka)
{
broker = brokerResolver("Kafka");
}
else
{
broker = brokerResolver("MQTT");
}
// connect to broker
broker.Connect();
// check if we need a second broker
if (Settings.Instance.UseAltBrokerForReceivingUAOverMQTT)
{
altBroker = brokerResolver("MQTT");
altBroker.Connect(true);
}
// run the telemetry engine
_ = Task.Run(() => engine.Run());
// load our persistency file
if (Settings.Instance.AutoLoadPersistedNodes)
{
try
{
byte[] persistencyFile = File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "settings", "persistency.json"));
if (persistencyFile == null)
{
// no file persisted yet
throw new Exception("Persistency file not found.");
}
else
{
_ = Task.Run(() => publishedNodesFileHandler.ParseFile(persistencyFile));
}
}
catch (Exception ex)
{
logger.LogError(ex.Message);
}
}
});
}
}
}