Subversion Repositories HomeAutomation

Rev

Rev 1614 | Rev 1624 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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