Subversion Repositories HomeAutomation

Rev

Rev 1604 | Rev 1610 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1602 runge 1
 
1603 runge 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
}
21
 
1602 runge 22
function Module_OnChange(full_id, available)
23
{
1603 runge 24
    var parts = full_id.split(":", 2);
25
 
1609 runge 26
    if (global_legacy)
1604 runge 27
    {
28
        legacyOnState(full_id, parts[1], parts[0], available);
29
        return;
30
    }
31
 
1603 runge 32
    var module = GetModule(parts[0]);
33
 
34
    if (module)
35
    {
36
        module.SetStatus(parts[1], available);
37
    }
1602 runge 38
}
39
 
40
function Module_OnMessage()
41
{
42
    var full_id = arguments[0];
43
    var command = arguments[1];
44
    var number_of_variables = arguments[2];
45
 
46
    var variables = new Array();
47
 
48
    for (var i = 3; i < number_of_variables * 2 + 3; i += 2)
49
    {
50
        variables[arguments[i]] = arguments[i + 1];
51
    }
52
 
53
    variables.length = number_of_variables;
54
 
1603 runge 55
    var parts = full_id.split(":", 2);
56
 
1609 runge 57
    if (global_legacy)
1604 runge 58
    {
59
        legacyOnMessage(full_id, parts[1], parts[0], command, variables);
60
        return;
61
    }
62
 
63
 
1603 runge 64
    var module = GetModule(parts[0]);
65
 
66
    if (module)
67
    {
68
        module.ReceiveMessage(parts[1], command, variables);
69
    }
70
    else
71
    {
72
        //Log("No one to receive this message, ignoring...");
73
    }
1602 runge 74
}
75
 
76
function SendModuleMessage(full_id, command, variables)
77
{
78
    var args = new Array();
79
 
80
    args[args.length] = full_id;
81
    args[args.length] = command;
82
    args[args.length] = variables.length;
83
 
1603 runge 84
    for (var i = 0; i < variables.length; i++)
1602 runge 85
    {
1603 runge 86
        var obj = variables[i];
87
 
88
        for (var key in obj)
89
        {
90
            args[args.length] = key;
91
            args[args.length] = obj[key];
92
        }
1602 runge 93
    }
94
 
95
    Module_SendMessage.apply(null, Array.prototype.slice.call(args, 0));
96
}
1603 runge 97
 
98
 
99
function Module(name)
100
{
101
    this.name_ = name;
102
    this.ids_ = new Array();
103
 
104
    this.EventBase();
105
}
106
 
107
Extend(Module, EventBase);
108
 
109
Module.prototype.name_;
110
Module.prototype.ids_;
111
 
112
Module.prototype.SetStatus = function(id, available)
113
{
114
    if (typeof this.ids_[id] == 'undefined')
115
    {
116
        this.ids_[id] = available;
117
        this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available);
118
    }
119
    else if (this.ids[id] != available)
120
    {
121
        this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available);
122
    }
123
}
124
 
125
Module.prototype.ReceiveMessage = function(id, command, variables)
126
{
1609 runge 127
    //Log("received " + command);
1603 runge 128
}
129
 
130
Module.prototype.GetAvailableIds = function()
131
{
132
    var ids = new Array();
133
 
134
    for (var id in this.ids_)
135
    {
136
        if (this.ids_[id])
137
        {
138
            ids[ids.length] = id;
139
        }
140
    }
141
 
142
    return ids;
143
}
144
 
145
Module.prototype.IsAvailable = function(id)
146
{
147
    if (typeof this.ids_[id] == 'undefined' || this.ids_[id] == false)
148
    {
149
        return false;
150
    }
151
 
152
    return true;
153
}
154
 
155
Module.prototype.SendMessage = function(id, command, variables)
156
{
157
    if (this.IsAvailable(id))
158
    {
159
        SendModuleMessage(this.name_ + ":" + id, command, variables);
160
    }
161
}
1609 runge 162
 
163
 
164
// Console Commands
165
 
166
function modulelist()
167
{
168
    var result = "";
169
 
170
    for (var name in modules)
171
    {
172
        result += name + ":  " + modules[name].GetAvailableIds().toString() + "\n";
173
    }
174
 
175
    return result;
176
}
177
 
178
RegisterConsoleCommand("modulelist");
179