-
Notifications
You must be signed in to change notification settings - Fork 0
/
XBeeCoordinator.pas
executable file
·278 lines (243 loc) · 8.22 KB
/
XBeeCoordinator.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
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
unit XBeeCoordinator;
interface
uses
SysUtils, Classes, Controls, Forms, Dialogs, StdCtrls, ComCtrls, ExtCtrls,
HeliumDataMgmt, DatabaseEdit;
type
TCoordinatorForm = class(TForm)
cooCoBox: TComboBox;
cooPanel: TPanel;
NameLab: TLabel;
NameEd: TOraEdit;
IpLab: TLabel;
IpEd: TOraEdit;
AddressLab: TLabel;
AddressEd: TOraEdit;
PositionLab: TLabel;
PositionEd: TOraEdit;
CommentRiEd: TRichEdit;
CommentLab: TLabel;
PortCoBox: TComboBox;
PortLab: TLabel;
cooLab: TLabel;
SaveBtn: TButton;
DeleteBtn: TButton;
HintLab: TLabel;
ActiveLab: TLabel;
CancelBtn: TButton;
procedure cooCoBoxChange(Sender: TObject);
procedure SaveBtnClick(Sender: TObject);
procedure DeleteBtnClick(Sender: TObject);
procedure PortCoBoxChange(Sender: TObject);
private
FDataMgmt: TDataMgmt;
FPortList: TStringList;
procedure InitForm;
function FillCoBox(APort: string): TStringList;
public
constructor CreateWithProperties(ADataMgmt: TDataMgmt; AOwner: TComponent);
destructor Destroy; override;
end;
implementation
uses HeliumFunctions;
{$R *.dfm}
constructor TCoordinatorForm.CreateWithProperties(ADataMgmt: TDataMgmt; AOwner: TComponent);
begin
inherited Create(AOwner);
HintLab.Caption := '';
FDataMgmt := ADataMgmt;
FPortList := TStringList.Create;
InitForm;
end;
destructor TCoordinatorForm.Destroy;
begin
FPortList.Free;
inherited Destroy;
end;
procedure TCoordinatorForm.InitForm;
var i: integer;
begin
FPortList.Clear;
PortCoBox.Clear;
CooCoBox.Clear;
CooCoBox.AddItem('- CREATE NEW COORDINATOR -', nil);
with FDataMgmt do
try
LockWhileWorkingWithList;
RefreshCooListByDB;
for i := 0 to CoordinatorList.Count - 1 do
begin
FPortList.Add( TCoordinator(CoordinatorList[i]).coo_comport);
CooCoBox.AddItem(TCoordinator(CoordinatorList[i]).coo_name,
TObject(TCoordinator(CoordinatorList[i]).coo_id));
end;
finally
UnlockAfterWorkingWithList;
end;
ActiveLab.Caption := '';
NameEd.Text := '';
IpEd.Text := '';
AddressEd.Text := '';
PositionEd.Text := '';
CommentRiEd.Text := '';
IpEd.Enabled := true;
AddressEd.Enabled := true;
PortCoBox.Enabled := true;
DeleteBtn.Enabled := true;
CooCoBox.ItemIndex := 0;
ActiveControl := CooCoBox;
cooCoBoxChange(nil);
end;
function TCoordinatorForm.FillCoBox(APort: string): TStringList;
var i: integer;
begin
Result := GetPortList();
for i := Result.Count - 1 downto 0 do
if (Result[i] <> APort) and ((FPortList.IndexOf(Result[i])) > -1)
then Result.Delete(i);
end;
procedure TCoordinatorForm.cooCoBoxChange(Sender: TObject);
var i: integer;
LCoo: TCoordinator;
begin
if Assigned(Sender) then HintLab.Caption := '';
PortCoBox.Clear;
IpEd.Enabled := true;
AddressEd.Enabled := true;
PortCoBox.Enabled := true;
DeleteBtn.Enabled := true;
if (cooCoBox.ItemIndex = 0) then
begin
ActiveLab.Caption := 'Enter values for the new coordinator.';
NameEd.Text := '';
IpEd.Text := '';
AddressEd.Text := '';
PositionEd.Text := '';
CommentRiEd.Text := '';
PortCoBox.Items := FillCoBox('');
end;
if (cooCoBox.ItemIndex > 0) then
begin
with FDataMgmt, cooCoBox do
try
LockWhileWorkingWithList;
LCoo := GetCopyOfCoordinator(GetCooIndex(integer(Items.Objects[ItemIndex])));
finally
UnlockAfterWorkingWithList;
end;
if Assigned(LCoo) then
begin
if (LCoo.coo_status = 0) then
begin
ActiveLab.Caption := '';
end
else begin
IpEd.Enabled := false;
AddressEd.Enabled := false;
PortCoBox.Enabled := false;
DeleteBtn.Enabled := false;
ActiveLab.Caption := cooCoBox.Text + ' is currently active!';
end;
NameEd.Text := LCoo.coo_name;
IpEd.Text := LCoo.coo_ip;
AddressEd.Text := LCoo.coo_address;
PositionEd.Text := LCoo.coo_position;
CommentRiEd.Text := LCoo.coo_comment;
PortCoBox.Items := FillCoBox(LCoo.coo_comport);
for i := 0 to PortCoBox.Items.Count - 1 do
if (LCoo.coo_comport = PortCoBox.Items[i]) then
begin
PortCoBox.ItemIndex := i;
break;
end;
if (PortCoBox.ItemIndex = -1)
then HintLab.Caption := 'Port ' + LCoo.coo_comport + ' is not installed!';
end
else InitForm;
end;
end;
procedure TCoordinatorForm.PortCoBoxChange(Sender: TObject);
begin
HintLab.Caption := '';
end;
procedure TCoordinatorForm.SaveBtnClick(Sender: TObject);
var LCoo: TCoordinator;
begin
LCoo := nil;
if (cooCoBox.ItemIndex <> -1) then
if (trim(NameEd.Text) <> '') then
if (PortCoBox.ItemIndex <> -1) then
if (trim(IpEd.Text) <> '') then
if (trim(AddressEd.Text) <> '') then
if (trim(PositionEd.Text) <> '') then
begin
if (cooCoBox.ItemIndex = 0) then
if (MessageDlg('Do you really want to save the new coordinator?', mtCustom, [mbYes, mbNo], 0) = mrYes) then
with FDataMgmt do
try
LockWhileWorkingWithList;
LCoo := TCoordinator.Create;
LCoo.coo_id := 0;
LCoo.coo_name := NameEd.Text;
LCoo.coo_comport := PortCoBox.Text;
LCoo.coo_ip := IpEd.Text;
LCoo.coo_address := AddressEd.Text;
LCoo.coo_position := PositionEd.Text;
LCoo.coo_comment := CommentRiEd.Text;
if InsertCoordinatorToDb(LCoo)
then HintLab.Caption := 'Creating new coordinator succeeded.'
else HintLab.Caption := 'Creating new coordinator failed.';
finally
UnlockAfterWorkingWithList;
InitForm;
end;
if (cooCoBox.ItemIndex > 0) then
if (MessageDlg('Do you really want to change the coordinator settings?', mtCustom, [mbYes, mbNo], 0) = mrYes) then
with FDataMgmt, cooCoBox do
try
LockWhileWorkingWithList;
LCoo := GetCooItem(integer(Items.Objects[ItemIndex]));
if Assigned(LCoo) then
begin
LCoo.coo_name := NameEd.Text;
LCoo.coo_comport := PortCoBox.Text;
LCoo.coo_ip := IpEd.Text;
LCoo.coo_address := AddressEd.Text;
LCoo.coo_position := PositionEd.Text;
LCoo.coo_comment := CommentRiEd.Text;
if UpdateCoordinatorToDb(LCoo)
then HintLab.Caption := 'Update on ' + cooCoBox.Text + ' succeeded.'
else HintLab.Caption := 'Update on ' + cooCoBox.Text + ' failed.';
end
else HintLab.Caption := 'Update on ' + cooCoBox.Text + ' failed.';
finally
LCoo := nil;
UnlockAfterWorkingWithList;
InitForm;
end;
end
else showMessage('Please enter the position of the coordinator!')
else showMessage('Please enter the XBee address of the coordinator!')
else showMessage('Please enter the IP address of the coordinator (netnode)!')
else showMessage('Please select a virtual COM port for the coordinator!')
else showMessage('Please enter a name (netnode) for the coordinator!');
if Assigned(LCoo) then LCoo.Free;
end;
procedure TCoordinatorForm.DeleteBtnClick(Sender: TObject);
begin
if (cooCoBox.ItemIndex > 0) then
begin
if (MessageDlg('Do you really want to delete ' + cooCoBox.Text + '?', mtCustom, [mbYes, mbNo], 0) = mrYes) then
with FDataMgmt, cooCoBox do
try
LockWhileWorkingWithList;
if DeleteCoordinatorToDb(integer(Items.Objects[ItemIndex]))
then HintLab.Caption := cooCoBox.Text + ' successfully deleted.';
finally
UnlockAfterWorkingWithList;
InitForm;
end;
end
else showMessage('Select a coordinator to delete!');
end;
end.