-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathAasxFileServerInterfaceService.cs
290 lines (259 loc) · 11.6 KB
/
AasxFileServerInterfaceService.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
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
284
285
286
287
288
289
290
using AasxRestServerLibrary;
using AasxServer;
using AasxServerStandardBib.Exceptions;
using AasxServerStandardBib.Interfaces;
using AasxServerStandardBib.Logging;
using AdminShellNS;
using AdminShellNS.Models;
using Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace AasxServerStandardBib.Services
{
public class AasxFileServerInterfaceService : IAasxFileServerInterfaceService
{
private readonly IAppLogger<AasxFileServerInterfaceService> _logger;
private AdminShellPackageEnv[] _packages;
private string[] _envFileNames;
public AasxFileServerInterfaceService(IAppLogger<AasxFileServerInterfaceService> logger)
{
_logger = logger;
_packages = Program.env;
_envFileNames = Program.envFileName;
}
public void DeleteAASXByPackageId(string packageId)
{
int packageIndex = int.Parse(packageId);
var requestedPackage = _packages[packageIndex];
if (requestedPackage != null)
{
//Move the file to archive
var archivePath = Path.Combine(AasxHttpContextHelper.DataPath, "AasxFileArchive");
if (!Directory.Exists(archivePath))
{
Directory.CreateDirectory(archivePath);
}
var fileName = Path.Combine(archivePath, Path.GetFileName(_envFileNames[packageIndex]));
if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(fileName);
}
System.IO.File.Move(_envFileNames[packageIndex], fileName);
_packages[packageIndex] = null;
_envFileNames[packageIndex] = null;
//_packages[packageIndex].setWrite(true);
Program.signalNewData(2);
}
else
{
throw new NotFoundException($"Package with packageId {packageId} not found.");
}
}
public string? GetAASXByPackageId(string packageId, out byte[] content, out long fileSize, out IAssetAdministrationShell aas)
{
aas = null;
content = null;
fileSize = 0;
int packageIndex = int.Parse(packageId);
var requestedPackage = _packages[packageIndex];
var requestedFileName = _envFileNames[packageIndex];
if (!string.IsNullOrEmpty(requestedFileName) && requestedPackage != null)
{
//Create Temp file
string copyFileName = Path.GetTempFileName().Replace(".tmp", ".aasx");
System.IO.File.Copy(requestedFileName, copyFileName, true);
Program.env[packageIndex].SaveAs(copyFileName);
content = System.IO.File.ReadAllBytes(copyFileName);
string? fileName = Path.GetFileName(requestedFileName);
fileSize = content.Length;
_packages[packageIndex].SetFilename(requestedFileName);
//Set aas
aas = requestedPackage.AasEnv.AssetAdministrationShells[0];
//Delete Temp file
System.IO.File.Delete(copyFileName);
return fileName;
}
if (requestedPackage != null && string.IsNullOrEmpty(requestedFileName))
{
//File does not exist, may be AAS is added by REST-API
//Check if AAS exists
if (requestedPackage.AasEnv.AssetAdministrationShells.Count != 0)
{
string newFileName = Path.Combine(AasxHttpContextHelper.DataPath, requestedPackage.AasEnv.AssetAdministrationShells[0].IdShort + ".aasx");
using (new FileStream(newFileName, FileMode.CreateNew)) { }
Program.env[packageIndex].SetTempFn(newFileName);
//Create Temp file
string copyFileName = Path.GetTempFileName().Replace(".tmp", ".aasx");
System.IO.File.Copy(newFileName, copyFileName, true);
Program.env[packageIndex].SaveAs(copyFileName);
content = System.IO.File.ReadAllBytes(copyFileName);
string? fileName = Path.GetFileName(newFileName);
fileSize = content.Length;
System.IO.File.Copy(copyFileName, newFileName, true);
Program.envFileName[packageIndex] = newFileName;
_packages[packageIndex].SetFilename(newFileName);
//Set aas
aas = requestedPackage.AasEnv.AssetAdministrationShells[0];
//Delete Temp file
System.IO.File.Delete(copyFileName);
return fileName;
}
}
return null;
}
public List<PackageDescription> GetAllAASXPackageIds(string? aasId = null)
{
var output = new List<PackageDescription>();
for (var i = 0; i < _packages.Length; i++)
{
var package = _packages[i];
if (package != null)
{
var packageDescription = new PackageDescription {PackageId = i.ToString()};
var aasIdList = _packages[i].AasEnv?.AssetAdministrationShells?.Select(aas => aas.Id).ToList();
packageDescription.AasIds = aasIdList;
output.Add(packageDescription);
}
}
//Filter w..r.t aasId
if (output.Count != 0 && !string.IsNullOrEmpty(aasId))
{
output = output.Where(x => x.AasIds.Contains(aasId)).ToList();
}
return output;
}
public IAssetAdministrationShell GetAssetAdministrationShellByPackageId(string packageId)
{
int packageIndex = int.Parse(packageId);
var requestedPackage = _packages[packageIndex];
if (requestedPackage != null && requestedPackage.AasEnv != null)
{
if (requestedPackage.AasEnv.AssetAdministrationShells != null)
{
return requestedPackage.AasEnv.AssetAdministrationShells.First();
}
}
return null;
}
public string PostAASXPackage(byte[] fileContent, string fileName)
{
var newFileName = Path.Combine(AasxHttpContextHelper.DataPath, fileName);
//Check if file already exists
if (System.IO.File.Exists(newFileName))
{
throw new Exception($"File already exists");
}
//TODO:Check file extentsion ".aasx"
//Write the received file content to this temp file
//var content = Convert.FromBase64String(body);
System.IO.File.WriteAllBytes(newFileName, fileContent);
// open again
var newAasx = new AdminShellPackageEnv(newFileName, true);
if (newAasx != null)
{
if (EmptyPackageAvailable(out int emptyPackageIndex))
{
_packages[emptyPackageIndex] = newAasx;
_envFileNames[emptyPackageIndex] = newFileName;
var timeStamp = DateTime.UtcNow;
newAasx.AasEnv.AssetAdministrationShells[0].TimeStampCreate = timeStamp;
newAasx.AasEnv.AssetAdministrationShells[0].SetTimeStamp(timeStamp);
foreach (var submodel in newAasx.AasEnv.Submodels)
{
//submodel.SetAllParents();
submodel.TimeStampCreate = timeStamp;
submodel.SetParentAndTimestamp(timeStamp);
}
// newAasx.setWrite(true); this api is currently not connected to the database
Program.signalNewData(2);
return emptyPackageIndex.ToString();
}
else
{
throw new Exception($"Could not create the file as the datastructure is completely filled.");
}
}
else
{
throw new Exception($"Cannot load new package {fileName}.");
}
}
public void UpdateAASXPackageById(string packageId, byte[] fileContent, string fileName)
{
int packageIndex = int.Parse(packageId);
var package = _packages[packageIndex];
if (package != null)
{
var originalFile = _packages[packageIndex].Filename;
//Create temporary file
var tempNewFile = Path.GetTempFileName().Replace(".tmp", ".aasx");
//Write the received file content to this temp file
//var content = Convert.FromBase64String(fileContent);
System.IO.File.WriteAllBytes(tempNewFile, fileContent);
lock (Program.changeAasxFile)
{
try
{
_packages[packageIndex].Close();
//Create back up of existing file
System.IO.File.Copy(originalFile, originalFile + ".bak", overwrite: true);
}
catch (Exception e)
{
throw new Exception($"Cannot close/ backup old AASX {originalFile}. Aborting. Exception: {e.Message}");
}
try
{
//Replace existing file with temp file
//originalFile = fileName;
var rootPath = Path.GetDirectoryName(originalFile);
originalFile = Path.Combine(rootPath, fileName);
//Copy tempFile into originalFile location
System.IO.File.Copy(tempNewFile, originalFile, overwrite: true);
// open again
var newAasx = new AdminShellPackageEnv(originalFile, true);
if (newAasx != null)
{
foreach (var submodel in newAasx.AasEnv.Submodels)
{
submodel.SetAllParents();
}
_packages[packageIndex] = newAasx;
}
else
{
throw new Exception($"Cannot load new package {originalFile} for replacing via PUT. Aborting.");
}
//now delete tempFile
System.IO.File.Delete(tempNewFile);
}
catch (Exception e)
{
throw new Exception($"Cannot replace AASX {originalFile} with new {tempNewFile}. Aborting. Exception: {e.Message}");
}
}
Program.signalNewData(2);
}
else
{
throw new NotFoundException($"Requested package with packageId {packageId} not found.");
}
}
private bool EmptyPackageAvailable(out int emptyPackageIndex)
{
emptyPackageIndex = -1;
for (int envi = 0; envi < _packages.Length; envi++)
{
if (_packages[envi] == null)
{
emptyPackageIndex = envi;
_packages[emptyPackageIndex] = new AdminShellPackageEnv();
return true;
}
}
return false;
}
}
}