Subversion Repositories HomeAutomation

Rev

Rev 981 | Rev 997 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 981 Rev 986
Line 1... Line 1...
1
 
1
 
-
 
2
CanNodes = new Array();
2
CanServices = new Array();
3
CanServices = new Array();
3
CanOfflineTimer = new Interval(offlineCheck, 10000);
4
CanOfflineTimer = new Interval(offlineCheck, 10000);
-
 
5
 
-
 
6
function handleNMTMessage(className, commandName, data)
-
 
7
{
-
 
8
    var canMessage = new CanNMTMessage(className, commandName, eval("(" + data + ")"));
-
 
9
 
-
 
10
    switch (canMessage.getCommandName())
-
 
11
    {
-
 
12
    case "Reset":
-
 
13
    break;
-
 
14
   
-
 
15
    case "Bios_Start":
-
 
16
    break;
-
 
17
   
-
 
18
    case "Pgm_Start":
-
 
19
    break;
-
 
20
   
-
 
21
    case "Pgm_Data":
-
 
22
    break;
-
 
23
   
-
 
24
    case "Pgm_End":
-
 
25
    break;
-
 
26
   
-
 
27
    case "Pgm_Copy":
-
 
28
    break;
-
 
29
   
-
 
30
    case "Pgm_Ack":
-
 
31
    break;
-
 
32
   
-
 
33
    case "Pgm_Nack":
-
 
34
    break;
-
 
35
   
-
 
36
    case "Start_App":
-
 
37
    break;
-
 
38
   
-
 
39
    case "App_Start":
-
 
40
    break;
-
 
41
   
-
 
42
    case "Heartbeat":
-
 
43
    var node = CanNodes[canMessage.getData("HardwareId")];
-
 
44
   
-
 
45
    if (!node)
-
 
46
    {
-
 
47
        log("A can node with hardware id " + uint2hex(canMessage.getData("HardwareId"), 32) + " was not found, starting.\n");
-
 
48
        node = new CanNode(canMessage.getData("HardwareId"), canMessage.getData("NumberOfModules"));
-
 
49
        CanNodes[canMessage.getData("HardwareId")] = node;
-
 
50
    }
-
 
51
   
-
 
52
    node.handleHeartbeat(canMessage.getData("NumberOfModules"));
-
 
53
    break;
-
 
54
    }
-
 
55
}
-
 
56
 
-
 
57
// This is used to send messages to the can network
-
 
58
function sendNMTMessage(canMessage)
-
 
59
{
-
 
60
    sendCanNMTMessage(  canMessage.getClassName(),
-
 
61
                canMessage.getCommandName(),
-
 
62
                canMessage.getDataString());
-
 
63
}
4
 
64
 
5
// This is called when a message has been received from the can network
65
// This is called when a message has been received from the can network
6
function handleMessage(className, directionFlag, moduleName, moduleId, commandName, data)
66
function handleMessage(className, directionFlag, moduleName, moduleId, commandName, data)
7
{
67
{
8
    var canMessage = new CanMessage(className, directionFlag, moduleName, moduleId, commandName, eval("(" + data + ")"));
68
    var canMessage = new CanMessage(className, directionFlag, moduleName, moduleId, commandName, eval("(" + data + ")"));
-
 
69
   
9
    var fullId = canMessage.getModuleName() + canMessage.getModuleId();
70
    var fullId = canMessage.getModuleName() + canMessage.getModuleId();
10
 
71
 
11
    if (canMessage.getCommandName() == "List" && canMessage.getDirectionFlag() == "From_Owner")
72
    if (canMessage.getCommandName() == "List" && canMessage.getDirectionFlag() == "From_Owner")
12
    {
73
    {
13
        if (!CanServices[fullId])
74
        if (!CanServices[fullId])
14
        {
75
        {
15
            var service = ServiceManager.getService("Can", canMessage.getModuleName(), canMessage.getModuleId())
76
            var service = ServiceManager.getService("Can", canMessage.getModuleName(), canMessage.getModuleId())
16
 
77
 
17
            if (service != null)
78
            if (service != null)
18
            {
79
            {
-
 
80
                var node = CanNodes[canMessage.getData("HardwareId")];
-
 
81
           
-
 
82
                if (node)
-
 
83
                {
19
                CanServices[fullId] = service;
84
                    service.setNode(node);
-
 
85
                    node.addService(canMessage.getData("SequenceNumber"), service);
20
                CanServices[fullId].setHardwareId(canMessage.getData("HardwareId"));
86
                    CanNodes[canMessage.getData("HardwareId")] = node;
-
 
87
                }
-
 
88
               
21
                CanServices[fullId].onlineHandler();
89
                CanServices[fullId] = service;
22
            }
90
            }
23
            else
91
            else
24
            {
92
            {
25
                log("No such service found: " + canMessage.getModuleName() + "\n");
93
                log("No such service found: " + canMessage.getModuleName() + "\n");
26
            }
94
            }
27
        }
95
        }
28
    }
96
    }
29
    else
97
    else
30
    {
98
    {
31
        if (CanServices[fullId])
99
        if (CanServices[fullId])
32
        {
100
        {
33
            // Tell the service to handle the message, a module service must implement canMessageHandler or derive from CanService-class
101
            // Tell the service to handle the message, a module service must implement canMessageHandler or derive from CanService-class
34
            CanServices[fullId].canMessageHandler(canMessage);
102
            CanServices[fullId].canMessageHandler(canMessage);
35
        }
103
        }
36
        else if (canMessage.getModuleName() == "Debug")
104
        else if (canMessage.getModuleName() == "Debug")
37
        {
105
        {
38
            var service = ServiceManager.getService("Can", canMessage.getModuleName(), canMessage.getModuleId())
106
            var service = ServiceManager.getService("Can", canMessage.getModuleName(), canMessage.getModuleId())
39
            if (service != null)
107
            if (service != null)
40
            {
108
            {
41
                CanServices[fullId] = service;
109
                CanServices[fullId] = service;
42
                CanServices[fullId].canMessageHandler(canMessage);
110
                CanServices[fullId].canMessageHandler(canMessage);
43
            }
111
            }
44
        }
112
        }
45
    }
113
    }
46
}
114
}
47
 
115
 
48
// This is used to send messages to the can network
116
// This is used to send messages to the can network
49
function sendMessage(canMessage)
117
function sendMessage(canMessage)
50
{
118
{
51
    sendCanMessage( canMessage.getClassName(),
119
    sendCanMessage( canMessage.getClassName(),
52
            canMessage.getDirectionFlag(),
120
            canMessage.getDirectionFlag(),
53
            canMessage.getModuleName(),
121
            canMessage.getModuleName(),
54
            canMessage.getModuleId(),
122
            canMessage.getModuleId(),
55
            canMessage.getCommandName(),
123
            canMessage.getCommandName(),
56
            canMessage.getDataString());
124
            canMessage.getDataString());
57
}
125
}
58
 
-
 
59
function startOfflineCheck()
-
 
60
{
-
 
61
    CanOfflineTimer.start();
-
 
62
}
-
 
63
 
126
 
64
// This will be called at reqular intervals
127
// This will be called at reqular intervals
65
function offlineCheck()
128
function offlineCheck()
66
{
129
{
67
    for (var n in CanServices)
130
    for (var n in CanNodes)
68
    {
-
 
69
        CanServices[n].checkOffline();
-
 
70
    }
-
 
71
}
-
 
72
 
-
 
73
function setAllOffline()
-
 
74
{
-
 
75
    for (var n in CanServices)
-
 
76
    {
-
 
77
        CanServices[n].setOffline();
-
 
78
    }
-
 
79
}
-
 
80
 
-
 
81
// This is called when a node sends a heartbeat
-
 
82
function handleHeartbeat(hardwareId)
-
 
83
{
-
 
84
    var found = false;
-
 
85
 
-
 
86
    for (var n in CanServices)
-
 
87
    {
131
    {
88
        if (CanServices[n].getHardwareId() == hardwareId)
-
 
89
        {
-
 
90
            found = true;
-
 
91
           
-
 
92
            if (CanServices[n].isOnline())
132
        CanNodes[n].checkOffline();
93
            {
-
 
94
                CanServices[n].heartbeatHandler();
-
 
95
            }
-
 
96
            else
-
 
97
            {
-
 
98
                CanServices[n].onlineHandler();
-
 
99
            }
-
 
100
        }
133
    }
101
    }
134
}
102
   
135
 
-
 
136
function setAllOffline()
-
 
137
{
103
    if (!found)
138
    for (var n in CanNodes)
104
    {
139
    {
105
        log("Received heartbeat from unknown node, requesting module listing for hardware id 0x" + uint2hex(hardwareId, 32) + "\n");
-
 
106
        var canMessage = new CanMessage("nmt", "To_Owner", "", 0, "List");
-
 
107
        canMessage.setData("HardwareId", hardwareId);
-
 
108
        sendMessage(canMessage);
140
        CanNodes[n].setOffline();
109
    }
141
    }
110
}
142
}