Subversion Repositories HomeAutomation

Rev

Rev 1925 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1925 cougar 1
 
2
Counter_ModuleNames    = [ "counter" ];
3
Counter_Intervals      = function() { return [ 1, 5, 10, 15, 20 ]; };
4
Counter_Count          = function() { return [ 0, 500, 1000, 5000, 10000 ]; };
5
Counter_Aliases        = function() { return Module_GetAliasNames(Counter_ModuleNames); };
6
Counter_AvailableIds   = function() { return Module_GetAvailableIds(Counter_ModuleNames); };
7
 
8
function Counter_SetCount(alias_name, count)
9
{
10
    if (arguments.length < 2)
11
    {
12
        Log("\033[31mNot enough parameters given.\033[0m\n");
13
        return false;
14
    }
15
 
16
    var aliases_data = Module_ResolveAlias(alias_name, Counter_ModuleNames);
17
    var found = false;
18
 
19
    for (var name in aliases_data)
20
    {
2305 cougar 21
        var variables = {
22
            "PinId" : aliases_data[name]["specific"]["PinId"],
23
            "Count" : count };
1925 cougar 24
 
25
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "setCounter", variables))
26
        {
27
            Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
28
        }
29
        else
30
        {
31
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
32
        }
33
 
34
        found = true;
35
    }
36
 
37
    if (!found)
38
    {
39
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
40
        return false;
41
    }
42
 
43
    return true;
44
}
45
Console_RegisterCommand(Counter_SetCount, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Counter_Aliases(), Counter_Count()); });
46
 
47
function Counter_SetReportInterval(alias_name, interval)
48
{
49
    if (arguments.length < 2)
50
    {
51
        Log("\033[31mNot enough parameters given.\033[0m\n");
52
        return false;
53
    }
54
 
55
    var aliases_data = Module_ResolveAlias(alias_name, Counter_ModuleNames);
56
    var found = false;
57
 
58
    for (var name in aliases_data)
59
    {
60
        var variables = {"Time"     : interval };
61
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Report_Interval", variables))
62
        {
63
            Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
64
        }
65
        else
66
        {
67
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
68
        }
69
 
70
        found = true;
71
    }
72
 
73
    if (!found)
74
    {
75
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
76
        return false;
77
    }
78
 
79
    return true;
80
}
81
Console_RegisterCommand(Counter_SetReportInterval, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Counter_Aliases(), Counter_Intervals()); });
82
 
83
function Counter_OnMessage(module_name, module_id, command, variables)
84
{
85
    if (in_array(Counter_ModuleNames, module_name))
86
    {
87
        var aliases_data = Module_LookupAliases({
88
            "module_name" : module_name,
89
            "module_id"   : module_id,
90
            "group"       : false
91
        });
92
 
93
        switch (command)
94
        {
95
            case "Counter":
96
            {
97
                for (var alias_name in aliases_data)
98
                {
99
                    if (aliases_data[alias_name]["specific"]["PinId"] != variables["PinId"])
100
                    {
101
                        continue;
102
                    }
103
 
104
                    var last_value = {};
105
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
106
 
107
                    if (last_value_string)
108
                    {
109
                        last_value = eval("(" + last_value_string + ")");
110
                    }
111
 
112
                    last_value[command] = { "value" : variables["Count"], "timestamp" : get_time() };
113
 
114
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
115
                }
116
                break;
117
            }
118
        }
119
    }
120
}
121
Module_RegisterToOnMessage("all", Counter_OnMessage);