Subversion Repositories HomeAutomation

Rev

Rev 1610 | Rev 1619 | 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();
1614 runge 3
var module_aliases = new Array();
1603 runge 4
 
1614 runge 5
function CreateModuleAlias(alias, module_name, module_id, extra)
6
{
7
    module_aliases[alias] = { "is_group" : false, "module_name" : module_name, "module_id" : module_id, "extra" : extra };
8
}
9
 
10
function CreateModuleAliasGroup(alias, aliases)
11
{
12
    module_aliases[alias] = { "is_group" : true, "aliases" : aliases };
13
}
14
 
1603 runge 15
function GetModule(name)
16
{
17
    if (typeof modules[name] == 'undefined')
18
    {
19
        LoadScript("module/" + name + ".js");
20
 
21
        if (eval("typeof " + name + " != 'function'"))
22
        {
23
            Log("Failed to load " + name + " module.");
24
            return;
25
        }
26
 
27
        modules[name] = eval("(new " + name + "('" + name + "'))");
28
    }
29
 
30
    return modules[name];
31
}
32
 
1602 runge 33
function Module_OnChange(full_id, available)
34
{
1603 runge 35
    var parts = full_id.split(":", 2);
36
 
1609 runge 37
    if (global_legacy)
1604 runge 38
    {
39
        legacyOnState(full_id, parts[1], parts[0], available);
40
        return;
41
    }
42
 
1603 runge 43
    var module = GetModule(parts[0]);
44
 
45
    if (module)
46
    {
47
        module.SetStatus(parts[1], available);
48
    }
1602 runge 49
}
50
 
51
function Module_OnMessage()
52
{
53
    var full_id = arguments[0];
54
    var command = arguments[1];
55
    var number_of_variables = arguments[2];
56
 
57
    var variables = new Array();
58
 
59
    for (var i = 3; i < number_of_variables * 2 + 3; i += 2)
60
    {
61
        variables[arguments[i]] = arguments[i + 1];
62
    }
63
 
64
    variables.length = number_of_variables;
65
 
1603 runge 66
    var parts = full_id.split(":", 2);
67
 
1609 runge 68
    if (global_legacy)
1604 runge 69
    {
70
        legacyOnMessage(full_id, parts[1], parts[0], command, variables);
71
        return;
72
    }
73
 
74
 
1603 runge 75
    var module = GetModule(parts[0]);
76
 
77
    if (module)
78
    {
79
        module.ReceiveMessage(parts[1], command, variables);
80
    }
81
    else
82
    {
83
        //Log("No one to receive this message, ignoring...");
84
    }
1602 runge 85
}
86
 
87
function SendModuleMessage(full_id, command, variables)
88
{
89
    var args = new Array();
90
 
91
    args[args.length] = full_id;
92
    args[args.length] = command;
93
    args[args.length] = variables.length;
94
 
1603 runge 95
    for (var i = 0; i < variables.length; i++)
1602 runge 96
    {
1603 runge 97
        var obj = variables[i];
98
 
99
        for (var key in obj)
100
        {
101
            args[args.length] = key;
102
            args[args.length] = obj[key];
103
        }
1602 runge 104
    }
105
 
106
    Module_SendMessage.apply(null, Array.prototype.slice.call(args, 0));
107
}
1603 runge 108
 
109
 
110
function Module(name)
111
{
112
    this.name_ = name;
113
    this.ids_ = new Array();
114
 
115
    this.EventBase();
116
}
117
 
118
Extend(Module, EventBase);
119
 
120
Module.prototype.name_;
121
Module.prototype.ids_;
122
 
123
Module.prototype.SetStatus = function(id, available)
124
{
125
    if (typeof this.ids_[id] == 'undefined')
126
    {
127
        this.ids_[id] = available;
128
        this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available);
129
    }
130
    else if (this.ids[id] != available)
131
    {
132
        this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available);
133
    }
134
}
135
 
136
Module.prototype.ReceiveMessage = function(id, command, variables)
137
{
1609 runge 138
    //Log("received " + command);
1603 runge 139
}
140
 
141
Module.prototype.GetAvailableIds = function()
142
{
143
    var ids = new Array();
144
 
145
    for (var id in this.ids_)
146
    {
1614 runge 147
        if (this.ids_[id] && typeof this.ids_[id] != 'function')
1603 runge 148
        {
149
            ids[ids.length] = id;
150
        }
151
    }
152
 
153
    return ids;
154
}
155
 
1614 runge 156
Module.prototype.GetAvailableNames = function()
157
{
158
    var result = new Array();
159
    var available_ids = this.GetAvailableIds();
160
 
161
    for (var alias in module_aliases)
162
    {
163
        if (module_aliases[alias]["is_group"])
164
        {
165
            for (var n = 0; n < module_aliases[alias]["aliases"].length; n++)
166
            {
167
                var group_alias = module_aliases[alias]["aliases"][n];
168
 
169
                if (module_aliases[group_alias]["module_name"] == this.name_ && available_ids.contains(module_aliases[group_alias]["module_id"]))
170
                {
171
                    result.push(alias);
172
                    break;
173
                }
174
            }
175
        }
176
        else
177
        {
178
            if (module_aliases[alias]["module_name"] == this.name_ && available_ids.contains(module_aliases[alias]["module_id"]))
179
            {
180
                result.push(alias);
181
            }
182
        }
183
    }
184
 
185
    return result;
186
}
187
 
1603 runge 188
Module.prototype.IsAvailable = function(id)
189
{
190
    if (typeof this.ids_[id] == 'undefined' || this.ids_[id] == false)
191
    {
192
        return false;
193
    }
194
 
195
    return true;
196
}
197
 
198
Module.prototype.SendMessage = function(id, command, variables)
199
{
200
    if (this.IsAvailable(id))
201
    {
202
        SendModuleMessage(this.name_ + ":" + id, command, variables);
203
    }
204
}
1609 runge 205
 
206
 
207
// Console Commands
208
 
209
function modulelist()
210
{
211
    var result = "";
212
 
213
    for (var name in modules)
214
    {
215
        result += name + ":  " + modules[name].GetAvailableIds().toString() + "\n";
216
    }
217
 
218
    return result;
219
}
220
 
1610 runge 221
RegisterConsoleCommand(modulelist);
1609 runge 222