-
Notifications
You must be signed in to change notification settings - Fork 81
/
CircuitBreaker.cs
427 lines (373 loc) · 15.3 KB
/
CircuitBreaker.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// COPYRIGHT 2020 by the Open Rails project.
//
// This file is part of Open Rails.
//
// Open Rails is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Open Rails is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
using Orts.Common;
using Orts.Parsers.Msts;
using Orts.Simulation.AIs;
using Orts.Simulation.Physics;
using ORTS.Scripting.Api;
using System;
using System.IO;
namespace Orts.Simulation.RollingStocks.SubSystems.PowerSupplies
{
public class ScriptedCircuitBreaker : ISubSystem<ScriptedCircuitBreaker>
{
public ScriptedLocomotivePowerSupply PowerSupply { get; protected set; }
public MSTSLocomotive Locomotive => PowerSupply.Locomotive;
public Simulator Simulator => PowerSupply.Locomotive.Simulator;
public bool Activated = false;
string ScriptName = "Automatic";
CircuitBreaker Script;
private float DelayS = 0f;
public CircuitBreakerState State { get; private set; } = CircuitBreakerState.Open;
public bool DriverClosingOrder { get; private set; }
public bool DriverOpeningOrder { get; private set; }
public bool DriverClosingAuthorization { get; private set; }
public bool TCSClosingOrder
{
get
{
if (Locomotive.Train.LeadLocomotive is MSTSLocomotive locomotive)
return locomotive.TrainControlSystem.CircuitBreakerClosingOrder;
else
return false;
}
}
public bool TCSOpeningOrder
{
get
{
if (Locomotive.Train.LeadLocomotive is MSTSLocomotive locomotive)
return locomotive.TrainControlSystem.CircuitBreakerOpeningOrder;
else
return false;
}
}
public bool TCSClosingAuthorization
{
get
{
if (Locomotive.Train.LeadLocomotive is MSTSLocomotive locomotive)
return locomotive.TrainControlSystem.PowerAuthorization;
else
return false;
}
}
public bool ClosingAuthorization { get; private set; }
public ScriptedCircuitBreaker(ScriptedLocomotivePowerSupply powerSupply)
{
PowerSupply = powerSupply;
}
public void Copy(ScriptedCircuitBreaker other)
{
ScriptName = other.ScriptName;
State = other.State;
DelayS = other.DelayS;
}
public void Parse(string lowercasetoken, STFReader stf)
{
switch (lowercasetoken)
{
case "engine(ortscircuitbreaker":
ScriptName = stf.ReadStringBlock(null);
break;
case "engine(ortscircuitbreakerclosingdelay":
DelayS = stf.ReadFloatBlock(STFReader.UNITS.Time, null);
break;
}
}
public void Initialize()
{
if (!Activated)
{
if (ScriptName != null)
{
switch(ScriptName)
{
case "Automatic":
Script = new AutomaticCircuitBreaker() as CircuitBreaker;
break;
case "Manual":
Script = new ManualCircuitBreaker() as CircuitBreaker;
break;
default:
var pathArray = new string[] { Path.Combine(Path.GetDirectoryName(Locomotive.WagFilePath), "Script") };
Script = Simulator.ScriptManager.Load(pathArray, ScriptName) as CircuitBreaker;
break;
}
}
// Fallback to automatic circuit breaker if the above failed.
if (Script == null)
{
Script = new AutomaticCircuitBreaker() as CircuitBreaker;
}
// AbstractScriptClass
Script.ClockTime = () => (float)Simulator.ClockTime;
Script.GameTime = () => (float)Simulator.GameTime;
Script.PreUpdate = () => Simulator.PreUpdate;
Script.DistanceM = () => Locomotive.DistanceM;
Script.SpeedMpS = () => Math.Abs(Locomotive.SpeedMpS);
Script.Confirm = Locomotive.Simulator.Confirmer.Confirm;
Script.Message = Locomotive.Simulator.Confirmer.Message;
Script.SignalEvent = Locomotive.SignalEvent;
Script.SignalEventToTrain = (evt) =>
{
if (Locomotive.Train != null)
{
Locomotive.Train.SignalEvent(evt);
}
};
// TractionCutOffSubsystem getters
Script.SupplyType = () => PowerSupply.Type;
Script.CurrentState = () => State;
Script.CurrentPantographState = () => Locomotive?.Pantographs.State ?? PantographState.Unavailable;
Script.CurrentDieselEngineState = () => (Locomotive as MSTSDieselLocomotive)?.DieselEngines.State ?? DieselEngineState.Unavailable;
Script.CurrentPowerSupplyState = () => PowerSupply.MainPowerSupplyState;
Script.DriverClosingOrder = () => DriverClosingOrder;
Script.DriverOpeningOrder = () => DriverOpeningOrder;
Script.DriverClosingAuthorization = () => DriverClosingAuthorization;
Script.TCSClosingAuthorization = () => TCSClosingAuthorization;
Script.ClosingAuthorization = () => ClosingAuthorization;
Script.IsLowVoltagePowerSupplyOn = () => PowerSupply.LowVoltagePowerSupplyOn;
Script.IsCabPowerSupplyOn = () => PowerSupply.CabPowerSupplyOn;
Script.ClosingDelayS = () => DelayS;
// TractionCutOffSubsystem setters
Script.SetDriverClosingOrder = (value) => DriverClosingOrder = value;
Script.SetDriverOpeningOrder = (value) => DriverOpeningOrder = value;
Script.SetDriverClosingAuthorization = (value) => DriverClosingAuthorization = value;
Script.SetClosingAuthorization = (value) => ClosingAuthorization = value;
// CircuitBreaker getters
Script.CurrentState = () => State;
Script.TCSClosingOrder = () => TCSClosingOrder;
Script.TCSOpeningOrder = () => TCSOpeningOrder;
// CircuitBreaker setters
Script.SetCurrentState = (value) =>
{
State = value;
TCSEvent CircuitBreakerEvent = State == CircuitBreakerState.Closed ? TCSEvent.CircuitBreakerClosed : TCSEvent.CircuitBreakerOpen;
Locomotive.TrainControlSystem.HandleEvent(CircuitBreakerEvent);
};
Script.Initialize();
Activated = true;
}
}
public void InitializeMoving()
{
Script?.InitializeMoving();
State = CircuitBreakerState.Closed;
}
public void Update(float elapsedSeconds)
{
if (Locomotive.Train.TrainType == Train.TRAINTYPE.AI || Locomotive.Train.TrainType == Train.TRAINTYPE.AI_AUTOGENERATE
|| Locomotive.Train.TrainType == Train.TRAINTYPE.AI_PLAYERHOSTING)
{
State = CircuitBreakerState.Closed;
}
else
{
if (Script != null)
{
Script.Update(elapsedSeconds);
}
}
}
public void HandleEvent(PowerSupplyEvent evt)
{
if (Script != null)
{
Script.HandleEvent(evt);
}
}
public void Save(BinaryWriter outf)
{
outf.Write(ScriptName);
outf.Write(DelayS);
outf.Write(State.ToString());
outf.Write(DriverClosingOrder);
outf.Write(DriverOpeningOrder);
outf.Write(DriverClosingAuthorization);
outf.Write(ClosingAuthorization);
}
public void Restore(BinaryReader inf)
{
ScriptName = inf.ReadString();
DelayS = inf.ReadSingle();
State = (CircuitBreakerState)Enum.Parse(typeof(CircuitBreakerState), inf.ReadString());
DriverClosingOrder = inf.ReadBoolean();
DriverOpeningOrder = inf.ReadBoolean();
DriverClosingAuthorization = inf.ReadBoolean();
ClosingAuthorization = inf.ReadBoolean();
}
}
class AutomaticCircuitBreaker : CircuitBreaker
{
private Timer ClosingTimer;
private CircuitBreakerState PreviousState;
public override void Initialize()
{
ClosingTimer = new Timer(this);
ClosingTimer.Setup(ClosingDelayS());
SetDriverClosingOrder(false);
SetDriverOpeningOrder(false);
SetDriverClosingAuthorization(true);
}
public override void Update(float elapsedSeconds)
{
SetClosingAuthorization(TCSClosingAuthorization() && CurrentPantographState() == PantographState.Up);
switch (CurrentState())
{
case CircuitBreakerState.Closed:
if (!ClosingAuthorization())
{
SetCurrentState(CircuitBreakerState.Open);
}
break;
case CircuitBreakerState.Closing:
if (ClosingAuthorization())
{
if (!ClosingTimer.Started)
{
ClosingTimer.Start();
}
if (ClosingTimer.Triggered)
{
ClosingTimer.Stop();
SetCurrentState(CircuitBreakerState.Closed);
}
}
else
{
ClosingTimer.Stop();
SetCurrentState(CircuitBreakerState.Open);
}
break;
case CircuitBreakerState.Open:
if (ClosingAuthorization())
{
SetCurrentState(CircuitBreakerState.Closing);
}
break;
}
if (PreviousState != CurrentState())
{
switch (CurrentState())
{
case CircuitBreakerState.Open:
SignalEvent(Event.CircuitBreakerOpen);
break;
case CircuitBreakerState.Closing:
SignalEvent(Event.CircuitBreakerClosing);
break;
case CircuitBreakerState.Closed:
SignalEvent(Event.CircuitBreakerClosed);
break;
}
}
PreviousState = CurrentState();
}
public override void HandleEvent(PowerSupplyEvent evt)
{
// Nothing to do since it is automatic
}
}
class ManualCircuitBreaker : CircuitBreaker
{
private Timer ClosingTimer;
private CircuitBreakerState PreviousState;
public override void Initialize()
{
ClosingTimer = new Timer(this);
ClosingTimer.Setup(ClosingDelayS());
SetDriverClosingAuthorization(true);
}
public override void Update(float elapsedSeconds)
{
SetClosingAuthorization(TCSClosingAuthorization() && CurrentPantographState() == PantographState.Up);
switch (CurrentState())
{
case CircuitBreakerState.Closed:
if (!ClosingAuthorization() || DriverOpeningOrder())
{
SetCurrentState(CircuitBreakerState.Open);
}
break;
case CircuitBreakerState.Closing:
if (ClosingAuthorization() && DriverClosingOrder())
{
if (!ClosingTimer.Started)
{
ClosingTimer.Start();
}
if (ClosingTimer.Triggered)
{
ClosingTimer.Stop();
SetCurrentState(CircuitBreakerState.Closed);
}
}
else
{
ClosingTimer.Stop();
SetCurrentState(CircuitBreakerState.Open);
}
break;
case CircuitBreakerState.Open:
if (ClosingAuthorization() && DriverClosingOrder())
{
SetCurrentState(CircuitBreakerState.Closing);
}
break;
}
if (PreviousState != CurrentState())
{
switch (CurrentState())
{
case CircuitBreakerState.Open:
SignalEvent(Event.CircuitBreakerOpen);
break;
case CircuitBreakerState.Closing:
SignalEvent(Event.CircuitBreakerClosing);
break;
case CircuitBreakerState.Closed:
SignalEvent(Event.CircuitBreakerClosed);
break;
}
}
PreviousState = CurrentState();
}
public override void HandleEvent(PowerSupplyEvent evt)
{
switch (evt)
{
case PowerSupplyEvent.CloseCircuitBreaker:
SetDriverClosingOrder(true);
SetDriverOpeningOrder(false);
SignalEvent(Event.CircuitBreakerClosingOrderOn);
Confirm(CabControl.CircuitBreakerClosingOrder, CabSetting.On);
if (!ClosingAuthorization())
{
Message(ConfirmLevel.Warning, Simulator.Catalog.GetString("Circuit breaker closing not authorized"));
}
break;
case PowerSupplyEvent.OpenCircuitBreaker:
SetDriverClosingOrder(false);
SetDriverOpeningOrder(true);
SignalEvent(Event.CircuitBreakerClosingOrderOff);
Confirm(CabControl.CircuitBreakerClosingOrder, CabSetting.Off);
break;
}
}
}
}