Subversion Repositories HomeAutomation

Rev

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

Rev 444 Rev 452
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 = 3; //0, 1, 2, 3
10
    const byte DEBUG_LEVEL = 3; //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(5);
38
            Thread.Sleep(1);
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
            string tcpdata;
46
            string tcpdata;
47
            bool hasData = tcps.getData(out tcpdata);
47
            bool hasData = tcps.getData(out tcpdata);
48
            if (hasData) {
48
            if (hasData) {
49
                if (tcpdata.Length>3) {
49
                if (tcpdata.Length>3) {
50
                    //string canraw = tcpdata.Substring(4);
50
                    //string canraw = tcpdata.Substring(4);
51
                    cp = new CanPacket(tcpdata.Substring(4));
51
                    cp = new CanPacket(tcpdata.Substring(4));
52
                    //cp.setData(tcpdata.Substring(4));
52
                    //cp.setData(tcpdata.Substring(4));
53
                    if (sc.writePacket(cp)) {
53
                    if (sc.writePacket(cp)) {
54
                        if (DEBUG_LEVEL>2) { Console.WriteLine("<"+cp.toRawString()); }
54
                        if (DEBUG_LEVEL>2) { Console.WriteLine("<"+cp.toRawString()); }
55
                    }
55
                    }
56
                }
56
                }
57
            }
57
            }
58
        }
58
        }
59
    }
59
    }
60
}
60
}
61
 
61