Subversion Repositories HomeAutomation

Rev

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

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