Subversion Repositories HomeAutomation

Rev

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

Rev 1602 Rev 1603
Line -... Line 1...
-
 
1
 
-
 
2
var modules = new Array();
-
 
3
 
-
 
4
function GetModule(name)
-
 
5
{
-
 
6
    if (typeof modules[name] == 'undefined')
-
 
7
    {
-
 
8
        LoadScript("module/" + name + ".js");
-
 
9
       
-
 
10
        if (eval("typeof " + name + " != 'function'"))
-
 
11
        {
-
 
12
            Log("Failed to load " + name + " module.");
-
 
13
            return;
-
 
14
        }
-
 
15
       
-
 
16
        modules[name] = eval("(new " + name + "('" + name + "'))");
-
 
17
    }
-
 
18
   
-
 
19
    return modules[name];
-
 
20
}
1
 
21
 
2
function Module_OnChange(full_id, available)
22
function Module_OnChange(full_id, available)
3
{
23
{
-
 
24
    var parts = full_id.split(":", 2);
-
 
25
   
-
 
26
    var module = GetModule(parts[0]);
-
 
27
   
-
 
28
    if (module)
-
 
29
    {
4
    Log(full_id + " is now " + (available ? "available" : "unavailable"));
30
        module.SetStatus(parts[1], available);
-
 
31
    }
5
}
32
}
6
 
33
 
7
function Module_OnMessage()
34
function Module_OnMessage()
8
{
35
{
9
    var full_id = arguments[0];
36
    var full_id = arguments[0];
Line 12... Line 39...
12
   
39
   
13
    var variables = new Array();
40
    var variables = new Array();
14
   
41
   
15
    for (var i = 3; i < number_of_variables * 2 + 3; i += 2)
42
    for (var i = 3; i < number_of_variables * 2 + 3; i += 2)
16
    {
43
    {
17
        //Log(arguments[i] + " = " + arguments[i + 1]);
-
 
18
        variables[arguments[i]] = arguments[i + 1];
44
        variables[arguments[i]] = arguments[i + 1];
19
    }
45
    }
20
   
46
   
21
    variables.length = number_of_variables;
47
    variables.length = number_of_variables;
-
 
48
   
-
 
49
    var parts = full_id.split(":", 2);
-
 
50
   
-
 
51
    var module = GetModule(parts[0]);
22
   
52
   
-
 
53
    if (module)
-
 
54
    {
23
    //Log(full_id + " got message: " + command + " with " + variables.length + " arguments");
55
        module.ReceiveMessage(parts[1], command, variables);
-
 
56
    }
-
 
57
    else
-
 
58
    {
-
 
59
        //Log("No one to receive this message, ignoring...");
-
 
60
    }
24
}
61
}
25
 
62
 
26
function SendModuleMessage(full_id, command, variables)
63
function SendModuleMessage(full_id, command, variables)
27
{
64
{
28
    var args = new Array();
65
    var args = new Array();
29
   
66
   
30
    args[args.length] = full_id;
67
    args[args.length] = full_id;
31
    args[args.length] = command;
68
    args[args.length] = command;
32
    args[args.length] = variables.length;
69
    args[args.length] = variables.length;
33
   
70
   
-
 
71
    for (var i = 0; i < variables.length; i++)
-
 
72
    {
-
 
73
        var obj = variables[i];
-
 
74
       
-
 
75
        for (var key in obj)
-
 
76
        {
-
 
77
            args[args.length] = key;
-
 
78
            args[args.length] = obj[key];
-
 
79
        }
-
 
80
    }
-
 
81
   
-
 
82
    Module_SendMessage.apply(null, Array.prototype.slice.call(args, 0));
-
 
83
}
-
 
84
 
-
 
85
 
-
 
86
function Module(name)
-
 
87
{
-
 
88
    this.name_ = name;
-
 
89
    this.ids_ = new Array();
-
 
90
   
-
 
91
    this.EventBase();
-
 
92
}
-
 
93
 
-
 
94
Extend(Module, EventBase);
-
 
95
 
-
 
96
Module.prototype.name_;
-
 
97
Module.prototype.ids_;
-
 
98
 
-
 
99
Module.prototype.SetStatus = function(id, available)
-
 
100
{
-
 
101
    if (typeof this.ids_[id] == 'undefined')
-
 
102
    {
-
 
103
        this.ids_[id] = available;
-
 
104
        this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available);
-
 
105
    }
-
 
106
    else if (this.ids[id] != available)
-
 
107
    {
-
 
108
        this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available);
-
 
109
    }
-
 
110
}
-
 
111
 
-
 
112
Module.prototype.ReceiveMessage = function(id, command, variables)
-
 
113
{
-
 
114
   
-
 
115
}
-
 
116
 
-
 
117
Module.prototype.GetAvailableIds = function()
-
 
118
{
-
 
119
    var ids = new Array();
-
 
120
   
34
    for (var n in variables)
121
    for (var id in this.ids_)
-
 
122
    {
-
 
123
        if (this.ids_[id])
-
 
124
        {
-
 
125
            ids[ids.length] = id;
-
 
126
        }
-
 
127
    }
-
 
128
   
-
 
129
    return ids;
-
 
130
}
-
 
131
 
-
 
132
Module.prototype.IsAvailable = function(id)
-
 
133
{
-
 
134
    if (typeof this.ids_[id] == 'undefined' || this.ids_[id] == false)
-
 
135
    {
-
 
136
        return false;
-
 
137
    }
-
 
138
   
-
 
139
    return true;
-
 
140
}
-
 
141
 
-
 
142
Module.prototype.SendMessage = function(id, command, variables)
35
    {
143
{
36
        args[args.length] = n;
144
    if (this.IsAvailable(id))
-
 
145
    {
37
        args[args.length] = variables[n];
146
        SendModuleMessage(this.name_ + ":" + id, command, variables);
38
    }
147
    }
39
   
-
 
40
    Module_SendMessage.apply(null, Array.prototype.slice.call(args, 0));
-
 
41
}
148
}