Subversion Repositories HomeAutomation

Rev

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