Subversion Repositories HomeAutomation

Rev

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

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