-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAWS_ATB.cs
494 lines (451 loc) · 23.7 KB
/
AWS_ATB.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
using System;
using System.Reflection;
using System.Collections;
using OpenBveApi.Runtime;
namespace Plugin
{
/// <summary>Represents the AWS device.</summary>
internal partial class 123 : Device
{
// --- members ---
/// <summary>The underlying train.</summary>
private Train Train;
/// <summary>The Automatic Warning System acknowledgement timeout period.</summary>
internal int CancelTimeout;
/// <summary>The timer which keeps track of the Automatic Warning System acknowledgement countdown in milliseconds.</summary>
private int MyCancelTimer;
/// <summary>The timer which keeps track of the Automatic Warning System north pole detection countdown in milliseconds.</summary>
private int MyDetectionTimer;
/// <summary>Whether or not the next encountered AWS permanent magnet should be suppressed (ignored).</summary>
private bool MySuppressionActive;
/// <summary>The location where the last AWS suppression megnet was detected.</summary>
private double MySuppressionLocation;
/// <summary>The current warning state of the Automatic Warning System.</summary>
private SafetyStates MySafetyState;
/// <summary>The current state of the Automatic Warning System sunflower indicator.</summary>
private SunflowerStates SunflowerState;
// properties
/// <summary>Gets the current warning state of the Automatic Warning System.</summary>
internal SafetyStates SafetyState
{
get { return this.MySafetyState; }
}
/// <summary>Gets the current value of the Automatic Warning System cancellation timer.</summary>
internal int CancelTimer
{
get { return this.MyCancelTimer; }
}
internal AWS(Train train)
{
this.Train = train;
}
/// <summary>Creates a new instance of this class.</summary>
/// <remarks>Any default values are assigned in this constructor - they will be used if none are loaded from the configuration file,
/// and they will determine what values are written into a new configuration file if necessary.</remarks>
internal AWS()
{
base.MyOperativeState = OperativeStates.Normal;
base.MyEnabled = false;
this.CancelTimeout = 3000;
this.MyCancelTimer = this.CancelTimeout;
this.MyDetectionTimer = 0;
this.MySuppressionActive = false;
this.MySuppressionLocation = 0;
this.MySafetyState = SafetyStates.None;
this.SunflowerState = SunflowerStates.Warn;
}
// events
/// <summary>Event signalling that the AWS warning horn has been acknowledged.</summary>
internal event EventHandler AwsWarningAcknowledged;
// event publishing methods
/// <summary>Publishes an event signalling that the AWS warning horn has been acknowledged.</summary>
private void OnAwsWarningAcknowledged(EventArgs e)
{
EventHandler handler = AwsWarningAcknowledged;
if (handler != null)
{
handler(this, e);
}
/*if (Plugin.DebugMode) {
Plugin.ReportLogEntry(
string.Format(
"{0} {1} {2} Aws: [EVENT] - AWS warning acknowledged at {3} metres",
DateTime.Now.TimeOfDay.ToString(),
this.GetType().Name.ToString(),
MethodInfo.GetCurrentMethod().ToString(),
Plugin.TrainLocation.ToString()
)
);
}*/
}
// event handling methods
/// <summary>This method is called when an AWS initiated TPWS Brake Demand event occurs.</summary>
/// <remarks>Use this event to suppress an Automatic Warning System warning when a TPWS Brake Demand has already been issued.</remarks>
internal void HandleTpwsAwsBrakeDemand(Object sender, EventArgs e)
{
if (this.MySafetyState != SafetyStates.Isolated)
{
this.MySafetyState = SafetyStates.TPWSAWSBrakeDemandIssued;
}
}
/// <summary>This method is called when a TPWS Brake Demand event occurs.</summary>
/// <remarks>Use this event to suppress an Automatic Warning System warning when a TPWS Brake Demand has already been issued.</remarks>
internal void HandleTpwsTssBrakeDemand(Object sender, EventArgs e)
{
if (this.MySafetyState != SafetyStates.Isolated)
{
this.MySafetyState = SafetyStates.TPWSTssBrakeDemandIssued;
}
}
/// <summary>This method is called when a TPWS isolation event occurs.</summary>
/// <remarks>Use this event to disable the Automatic Warning System when the TPWS is isolated.</remarks>
internal void HandleTpwsIsolated(Object sender, EventArgs e)
{
this.Isolate();
}
/// <summary>This method is called when a TPWS no-longer-isolated event occurs.</summary>
/// <remarks>Use this event to re-enable the Automatic Warning System when the TPWS is no longer isolated.</remarks>
internal void HandleTpwsNotIsolated(Object sender, EventArgs e)
{
this.Reset();
}
// instance methods
/// <summary>Re-initialises the system to its default state. This will also clear any active warnings, so if calling this method externally,
/// only do so via the Plugin.Initialize() method.</summary>
/// <param name="mode">The initialisation mode as set by the host application.</param>
internal void Reinitialise(InitializationModes mode)
{
this.MyCancelTimer = this.CancelTimeout;
this.MyDetectionTimer = 0;
this.MySuppressionActive = false;
this.MySuppressionLocation = 0;
this.MySafetyState = SafetyStates.None;
this.SunflowerState = SunflowerStates.Clear;
}
/// <summary>Call this method to unconditionally reset the system in order to cancel any warnings. This does not override the behaviour of the Interlock Manager, however.</summary>
/// <remarks>This method will unconditionally reset the system, regardless of circumstances, so use it carefully. Any conditional reset of the system is handled within
/// the Update() method instead.</remarks>
internal void Reset()
{
/* Unconditionally resets the Automatic Warning System, cancelling any warnings which are already in effect */
this.MyCancelTimer = this.CancelTimeout;
this.MyDetectionTimer = 0;
this.MySuppressionActive = false;
this.MySuppressionLocation = 0;
this.MySafetyState = SafetyStates.None;
this.SunflowerState = SunflowerStates.Warn;
/* InterlockManager.RequestBrakeReset();
InterlockManager.RequestTractionPowerReset(); */
}
//Switch Block to enable/ disable to be added
/// <summary>This should be called once during each Elapse() call, and defines the behaviour of the system.
/// Power and brake handle positions should be controlled by issuing demands and requests to the Interlock Manager.</summary>
/// <param name="elapsedTime">The elapsed time since the last call to this method in milliseconds.</param>
/// <param name="panel">The array of panel variables the plugin initialized in the Load call.</param>
/// <param name="debugBuilder">A string builder object, to which debug text can be appended.</param>
internal override void Elapse(ElapseData data, ref bool blocking)
{
/* If disabled, no processing is done */
if (base.Enabled)
{
/* Set the conditions under which this system's on behaviour will function */
if (base.OperativeState != OperativeStates.Failed)
{
if (this.MySuppressionActive)
{
/* Cancel any suppression which is in effect, if the train is not within range of the last suppression magnet location */
if (Train.trainspeed > 0)
{
if (Train.trainlocation > this.MySuppressionLocation + 2)
{
this.MySuppressionLocation = 0;
this.MySuppressionActive = false;
}
}
else
{
if (Train.trainlocation < this.MySuppressionLocation - 2)
{
this.MySuppressionLocation = 0;
this.MySuppressionActive = false;
}
}
}
if (this.SafetyState != SafetyStates.Isolated)
{
/* Select behaviour depending upon the safety state */
if (this.MySafetyState == SafetyStates.SelfTest)
{
/* Show or hide the AWS sunflower during the AWS is in self-test mode */
if (StartupSelfTestManager.SequenceTimer < 1300 && StartupSelfTestManager.SequenceTimer > 500)
{
this.SunflowerState = SunflowerStates.Warn;
}
else if (StartupSelfTestManager.SequenceTimer <= 0)
{
this.MySafetyState = SafetyStates.SelfTestWarning;
}
else
{
this.SunflowerState = SunflowerStates.Clear;
}
}
else if (this.MySafetyState == SafetyStates.SelfTestWarning)
{
/* Clear the AWS sunflower indication while the warning is sounding during the self-test */
this.SunflowerState = SunflowerStates.Clear;
// SoundManager.Play(SoundIndices.AwsHorn, 1.0, 1.0, true);
}
else if (this.MySafetyState == SafetyStates.None)
{
/* if (SoundManager.IsPlaying(SoundIndices.AwsHorn)) {
SoundManager.Stop(SoundIndices.AwsHorn);
} */
}
else if (this.MySafetyState == SafetyStates.Primed)
{
/* An AWS magnet south pole has primed the AWS */
this.SunflowerState = SunflowerStates.Clear;
this.MyDetectionTimer = this.MyDetectionTimer + (int)elapsedTime;
if (this.MyDetectionTimer > 1000)
{
/* No north pole has been detected within the timeout period, so reset the detection timer and issue an AWS warning */
this.MyDetectionTimer = 0;
this.MySafetyState = SafetyStates.CancelTimerActive;
/* if (Plugin.DebugMode) {
Plugin.ReportLogEntry(
string.Format(
"{0} {1} {2} AWS: [INFORMATION] - AWS delay period has elapsed at location {3} metres - issuing AWS warning",
DateTime.Now.TimeOfDay.ToString(),
this.GetType().Name.ToString(),
MethodInfo.GetCurrentMethod().ToString(),
Train.trainlocation.ToString()
)
);
}*/
}
}
else if (this.MySafetyState == SafetyStates.Clear)
{
/* The AWS indicates a clear signal */
this.Reset();
this.SunflowerState = SunflowerStates.Clear;
/*SoundManager.Play(SoundIndices.AwsBing, 1.0, 1.0, false);
SoundManager.Stop(SoundIndices.AwsHorn); */
}
else if (this.MySafetyState == SafetyStates.CancelTimerActive)
{
/* An AWS warning has been issued */
/*SoundManager.Play(SoundIndices.AwsHorn, 1.0, 1.0, true); */
this.SunflowerState = SunflowerStates.Clear;
this.MyCancelTimer = this.MyCancelTimer - (int)elapsedTime;
if (this.MyCancelTimer < 0)
{
this.MyCancelTimer = 0;
this.MySafetyState = SafetyStates.CancelTimerExpired;
}
}
else if (this.MySafetyState == SafetyStates.WarningAcknowledged)
{
/* An AWS warning was acknowledged in time */
/*SoundManager.Stop(SoundIndices.AwsHorn);*/
this.Reset();
}
else if (this.MySafetyState == SafetyStates.CancelTimerExpired)
{
/* An AWS warning was not acknowledged in time */
/*SoundManager.Play(SoundIndices.AwsHorn, 1.0, 1.0, true);*/
if (Train.TPWS.Enabled)
{
Train.TPWS.IssueBrakeDemand();
}
else
{
/*InterlockManager.DemandBrakeApplication();
if (Plugin.Diesel.Enabled) {
InterlockManager.DemandTractionPowerCutoff();*/
}
}
}
else if (this.MySafetyState == SafetyStates.TPWSAWSBrakeDemandIssued)
{
/* The TPWS issued an AWS Brake Demand due to the AWS not being acknowledged in time */
/*SoundManager.Play(SoundIndices.AwsHorn, 1.0, 1.0, true);*/
}
}
else if (this.SafetyState == SafetyStates.Isolated)
{
/*if (SoundManager.IsPlaying(SoundIndices.AwsHorn))
{
SoundManager.Stop(SoundIndices.AwsHorn);
}*/
this.MyCancelTimer = this.CancelTimeout;
this.SunflowerState = SunflowerStates.Warn;
}
/* Set the state of the AWS Sunflower instrument */
if (this.SunflowerState == SunflowerStates.Warn)
{
/*panel[PanelIndices.AwsSunflower] = (int)SunflowerState;*/
}
}
}
/* Add any information to display via openBVE's in-game debug interface mode below */
/*if (Plugin.DebugMode) {
debugBuilder.AppendFormat("[Aws:{0}]",
this.MySafetyState.ToString());
}*/
// other methods
/// <summary>Call this function to issue an AWS clear indication, as a result of passing the north pole of an AWS electromagnet. This should be done only via the SetBeacon() method.</summary>
internal void IssueClear()
{
if (this.MySafetyState != SafetyStates.Isolated && (this.MySafetyState == SafetyStates.Primed || this.MySafetyState == SafetyStates.CancelTimerActive))
{
this.MySafetyState = SafetyStates.Clear;
/* if (Plugin.DebugMode)
{
Plugin.ReportLogEntry(
string.Format(
"{0} {1} {2} AWS: [INFORMATION] - AWS north pole detected at location {3} metres - issuing clear indication and resetting delay period timer",
DateTime.Now.TimeOfDay.ToString(),
this.GetType().Name.ToString(),
MethodInfo.GetCurrentMethod().ToString(),
Train.trainlocation.ToString()
)
);
} */
}
}
/// <summary>Call this function to issue an AWS clear indication with legacy behaviour only. This should be done only via the SetBeacon() method.</summary>
internal void IssueLegacyClear()
{
if (this.MySafetyState != SafetyStates.Isolated)
{
this.MySafetyState = SafetyStates.Clear;
/* if (Plugin.DebugMode)
{
Plugin.ReportLogEntry(
string.Format(
"{0} {1} {2} AWS: [INFORMATION] - AWS magnet (legacy mode) detected at location {3} metres - issuing clear indication and resetting delay period timer",
DateTime.Now.TimeOfDay.ToString(),
this.GetType().Name.ToString(),
MethodInfo.GetCurrentMethod().ToString(),
Train.trainlocation.ToString()
)
);
} */
}
}
/// <summary>Call this function to prime the AWS as a result of passing the south pole of an AWS permanent magnet. This should be done only via the SetBeacon() method.</summary>
internal void Prime()
{
/* Primes the AWS state, ready for north pole detection, unless a TPWS Brake Demand is already in effect */
if (this.SafetyState != SafetyStates.Isolated && this.MySafetyState != SafetyStates.Primed &&
this.MySafetyState != SafetyStates.TPWSAWSBrakeDemandIssued && Train.TPWS.SafetyState != TPWS.SafetyStates.TssBrakeDemand &&
Train.TPWS.SafetyState != TPWS.SafetyStates.BrakeDemandAcknowledged)
{
if (!this.MySuppressionActive)
{
/* Only act upon this, if the permanent magnet is not being suppressed */
this.MySafetyState = SafetyStates.Primed;
}
else if (this.MySuppressionActive)
{
/* The magnet is being suppressed - reset state for the next magnet to be encountered */
this.MySuppressionActive = false;
}
/* if (Plugin.DebugMode)
{
Plugin.ReportLogEntry(
string.Format(
"{0} {1} {2} AWS: [INFORMATION] - AWS south pole detected at location {3} metres - system is primed and delay period timer is active",
DateTime.Now.TimeOfDay.ToString(),
this.GetType().Name.ToString(),
MethodInfo.GetCurrentMethod().ToString(),
Train.trainlocation.ToString()
)
);
} */
}
}
/// <summary>Call this function to suppress detection of an AWS permanent magnet. This should be done only via the SetBeacon() method.</summary>
internal void Suppress(double location)
{
if (this.MySafetyState != SafetyStates.Isolated)
{
this.MySuppressionActive = true;
this.MySuppressionLocation = location;
/* if (Plugin.DebugMode)
{
Plugin.ReportLogEntry(
string.Format(
"{0} {1} {2} AWS: [INFORMATION] - AWS suppression at location {3} metres - ignoring the next permanent magnet within 2 metres",
DateTime.Now.TimeOfDay.ToString(),
this.GetType().Name.ToString(),
MethodInfo.GetCurrentMethod().ToString(),
Train.trainlocation.ToString()
)
);
} */
}
}
internal void AcknowledgeWarning()
{
bool issueWarningAcknowledgedEvent;
if (!base.MyEnabled)
{
/* Raise an event signalling that an AWS warning has been acknowledged for event subscribers
* (such as the Vigilance Device and TPWS).
*
* This is here so that TPWS can function without AWS enabled */
issueWarningAcknowledgedEvent = true;
this.MySafetyState = SafetyStates.None;
}
else if (this.MySafetyState != SafetyStates.Isolated &&
this.MySafetyState == SafetyStates.CancelTimerActive || this.MySafetyState == SafetyStates.SelfTestWarning ||
this.MySafetyState == SafetyStates.CancelTimerExpired || this.MySafetyState == SafetyStates.TPWSAWSBrakeDemandIssued ||
this.MySafetyState == SafetyStates.TPWSTssBrakeDemandIssued)
{
/* Handle situations where an AWS warning is indicated and/or a TPWS brake demand has been issued */
this.MySafetyState = SafetyStates.WarningAcknowledged;
issueWarningAcknowledgedEvent = true;
}
else if (Train.TPWS.Enabled)
{
if (Train.TPWS.SafetyState == TPWS.SafetyStates.TssBrakeDemand && this.MySafetyState == SafetyStates.None)
{
/* Handle a situation where AWS may be clear but a TPWS brake demand has been issued */
issueWarningAcknowledgedEvent = true;
}
else
{
issueWarningAcknowledgedEvent = false;
}
}
else
{
issueWarningAcknowledgedEvent = false;
}
if (issueWarningAcknowledgedEvent)
{
/* Raise an event signalling that an AWS warning has been acknowledged for event subscribers */
this.OnAwsWarningAcknowledged(new EventArgs());
}
}
/// <summary>Call this function to put the Automatic Warning System into self-test mode. This should only be done via the StartupSelfTestManager.Update() method.</summary>
internal void SelfTest()
{
this.MySafetyState = SafetyStates.SelfTest;
}
/// <summary>Call this method to isolate the Automatic Warning System. This will only succeed if there is no safety intervention in effect.
/// This does not override the behaviour of the Interlock Manager.</summary>
internal void Isolate()
{
if (this.MySafetyState != SafetyStates.CancelTimerExpired &&
this.MySafetyState != SafetyStates.TPWSAWSBrakeDemandIssued &&
this.MySafetyState != SafetyStates.TPWSTssBrakeDemandIssued)
{
this.MySafetyState = SafetyStates.Isolated;
}
}
}
}