Subversion Repositories HomeAutomation

Rev

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

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