Subversion Repositories HomeAutomation

Rev

Rev 452 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5.  
  6. public class Daemon {
  7.    
  8.     private SerialConnection sc;
  9.     private bool running;
  10.     const byte DEBUG_LEVEL = 3; //0, 1, 2, 3
  11.     private TcpServer tcps;
  12.    
  13.     public Daemon(SerialConnection sc) {
  14.         this.sc = sc;
  15.         running = true;
  16.     }
  17.    
  18.     public void stop () {
  19.         running = false;
  20.         Thread.Sleep(40);
  21.         tcps.stop();
  22.        
  23.         // send a disconnect to avr? (call a function in serialconnection)
  24.        
  25.         //...för varje tcp-server...
  26.     }
  27.    
  28.     public void addServer (TcpServer tcps) {
  29.         //...lägg till servern i en lista...
  30.         this.tcps = tcps;
  31.         Thread t = new Thread(tcps.thread);
  32.         t.Start();
  33.     }
  34.    
  35.     public void thread() {
  36.         CanPacket cp = null;
  37.         while (running) {
  38.             Thread.Sleep(1);
  39.             bool hasMessage = sc.getPacket(out cp);
  40.             if (hasMessage) {
  41.                 tcps.sendCanPacket(cp);
  42.                
  43.                 if (DEBUG_LEVEL>2) { Console.WriteLine(">"+cp.toRawString()); }
  44.             }
  45.            
  46.             string tcpdata;
  47.             bool hasData = tcps.getData(out tcpdata);
  48.             if (hasData) {
  49.                 if (tcpdata.Length>3) {
  50.                     //string canraw = tcpdata.Substring(4);
  51.                     cp = new CanPacket(tcpdata.Substring(4));
  52.                     //cp.setData(tcpdata.Substring(4));
  53.                     if (sc.writePacket(cp)) {
  54.                         if (DEBUG_LEVEL>2) { Console.WriteLine("<"+cp.toRawString()); }
  55.                     }
  56.                 }
  57.             }
  58.         }
  59.     }
  60. }
  61.