-
Notifications
You must be signed in to change notification settings - Fork 0
/
uControler.pas
162 lines (130 loc) · 5.05 KB
/
uControler.pas
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
unit uControler;
interface
uses
System.Generics.Collections, FMX.Types, System.Classes,
uIObserveable, uIObserver, uRoad;
type
TTrafficLightControler = class(TComponent, IObserveable)
private
FTimer : TTimer;
{$REGION 'Gets & Setts'}
FObservers : TList<IObserver>;
FControlerState : TLightState;
FDurationsOfLights : TDictionary<TLightState, integer>;
FActiveLights : TList<TLightState>;
function GetPower: Boolean;
procedure SetPower(const Value: Boolean);
procedure SetControlerState(const Value: TLightState);
{$ENDREGION}
public
/// <summary> Represent list of switched on lights on each traffic light group. </summary>
property ActiveLights : TList<TLightState> read FActiveLights;
/// <summary> Represent duration of each light on leading traffic light group. </summary>
property DurationsOfLights : TDictionary<TLightState, integer> read FDurationsOfLights;
/// <summary> Swithes traffic light controller. Run/Stop timer/controlling method of traffic light controller </summary>
property Power : Boolean read GetPower write SetPower;
/// <summary> Represent controller state as state of leading traffic light group. </summary>
property ControlerState : TLightState read FControlerState write SetControlerState;
/// <summary> Set duration in miliseconds of each light in leading group.</summary>
/// <param name="AVals">The array of pairs(<c>TLightState, integer</c>) where <c>TLightState</c> denotes light state and <c>integer</c> denote duration of corresponding light state.</param>
procedure InitLightIntervals(AVals : TArray<TPair<TLightState, integer>>);
/// <summary> Notify all subscribers of changes. Implementation of observer pattern <c>Notify</c> method.</summary>
procedure Notify;
/// <summary> Subscribe parameter object to notifications. Implementation of observer pattern <c>Subscribe</c> method.</summary>
/// <param name="AObs">Object to be notified on changes.</param>
procedure Subscribe(AObs : IObserver);
/// <summary> Remove parameter object from subscription to notifications. Implementation of observer pattern <c>Unsubscribe</c> method.</summary>
/// <param name="AObs">Object to be removed from notification list.</param>
procedure UnSubscribe(AObs : IObserver);
/// <summary> Remove parameter object from subscription to notifications.
/// Implementation of observer pattern. <c>Unsubscribe</c> method.</summary>
/// <param name="AObs">Object to be removed from notification list.</param>
procedure GroupSwitchLight(AGroup : integer; ALight : TLightState);
procedure Controling(ASender : TObject); // Do be changed
/// <summary> Get duration of light in miliseconds for specified light.</summary>
/// <param name="ALightState">State of traffic light</param>
/// <returns>Duration in miliseconds.</returns>
function GetLightDuration(ALightState : TLightState) : integer;
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
end;
implementation
{ TTrafficLightControler }
procedure TTrafficLightControler.Controling(ASender: TObject);
var F : integer;
procedure SetLights(ALight : TLightState);
begin
ActiveLights.Items[0] := ALight;
ActiveLights.Items[1] := TLightState((ord(ALight) + 2) mod 5);
ControlerState := ALight;
end;
begin
F := ord(FControlerState);
Inc(F);
F := F mod 5;
if F = 0 then
F := 1;
SetLights(TLightState(F))
end;
constructor TTrafficLightControler.Create(AOwner : TComponent);
begin
FObservers := TList<IObserver>.Create;
FActiveLights := TList<TLightState>.Create;
FDurationsOfLights := TDictionary<TLightState, integer>.Create;
FTimer := TTimer.Create(self);
FTimer.Interval := 3000;
FTimer.Enabled := false;
FTimer.OnTimer := Controling;
end;
destructor TTrafficLightControler.Destroy;
begin
FObservers.DisposeOf;
ActiveLights.DisposeOf;
DurationsOfLights.DisposeOf;
inherited;
end;
function TTrafficLightControler.GetLightDuration(ALightState: TLightState): integer;
begin
result := 0;
if DurationsOfLights.ContainsKey(ALightState) then
Result := DurationsOfLights.Items[ALightState];
end;
function TTrafficLightControler.GetPower: Boolean;
begin
Result := FTimer.Enabled;
end;
procedure TTrafficLightControler.GroupSwitchLight(AGroup: integer; ALight: TLightState);
begin
ActiveLights[AGroup] := ALight
end;
procedure TTrafficLightControler.InitLightIntervals(AVals: TArray<TPair<TLightState, integer>>);
var P : TPair<TLightState, integer>;
begin
for p in AVals do
DurationsOfLights.AddOrSetValue(p.Key, p.Value);
end;
procedure TTrafficLightControler.Notify;
var
T: IObserver;
begin
for T in FObservers do
T.Update(self);
end;
procedure TTrafficLightControler.SetControlerState(const Value: TLightState);
begin
FControlerState := Value;
Notify;
end;
procedure TTrafficLightControler.SetPower(const Value: Boolean);
begin
FTimer.Enabled := Value;
end;
procedure TTrafficLightControler.Subscribe(AObs: IObserver);
begin
FObservers.Add(aobs);
end;
procedure TTrafficLightControler.UnSubscribe(AObs: IObserver);
begin
FObservers.Remove(Aobs);
end;
end.