@dnv-plant/typescriptpws 1.0.27 → 1.0.38-alpha.1846913

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,432 @@
1
+ /***********************************************************************
2
+ * This file has been auto-generated by a code generation tool.
3
+ * Version: 1.0.38
4
+ * Date/time: 24 Mar 2025 16:46:20
5
+ * Template: templates/typescriptpws/calculations.razor.
6
+ ***********************************************************************/
7
+
8
+
9
+
10
+ import * as Enums from "../enums";
11
+ import * as Entities from "../entities";
12
+ import * as EntitySchemas from "../entity-schemas";
13
+ import { getAnalyticsApiTarget, getClientAliasId, postRequest } from "../utilities";
14
+
15
+ import Joi from "joi";
16
+ import { AxiosResponse } from "axios";
17
+
18
+
19
+ class CalculationRequestBase {
20
+ /**
21
+ * Calculation request base class.
22
+ */
23
+ constructor() {
24
+ // Base class initialization code
25
+ }
26
+ }
27
+
28
+ class CalculationBase {
29
+ resultCode?: Enums.ResultCode = Enums.ResultCode.SUCCESS;
30
+ messages: string[] = [];
31
+ calculationElapsedTime?: number = 0.0;
32
+ operationId?: string = "";
33
+
34
+ /**
35
+ * Post JSON to URL and time the call
36
+ */
37
+ async postRequest(url: string, data: string): Promise<AxiosResponse> {
38
+ return postRequest(url, data);
39
+ }
40
+
41
+ /**
42
+ * Utility method to print the messages returned by the calculation.
43
+ */
44
+ printMessages(): void {
45
+ if (this.messages && this.messages.length > 0) {
46
+ this.messages.forEach((message) => console.log(message));
47
+ } else {
48
+ console.log("No messages");
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Utility method to handle a failed response.
54
+ */
55
+ handleFailedResponse(response: AxiosResponse): void {
56
+ try {
57
+ const validatedFailedResponse = new CalculationFailedResponseSchema().validate(response.data);
58
+
59
+ this.resultCode = validatedFailedResponse.resultCode;
60
+ this.messages.push(...(validatedFailedResponse.messages ?? []));
61
+ this.calculationElapsedTime = validatedFailedResponse.calculationElapsedTime;
62
+ this.operationId = validatedFailedResponse.operationId;
63
+ } catch (error) {
64
+ if (error instanceof Error) {
65
+ this.messages.push(`Failed to parse response: ${error.message}`);
66
+ } else {
67
+ this.messages.push("An unknown error occurred during response parsing.");
68
+ }
69
+ console.error("Failed to parse response:", error);
70
+ } finally {
71
+ this.messages.push(`${response.statusText} (Status code: ${response.status})`);
72
+ }
73
+ }
74
+ }
75
+
76
+ class CalculationResponseBase {
77
+ resultCode?: Enums.ResultCode;
78
+ messages?: string[];
79
+ calculationElapsedTime?: number;
80
+ operationId?: string;
81
+
82
+ /**
83
+ * Calculation response base class.
84
+ */
85
+ constructor(
86
+ resultCode?: Enums.ResultCode,
87
+ messages?: string[],
88
+ calculationElapsedTime?: number,
89
+ operationId?: string
90
+ ) {
91
+ this.resultCode = resultCode;
92
+ this.messages = messages;
93
+ this.calculationElapsedTime = calculationElapsedTime;
94
+ this.operationId = operationId;
95
+ }
96
+ }
97
+
98
+ class CalculationFailedResponse extends CalculationResponseBase {
99
+ /**
100
+ * Calculation failed response class.
101
+ */
102
+ constructor(
103
+ resultCode?: Enums.ResultCode,
104
+ messages: string[] = [],
105
+ calculationElapsedTime: number = 0,
106
+ operationId: string = ""
107
+ ) {
108
+ super(resultCode, messages, calculationElapsedTime, operationId);
109
+ }
110
+ }
111
+
112
+ class CalculationFailedResponseSchema {
113
+ schema: Joi.ObjectSchema;
114
+
115
+ /**
116
+ * Calculation failed response schema.
117
+ */
118
+ constructor() {
119
+ this.schema = Joi.object({
120
+ resultCode: Joi.string()
121
+ .valid(...Object.values(Enums.ResultCode))
122
+ .required(),
123
+ messages: Joi.array().items(Joi.string()).required(),
124
+ calculationElapsedTime: Joi.number().required(),
125
+ operationId: Joi.string().required(),
126
+ }).unknown(true);
127
+ }
128
+
129
+ validate(data: {
130
+ resultCode: Enums.ResultCode;
131
+ messages: string[];
132
+ calculationElapsedTime: number;
133
+ operationId: string;
134
+ }) {
135
+ const { error, value } = this.schema.validate(data);
136
+ if (error) throw new Error(`Validation error: ${error.details.map((x) => x.message).join(", ")}`);
137
+ return this.makeCalculationFailedResponse(value);
138
+ }
139
+
140
+ makeCalculationFailedResponse(data: {
141
+ resultCode: Enums.ResultCode;
142
+ messages: string[];
143
+ calculationElapsedTime: number;
144
+ operationId: string;
145
+ }) {
146
+ return new CalculationFailedResponse(data.resultCode, data.messages, data.calculationElapsedTime, data.operationId);
147
+ }
148
+ }
149
+
150
+ export interface TankFireCalculationRequestSchemaData {
151
+ atmosphericStorageTank: Entities.AtmosphericStorageTank;
152
+ weather: Entities.Weather;
153
+ substrate: Entities.Substrate;
154
+ flammableParameters: Entities.FlammableParameters;
155
+ }
156
+
157
+ class TankFireCalculationRequest extends CalculationRequestBase {
158
+ atmosphericStorageTank: Entities.AtmosphericStorageTank;
159
+ weather: Entities.Weather;
160
+ substrate: Entities.Substrate;
161
+ flammableParameters: Entities.FlammableParameters;
162
+
163
+ /**
164
+ * TankFire calculation request class.
165
+ *
166
+ * @param {Entities.AtmosphericStorageTank} atmosphericStorageTank - An Atmospheric Storage Tank entity.
167
+ * @param {Entities.Weather} weather - A Weather entity.
168
+ * @param {Entities.Substrate} substrate - A Substrate entity.
169
+ * @param {Entities.FlammableParameters} flammableParameters - A Flammable Parameters entity.
170
+ */
171
+ constructor(
172
+ atmosphericStorageTank: Entities.AtmosphericStorageTank,
173
+ weather: Entities.Weather,
174
+ substrate: Entities.Substrate,
175
+ flammableParameters: Entities.FlammableParameters
176
+ ) {
177
+ super();
178
+ this.atmosphericStorageTank = atmosphericStorageTank;
179
+ this.weather = weather;
180
+ this.substrate = substrate;
181
+ this.flammableParameters = flammableParameters;
182
+ }
183
+ }
184
+
185
+ export class TankFireCalculationRequestSchema {
186
+ schema: Joi.ObjectSchema;
187
+ propertyTypes: Record<string, string>;
188
+
189
+ /**
190
+ * Schema for the TankFire calculation request.
191
+ */
192
+ constructor() {
193
+ this.schema = Joi.object({
194
+ atmosphericStorageTank: new EntitySchemas.AtmosphericStorageTankSchema().schema,
195
+ weather: new EntitySchemas.WeatherSchema().schema,
196
+ substrate: new EntitySchemas.SubstrateSchema().schema,
197
+ flammableParameters: new EntitySchemas.FlammableParametersSchema().schema,
198
+ }).unknown(true);
199
+
200
+ this.propertyTypes = {
201
+ atmosphericStorageTank: "Entities.AtmosphericStorageTank",
202
+ weather: "Entities.Weather",
203
+ substrate: "Entities.Substrate",
204
+ flammableParameters: "Entities.FlammableParameters",
205
+ };
206
+ }
207
+
208
+ validate(data: TankFireCalculationRequestSchemaData): TankFireCalculationRequest {
209
+ const { error, value } = this.schema.validate(data, { abortEarly: false });
210
+ if (error) {
211
+ throw new Error(`Validation error: ${error.details.map((x) => x.message).join(", ")}`);
212
+ }
213
+ return this.makeCalculationRequest(value);
214
+ }
215
+
216
+ makeCalculationRequest(data: TankFireCalculationRequestSchemaData): TankFireCalculationRequest {
217
+ return new TankFireCalculationRequest(
218
+ data.atmosphericStorageTank,
219
+ data.weather,
220
+ data.substrate,
221
+ data.flammableParameters
222
+ );
223
+ }
224
+ }
225
+
226
+ export class TankFireCalculation extends CalculationBase {
227
+ atmosphericStorageTank: Entities.AtmosphericStorageTank;
228
+ weather: Entities.Weather;
229
+ substrate: Entities.Substrate;
230
+ flammableParameters: Entities.FlammableParameters;
231
+ poolFireFlameResult?: Entities.PoolFireFlameResult;
232
+ flameRecords?: Entities.FlameRecord[];
233
+
234
+ /**
235
+ * Calculates the flame result and flame coordinates from a pool fire in an open roof tank. The calculations are driven from the atmospheric storage tank entity and are analogous to the standalone pool fire model.
236
+ *
237
+ * @param {Entities.AtmosphericStorageTank} atmosphericStorageTank - An Atmospheric Storage Tank entity.
238
+ * @param {Entities.Weather} weather - A Weather entity.
239
+ * @param {Entities.Substrate} substrate - A Substrate entity.
240
+ * @param {Entities.FlammableParameters} flammableParameters - A Flammable Parameters entity.
241
+ */
242
+ constructor(
243
+ atmosphericStorageTank: Entities.AtmosphericStorageTank,
244
+ weather: Entities.Weather,
245
+ substrate: Entities.Substrate,
246
+ flammableParameters: Entities.FlammableParameters
247
+ ) {
248
+ super();
249
+ this.atmosphericStorageTank = atmosphericStorageTank;
250
+ this.weather = weather;
251
+ this.substrate = substrate;
252
+ this.flammableParameters = flammableParameters;
253
+ }
254
+
255
+ async run() {
256
+ try {
257
+ const request = new TankFireCalculationRequest(
258
+ this.atmosphericStorageTank,
259
+ this.weather,
260
+ this.substrate,
261
+ this.flammableParameters
262
+ );
263
+
264
+ const schema = new TankFireCalculationRequestSchema();
265
+ const validatedRequest = schema.validate(request);
266
+
267
+ const requestJson = JSON.stringify(validatedRequest);
268
+ const url = `${getAnalyticsApiTarget()}calculatetankfire?clientId=${getClientAliasId()}`;
269
+
270
+ this.resultCode = Enums.ResultCode.UNEXPECTED_APPLICATION_ERROR;
271
+
272
+ const response = await this.postRequest(url, requestJson);
273
+
274
+ if (response.status >= 200 && response.status < 300) {
275
+ const schema = new TankFireCalculationResponseSchema();
276
+ const validatedResponse = schema.validate(response.data);
277
+
278
+ this.resultCode = validatedResponse.resultCode;
279
+ if (this.resultCode === Enums.ResultCode.SUCCESS) {
280
+ this.poolFireFlameResult = validatedResponse.poolFireFlameResult;
281
+ this.flameRecords = validatedResponse.flameRecords;
282
+ this.resultCode = validatedResponse.resultCode;
283
+ this.messages = validatedResponse.messages ?? [];
284
+ this.calculationElapsedTime = validatedResponse.calculationElapsedTime;
285
+ this.operationId = validatedResponse.operationId;
286
+ } else {
287
+ this.messages.push(...(validatedResponse.messages ?? []));
288
+ }
289
+ } else {
290
+ this.handleFailedResponse(response);
291
+ }
292
+ } catch (error) {
293
+ if (error instanceof Error) {
294
+ this.messages.push(`Error: ${error.message}`);
295
+ } else {
296
+ this.messages.push(`Unexpected error: ${JSON.stringify(error)}`);
297
+ }
298
+ console.error(error);
299
+ this.resultCode = Enums.ResultCode.UNEXPECTED_APPLICATION_ERROR;
300
+ }
301
+
302
+ return this.resultCode;
303
+ }
304
+
305
+ toString() {
306
+ const parts = ["* TankFire"];
307
+
308
+ parts.push(`poolFireFlameResult: ${String(this.poolFireFlameResult)}`);
309
+ parts.push("*** flameRecords:");
310
+ parts.push(
311
+ this.flameRecords && this.flameRecords.length > 0
312
+ ? this.flameRecords.map((point) => `flameRecordsElement: ${point}`).join("\n")
313
+ : "flameRecords does not contain any elements"
314
+ );
315
+ parts.push(`resultCode: ${String(this.resultCode)}`);
316
+ parts.push("*** messages:");
317
+ parts.push(`messages: ${this.messages !== undefined ? this.messages : "(None)"}`);
318
+ parts.push(`calculationElapsedTime: ${this.calculationElapsedTime !== undefined ? this.calculationElapsedTime : "(None)"}`);
319
+ parts.push(`operationId: ${this.operationId !== undefined ? this.operationId : "(None)"}`);
320
+
321
+ return parts.join("\n");
322
+ }
323
+ }
324
+
325
+ export class TankFireCalculationResponse extends CalculationResponseBase {
326
+ poolFireFlameResult: Entities.PoolFireFlameResult;
327
+ flameRecords: Entities.FlameRecord[];
328
+
329
+ /**
330
+ * TankFire calculation response class.
331
+ *
332
+ * @param {Entities.PoolFireFlameResult} poolFireFlameResult - Scalar flame results.
333
+ * @param {Entities.FlameRecord[]} flameRecords - An array of pool fire Flame Records.
334
+ */
335
+ constructor(
336
+ poolFireFlameResult: Entities.PoolFireFlameResult,
337
+ flameRecords: Entities.FlameRecord[],
338
+ resultCode: Enums.ResultCode,
339
+ messages: string[],
340
+ calculationElapsedTime: number,
341
+ operationId: string
342
+ ) {
343
+ super();
344
+ this.poolFireFlameResult = poolFireFlameResult;
345
+ this.flameRecords = flameRecords;
346
+ this.resultCode = resultCode;
347
+ this.messages = messages;
348
+ this.calculationElapsedTime = calculationElapsedTime;
349
+ this.operationId = operationId;
350
+ }
351
+
352
+ initialiseFromDictionary(data: { [key: string]: unknown }) {
353
+ if (data.poolFireFlameResult) {
354
+ this.poolFireFlameResult = new Entities.PoolFireFlameResult();
355
+ this.poolFireFlameResult.initialiseFromDictionary(data.poolFireFlameResult as { [key: string]: unknown });
356
+ }
357
+ if (data.flameRecords && Array.isArray(data.flameRecords)) {
358
+ this.flameRecords = data.flameRecords.map(
359
+ (item) => {
360
+ const record = new Entities.FlameRecord();
361
+ record.initialiseFromDictionary(item);
362
+ return record;
363
+ }
364
+ );
365
+ }
366
+ if (data.resultCode !== undefined && (typeof data.resultCode === "string" || typeof data.resultCode === "number")) {
367
+ this.resultCode = data.resultCode as Enums.ResultCode;
368
+ }
369
+ this.messages = this.messages ?? [];
370
+ if (data.messages && Array.isArray(data.messages)) {
371
+ this.messages.push(...data.messages);
372
+ }
373
+ if (data.calculationElapsedTime !== undefined && typeof data.calculationElapsedTime === "number") {
374
+ this.calculationElapsedTime = data.calculationElapsedTime as number;
375
+ }
376
+ if (data.operationId !== undefined && typeof data.operationId === "string") {
377
+ this.operationId = data.operationId as string;
378
+ }
379
+ }
380
+ }
381
+
382
+ export interface TankFireCalculationResponseSchemaData {
383
+ poolFireFlameResult: Entities.PoolFireFlameResult;
384
+ flameRecords: Entities.FlameRecord[];
385
+ resultCode: Enums.ResultCode;
386
+ messages: string[];
387
+ calculationElapsedTime: number;
388
+ operationId: string;
389
+ }
390
+
391
+ export class TankFireCalculationResponseSchema {
392
+ schema: Joi.ObjectSchema;
393
+ propertyTypes: Record<string, string>;
394
+
395
+ /**
396
+ * Schema for the TankFire calculation response.
397
+ */
398
+ constructor() {
399
+ this.schema = Joi.object({
400
+ poolFireFlameResult: new EntitySchemas.PoolFireFlameResultSchema().schema,
401
+ flameRecords: Joi.array().items(new EntitySchemas.FlameRecordSchema().schema).allow(null),
402
+ resultCode: Joi.string().valid(...Object.values(Enums.ResultCode)),
403
+ messages: Joi.array().items(Joi.string()),
404
+ calculationElapsedTime: Joi.number().unsafe(),
405
+ operationId: Joi.string().uuid().allow(null),
406
+ }).unknown(true);
407
+
408
+ this.propertyTypes = {
409
+ poolFireFlameResult: "Entities.PoolFireFlameResult",
410
+ flameRecords: "Entities.FlameRecord[]",
411
+ };
412
+ }
413
+
414
+ validate(data: TankFireCalculationResponseSchemaData): TankFireCalculationResponse {
415
+ const { error, value } = this.schema.validate(data, { abortEarly: false });
416
+ if (error) {
417
+ throw new Error(`Validation error: ${error.details.map((x) => x.message).join(", ")}`);
418
+ }
419
+ return this.makeCalculationResponse(value);
420
+ }
421
+
422
+ makeCalculationResponse(data: TankFireCalculationResponseSchemaData): TankFireCalculationResponse {
423
+ return new TankFireCalculationResponse(
424
+ data.poolFireFlameResult,
425
+ data.flameRecords,
426
+ data.resultCode,
427
+ data.messages,
428
+ data.calculationElapsedTime,
429
+ data.operationId
430
+ );
431
+ }
432
+ }