-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
283 lines (215 loc) · 12.6 KB
/
Program.cs
File metadata and controls
283 lines (215 loc) · 12.6 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using AxOpen.Security;
using AxOpen.Security.Entities;
using AxOpen.Security.Services;
using AXOpen;
using AXOpen.Base.Data;
using AXOpen.Data;
using AXOpen.Data.Json;
using AXOpen.Data.MongoDb;
using AXOpen.Logging;
using AXSharp.Connector;
using AXSharp.Presentation.Blazor.Services;
using librarytemplate;
using Serilog;
using System.Data;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
//var jsonRepositoryLocation = CreateJsonRepositoryDirectory();
//builder.Services.ConfigureAxBlazorSecurity(SetUpJsonSecurityRepository(jsonRepositoryLocation), Roles.CreateRoles(), true);
builder.Services.ConfigureAxBlazorSecurity(SetUpMongoSecurityRepository(), Roles.CreateRoles(), true);
builder.Services.AddLocalization();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddIxBlazorServices();
builder.Services.AddAxoCoreServices();
Entry.Plc.Connector.SubscriptionMode = ReadSubscriptionMode.Polling;
Entry.Plc.Connector.BuildAndStart().ReadWriteCycleDelay = 250;
Entry.Plc.Connector.ExceptionBehaviour = CommExceptionBehaviour.ReThrow;
Entry.Plc.Connector.SetLoggerConfiguration(new LoggerConfiguration()
.WriteTo
.Console()
.WriteTo
.File($"connector.log",
outputTemplate: "{Timestamp:yyyy-MMM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}",
fileSizeLimitBytes: 100000)
.MinimumLevel.Debug()
.CreateLogger());
await Entry.Plc.Connector.IdentityProvider.ConstructIdentitiesAsync();
AxoApplication.CreateBuilder().ConfigureLogger(new SerilogLogger(new LoggerConfiguration()
.WriteTo.Console().MinimumLevel.Verbose()
.CreateLogger()));
//<SetUpAxoDataPersistentExchange>
IRepository<AXOpen.Data.PersistentRecord> persistentRepository;
// *** IN MEMORY REPOSITORY ***
//persistentRepository = new InMemoryRepositorySettings<AXOpen.Data.PersistentRecord>().Factory();
//***JSON REPOSITORY***
//var persistentLocation = Path.Combine(jsonRepositoryLocation, "PersistentData");
//persistentRepository = new JsonRepositorySettings<AXOpen.Data.PersistentRecord>(persistentLocation).Factory();
// *** MONGO REPOSITORY ***
persistentRepository = AXOpen.Data.MongoDb.Repository.Factory<AXOpen.Data.PersistentRecord>(new MongoDbRepositorySettings<AXOpen.Data.PersistentRecord>("mongodb://localhost:27017", "AxOpenData", "PersistentData"));
Entry.Plc.AxoDataPersistentContext.DataManager.InitializeRemoteDataExchange(
Entry.Plc.AxoDataPersistentContext.PersistentRootObject,
persistentRepository
);
//</SetUpAxoDataPersistentExchange>
//<SetUpAxoDataFragmentExchange>
IRepository<Pocos.AxoDataFramentsExchangeExample.SharedDataHeaderData> SharedDataHeaderDataRepository;
IRepository<Pocos.AxoDataFramentsExchangeExample.Station_1_Data> Station_1_DataRepository;
// *** IN MEMORY REPOSITORY ***
//SharedDataHeaderDataRepository = new InMemoryRepositorySettings<Pocos.AxoDataFramentsExchangeExample.SharedDataHeaderData>().Factory();
//Station_1_DataRepository = new InMemoryRepositorySettings<Pocos.AxoDataFramentsExchangeExample.Station_1_Data>().Factory();
// *** JSON REPOSITORY ***
//var SharedHeaderLocation = Path.Combine(jsonRepositoryLocation, "SharedDataHeader");
//var Station_1_Location = Path.Combine(jsonRepositoryLocation, "Station_1");
//SharedDataHeaderDataRepository = new JsonRepositorySettings<Pocos.AxoDataFramentsExchangeExample.SharedDataHeaderData>(SharedHeaderLocation).Factory();
//Station_1_DataRepository = new JsonRepositorySettings<Pocos.AxoDataFramentsExchangeExample.Station_1_Data>(Station_1_Location).Factory();
// *** MONGO REPOSITORY ***
SharedDataHeaderDataRepository = AXOpen.Data.MongoDb.Repository.Factory<Pocos.AxoDataFramentsExchangeExample.SharedDataHeaderData>(new MongoDbRepositorySettings<Pocos.AxoDataFramentsExchangeExample.SharedDataHeaderData>("mongodb://localhost:27017", "AxOpenData", "SharedDataHeader"));
Station_1_DataRepository = AXOpen.Data.MongoDb.Repository.Factory<Pocos.AxoDataFramentsExchangeExample.Station_1_Data>(new MongoDbRepositorySettings<Pocos.AxoDataFramentsExchangeExample.Station_1_Data>("mongodb://localhost:27017", "AxOpenData", "Station_1"));
var AxoProcessDataManager = Entry.Plc.AxoDataFragmentsExchangeContext.DataManager.CreateDataFragments<AxoDataFramentsExchangeExample.AxoProcessDataManager>();
AxoProcessDataManager.SharedHeader.SetRepository(SharedDataHeaderDataRepository);
AxoProcessDataManager.Station_1.SetRepository(Station_1_DataRepository);
AxoProcessDataManager.InitializeRemoteDataExchange();
//</SetUpAxoDataFragmentExchange>
//<SetUpAxoDataExchange>
IRepository<Pocos.AxoDataExchangeExample.AxoProcessData> AxoProcessDataRepository;
// *** IN MEMORY REPOSITORY ***
//AxoProcessDataRepository = new InMemoryRepositorySettings<Pocos.AxoDataExchangeExample.AxoProcessData>().Factory();
//Entry.Plc.AxoDataExchangeContext.DataManager.InitializeRemoteDataExchange(AxoProcessDataRepository);
// *** JSON REPOSITORY ***
//var ProcessDataLocation = Path.Combine(jsonRepositoryLocation, "ProcessData");
//AxoProcessDataRepository = new JsonRepositorySettings<Pocos.AxoDataExchangeExample.AxoProcessData>(ProcessDataLocation).Factory();
// *** MONGO REPOSITORY ***
AxoProcessDataRepository = AXOpen.Data.MongoDb.Repository.Factory<Pocos.AxoDataExchangeExample.AxoProcessData>(new MongoDbRepositorySettings<Pocos.AxoDataExchangeExample.AxoProcessData>("mongodb://localhost:27017", "AxOpenData", "AxoDataExchangeExample"));
Entry.Plc.AxoDataExchangeContext.DataManager.InitializeRemoteDataExchange(AxoProcessDataRepository);
//</SetUpAxoDataExchange>
//<CleanUp>
// Clean Temp directory
AXOpen.Data.IAxoDataExchange.CleanUp();
//</CleanUp>
IRepository<Pocos.AxoDataDistributedExample.SharedHeader_Data> distributedHeaderRepository;
IRepository<Pocos.AxoDataDistributedExample.Station_Data> distributedStationRepository;
distributedHeaderRepository = AXOpen.Data.MongoDb.Repository.Factory<Pocos.AxoDataDistributedExample.SharedHeader_Data>(new MongoDbRepositorySettings<Pocos.AxoDataDistributedExample.SharedHeader_Data>("mongodb://localhost:27017", "AxOpenData", "DistributedHeader"));
distributedStationRepository = AXOpen.Data.MongoDb.Repository.Factory<Pocos.AxoDataDistributedExample.Station_Data>(new MongoDbRepositorySettings<Pocos.AxoDataDistributedExample.Station_Data>("mongodb://localhost:27017", "AxOpenData", "DistributedStation"));
Entry.Plc.AxoDataDistributedContext.ControlledUnit_1.EntityHeader.SetRepository(distributedHeaderRepository);
Entry.Plc.AxoDataDistributedContext.ControlledUnit_2.EntityHeader.SetRepository(distributedHeaderRepository);
Entry.Plc.AxoDataDistributedContext.ControlledUnit_1.ProcessData.SetRepository(distributedStationRepository);
Entry.Plc.AxoDataDistributedContext.ControlledUnit_2.ProcessData.SetRepository(distributedStationRepository);
Entry.Plc.AxoDataDistributedContext.ControlledUnit_1.EntityHeader.InitializeRemoteDataExchange();
Entry.Plc.AxoDataDistributedContext.ControlledUnit_2.EntityHeader.InitializeRemoteDataExchange();
Entry.Plc.AxoDataDistributedContext.ControlledUnit_1.ProcessData. InitializeRemoteDataExchange();
Entry.Plc.AxoDataDistributedContext.ControlledUnit_2.ProcessData.InitializeRemoteDataExchange();
//<DistributedDataServices>
// DistributedDataExchangeService - will handle all instances of IAxoDataExchanges
var distributedDataService = new DistributedDataExchangeService();
builder.Services.AddSingleton<IDistributedDataExchangeService>(distributedDataService);
// AxoDataExchangeConfigurationService - handle configuraion for any IAxoDataExchange
var exchangeConfigurationService = new AxoDataExchangeConfigurationService();
builder.Services.AddSingleton<IAxoDataExchangeConfigurationService>(exchangeConfigurationService);
//</DistributedDataServices>
//<CollectingExchanges>
// You can manually add AxoDataExchanges to the service:
// distributedDataService.Add(Entry.Plc.AxoDataDistributedContext.ControlledUnit_1.ProcessData, new() { "default" });
// distributedDataService.Add(Entry.Plc.AxoDataDistributedContext.ControlledUnit_1.EntityHeader, new() { "default" });
// ...
// Or collect them automatically using reflection:
distributedDataService.CollectAxoDataExchanges(Entry.Plc.AxoDataDistributedContext.ControlledUnit_1);
distributedDataService.CollectAxoDataExchanges(Entry.Plc.AxoDataDistributedContext.ControlledUnit_2);
//</CollectingExchanges>
//<AxoDataExchangeConfigurationService>
exchangeConfigurationService.AddConfiguration<Pocos.AxoDataDistributedExample.SharedHeader_Data>(
suffix: "",
configAction: a =>
{
a.AddColumn("Global Result", x => x.HeaderGlogalPass, true, typeof(CustomBoolTemplate))
.AddColumn("Index", x => x.HeaderIndex, false, typeof(CustomIntTemplate))
.EnableSorting()
.AddSorting(x => x.HeaderPartialName);
});
exchangeConfigurationService.AddConfiguration<Pocos.AxoDataDistributedExample.Station_Data>(
suffix: "",
configAction: a =>
{
a.AddColumn("Station Result", x => x.StationPass, true, typeof(CustomBoolTemplate))
.AddColumn("Name", x => x.StationName, true, null)
.AddColumn("Operation", x => x.StaionOperation, true, null)
.EnableSorting();
});
//</AxoDataExchangeConfigurationService>
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/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.UseRouting();
var supportedCultures = new[] { "en-US", "sk-SK", "es-ES" };
var localizationOptions = new RequestLocalizationOptions()
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);
app.UseAuthorization();
app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();
static string CreateJsonRepositoryDirectory(string path = "..\\..\\..\\..\\..\\JSONREPOS\\")
{
var executingAssemblyFile = new FileInfo(Assembly.GetExecutingAssembly().Location);
var repositoryDirectory = Path.GetFullPath($"{executingAssemblyFile.Directory}{path}");
if (!Directory.Exists(repositoryDirectory))
{
Directory.CreateDirectory(repositoryDirectory);
}
return repositoryDirectory;
}
static (IRepository<User>, IRepository<Group>) SetUpJsonSecurityRepository(string repositoryDirectory)
{
IRepository<User> userRepo = new JsonRepository<User>(new JsonRepositorySettings<User>(Path.Combine(repositoryDirectory, "Users")));
IRepository<Group> groupRepo = new JsonRepository<Group>(new JsonRepositorySettings<Group>(Path.Combine(repositoryDirectory, "Groups")));
return (userRepo, groupRepo);
}
static (IRepository<User>, IRepository<Group>) SetUpMongoSecurityRepository(string databaseName = "AxOpenData")
{
var MongoDatabaseName = databaseName;
var MongoConnectionString = "mongodb://localhost:27017";
IRepository<User> userRepo = AXOpen.Data.MongoDb.Repository.Factory<User>(new MongoDbRepositorySettings<User>(MongoConnectionString, MongoDatabaseName, "Users", idExpression: t => t.Id));
IRepository<Group> groupRepo = AXOpen.Data.MongoDb.Repository.Factory<Group>(new MongoDbRepositorySettings<Group>(MongoConnectionString, MongoDatabaseName, "Groups"));
return (userRepo, groupRepo);
}
public static class Roles
{
//<CollectAllDataExchangeRoles>
public static List<Role> CreateRoles()
{
var roles = new List<Role> // your custom application roles
{
new Role(process_settings_access),
new Role(process_traceability_access),
new Role(can_run_ground_mode),
new Role(can_run_automat_mode),
new Role(can_run_service_mode),
new Role(can_skip_steps_in_sequence),
};
//add all roles from AXOpen.Data
foreach (var item in typeof(AXOpen.Data.DataExchangeRoleNames).
GetFields(BindingFlags.Public | BindingFlags.Static).
Where(f => f.FieldType == typeof(string)))
{
roles.Add(new Role(item.Name));
}
return roles;
}
//</CollectAllDataExchangeRoles>
public const string can_run_ground_mode = nameof(can_run_ground_mode);
public const string can_run_automat_mode = nameof(can_run_automat_mode);
public const string can_run_service_mode = nameof(can_run_service_mode);
public const string process_settings_access = nameof(process_settings_access);
public const string process_traceability_access = nameof(process_traceability_access);
public const string can_skip_steps_in_sequence = nameof(can_skip_steps_in_sequence);
}