-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScadaSeeder.cs
More file actions
110 lines (99 loc) · 3.52 KB
/
Copy pathScadaSeeder.cs
File metadata and controls
110 lines (99 loc) · 3.52 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
using Microsoft.EntityFrameworkCore;
using SCADASampleAPI.Models;
namespace SCADASampleAPI.Data;
public static class ScadaSeeder
{
public static async Task SeedScadaAsync(ApplicationDbContext db, CancellationToken cancellationToken = default)
{
if (await db.Pipelines.AnyAsync(cancellationToken))
return;
var pipelines = new[]
{
new Pipeline { Name = "North Transfer", Code = "PL-N01", IsActive = true },
new Pipeline { Name = "South Blending", Code = "PL-S02", IsActive = true },
new Pipeline { Name = "East Storage", Code = "PL-E03", IsActive = true }
};
db.Pipelines.AddRange(pipelines);
await db.SaveChangesAsync(cancellationToken);
var reloaded = await db.Pipelines.OrderBy(p => p.Code).ToListAsync(cancellationToken);
foreach (var p in reloaded)
{
var mid = 50.0;
var tags = new List<Tag>
{
new()
{
PipelineId = p.PipelineId,
Name = "Inlet Temperature",
Unit = "°C",
MinValue = 20,
MaxValue = 90,
CurrentValue = mid,
LastUpdatedUtc = DateTimeOffset.UtcNow
},
new()
{
PipelineId = p.PipelineId,
Name = "Discharge Pressure",
Unit = "bar",
MinValue = 0.5,
MaxValue = 8,
CurrentValue = 3.5,
LastUpdatedUtc = DateTimeOffset.UtcNow
},
new()
{
PipelineId = p.PipelineId,
Name = "Flow Rate",
Unit = "m³/h",
MinValue = 10,
MaxValue = 120,
CurrentValue = 65,
LastUpdatedUtc = DateTimeOffset.UtcNow
},
new()
{
PipelineId = p.PipelineId,
Name = "Tank Level",
Unit = "%",
MinValue = 5,
MaxValue = 95,
CurrentValue = 55,
LastUpdatedUtc = DateTimeOffset.UtcNow
}
};
db.Tags.AddRange(tags);
}
await db.SaveChangesAsync(cancellationToken);
var allTags = await db.Tags.Include(t => t.Pipeline).ToListAsync(cancellationToken);
foreach (var tag in allTags.Where(t => t.Name == "Inlet Temperature"))
{
db.Alarms.Add(new Alarm
{
PipelineId = tag.PipelineId,
TagId = tag.TagId,
AlarmType = "HI",
SetPoint = 75f,
Severity = 2,
Message = $"{tag.Pipeline?.Name}: inlet temperature high",
IsEnabled = true,
IsActive = false
});
}
foreach (var tag in allTags.Where(t => t.Name == "Tank Level"))
{
db.Alarms.Add(new Alarm
{
PipelineId = tag.PipelineId,
TagId = tag.TagId,
AlarmType = "LO",
SetPoint = 15f,
Severity = 1,
Message = $"{tag.Pipeline?.Name}: tank level low",
IsEnabled = true,
IsActive = false
});
}
await db.SaveChangesAsync(cancellationToken);
}
}