-
Notifications
You must be signed in to change notification settings - Fork 5
/
MQTTServerTest.lpr
191 lines (166 loc) · 5.15 KB
/
MQTTServerTest.lpr
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
program MQTTServerTest;
{$mode objfpc}{$H+}
{$define use_tftp}
uses
RaspberryPi3,
GlobalConfig,
GlobalConst,
GlobalTypes,
Platform,
Threads,
SysUtils,
Classes, Console,
Ultibo, uMQTTServer, winsock2, uLog, uTFTP, uMQTT
{ Add additional units here };
type
{ THelper }
THelper = class
i : integer;
procedure MQCheckUser (Sender : TObject; aUser, aPass : UTF8String; var Allowed : Boolean);
procedure MQObituary (Sender : TObject; var aTopic, aMessage : UTF8String; var aQos : TMQTTQOSType);
procedure MQSubscription (Sender : TObject; aTopic : UTF8String; var RequestedQos : TMQTTQOSType);
procedure MQCMsg (Sender : TObject; aTopic : UTF8String; aMessage : AnsiString; aQos : TMQTTQOSType; aRetained : boolean);
end;
var
Console1, Console2, Console3 : TWindowHandle;
{$ifdef use_tftp}
IPAddress : string;
{$endif}
ch : char;
MQ : TMQTTServer;
MQC : TMQTTClient;
Helper : THelper;
MQT : TMQTTThread;
procedure Log1 (s : string);
begin
ConsoleWindowWriteLn (Console1, s);
end;
procedure Log2 (s : string);
begin
ConsoleWindowWriteLn (Console2, s);
end;
procedure Log3 (s : string);
begin
ConsoleWindowWriteLn (Console3, s);
end;
procedure Msg2 (Sender : TObject; s : string);
begin
Log2 ('TFTP : ' + s);
end;
{$ifdef use_tftp}
function WaitForIPComplete : string;
var
TCP : TWinsock2TCPClient;
begin
TCP := TWinsock2TCPClient.Create;
Result := TCP.LocalAddress;
if (Result = '') or (Result = '0.0.0.0') or (Result = '255.255.255.255') then
begin
while (Result = '') or (Result = '0.0.0.0') or (Result = '255.255.255.255') do
begin
sleep (1000);
Result := TCP.LocalAddress;
end;
end;
TCP.Free;
end;
{$endif}
procedure WaitForSDDrive;
begin
while not DirectoryExists ('C:\') do sleep (500);
end;
{ THelper }
procedure THelper.MQCheckUser (Sender: TObject; aUser, aPass: UTF8String;
var Allowed: Boolean);
begin
Log (' Check User ' + aUser + ' pass ' + aPass);
Allowed := true;
end;
procedure THelper.MQObituary (Sender : TObject; var aTopic, aMessage : UTF8String; var aQos : TMQTTQOSType);
begin
Log (' Obituary ' + aTopic + ' ' + aMessage + ' @ ' + QOSNames[aQos]);
end;
procedure THelper.MQSubscription (Sender : TObject; aTopic : UTF8String; var RequestedQos : TMQTTQOSType);
begin
Log (' Subscription "' + aTopic + '" @ ' + QOSNames[RequestedQos]);
end;
procedure THelper.MQCMsg (Sender : TObject; aTopic : UTF8String; aMessage : AnsiString; aQos : TMQTTQOSType; aRetained : boolean);
var
x : integer;
l : integer;
begin
Log (' Topic "' + aTopic + '" @ ' + QOSNames[aQos] + ' Retained ' + ny[aRetained]);
if aTopic = 'update/memo' then
begin
x := 1;
while x + 1 <= length (aMessage) do
begin
l := ord (aMessage[x]) * $100 + ord (aMessage[x + 1]);
if x + l + 1 <= length (aMessage) then
Log3 (Copy (aMessage, x + 2, l));
x := x + l + 2;
end;
end;
end;
begin
Console1 := ConsoleWindowCreate (ConsoleDeviceGetDefault, CONSOLE_POSITION_LEFT, true);
Console2 := ConsoleWindowCreate (ConsoleDeviceGetDefault, CONSOLE_POSITION_TOPRIGHT, false);
Console3 := ConsoleWindowCreate (ConsoleDeviceGetDefault, CONSOLE_POSITION_BOTTOMRIGHT, false);
SetLogProc (@Log1);
Log3 ('MQTT Client & Server Tester.');
Log3 ('');
WaitForSDDrive;
{$ifdef use_tftp}
IPAddress := WaitForIPComplete;
Log2 ('TFTP : Usage tftp -i ' + IPAddress + ' put kernel7.img');
SetOnMsg (@Msg2);
Log2 ('');
{$endif}
MQ := TMQTTServer.Create;
Helper := THelper.Create;
Helper.i := 0; // suppress note
MQ.OnCheckUser := @Helper.MQCheckUser;
MQ.OnObituary := @Helper.MQObituary;
MQ.OnSubscription := @Helper.MQSubscription;
MQC := TMQTTClient.Create;
MQC.OnMsg := @Helper.MQCMsg;
ch := #0;
while true do
begin
if ConsoleGetKey (ch, nil) then
case (UpperCase (ch)) of
'1' : MQ.SetTimer (1, 8000, true);
'2' : MQ.SetTimer (2, 1000, false);
'3' : MQ.KillTimer (1);
'4' : MQ.KillTimer (2);
'5' : MQ.Activate (true);
'6' : MQ.Activate (false);
'7' :
begin
MQC.Host := '10.0.0.4';
MQC.Username := 'user';
MQC.Password := 'pass';
MQC.LocalBounce := false;
MQC.Activate (true);
end;
'8' : MQC.Activate (false);
'9' :
begin
MQC.Subscribe ('update/memo', qtEXACTLY_ONCE);
MQC.Subscribe ('update/png/+', qtEXACTLY_ONCE);
MQC.Subscribe ('will/#', qtEXACTLY_ONCE);
end;
'0' : MQC.Publish ('update/memo', #0#11'hello there', qtEXACTLY_ONCE, false);
'Q' :
begin
MQT := TMQTTThread (MQ.Threads.First);
while (MQT <> nil) do
begin
MQT.Server.Disconnect;
MQT := TMQTTThread (MQT.Next);
end;
end;
end;
end;
ThreadHalt (0);
end.