-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvents.cs
314 lines (236 loc) · 9.17 KB
/
Events.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
//
// Events.cs
//
// Implement classes to handle TsmApi events.
//
using System.Diagnostics;
using System.Runtime.InteropServices.ComTypes;
using Tsm;
namespace TsmXiL
{
internal class CTsmEvents<T>
{
private int _cookie = -1;
private IConnectionPoint _icp;
public CTsmEvents(ITsmApplication tsm)
{
_icp = null;
_cookie = -1;
Connect(tsm);
}
public int Cookie => _cookie;
~CTsmEvents()
{
Debug.WriteLine("Unload CTsmEvent");
Disconnect();
}
// This wraps a call to IConnectionPoint::Advise(...) so that the
// ID of the connection point can be saved in m_cookie for use
// later in calling of Advance(...) method in ITsmApplication.
// At the same time, subscribe Simulation and sensor Events.
public void Connect(ITsmApplication tsm)
{
var id = typeof(T).GUID;
IConnectionPointContainer icpc;
icpc = tsm as IConnectionPointContainer;
icpc.FindConnectionPoint(ref id, out _icp);
_icp.Advise(this, out _cookie);
}
public void Disconnect()
{
if (_cookie > -1)
{
_icp.Unadvise(_cookie);
_cookie = -1;
}
}
}
// ============================================================================
public delegate void NoRetValueHandler<T>(T e);
public delegate void NoRetValueNoParamHandler();
public delegate double AdvanceEventHandler(double time);
public delegate void StartSimulationEventHandler(short iRun, TsmRunType iRunType, bool bPreload);
public delegate void SensorEventsHandler(int sid, int vid, double dTime, float fSpeed);
public delegate void NewSignalStateEventHandler(int idSignal, double dTime, uint ulState);
public delegate void StartPlanEventHandler(int cookie, TsmControlClass cc, object ids);
public delegate void AVLEventHandler(TsmVehicle pVehicle, short iClass, short nOccupants, short iType,
ref STsmCoord3 pLocation);
public delegate void ArrivalEventHandler(int idVehicle, double dTime);
public delegate void DepartureEventHandler(int idVehicle, double dTime);
public delegate void NewLaneEventHandler(TsmVehicle pVehicle, int idLane, double dTime);
public delegate void NewLinkEventHandler(TsmVehicle pVehicle, int idLink, double dTime);
public delegate void NewPathEventHandler(TsmVehicle pVehicle, int idPath, int iPosition, double dTime);
public delegate dynamic GetPropertyEventHandler(TsmVehicle pVehicle, short iCol);
public delegate void SetPropertyEventHandler(TsmVehicle pVehicle, short iCol, object newVal);
public delegate float TransitStopEventHandler(double dTime, TsmVehicle pVehicle, TsmRoute pRoute, TsmStop pStop,
short nMaxCapacity, short nPassengers, float fSchedDev, float fDwellTime);
public delegate void NewSegmentEventHandler(TsmVehicle pVehicle, int idSegment, double dTime);
public delegate void LinkCostEventHandler(short iClass, short nOccupancy, short iGroup, uint lType, double dTime,
TsmLink pFromLink, TsmLink idLink, ref float pVal);
//used for child-parent communication
public delegate void ScheduledAction();
public delegate void OnScheduleChangeEventHandler(double time, ScheduledAction action);
public delegate void OnSetAdvanceTimeEventHandler(double time);
internal class CSimulationEvents : CTsmEvents<_ISimulationEvents>, _ISimulationEvents
{
public CSimulationEvents(ITsmApplication tsm) : base(tsm)
{
}
public virtual void OpenProject(string fname)
{
OnOpenProject?.Invoke(fname);
}
// Called just before simulation is about to start
public virtual void StartSimulation(short iRun, TsmRunType iRunType, bool bPreload)
{
OnStartSimulation?.Invoke(iRun, iRunType, bPreload);
}
// Called just before simulation loop begins, after all TransModeler
// internal modules has been initialized
public virtual void SimulationStarted()
{
OnSimulationStarted?.Invoke();
}
public virtual double Advance(double dTime)
{
if (OnAdvance != null)
{
return OnAdvance(dTime);
}
return double.MaxValue;
}
public virtual void EndSimulation(TsmState iState)
{
OnEndSimulation?.Invoke(iState);
}
public virtual void SimulationStopped(TsmState iState)
{
OnSimulationStopped?.Invoke(iState);
}
public virtual void CloseProject()
{
OnCloseProject?.Invoke();
}
public virtual void ExitApplication()
{
OnExitApplication?.Invoke();
}
public event NoRetValueHandler<string> OnOpenProject;
public event NoRetValueNoParamHandler OnCloseProject;
public event NoRetValueNoParamHandler OnSimulationStarted;
public event NoRetValueNoParamHandler OnExitApplication;
public event AdvanceEventHandler OnAdvance;
public event NoRetValueHandler<TsmState> OnEndSimulation;
public event NoRetValueHandler<TsmState> OnSimulationStopped;
public event StartSimulationEventHandler OnStartSimulation;
}
internal class CSensorEvents : CTsmEvents<_ISensorEvents>, _ISensorEvents
{
public CSensorEvents(ITsmApplication tsm)
: base(tsm)
{
}
public virtual void Arrive(int sid, int vid, double dTime, float fSpeed)
{
OnArrive?.Invoke(sid, vid, dTime, fSpeed);
}
public virtual void Leave(int sid, int vid, double dTime, float fSpeed)
{
OnLeave?.Invoke(sid, vid, dTime, fSpeed);
}
public event SensorEventsHandler OnArrive;
public event SensorEventsHandler OnLeave;
}
internal class CSignalEvent : CTsmEvents<_ISignalEvents>, _ISignalEvents
{
public CSignalEvent(ITsmApplication tsm)
: base(tsm)
{
}
public virtual void EndPlan(int cookie)
{
OnEndPlan(cookie);
}
public virtual void NewSignalState(int idSignal, double dTime, uint ulState)
{
OnNewSignalState(idSignal, dTime, ulState);
}
public virtual void StartPlan(int cookie, TsmControlClass cc, object ids)
{
OnStartPlan(cookie, cc, ids);
}
public void StartFares(int idEntrance)
{
}
public void EndFares(int idEntrance)
{
}
public event NoRetValueHandler<int> OnEndPlan = delegate { };
public event NewSignalStateEventHandler OnNewSignalState = delegate { };
public event StartPlanEventHandler OnStartPlan = delegate { };
}
internal class CVehicleEvents : CTsmEvents<_IVehicleEvents>, _IVehicleEvents
{
public CVehicleEvents(ITsmApplication tsm) : base(tsm)
{
}
public void Departure(int idVehicle, double dTime)
{
}
public void Parked(int idVehicle, double dTime)
{
}
public void Stalled(int idVehicle, double dTime, bool bStalled)
{
}
public void Arrival(int idVehicle, double dTime)
{
OnArrive?.Invoke(idVehicle, dTime);
}
public void NewLane(TsmVehicle pVehicle, int idLane, double dTime)
{
}
public void NewSegment(TsmVehicle pVehicle, int idSegment, double dTime)
{
}
public void NewLink(TsmVehicle pVehicle, int idLink, double dTime)
{
}
public void NewPath(TsmVehicle pVehicle, int idPath, int iPosition, double dTime)
{
}
public float TransitStop(double dTime, TsmVehicle pVehicle, TsmRoute pRoute, TsmStop pStop, short nMaxCapacity,
short nPassengers, float fSchedDev, float fDwellTime)
{
return float.MinValue;
}
public float CarFollowingAcceleration(double dTime, TsmVehicle pVehicle, float fRate)
{
return float.MinValue;
}
public float Acceleration(double dTime, TsmVehicle pVehicle, float fRate)
{
return float.MinValue;
}
public short LaneChange(double dTime, TsmVehicle pVehicle, short iChange, ref bool pMandatory)
{
return short.MinValue;
}
public void AVL(TsmVehicle pVehicle, short iType, ref STsmCoord3 pLocation, double dTime)
{
}
public float LinkCost(short iClass, short nOccupancy, short iGroup, uint lType, double dTime, TsmLink pFromLink, TsmLink pLink,
float fCostVal)
{
return float.MinValue;
}
public object GetProperty(TsmVehicle pVehicle, short iCol)
{
return null;
}
public void SetProperty(TsmVehicle pVehicle, short iCol, object newVal)
{
}
public event ArrivalEventHandler OnArrive;
}
}