-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDlgConfigDevices.cpp
More file actions
282 lines (250 loc) · 7.17 KB
/
DlgConfigDevices.cpp
File metadata and controls
282 lines (250 loc) · 7.17 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
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
#include "stdafx.h"
#include "krs.h"
#include "DlgConfigDevices.h"
#include <direct.h>
#include <CHECK.h>
#include <SERV.h>
#include "DlgEditDevice.h"
#include "DlgEditSensor.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
DlgConfigDevices::DlgConfigDevices(CWnd* pParent /*=NULL*/)
: CDialog(DlgConfigDevices::IDD, pParent)
{
//{{AFX_DATA_INIT(DlgConfigDevices)
//}}AFX_DATA_INIT
}
void DlgConfigDevices::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(DlgConfigDevices)
DDX_Control(pDX, IDC_TREE_DEVICES, m_devices_tree);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(DlgConfigDevices, CDialog)
//{{AFX_MSG_MAP(DlgConfigDevices)
ON_NOTIFY(TVN_SELCHANGING, IDC_TREE_DEVICES, OnSelchangingTreeDevices)
ON_BN_CLICKED(IDC_BUTTON_ADD_DEVICE, OnButtonAddDevice)
ON_BN_CLICKED(IDC_BUTTON_ADD_SENSOR, OnButtonAddChannel)
ON_BN_CLICKED(IDC_SAVE_CONFIG, OnSaveConfig)
ON_BN_CLICKED(IDC_BUTTON_EDIT, OnButtonEdit)
ON_BN_CLICKED(IDC_LOAD_CONFIG, OnLoadConfig)
ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void DlgConfigDevices::ResetCfgName()
{
CString path = m_tmp_cfg->GetFileName();
CString name;
int pos = path.Find(KRS_root_path);
if (pos == 0)
{
name = path.Right(path.GetLength() - KRS_root_path.GetLength() - 1);
}
else
name = path;
SetDlgItemText(IDC_EDIT_CURRENT_CONFIG, name);
}
void DlgConfigDevices::RebuildTree()
{
ResetCfgName();
m_devices_tree.DeleteAllItems();
HTREEITEM device_item, channel_item;
SERV_Device* device;
SERV_Channel* channel;
SERV_ChannelsMap::iterator end_s, current_s;
SERV_DevicesMap::iterator current_d = m_tmp_cfg->GetDevicesBegin();
while (current_d != m_tmp_cfg->GetDevicesEnd())
{
device = current_d->second;
device_item = m_devices_tree.InsertItem(device->GetInfo(), 0, 0);
m_devices_tree.SetItemData(device_item, device->GetAddr());
current_s = device->GetChannelsBegin();
end_s = device->GetChannelsEnd();
while (current_s != end_s)
{
channel = current_s->second;
channel_item = m_devices_tree.InsertItem(channel->GetInfo(), 1, 1, device_item);
m_devices_tree.SetItemData(channel_item, channel->GetUniqueNumber());
current_s++;
}
//m_devices_tree.Expand(device_item, TVE_EXPAND);
current_d++;
}
}
BOOL DlgConfigDevices::OnInitDialog()
{
CDialog::OnInitDialog();
m_image_list.Create(16, 16, ILC_COLOR4, 0, 2);
m_bitmap_device.LoadBitmap(IDB_BITMAP_DEVICE);
m_bitmap_channel.LoadBitmap(IDB_BITMAP_CHANNEL);
m_image_list.Add(&m_bitmap_device, 0x01);
m_image_list.Add(&m_bitmap_channel, 0x01);
m_devices_tree.SetImageList(&m_image_list, TVSIL_NORMAL);
{
CHECK(SERV_current_device_cfg != NULL);
MutexWrap cfg_access(SERV_current_device_cfg_mutex);
m_tmp_cfg = SERV_current_device_cfg->MakeCopy();
}
RebuildTree();
return TRUE;
}
void DlgConfigDevices::OnCancel()
{
CHECK(m_tmp_cfg != NULL);
delete m_tmp_cfg;
CDialog::OnCancel();
}
void DlgConfigDevices::OnOK()
{
MutexWrap cfg_access(SERV_current_device_cfg_mutex);
CHECK(SERV_current_device_cfg != NULL);
CHECK(m_tmp_cfg != NULL);
SERV_DeviceCfg* prev_cfg = SERV_current_device_cfg;
SERV_current_device_cfg = m_tmp_cfg;
delete prev_cfg;
SERV_current_device_cfg->Save();
CDialog::OnOK();
}
void DlgConfigDevices::EnableEditDelete(BOOL enable)
{
GetDlgItem(IDC_BUTTON_EDIT)->EnableWindow(enable);
GetDlgItem(IDC_BUTTON_DELETE)->EnableWindow(enable);
}
void DlgConfigDevices::OnSelchangingTreeDevices(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
EnableEditDelete(TRUE);
*pResult = 0;
}
void DlgConfigDevices::OnButtonAddDevice()
{
SERV_Device* new_device = new SERV_Device("Óñòðîéñòâî", 0, 1000, m_tmp_cfg);
DlgEditDevice dlg(new_device);
if (dlg.DoModal() == IDCANCEL)
{
delete new_device;
return;
}
HTREEITEM device_item = m_devices_tree.InsertItem(new_device->GetInfo(), 0, 0);
m_devices_tree.SetItemData(device_item, new_device->GetAddr());
m_devices_tree.SelectItem(device_item);
}
void DlgConfigDevices::OnButtonAddChannel()
{
HTREEITEM device_item = m_devices_tree.GetSelectedItem();
if (device_item == 0)
{
MessageBox("Íå âûáðàíî óñòðîéñòâî", "Äîáàâëåíèå êàíàëà íåâîçìîæíî");
return;
}
DWORD data = m_devices_tree.GetItemData(device_item);
byte dev_addr;
if ((data & 0xFF00) != 0)
{
device_item = m_devices_tree.GetParentItem(device_item);
dev_addr = (BYTE)(data >> 8);
}
else
dev_addr = (BYTE)data;
SERV_Device* device = m_tmp_cfg->GetDevice(dev_addr);
CHECK(device != NULL);
byte offset = device->FindFreeOffset();
if (offset == 0xFF)
{
MessageBox("Óñòðîéñòâî íå èìååò ñâîáîäíûõ ñìåùåíèé äàííûõ", "Íåâîçìîæíî ñîçäàòü êàíàë");
return;
}
SERV_Channel* new_channel = new SERV_Channel("Êàíàë", device, offset);
DlgEditSensor dlg(new_channel);
if (dlg.DoModal() == IDCANCEL)
{
device->RemoveChannel(new_channel);
return;
}
HTREEITEM channel_item = m_devices_tree.InsertItem(new_channel->GetInfo(), 1, 1, device_item);
m_devices_tree.SetItemData(channel_item, new_channel->GetUniqueNumber());
m_devices_tree.SelectItem(channel_item);
}
void DlgConfigDevices::OnButtonEdit()
{
HTREEITEM node = m_devices_tree.GetSelectedItem();
WORD data = (WORD)(m_devices_tree.GetItemData(node));
if ((data & 0xFF00) != 0)
{
SERV_Channel* channel = m_tmp_cfg->GetChannel(data);
CHECK(channel != NULL);
DlgEditSensor dlg(channel);
if (dlg.DoModal() == IDOK)
{
m_devices_tree.SetItemText(node, channel->GetInfo());
m_devices_tree.SetItemData(node, channel->GetUniqueNumber());
}
}
else
{
SERV_Device* device = m_tmp_cfg->GetDevice((BYTE)(data));
byte prev_addr = device->GetAddr();
CHECK(device != NULL);
DlgEditDevice dlg(device);
if (dlg.DoModal() == IDOK)
{
if (prev_addr != device->GetAddr())
RebuildTree();
else
m_devices_tree.SetItemText(node, device->GetInfo());
}
}
}
void DlgConfigDevices::OnButtonDelete()
{
HTREEITEM node = m_devices_tree.GetSelectedItem();
DWORD data = m_devices_tree.GetItemData(node);
if ((data & 0xFF00) != 0)
{
SERV_Channel* channel = m_tmp_cfg->GetChannel(data);
CHECK(channel != NULL);
channel->GetDevice()->RemoveChannel(channel);
}
else
{
SERV_Device* device = m_tmp_cfg->GetDevice((BYTE)(data&0x000F));
CHECK(device != NULL);
delete device;
}
HTREEITEM next = m_devices_tree.GetNextItem(node, TVGN_NEXTVISIBLE);
if (next != NULL && next != node)
m_devices_tree.SelectItem(next);
else
{
EnableEditDelete(FALSE);
}
m_devices_tree.DeleteItem(node);
}
void DlgConfigDevices::OnLoadConfig()
{
CFileDialog dlg(TRUE, NULL, m_tmp_cfg->GetFileName(), OFN_HIDEREADONLY, "Ôàéëû êîíôèãóðàöèè(*.cfg)|*.cfg||");
if (dlg.DoModal() != IDOK)
return;
SERV_DeviceCfg* tmp2 = new SERV_DeviceCfg;
if (tmp2->Load(dlg.GetPathName()))
{
SERV_DeviceCfg* to_del = m_tmp_cfg;
m_tmp_cfg = tmp2;
delete to_del;
RebuildTree();
AfxGetApp()->WriteProfileString("DEVICES", "Config file name", dlg.GetPathName());
}
}
void DlgConfigDevices::OnSaveConfig()
{
CHECK(m_tmp_cfg != NULL);
CFileDialog dlg(FALSE, "cfg", m_tmp_cfg->GetFileName(), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Ôàéëû êîíôèãóðàöèè(*.cfg)|*.cfg||");
if (dlg.DoModal() != IDOK)
return;
m_tmp_cfg->Save(dlg.GetPathName());
ResetCfgName();
}