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