/[H7]/trunk/Client/ClientDlg.cpp
ViewVC logotype

Contents of /trunk/Client/ClientDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (show annotations) (download)
Wed Jan 31 17:05:15 2007 UTC (17 years, 3 months ago) by kevin
File size: 9307 byte(s)
baah nu burde den spille efter torben har fixet. :D
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, Updatevariable;
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 //Buffcheck(buff);
258 //Close();
259 }
260 void CClientDlg::OnBnClickedIpport()
261 {
262 //Set new port and ip for the server in regedit
263 m_ip.GetWindowText(strIp);
264 m_port.GetWindowText(strPort);
265 m_delay.GetWindowText(Delay);
266 int test;
267 test = atoi(Delay);
268 if(test < 10000 && test > 100)
269 {
270 CRegKey reg;
271 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
272 reg.SetStringValue("","Project-data");
273 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
274 reg.SetStringValue("IP",strIp);
275 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
276 reg.SetDWORDValue("Port",atoi(strPort));
277 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
278 reg.SetStringValue("Delay",Delay);
279 reg.Close();
280 }
281 else
282 {
283 MessageBox("fejl i delay",0,MB_OK);
284 }
285 }
286
287 void Start()
288 {
289 sockClient.Create();
290 if (sockClient.Connect(Ip,Port))
291 {
292
293 }
294 else
295 {
296 int error = GetLastError();
297 CString tmp;
298 tmp.Format("%d", error);
299 }
300 }
301 UINT Status( LPVOID Param )
302 {
303 while(continueThread)
304 {
305 CString status;
306 int statusint = 100;
307 status.Format("%d",statusint);
308 sockClient.Send(status,status.GetLength(),NULL);
309 Sleep(atoi(Delay));
310 }
311 return TRUE;
312 }
313 UINT Update( LPVOID param )
314 {
315 sockClient.Send(Updatevariable,Updatevariable.GetLength(),NULL);
316 return TRUE;
317 }
318 void Buffcheck(CString data)
319 {
320 int intbuff=0, temp, pot;
321 intbuff = atoi(data);
322
323 temp = intbuff; // til temperatur case
324 pot = intbuff; // til potmeter case
325 switch (intbuff)
326 {
327 case 110: /*Led 1 Slukket*/
328 {
329 m_led1.SetCheck(0);
330 break;
331 }
332 case 111: /*Led 1 Tændt*/
333 {
334 m_led1.SetCheck(1);
335 break;
336 }
337 case 120: /*Led 2 Slukket*/
338 {
339 m_led2.SetCheck(0);
340 break;
341 }
342 case 121: /*Led 2 Tændt*/
343 {
344 m_led2.SetCheck(1);
345 break;
346 }
347 case 130: /*Led 3 Slukket*/
348 {
349 m_led3.SetCheck(0);
350 break;
351 }
352 case 131: /*Led 3 Tændt*/
353 {
354 m_led3.SetCheck(1);
355 break;
356 }
357 case 140: /*Knap 1 off*/
358 {
359 m_knap1.SetCheck(0);
360 break;
361 }
362 case 141: /*Knap 1 on*/
363 {
364 m_knap1.SetCheck(1);
365 break;
366 }
367 case 150: /*Knap 2 off*/
368 {
369 m_knap2.SetCheck(0);
370 break;
371 }
372 case 151: /*Knap 2 on*/
373 {
374 m_knap2.SetCheck(1);
375 break;
376 }
377 }
378 if (temp > 3999 && temp < 5000) //skriv temperaturen
379 {
380 CString strtemp;
381 temp = (temp-4000);
382 strtemp.Format("%d",temp);
383 m_temp.SetWindowText(strtemp);
384 }
385 if (pot > 1999 && pot < 3050) //skriv potmeter værdien
386 {
387 CString strpot;
388 pot = (pot-2000);
389 strpot.Format("%d",pot);
390 m_pot.SetWindowText(strpot);
391 }
392
393 }
394 void CClientDlg::OnBnClickedLed1()
395 {
396 int led, code, send;
397 led = 110;
398 code = m_led1.GetCheck();
399 send = (led+code);
400 Updatevariable.Format("%d",send);
401 AfxBeginThread(Update,0,THREAD_PRIORITY_NORMAL,0,0,NULL);
402 }
403
404 void CClientDlg::OnBnClickedLed2()
405 {
406 int led, code, send;
407 led = 120;
408 code = m_led2.GetCheck();
409 send = (led+code);
410 Updatevariable.Format("%d",send);
411 AfxBeginThread(Update,0,THREAD_PRIORITY_NORMAL,0,0,NULL);
412 }
413
414 void CClientDlg::OnBnClickedLed3()
415 {
416 int led, code, send;
417 led = 130;
418 code = m_led3.GetCheck();
419 send = (led+code);
420 Updatevariable.Format("%d",send);
421 AfxBeginThread(Update,0,THREAD_PRIORITY_NORMAL,0,0,NULL);
422 }
423
424 void CClientDlg::OnBnClickedOk()
425 {
426 sockClient.Close();
427 continueThread = false;
428 OnOK();
429 }
430
431 void CClientDlg::OnBnClickedCancel()
432 {
433 sockClient.Close();
434 continueThread = false;
435 OnCancel();
436 }

  ViewVC Help
Powered by ViewVC 1.1.20