Subversion Repositories HomeAutomation

Rev

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

Rev 1897 Rev 2078
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_in)
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
    var level = level_in;
101
   
102
   
102
    for (var name in aliases_data)
103
    for (var name in aliases_data)
103
    {
104
    {
104
        if (aliases_data[name]["module_name"]=="hwPWM") {
105
        if (aliases_data[name]["module_name"]=="hwPWM") {
105
          level = level*39;
106
          level = level_in*39;
106
        }
107
        }
107
        var variables = {
108
        var variables = {
108
        "Channel"   : aliases_data[name]["specific"]["Channel"],
109
        "Channel"   : aliases_data[name]["specific"]["Channel"],
109
        "Speed"     : speed,
110
        "Speed"     : speed,
110
        "EndValue"  : level };
111
        "EndValue"  : level };
111
       
112
       
112
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Abs_Fade", variables))
113
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Abs_Fade", variables))
113
        {
114
        {
114
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
115
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
115
        }
116
        }
116
        else
117
        else
117
        {
118
        {
118
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
119
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
119
        }
120
        }
120
       
121
       
121
        found = true;
122
        found = true;
122
    }
123
    }
123
   
124
   
124
    if (!found)
125
    if (!found)
125
    {
126
    {
126
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
127
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
127
        return false;
128
        return false;
128
    }
129
    }
129
   
130
   
130
    return true;
131
    return true;
131
}
132
}
132
Console_RegisterCommand(Dimmer_AbsoluteFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_StrengthLevels()); });
133
Console_RegisterCommand(Dimmer_AbsoluteFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_StrengthLevels()); });
133
 
134
 
134
function Dimmer_RelativeFade(alias_name, speed, direction, steps)
135
function Dimmer_RelativeFade(alias_name, speed, direction, steps)
135
{
136
{
136
    if (arguments.length < 4)
137
    if (arguments.length < 4)
137
    {
138
    {
138
        Log("\033[31mNot enough parameters given.\033[0m\n");
139
        Log("\033[31mNot enough parameters given.\033[0m\n");
139
        return false;
140
        return false;
140
    }
141
    }
141
   
142
   
142
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
143
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
143
    var found = false;
144
    var found = false;
144
   
145
   
145
    for (var name in aliases_data)
146
    for (var name in aliases_data)
146
    {
147
    {
147
        if (aliases_data[name]["module_name"]=="hwPWM") {
148
        if (aliases_data[name]["module_name"]=="hwPWM") {
148
          steps = steps*39;
149
          steps = steps*39;
149
        }
150
        }
150
        var variables = {
151
        var variables = {
151
        "Channel"   : aliases_data[name]["specific"]["Channel"],
152
        "Channel"   : aliases_data[name]["specific"]["Channel"],
152
        "Speed"     : speed,
153
        "Speed"     : speed,
153
        "Direction" : direction,
154
        "Direction" : direction,
154
        "Steps"     : steps };
155
        "Steps"     : steps };
155
       
156
       
156
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Rel_Fade", variables))
157
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Rel_Fade", variables))
157
        {
158
        {
158
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
159
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
159
        }
160
        }
160
        else
161
        else
161
        {
162
        {
162
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
163
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
163
        }
164
        }
164
       
165
       
165
        found = true;
166
        found = true;
166
    }
167
    }
167
   
168
   
168
    if (!found)
169
    if (!found)
169
    {
170
    {
170
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
171
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
171
        return false;
172
        return false;
172
    }
173
    }
173
   
174
   
174
    return true;
175
    return true;
175
}
176
}
176
Console_RegisterCommand(Dimmer_RelativeFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_Directions(), Dimmer_StepsLevels()); });
177
Console_RegisterCommand(Dimmer_RelativeFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_Directions(), Dimmer_StepsLevels()); });
177
 
178
 
178
function Dimmer_Demo(alias_name, speed, steps)
179
function Dimmer_Demo(alias_name, speed, steps)
179
{
180
{
180
    if (arguments.length < 3)
181
    if (arguments.length < 3)
181
    {
182
    {
182
        Log("\033[31mNot enough parameters given.\033[0m\n");
183
        Log("\033[31mNot enough parameters given.\033[0m\n");
183
        return false;
184
        return false;
184
    }
185
    }
185
   
186
   
186
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
187
    var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
187
    var found = false;
188
    var found = false;
188
   
189
   
189
    for (var name in aliases_data)
190
    for (var name in aliases_data)
190
    {
191
    {
191
        if (aliases_data[name]["module_name"]=="hwPWM") {
192
        if (aliases_data[name]["module_name"]=="hwPWM") {
192
          steps = steps*39;
193
          steps = steps*39;
193
        }
194
        }
194
        var variables = {
195
        var variables = {
195
        "Channel"   : aliases_data[name]["specific"]["Channel"],
196
        "Channel"   : aliases_data[name]["specific"]["Channel"],
196
        "Speed"     : speed,
197
        "Speed"     : speed,
197
        "Steps"     : steps };
198
        "Steps"     : steps };
198
       
199
       
199
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Demo", variables))
200
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Demo", variables))
200
        {
201
        {
201
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
202
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
202
        }
203
        }
203
        else
204
        else
204
        {
205
        {
205
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
206
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
206
        }
207
        }
207
       
208
       
208
        found = true;
209
        found = true;
209
    }
210
    }
210
   
211
   
211
    if (!found)
212
    if (!found)
212
    {
213
    {
213
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
214
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
214
        return false;
215
        return false;
215
    }
216
    }
216
   
217
   
217
    return true;
218
    return true;
218
}
219
}
219
Console_RegisterCommand(Dimmer_Demo, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_StepsLevels()); });
220
Console_RegisterCommand(Dimmer_Demo, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_StepsLevels()); });
220
 
221
 
221
function Dimmer_OnMessage(module_name, module_id, command, variables)
222
function Dimmer_OnMessage(module_name, module_id, command, variables)
222
{
223
{
223
    if (in_array(Dimmer_ModuleNames, module_name))
224
    if (in_array(Dimmer_ModuleNames, module_name))
224
    {
225
    {
225
        var aliases_data = Module_LookupAliases({
226
        var aliases_data = Module_LookupAliases({
226
            "module_name" : module_name,
227
            "module_name" : module_name,
227
            "module_id"   : module_id,
228
            "module_id"   : module_id,
228
            "group"       : false
229
            "group"       : false
229
        });
230
        });
230
       
231
       
231
        switch (command)
232
        switch (command)
232
        {
233
        {
233
            case "Netinfo":
234
            case "Netinfo":
234
            {
235
            {
235
                for (var alias_name in aliases_data)
236
                for (var alias_name in aliases_data)
236
                {
237
                {
237
                    if (aliases_data[alias_name]["specific"]["Channel"] != variables["Channel"])
238
                    if (aliases_data[alias_name]["specific"]["Channel"] != variables["Channel"])
238
                    {
239
                    {
239
                        continue;
240
                        continue;
240
                    }
241
                    }
241
                   
242
                   
242
                    var last_value = {};
243
                    var last_value = {};
243
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
244
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
244
 
245
 
245
                    if (last_value_string)
246
                    if (last_value_string)
246
                    {
247
                    {
247
                        last_value = eval("(" + last_value_string + ")");
248
                        last_value = eval("(" + last_value_string + ")");
248
                    }
249
                    }
249
                   
250
                   
250
                    last_value["Connection"] = { "value" : variables["Connection"], "timestamp" : get_time() };
251
                    last_value["Connection"] = { "value" : variables["Connection"], "timestamp" : get_time() };
251
                    last_value["Frequency"] = { "value" : variables["Frequency"], "timestamp" : get_time() };
252
                    last_value["Frequency"] = { "value" : variables["Frequency"], "timestamp" : get_time() };
252
                    last_value["Level"] = { "value" : variables["DimmerValue"], "timestamp" : get_time() };
253
                    last_value["Level"] = { "value" : variables["DimmerValue"], "timestamp" : get_time() };
253
                   
254
                   
254
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
255
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
255
                }
256
                }
256
               
257
               
257
                break;
258
                break;
258
            }
259
            }
259
            case "Pwm":
260
            case "Pwm":
260
            {
261
            {
261
                for (var alias_name in aliases_data)
262
                for (var alias_name in aliases_data)
262
                {
263
                {
263
                    if (aliases_data[alias_name]["specific"]["Channel"] != variables["Channel"])
264
                    if (aliases_data[alias_name]["specific"]["Channel"] != variables["Channel"])
264
                    {
265
                    {
265
                        continue;
266
                        continue;
266
                    }
267
                    }
267
                   
268
                   
268
                    var last_value = {};
269
                    var last_value = {};
269
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
270
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
270
 
271
 
271
                    if (last_value_string)
272
                    if (last_value_string)
272
                    {
273
                    {
273
                        last_value = eval("(" + last_value_string + ")");
274
                        last_value = eval("(" + last_value_string + ")");
274
                    }
275
                    }
275
                    var value = 0 + variables["Value"];
276
                    var value = 0 + variables["Value"];
276
                    value = value / 39;
277
                    value = value / 39;
277
                    value = parseInt(value).toString();
278
                    value = parseInt(value).toString();
278
                    last_value["Level"] = { "value" : value, "timestamp" : get_time() };
279
                    last_value["Level"] = { "value" : value, "timestamp" : get_time() };
279
                   
280
                   
280
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
281
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
281
                }
282
                }
282
               
283
               
283
                break;
284
                break;
284
            }
285
            }
285
        }
286
        }
286
    }
287
    }
287
}
288
}
288
Module_RegisterToOnMessage("all", Dimmer_OnMessage);
289
Module_RegisterToOnMessage("all", Dimmer_OnMessage);
289
 
290