-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStationPlan.cs
75 lines (70 loc) · 2.75 KB
/
StationPlan.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DBSharp
{
/// <summary>
/// Connection plan for a station
/// </summary>
public class StationPlan
{
private readonly string _StationName;
private readonly ConcurrentDictionary<string, TrainConnection> _Connections;
public StationPlan(string stationName, IEnumerable<TrainConnection> connections = null)
{
_StationName = stationName ?? throw new ArgumentNullException("StationName can not be null");
if (connections != null)
_Connections = new ConcurrentDictionary<string, TrainConnection>(connections.ToDictionary(con => con.Uid));
else
_Connections = new ConcurrentDictionary<string, TrainConnection>();
}
/// <summary>
/// Human readable name of station
/// </summary>
public string StationName => _StationName;
/// <summary>
/// Dictionary of connections stored in this plan
/// Key = Connection UID
/// </summary>
public ConcurrentDictionary<string, TrainConnection> Connections => _Connections;
public void ApplyChangesets(params RealtimeChangeset[] changesets)
{
foreach (var changeset in changesets.Where(c => c?.TargetUid != null))
{
if (Connections.ContainsKey(changeset.TargetUid))
{
var connection = Connections[changeset.TargetUid];
if (connection.RealtimeChangeset != null)
connection.RealtimeChangeset.MergeWithNewChangeset(changeset);
else
connection.RealtimeChangeset = changeset;
}
else
{
//var newConnection = new TrainConnection(changeset.TargetUid);
//newConnection.RealtimeChangeset = changeset;
//Connections.Add(newConnection.Uid, newConnection);
}
}
}
/// <summary>
/// Appends more plans to this one (for creating a plan of multiple hours using IRISPlanRequest)
/// </summary>
/// <param name="plans"></param>
public void Append(params StationPlan[] plans)
{
foreach(var plan in plans)
{
if (plan.StationName != this.StationName)
throw new ArgumentException("Cannot join plans of different stations");
foreach(var con in plan.Connections)
{
Connections[con.Key] = con.Value;
}
}
}
}
}