Subversion Repositories HomeAutomation

Rev

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

Rev 1765 Rev 1897
1
 
1
 
2
Require("plugin/module.js");
2
Require("plugin/module.js");
3
 
3
 
4
Dimmer_ModuleNames    = [ "Dimmer230", "hwPWM" ];
4
Dimmer_ModuleNames    = [ "Dimmer230", "hwPWM" ];
5
Dimmer_SpeedLevels    = function() { return [ 50, 135, 200, 255 ]; };
5
Dimmer_SpeedLevels    = function() { return [ 50, 135, 200, 255 ]; };
6
Dimmer_StrengthLevels = function() { return [ 0, 50, 100, 150, 200, 255 ]; };
6
Dimmer_StrengthLevels = function() { return [ 0, 50, 100, 150, 200, 255 ]; };
7
Dimmer_StepsLevels    = function() { return [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 ]; };
7
Dimmer_StepsLevels    = function() { return [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 ]; };
8
Dimmer_Directions     = function() { return [ "Increase", "Decrease" ]; };
8
Dimmer_Directions     = function() { return [ "Increase", "Decrease" ]; };
9
Dimmer_Channels       = function() { return [ 0 ]; };
9
Dimmer_Channels       = function() { return [ 0 ]; };
10
Dimmer_Aliases        = function() { return Module_GetAliasNames(Dimmer_ModuleNames); };
10
Dimmer_Aliases        = function() { return Module_GetAliasNames(Dimmer_ModuleNames); };
11
Dimmer_AvailableIds   = function() { return Module_GetAvailableIds(Dimmer_ModuleNames); };
11
Dimmer_AvailableIds   = function() { return Module_GetAvailableIds(Dimmer_ModuleNames); };
12
 
12
 
13
function Dimmer_StartFade(alias_name, speed, direction)
13
function Dimmer_StartFade(alias_name, speed, direction)
14
{
14
{
15
    if (arguments.length < 3)
15
    if (arguments.length < 3)
16
    {  
16
    {  
17
        Log("\033[31mNot enough parameters given.\033[0m\n");
17
        Log("\033[31mNot enough parameters given.\033[0m\n");
18
        return false;
18
        return false;
19
    }
19
    }
20
   
20
   
21
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
21
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
22
    var found = false;
22
    var found = false;
23
   
23
   
24
    for (var name in aliases_data)
24
    for (var name in aliases_data)
25
    {
25
    {
26
        var variables = {
26
        var variables = {
27
            "Channel"   : aliases_data[name]["specific"]["Channel"],
27
            "Channel"   : aliases_data[name]["specific"]["Channel"],
28
            "Speed"     : speed,
28
            "Speed"     : speed,
29
            "Direction" : direction };
29
            "Direction" : direction };
30
   
30
   
31
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Start_Fade", variables))
31
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Start_Fade", variables))
32
        {
32
        {
33
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
33
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
34
        }
34
        }
35
        else
35
        else
36
        {
36
        {
37
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
37
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
38
        }
38
        }
39
       
39
       
40
        found = true;
40
        found = true;
41
    }
41
    }
42
   
42
   
43
    if (!found)
43
    if (!found)
44
    {
44
    {
45
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
45
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
46
        return false;
46
        return false;
47
    }
47
    }
48
   
48
   
49
    return true;
49
    return true;
50
}
50
}
51
Console_RegisterCommand(Dimmer_StartFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_Directions()); });
51
Console_RegisterCommand(Dimmer_StartFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_Directions()); });
52
 
52
 
53
function Dimmer_StopFade(alias_name)
53
function Dimmer_StopFade(alias_name)
54
{
54
{
55
    if (arguments.length < 1)
55
    if (arguments.length < 1)
56
    {
56
    {
57
        Log("\033[31mNot enough parameters given.\033[0m\n");
57
        Log("\033[31mNot enough parameters given.\033[0m\n");
58
        return false;
58
        return false;
59
    }
59
    }
60
   
60
   
61
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
61
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
62
    var found = false;
62
    var found = false;
63
   
63
   
64
    for (var name in aliases_data)
64
    for (var name in aliases_data)
65
    {
65
    {
66
        var variables = {
66
        var variables = {
67
        "Channel" : aliases_data[name]["specific"]["Channel"] };
67
        "Channel" : aliases_data[name]["specific"]["Channel"] };
68
       
68
       
69
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Stop_Fade", variables))
69
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Stop_Fade", variables))
70
        {
70
        {
71
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
71
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
72
        }
72
        }
73
        else
73
        else
74
        {
74
        {
75
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
75
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
76
        }
76
        }
77
       
77
       
78
        found = true;
78
        found = true;
79
    }
79
    }
80
   
80
   
81
    if (!found)
81
    if (!found)
82
    {
82
    {
83
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
83
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
84
        return false;
84
        return false;
85
    }
85
    }
86
   
86
   
87
    return true;
87
    return true;
88
}
88
}
89
Console_RegisterCommand(Dimmer_StopFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases()); });
89
Console_RegisterCommand(Dimmer_StopFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases()); });
90
 
90
 
91
function Dimmer_AbsoluteFade(alias_name, speed, level)
91
function Dimmer_AbsoluteFade(alias_name, speed, level)
92
{
92
{
93
    if (arguments.length < 3)
93
    if (arguments.length < 3)
94
    {
94
    {
95
        Log("\033[31mNot enough parameters given.\033[0m\n");
95
        Log("\033[31mNot enough parameters given.\033[0m\n");
96
        return false;
96
        return false;
97
    }
97
    }
98
   
98
   
99
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
99
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
100
    var found = false;
100
    var found = false;
101
   
101
   
102
    for (var name in aliases_data)
102
    for (var name in aliases_data)
103
    {
103
    {
104
        if (aliases_data[name]["module_name"]=="hwPWM") {
104
        if (aliases_data[name]["module_name"]=="hwPWM") {
105
          level = level*39;
105
          level = level*39;
106
        }
106
        }
107
        var variables = {
107
        var variables = {
108
        "Channel"   : aliases_data[name]["specific"]["Channel"],
108
        "Channel"   : aliases_data[name]["specific"]["Channel"],
109
        "Speed"     : speed,
109
        "Speed"     : speed,
110
        "EndValue"  : level };
110
        "EndValue"  : level };
111
       
111
       
112
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Abs_Fade", variables))
112
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Abs_Fade", variables))
113
        {
113
        {
114
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
114
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
115
        }
115
        }
116
        else
116
        else
117
        {
117
        {
118
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
118
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
119
        }
119
        }
120
       
120
       
121
        found = true;
121
        found = true;
122
    }
122
    }
123
   
123
   
124
    if (!found)
124
    if (!found)
125
    {
125
    {
126
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
126
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
127
        return false;
127
        return false;
128
    }
128
    }
129
   
129
   
130
    return true;
130
    return true;
131
}
131
}
132
Console_RegisterCommand(Dimmer_AbsoluteFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_StrengthLevels()); });
132
Console_RegisterCommand(Dimmer_AbsoluteFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_StrengthLevels()); });
133
 
133
 
134
function Dimmer_RelativeFade(alias_name, speed, direction, steps)
134
function Dimmer_RelativeFade(alias_name, speed, direction, steps)
135
{
135
{
136
    if (arguments.length < 4)
136
    if (arguments.length < 4)
137
    {
137
    {
138
        Log("\033[31mNot enough parameters given.\033[0m\n");
138
        Log("\033[31mNot enough parameters given.\033[0m\n");
139
        return false;
139
        return false;
140
    }
140
    }
141
   
141
   
142
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
142
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
143
    var found = false;
143
    var found = false;
144
   
144
   
145
    for (var name in aliases_data)
145
    for (var name in aliases_data)
146
    {
146
    {
147
        if (aliases_data[name]["module_name"]=="hwPWM") {
147
        if (aliases_data[name]["module_name"]=="hwPWM") {
148
          steps = steps*39;
148
          steps = steps*39;
149
        }
149
        }
150
        var variables = {
150
        var variables = {
151
        "Channel"   : aliases_data[name]["specific"]["Channel"],
151
        "Channel"   : aliases_data[name]["specific"]["Channel"],
152
        "Speed"     : speed,
152
        "Speed"     : speed,
153
        "Direction" : direction,
153
        "Direction" : direction,
154
        "Steps"     : steps };
154
        "Steps"     : steps };
155
       
155
       
156
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Rel_Fade", variables))
156
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Rel_Fade", variables))
157
        {
157
        {
158
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
158
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
159
        }
159
        }
160
        else
160
        else
161
        {
161
        {
162
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
162
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
163
        }
163
        }
164
       
164
       
165
        found = true;
165
        found = true;
166
    }
166
    }
167
   
167
   
168
    if (!found)
168
    if (!found)
169
    {
169
    {
170
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
170
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
171
        return false;
171
        return false;
172
    }
172
    }
173
   
173
   
174
    return true;
174
    return true;
175
}
175
}
176
Console_RegisterCommand(Dimmer_RelativeFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_Directions(), Dimmer_StepsLevels()); });
176
Console_RegisterCommand(Dimmer_RelativeFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_Directions(), Dimmer_StepsLevels()); });
177
 
177
 
178
function Dimmer_Demo(alias_name, speed, steps)
178
function Dimmer_Demo(alias_name, speed, steps)
179
{
179
{
180
    if (arguments.length < 3)
180
    if (arguments.length < 3)
181
    {
181
    {
182
        Log("\033[31mNot enough parameters given.\033[0m\n");
182
        Log("\033[31mNot enough parameters given.\033[0m\n");
183
        return false;
183
        return false;
184
    }
184
    }
185
   
185
   
186
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
186
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
187
    var found = false;
187
    var found = false;
188
   
188
   
189
    for (var name in aliases_data)
189
    for (var name in aliases_data)
190
    {
190
    {
191
        if (aliases_data[name]["module_name"]=="hwPWM") {
191
        if (aliases_data[name]["module_name"]=="hwPWM") {
192
          steps = steps*39;
192
          steps = steps*39;
193
        }
193
        }
194
        var variables = {
194
        var variables = {
195
        "Channel"   : aliases_data[name]["specific"]["Channel"],
195
        "Channel"   : aliases_data[name]["specific"]["Channel"],
196
        "Speed"     : speed,
196
        "Speed"     : speed,
197
        "Steps"     : steps };
197
        "Steps"     : steps };
198
       
198
       
199
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Demo", variables))
199
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Demo", variables))
200
        {
200
        {
201
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
201
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
202
        }
202
        }
203
        else
203
        else
204
        {
204
        {
205
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
205
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
206
        }
206
        }
207
       
207
       
208
        found = true;
208
        found = true;
209
    }
209
    }
210
   
210
   
211
    if (!found)
211
    if (!found)
212
    {
212
    {
213
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
213
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
214
        return false;
214
        return false;
215
    }
215
    }
216
   
216
   
217
    return true;
217
    return true;
218
}
218
}
219
Console_RegisterCommand(Dimmer_Demo, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_StepsLevels()); });
219
Console_RegisterCommand(Dimmer_Demo, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_StepsLevels()); });
220
 
220
 
221
function Dimmer_OnMessage(module_name, module_id, command, variables)
221
function Dimmer_OnMessage(module_name, module_id, command, variables)
222
{
222
{
223
    if (in_array(Dimmer_ModuleNames, module_name))
223
    if (in_array(Dimmer_ModuleNames, module_name))
224
    {
224
    {
225
        var aliases_data = Module_LookupAliases({
225
        var aliases_data = Module_LookupAliases({
226
            "module_name" : module_name,
226
            "module_name" : module_name,
227
            "module_id"   : module_id,
227
            "module_id"   : module_id,
228
            "group"       : false
228
            "group"       : false
229
        });
229
        });
230
       
230
       
231
        switch (command)
231
        switch (command)
232
        {
232
        {
233
            case "Netinfo":
233
            case "Netinfo":
234
            {
234
            {
235
                for (var alias_name in aliases_data)
235
                for (var alias_name in aliases_data)
236
                {
236
                {
237
                    if (aliases_data[alias_name]["specific"]["Channel"] != variables["Channel"])
237
                    if (aliases_data[alias_name]["specific"]["Channel"] != variables["Channel"])
238
                    {
238
                    {
239
                        continue;
239
                        continue;
240
                    }
240
                    }
241
                   
241
                   
242
                    var last_value = {};
242
                    var last_value = {};
243
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
243
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
244
 
244
 
245
                    if (last_value_string)
245
                    if (last_value_string)
246
                    {
246
                    {
247
                        last_value = eval("(" + last_value_string + ")");
247
                        last_value = eval("(" + last_value_string + ")");
248
                    }
248
                    }
249
                   
249
                   
250
                    last_value["Connection"] = { "value" : variables["Connection"], "timestamp" : get_time() };
250
                    last_value["Connection"] = { "value" : variables["Connection"], "timestamp" : get_time() };
251
                    last_value["Frequency"] = { "value" : variables["Frequency"], "timestamp" : get_time() };
251
                    last_value["Frequency"] = { "value" : variables["Frequency"], "timestamp" : get_time() };
252
                    last_value["Level"] = { "value" : variables["DimmerValue"], "timestamp" : get_time() };
252
                    last_value["Level"] = { "value" : variables["DimmerValue"], "timestamp" : get_time() };
253
                   
253
                   
254
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
254
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
255
                }
255
                }
256
               
256
               
257
                break;
257
                break;
258
            }
258
            }
259
            case "Pwm":
259
            case "Pwm":
260
            {
260
            {
261
                for (var alias_name in aliases_data)
261
                for (var alias_name in aliases_data)
262
                {
262
                {
263
                    if (aliases_data[alias_name]["specific"]["Channel"] != variables["Id"])
263
                    if (aliases_data[alias_name]["specific"]["Channel"] != variables["Channel"])
264
                    {
264
                    {
265
                        continue;
265
                        continue;
266
                    }
266
                    }
267
                   
267
                   
268
                    var last_value = {};
268
                    var last_value = {};
269
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
269
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
270
 
270
 
271
                    if (last_value_string)
271
                    if (last_value_string)
272
                    {
272
                    {
273
                        last_value = eval("(" + last_value_string + ")");
273
                        last_value = eval("(" + last_value_string + ")");
274
                    }
274
                    }
275
                    var value = 0 + variables["Value"];
275
                    var value = 0 + variables["Value"];
276
                    value = value / 39;
276
                    value = value / 39;
277
                    value = parseInt(value).toString();
277
                    value = parseInt(value).toString();
278
                    last_value["Level"] = { "value" : value, "timestamp" : get_time() };
278
                    last_value["Level"] = { "value" : value, "timestamp" : get_time() };
279
                   
279
                   
280
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
280
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
281
                }
281
                }
282
               
282
               
283
                break;
283
                break;
284
            }
284
            }
285
        }
285
        }
286
    }
286
    }
287
}
287
}
288
Module_RegisterToOnMessage("all", Dimmer_OnMessage);
288
Module_RegisterToOnMessage("all", Dimmer_OnMessage);
289
 
289