1 |
// ClientDlg.cpp : implementation file
|
2 |
//
|
3 |
|
4 |
#include "stdafx.h"
|
5 |
#include "Client.h"
|
6 |
#include "ClientDlg.h"
|
7 |
#include ".\clientdlg.h"
|
8 |
|
9 |
#ifdef _DEBUG
|
10 |
#define new DEBUG_NEW
|
11 |
#endif
|
12 |
|
13 |
//Globale variabler osv. :-S
|
14 |
CString Ip, Delay;
|
15 |
unsigned long Port;
|
16 |
UINT Status( LPVOID Param );
|
17 |
UINT Update( LPVOID Param );
|
18 |
void Buffcheck(CString);
|
19 |
void Start();
|
20 |
CButton m_led1;
|
21 |
CButton m_led2;
|
22 |
CButton m_led3;
|
23 |
CButton m_knap1;
|
24 |
CButton m_knap2;
|
25 |
CEdit m_temp;
|
26 |
CEdit m_pot;
|
27 |
|
28 |
// CAboutDlg dialog used for App About
|
29 |
|
30 |
bool continueThread;
|
31 |
|
32 |
class CAboutDlg : public CDialog
|
33 |
{
|
34 |
public:
|
35 |
CAboutDlg();
|
36 |
|
37 |
// Dialog Data
|
38 |
enum { IDD = IDD_ABOUTBOX };
|
39 |
|
40 |
protected:
|
41 |
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
|
42 |
|
43 |
// Implementation
|
44 |
protected:
|
45 |
DECLARE_MESSAGE_MAP()
|
46 |
};
|
47 |
|
48 |
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
|
49 |
{
|
50 |
}
|
51 |
|
52 |
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
|
53 |
{
|
54 |
CDialog::DoDataExchange(pDX);
|
55 |
}
|
56 |
|
57 |
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
|
58 |
END_MESSAGE_MAP()
|
59 |
|
60 |
|
61 |
// CClientDlg dialog
|
62 |
|
63 |
|
64 |
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
|
65 |
: CDialog(CClientDlg::IDD, pParent)
|
66 |
{
|
67 |
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
|
68 |
}
|
69 |
|
70 |
void CClientDlg::DoDataExchange(CDataExchange* pDX)
|
71 |
{
|
72 |
CDialog::DoDataExchange(pDX);
|
73 |
DDX_Control(pDX, IDC_LED1, m_led1);
|
74 |
DDX_Control(pDX, IDC_LED2, m_led2);
|
75 |
DDX_Control(pDX, IDC_LED3, m_led3);
|
76 |
DDX_Control(pDX, IDC_KNAP1, m_knap1);
|
77 |
DDX_Control(pDX, IDC_KNAP2, m_knap2);
|
78 |
DDX_Control(pDX, IDC_TEMP, m_temp);
|
79 |
DDX_Control(pDX, IDC_POT, m_pot);
|
80 |
DDX_Control(pDX, IDC_IPADDRESS1, m_ip);
|
81 |
DDX_Control(pDX, IDC_PORT, m_port);
|
82 |
DDX_Control(pDX, IDC_DELAY, m_delay);
|
83 |
}
|
84 |
|
85 |
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
|
86 |
ON_WM_SYSCOMMAND()
|
87 |
ON_WM_PAINT()
|
88 |
ON_WM_QUERYDRAGICON()
|
89 |
//}}AFX_MSG_MAP
|
90 |
ON_BN_CLICKED(IDC_IPPORT, OnBnClickedIpport)
|
91 |
ON_BN_CLICKED(IDC_LED1, OnBnClickedLed1)
|
92 |
ON_BN_CLICKED(IDC_LED2, OnBnClickedLed2)
|
93 |
ON_BN_CLICKED(IDC_LED3, OnBnClickedLed3)
|
94 |
ON_BN_CLICKED(IDOK, OnBnClickedOk)
|
95 |
ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
|
96 |
END_MESSAGE_MAP()
|
97 |
|
98 |
|
99 |
// CClientDlg message handlers
|
100 |
/*Client socket class*/
|
101 |
class ClientSocket : public CSocket{
|
102 |
public:
|
103 |
ClientSocket() { };
|
104 |
virtual void OnReceive(int nErrorCode);
|
105 |
};
|
106 |
BOOL CClientDlg::OnInitDialog()
|
107 |
{
|
108 |
CDialog::OnInitDialog();
|
109 |
|
110 |
// Add "About..." menu item to system menu.
|
111 |
|
112 |
// IDM_ABOUTBOX must be in the system command range.
|
113 |
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
|
114 |
ASSERT(IDM_ABOUTBOX < 0xF000);
|
115 |
|
116 |
CMenu* pSysMenu = GetSystemMenu(FALSE);
|
117 |
if (pSysMenu != NULL)
|
118 |
{
|
119 |
CString strAboutMenu;
|
120 |
strAboutMenu.LoadString(IDS_ABOUTBOX);
|
121 |
if (!strAboutMenu.IsEmpty())
|
122 |
{
|
123 |
pSysMenu->AppendMenu(MF_SEPARATOR);
|
124 |
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
|
125 |
}
|
126 |
}
|
127 |
|
128 |
// Set the icon for this dialog. The framework does this automatically
|
129 |
// when the application's main window is not a dialog
|
130 |
SetIcon(m_hIcon, TRUE); // Set big icon
|
131 |
SetIcon(m_hIcon, FALSE); // Set small icon
|
132 |
|
133 |
// TODO: Add extra initialization here
|
134 |
|
135 |
// Get data from regedit
|
136 |
CString strip, strport;
|
137 |
char chaip[16];
|
138 |
char chadelay[16];
|
139 |
CRegKey reg;
|
140 |
unsigned long size = 16;
|
141 |
LONG res=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
|
142 |
|
143 |
if (res == ERROR_SUCCESS)
|
144 |
{
|
145 |
res=reg.QueryStringValue("IP", chaip, &size);
|
146 |
}
|
147 |
|
148 |
|
149 |
Ip = chaip;
|
150 |
m_ip.SetWindowText(Ip);
|
151 |
LONG res2=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
|
152 |
|
153 |
if (res2 == ERROR_SUCCESS)
|
154 |
{
|
155 |
res2=reg.QueryDWORDValue("Port", Port);
|
156 |
}
|
157 |
strport.Format("%d",Port);
|
158 |
m_port.SetWindowText(strport);
|
159 |
|
160 |
LONG res3=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
|
161 |
|
162 |
if (res3 == ERROR_SUCCESS)
|
163 |
{
|
164 |
res3=reg.QueryStringValue("Delay",chadelay,&size);
|
165 |
}
|
166 |
Delay = chadelay;
|
167 |
m_delay.SetWindowText(Delay);
|
168 |
reg.Close();
|
169 |
|
170 |
//connect til server
|
171 |
Start();
|
172 |
continueThread = true;
|
173 |
AfxBeginThread(Status,0,THREAD_PRIORITY_NORMAL,0,0,NULL);
|
174 |
|
175 |
|
176 |
return TRUE; // return TRUE unless you set the focus to a control
|
177 |
}
|
178 |
|
179 |
void CClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
|
180 |
{
|
181 |
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
|
182 |
{
|
183 |
CAboutDlg dlgAbout;
|
184 |
dlgAbout.DoModal();
|
185 |
}
|
186 |
else
|
187 |
{
|
188 |
CDialog::OnSysCommand(nID, lParam);
|
189 |
}
|
190 |
}
|
191 |
|
192 |
// If you add a minimize button to your dialog, you will need the code below
|
193 |
// to draw the icon. For MFC applications using the document/view model,
|
194 |
// this is automatically done for you by the framework.
|
195 |
|
196 |
void CClientDlg::OnPaint()
|
197 |
{
|
198 |
if (IsIconic())
|
199 |
{
|
200 |
CPaintDC dc(this); // device context for painting
|
201 |
|
202 |
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
|
203 |
|
204 |
// Center icon in client rectangle
|
205 |
int cxIcon = GetSystemMetrics(SM_CXICON);
|
206 |
int cyIcon = GetSystemMetrics(SM_CYICON);
|
207 |
CRect rect;
|
208 |
GetClientRect(&rect);
|
209 |
int x = (rect.Width() - cxIcon + 1) / 2;
|
210 |
int y = (rect.Height() - cyIcon + 1) / 2;
|
211 |
|
212 |
// Draw the icon
|
213 |
dc.DrawIcon(x, y, m_hIcon);
|
214 |
}
|
215 |
else
|
216 |
{
|
217 |
CDialog::OnPaint();
|
218 |
}
|
219 |
}
|
220 |
|
221 |
// The system calls this function to obtain the cursor to display while the user drags
|
222 |
// the minimized window.
|
223 |
HCURSOR CClientDlg::OnQueryDragIcon()
|
224 |
{
|
225 |
return static_cast<HCURSOR>(m_hIcon);
|
226 |
}
|
227 |
|
228 |
|
229 |
ClientSocket sockClient;
|
230 |
CString strIp, strPort, buff;
|
231 |
|
232 |
/*Clientsocket class used for client socket connections*/
|
233 |
void ClientSocket::OnReceive( int nErrorCode )
|
234 |
{
|
235 |
char buffer[4096];
|
236 |
int size;
|
237 |
|
238 |
size = Receive(buffer,4095);
|
239 |
buffer[size] = 0;
|
240 |
buff = buffer;
|
241 |
|
242 |
OutputDebugString(buff + "\n");
|
243 |
|
244 |
while (1) //homebrewed tokenizer
|
245 |
{
|
246 |
int pos = buff.Find('|',0);
|
247 |
if (pos != -1)
|
248 |
{
|
249 |
CString token = buff.Left(pos);
|
250 |
buff = buff.Right( buff.GetLength() - pos -1);
|
251 |
Buffcheck(token);
|
252 |
}
|
253 |
else
|
254 |
break;
|
255 |
}
|
256 |
}
|
257 |
void CClientDlg::OnBnClickedIpport()
|
258 |
{
|
259 |
//Set new port and ip for the server in regedit
|
260 |
m_ip.GetWindowText(strIp);
|
261 |
m_port.GetWindowText(strPort);
|
262 |
m_delay.GetWindowText(Delay);
|
263 |
int test;
|
264 |
test = atoi(Delay);
|
265 |
if(test < 10000 && test > 500)
|
266 |
{
|
267 |
CRegKey reg;
|
268 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
|
269 |
reg.SetStringValue("","Project-data");
|
270 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
|
271 |
reg.SetStringValue("IP",strIp);
|
272 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
|
273 |
reg.SetDWORDValue("Port",atoi(strPort));
|
274 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
|
275 |
reg.SetStringValue("Delay",Delay);
|
276 |
reg.Close();
|
277 |
}
|
278 |
else
|
279 |
{
|
280 |
MessageBox("fejl i delay",0,MB_OK);
|
281 |
}
|
282 |
}
|
283 |
|
284 |
void Start()
|
285 |
{
|
286 |
sockClient.Create();
|
287 |
if (sockClient.Connect(Ip,Port))
|
288 |
{
|
289 |
|
290 |
}
|
291 |
else
|
292 |
{
|
293 |
int error = GetLastError();
|
294 |
CString tmp;
|
295 |
tmp.Format("%d", error);
|
296 |
}
|
297 |
}
|
298 |
UINT Status( LPVOID Param )
|
299 |
{
|
300 |
while(continueThread)
|
301 |
{
|
302 |
CString status;
|
303 |
int statusint = 100;
|
304 |
status.Format("%d",statusint);
|
305 |
sockClient.Send(status,status.GetLength(),NULL);
|
306 |
Sleep(atoi(Delay));
|
307 |
}
|
308 |
return TRUE;
|
309 |
}
|
310 |
void Buffcheck(CString data)
|
311 |
{
|
312 |
int intbuff=0, temp, pot;
|
313 |
intbuff = atoi(data);
|
314 |
|
315 |
temp = intbuff; // til temperatur case
|
316 |
pot = intbuff; // til potmeter case
|
317 |
switch (intbuff)
|
318 |
{
|
319 |
case 110: /*Led 1 Slukket*/
|
320 |
{
|
321 |
m_led1.SetCheck(0);
|
322 |
break;
|
323 |
}
|
324 |
case 111: /*Led 1 Tændt*/
|
325 |
{
|
326 |
m_led1.SetCheck(1);
|
327 |
break;
|
328 |
}
|
329 |
case 120: /*Led 2 Slukket*/
|
330 |
{
|
331 |
m_led2.SetCheck(0);
|
332 |
break;
|
333 |
}
|
334 |
case 121: /*Led 2 Tændt*/
|
335 |
{
|
336 |
m_led2.SetCheck(1);
|
337 |
break;
|
338 |
}
|
339 |
case 130: /*Led 3 Slukket*/
|
340 |
{
|
341 |
m_led3.SetCheck(0);
|
342 |
break;
|
343 |
}
|
344 |
case 131: /*Led 3 Tændt*/
|
345 |
{
|
346 |
m_led3.SetCheck(1);
|
347 |
break;
|
348 |
}
|
349 |
case 140: /*Knap 1 off*/
|
350 |
{
|
351 |
m_knap1.SetCheck(0);
|
352 |
break;
|
353 |
}
|
354 |
case 141: /*Knap 1 on*/
|
355 |
{
|
356 |
m_knap1.SetCheck(1);
|
357 |
break;
|
358 |
}
|
359 |
case 150: /*Knap 2 off*/
|
360 |
{
|
361 |
m_knap2.SetCheck(0);
|
362 |
break;
|
363 |
}
|
364 |
case 151: /*Knap 2 on*/
|
365 |
{
|
366 |
m_knap2.SetCheck(1);
|
367 |
break;
|
368 |
}
|
369 |
}
|
370 |
if (temp > 3999 && temp < 5000) //skriv temperaturen
|
371 |
{
|
372 |
CString strtemp;
|
373 |
temp = (temp-4000);
|
374 |
strtemp.Format("%d",temp);
|
375 |
m_temp.SetWindowText(strtemp);
|
376 |
}
|
377 |
if (pot > 1999 && pot < 3050) //skriv potmeter værdien
|
378 |
{
|
379 |
CString strpot;
|
380 |
pot = (pot-2000);
|
381 |
strpot.Format("%d",pot);
|
382 |
m_pot.SetWindowText(strpot);
|
383 |
}
|
384 |
|
385 |
}
|
386 |
void CClientDlg::OnBnClickedLed1()
|
387 |
{
|
388 |
CString Updatevariable;
|
389 |
int led, code, send;
|
390 |
led = 110;
|
391 |
code = m_led1.GetCheck();
|
392 |
send = (led+code);
|
393 |
Updatevariable.Format("%d",send);
|
394 |
sockClient.Send(Updatevariable,Updatevariable.GetLength(),NULL);
|
395 |
}
|
396 |
|
397 |
void CClientDlg::OnBnClickedLed2()
|
398 |
{
|
399 |
CString Updatevariable;
|
400 |
int led, code, send;
|
401 |
led = 120;
|
402 |
code = m_led2.GetCheck();
|
403 |
send = (led+code);
|
404 |
Updatevariable.Format("%d",send);
|
405 |
sockClient.Send(Updatevariable,Updatevariable.GetLength(),NULL);
|
406 |
}
|
407 |
|
408 |
void CClientDlg::OnBnClickedLed3()
|
409 |
{
|
410 |
CString Updatevariable;
|
411 |
int led, code, send;
|
412 |
led = 130;
|
413 |
code = m_led3.GetCheck();
|
414 |
send = (led+code);
|
415 |
Updatevariable.Format("%d",send);
|
416 |
sockClient.Send(Updatevariable,Updatevariable.GetLength(),NULL);
|
417 |
}
|
418 |
|
419 |
void CClientDlg::OnBnClickedOk()
|
420 |
{
|
421 |
sockClient.Close();
|
422 |
continueThread = false;
|
423 |
OnOK();
|
424 |
}
|
425 |
|
426 |
void CClientDlg::OnBnClickedCancel()
|
427 |
{
|
428 |
sockClient.Close();
|
429 |
continueThread = false;
|
430 |
OnCancel();
|
431 |
}
|