Subversion Repositories HomeAutomation

Rev

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