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