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

Diff of /trunk/Client/ClientDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4 by kevin, Mon Jan 29 10:49:43 2007 UTC revision 32 by kevin, Wed Jan 31 17:05:15 2007 UTC
# Line 10  Line 10 
10  #define new DEBUG_NEW  #define new DEBUG_NEW
11  #endif  #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  // CAboutDlg dialog used for App About
29    
30    bool continueThread;
31    
32  class CAboutDlg : public CDialog  class CAboutDlg : public CDialog
33  {  {
34  public:  public:
# Line 45  END_MESSAGE_MAP() Line 61  END_MESSAGE_MAP()
61  // CClientDlg dialog  // CClientDlg dialog
62    
63    
   
64  CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)  CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
65          : CDialog(CClientDlg::IDD, pParent)          : CDialog(CClientDlg::IDD, pParent)
66  {  {
# Line 64  void CClientDlg::DoDataExchange(CDataExc Line 79  void CClientDlg::DoDataExchange(CDataExc
79          DDX_Control(pDX, IDC_POT, m_pot);          DDX_Control(pDX, IDC_POT, m_pot);
80          DDX_Control(pDX, IDC_IPADDRESS1, m_ip);          DDX_Control(pDX, IDC_IPADDRESS1, m_ip);
81          DDX_Control(pDX, IDC_PORT, m_port);          DDX_Control(pDX, IDC_PORT, m_port);
82            DDX_Control(pDX, IDC_DELAY, m_delay);
83  }  }
84    
85  BEGIN_MESSAGE_MAP(CClientDlg, CDialog)  BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
# Line 72  BEGIN_MESSAGE_MAP(CClientDlg, CDialog) Line 88  BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
88          ON_WM_QUERYDRAGICON()          ON_WM_QUERYDRAGICON()
89          //}}AFX_MSG_MAP          //}}AFX_MSG_MAP
90          ON_BN_CLICKED(IDC_IPPORT, OnBnClickedIpport)          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()  END_MESSAGE_MAP()
97    
98    
99  // CClientDlg message handlers  // 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()  BOOL CClientDlg::OnInitDialog()
107  {  {
108          CDialog::OnInitDialog();          CDialog::OnInitDialog();
# Line 105  BOOL CClientDlg::OnInitDialog() Line 131  BOOL CClientDlg::OnInitDialog()
131          SetIcon(m_hIcon, FALSE);                // Set small icon          SetIcon(m_hIcon, FALSE);                // Set small icon
132    
133          // TODO: Add extra initialization here          // TODO: Add extra initialization here
134            
135            // Get data from regedit
136          CString strip, strport;          CString strip, strport;
137          char Ip[16];          char chaip[16];
138          char Port[6];          char chadelay[16];
139          CRegKey reg;          CRegKey reg;
140          unsigned long size = 16;          unsigned long size = 16;
141          LONG res=reg.Open(HKEY_CLASSES_ROOT,"Project",KEY_READ);          LONG res=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
142    
143          if (res == ERROR_SUCCESS)          if (res == ERROR_SUCCESS)
144          {          {
145                  res=reg.QueryStringValue("IP", Ip,&size);                  res=reg.QueryStringValue("IP", chaip, &size);
146          }          }
147    
148            
149            Ip = chaip;
150          m_ip.SetWindowText(Ip);          m_ip.SetWindowText(Ip);
151          LONG res2=reg.Open(HKEY_CLASSES_ROOT,"Project",KEY_READ);          LONG res2=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
152    
153          if (res2 == ERROR_SUCCESS)          if (res2 == ERROR_SUCCESS)
154          {          {
155                  res2=reg.QueryStringValue("Port", Port,&size);                  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          m_port.SetWindowText(Port);          Delay = chadelay;
167            m_delay.SetWindowText(Delay);
168          reg.Close();          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          return TRUE;  // return TRUE  unless you set the focus to a control
# Line 182  HCURSOR CClientDlg::OnQueryDragIcon() Line 226  HCURSOR CClientDlg::OnQueryDragIcon()
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()  void CClientDlg::OnBnClickedIpport()
261  {  {
262          CString Ip, Port;          //Set new port and ip for the server in regedit
263          m_ip.GetWindowText(Ip);          m_ip.GetWindowText(strIp);
264          m_port.GetWindowText(Port);          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;          CRegKey reg;
271          reg.Create(HKEY_CLASSES_ROOT, "Project");          reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
272          reg.SetStringValue("","Project-data");          reg.SetStringValue("","Project-data");
273          reg.Create(HKEY_CLASSES_ROOT, "Project");          reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
274          reg.SetStringValue("IP",Ip);          reg.SetStringValue("IP",strIp);
275          reg.Create(HKEY_CLASSES_ROOT, "Project");          reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
276          reg.SetStringValue("Port",Port);          reg.SetDWORDValue("Port",atoi(strPort));
277            reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
278            reg.SetStringValue("Delay",Delay);
279          reg.Close();          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  }  }

Legend:
Removed from v.4  
changed lines
  Added in v.32

  ViewVC Help
Powered by ViewVC 1.1.20