Subversion Repositories HomeAutomation

Rev

Rev 434 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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