This repository was archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathM2100Switcher.pas
More file actions
220 lines (196 loc) · 5.88 KB
/
M2100Switcher.pas
File metadata and controls
220 lines (196 loc) · 5.88 KB
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
unit M2100Switcher;
{$DEFINE LOG_MESSAGE_CONTENT_BEFORE_PROCESSING}
{$DEFINE LOG_MESSAGE_CONTENT_BEFORE_SENDING}
{ $DEFINE LOG_MESSAGE_STREAM_BEFORE_SENGING}
{$DEFINE LOG_ATTACH_POLLING_TAG}
//< affects
// LOG_MESSAGE_CONTENT_BEFORE_PROCESSING
// and
// LOG_MESSAGE_CONTENT_BEFORE_SENDING
{ $DEFINE LOG_COMMAND_ON_COMMAND_PROCESSING}
{ $DEFINE LOG_SUBCOMMAND_ON_SUBCOMMAND_PROCESSING}
interface
uses
SysUtils,
Classes,
UAdditionalTypes,
UAdditionalExceptions,
UCustomThread,
UStreamUtilities,
UExceptionTracer,
UTextUtilities,
CustomLogEntity,
EmptyLogEntity,
CustomSwitcherMessageUnit,
CustomSwitcherUnit,
CustomSwitcherFactoryUnit,
M2100Message,
M2100Keyer,
M2100Command,
M2100MessageDecoder,
M2100MessageEncoder;
type
TM2100Switcher = class(TCustomSwitcher)
public
constructor Create; override;
procedure Startup; override;
public const
DefaultSendReceiveThreadWaitInterval = 1;
protected
FKeyers: TM2100KeyersStatus;
fAutomationStatus: boolean;
procedure AssignDefaults;
procedure InitializeKeyers;
function GetAdditionalMessageLogTags(const aMessage: TCustomSwitcherMessage): string; override;
function IsAutoStatPolling(const aMessage: TM2100Message): boolean;
function ProcessMessage(const aMessage: TCustomSwitcherMessage): TCustomSwitcherMessage; override;
function ProcessCommands(const aMessage: TM2100Message): TM2100Message;
function ProcessCommand(const aCommand: TM2100Command): TM2100Command;
function ProcessSubCommand(const aSubCommand: TM2100SubCommand): TM2100SubCommand;
procedure DestroyThis;
public
property Keyers: TM2100KeyersStatus read FKeyers;
// this propery should be assigned by the user of this class
property AutomationStatus: boolean read fAutomationStatus write fAutomationStatus;
destructor Destroy; override;
end;
implementation
constructor TM2100Switcher.Create;
begin
inherited Create;
end;
procedure TM2100Switcher.Startup;
begin
InitializeKeyers;
AssignDefaults;
end;
procedure TM2100Switcher.InitializeKeyers;
begin
FKeyers := TM2100KeyersStatus.Create(0);
Keyers.SetDefault;
end;
procedure TM2100Switcher.AssignDefaults;
begin
AutomationStatus := true;
FDecoderClass := TM2100MessageDecoder;
FEncoderClass := TM2100MessageEncoder;
end;
function TM2100Switcher.GetAdditionalMessageLogTags(const aMessage: TCustomSwitcherMessage): string;
var
mssage: TM2100Message;
begin
result := '';
AssertType(aMessage, TM2100Message);
mssage := aMessage as TM2100Message;
if IsAutoStatPolling(mssage) then
AppendSpaced(result, mssage.PollingTag);
end;
function TM2100Switcher.IsAutoStatPolling(const aMessage: TM2100Message): boolean;
var
command: TM2100Command;
begin
result := aMessage.Commands.Count = 1;
if not result then
exit;
command := (aMessage.Commands[0] as TM2100Command);
result := result and (command.SubCommands.Count = 1);
if not result then
exit;
result := result
and (command.SubCommands[0] is TM2100SubCommandAutoStat)
or (command.SubCommands[0] is TM2100SubCommandAutoStatAnswer);
end;
function TM2100Switcher.ProcessMessage(const aMessage: TCustomSwitcherMessage)
: TCustomSwitcherMessage;
var
mssage: TM2100Message;
begin
AssertType(aMessage, TM2100Message);
mssage := aMessage as TM2100Message;
{$IFDEF LOG_MESSAGE_CONTENT_BEFORE_PROCESSING}
Log.Write(
SpacedStrings([ 'Processing', GetAdditionalMessageLogTags(aMessage) ]),
'Now processing message:..' + sLineBreak + ' ' + mssage.ToText
);
{$ENDIF}
if mssage.IsAcknowledged then
begin
result := nil;
exit;
end;
result := ProcessCommands(mssage);
end;
function TM2100Switcher.ProcessCommands(const aMessage: TM2100Message): TM2100Message;
var
i: integer;
command: TM2100Command;
answerCommand: TM2100Command;
begin
result := TM2100Message.Create;
result.STX := aMessage.STX;
for i := 0 to aMessage.Commands.Count - 1 do
begin
command := aMessage.Commands[i] as TM2100Command;
answerCommand := ProcessCommand(command);
if answerCommand <> nil then
result.Commands.Add(answerCommand);
end;
end;
function TM2100Switcher.ProcessCommand(const aCommand: TM2100Command): TM2100Command;
var
i: integer;
subCommand: TM2100SubCommand;
answerSubCommand: TM2100SubCommand;
begin
AssertAssigned(aCommand, 'aCommand', TVariableType.Argument);
{$IFDEF LOG_COMMAND_ON_COMMAND_PROCESSING}
Log.Write('Now processing command:..' + sLineBreak + ' ' + aCommand.ToText);
{$ENDIF}
result := nil;
for i := 0 to aCommand.SubCommands.Count - 1 do
begin
subCommand := aCommand.SubCommands[i] as TM2100SubCommand;
answerSubCommand := ProcessSubCommand(subCommand);
if answerSubCommand <> nil then
begin
if result = nil then
begin
result := TM2100Command.Create;
if aCommand.CommandClass = M2100MessageCommandClass_QUERY then
result.CommandClass := M2100MessageCommandClass_STATUS;
end;
result.SubCommands.Add(answerSubCommand);
end;
end;
if result = nil then
begin
if aCommand.CommandClass = M2100MessageCommandClass_CMD then
begin
result := TM2100Command.Create;
result.CommandClass := M2100MessageCommandClass_ACKNOWLEDGED;
end;
end;
end;
function TM2100Switcher.ProcessSubCommand(const aSubCommand: TM2100SubCommand): TM2100SubCommand;
begin
{$IFDEF LOG_SUBCOMMAND_ON_SUBCOMMAND_PROCESSING}
Log.Write('Now processing subcommand ' + aSubCommand.ToText);
{$ENDIF}
result := nil;
if aSubCommand is TM2100SubCommandKeyStat then
result := TM2100SubCommandKeyStatAnswer.Create(Keyers.Value);
if aSubCommand is TM2100SubCommandAutoStat then
result := TM2100SubCommandAutoStatAnswer.Create(true);
end;
procedure TM2100Switcher.DestroyThis;
begin
FreeAndNil(FKeyers);
end;
destructor TM2100Switcher.Destroy;
begin
DestroyThis;
inherited;
end;
initialization
RegisterSwitcherClass(TM2100Switcher);
end.