Rev 2169 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2169 | arune | 1 | |
| 2 | //Require("plugin/module.js"); |
||
| 3 | |||
| 4 | Water_ModuleNames = [ "water" ]; |
||
| 5 | Water_Intervals = function() { return [ 1, 5, 10, 15, 20 ]; }; |
||
| 6 | Water_Volume = function() { return [ 0, 500, 1000, 5000, 10000 ]; }; |
||
| 7 | Water_Aliases = function() { return Module_GetAliasNames(Water_ModuleNames); }; |
||
| 8 | Water_AvailableIds = function() { return Module_GetAvailableIds(Water_ModuleNames); }; |
||
| 9 | |||
| 2188 | arune | 10 | |
| 2169 | arune | 11 | function Water_SetVolume(alias_name, volume) |
| 12 | { |
||
| 13 | if (arguments.length < 2) |
||
| 14 | { |
||
| 15 | Log("\033[31mNot enough parameters given.\033[0m\n"); |
||
| 16 | return false; |
||
| 17 | } |
||
| 18 | |||
| 19 | var aliases_data = Module_ResolveAlias(alias_name, Water_ModuleNames); |
||
| 20 | var found = false; |
||
| 21 | |||
| 22 | for (var name in aliases_data) |
||
| 23 | { |
||
| 24 | var variables = {"Volume" : volume }; |
||
| 25 | |||
| 26 | if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "setVolume", variables)) |
||
| 27 | { |
||
| 28 | Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n"); |
||
| 29 | } |
||
| 30 | else |
||
| 31 | { |
||
| 32 | Log("\033[31mFailed to send command to " + name + ".\033[0m\n"); |
||
| 33 | } |
||
| 34 | |||
| 35 | found = true; |
||
| 36 | } |
||
| 37 | |||
| 38 | if (!found) |
||
| 39 | { |
||
| 40 | Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n"); |
||
| 41 | return false; |
||
| 42 | } |
||
| 43 | |||
| 44 | return true; |
||
| 45 | } |
||
| 46 | Console_RegisterCommand(Water_SetVolume, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Water_Aliases(), Water_Volume()); }); |
||
| 2188 | arune | 47 | |
| 48 | |||
| 2169 | arune | 49 | function Water_SetReportInterval(alias_name, interval) |
| 50 | { |
||
| 51 | if (arguments.length < 2) |
||
| 52 | { |
||
| 53 | Log("\033[31mNot enough parameters given.\033[0m\n"); |
||
| 54 | return false; |
||
| 55 | } |
||
| 56 | |||
| 57 | var aliases_data = Module_ResolveAlias(alias_name, Water_ModuleNames); |
||
| 58 | var found = false; |
||
| 59 | |||
| 60 | for (var name in aliases_data) |
||
| 61 | { |
||
| 62 | var variables = {"Time" : interval }; |
||
| 63 | |||
| 64 | if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Report_Interval", variables)) |
||
| 65 | { |
||
| 66 | Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n"); |
||
| 67 | } |
||
| 68 | else |
||
| 69 | { |
||
| 70 | Log("\033[31mFailed to send command to " + name + ".\033[0m\n"); |
||
| 71 | } |
||
| 72 | |||
| 73 | found = true; |
||
| 74 | } |
||
| 75 | |||
| 76 | if (!found) |
||
| 77 | { |
||
| 78 | Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n"); |
||
| 79 | return false; |
||
| 80 | } |
||
| 81 | |||
| 82 | return true; |
||
| 83 | } |
||
| 84 | Console_RegisterCommand(Water_SetReportInterval, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Water_Aliases(), Water_Intervals()); }); |
||
| 85 | |||
| 86 | function Water_OnMessage(module_name, module_id, command, variables) |
||
| 87 | { |
||
| 88 | if (in_array(Water_ModuleNames, module_name)) |
||
| 89 | { |
||
| 90 | var aliases_data = Module_LookupAliases({ |
||
| 91 | "module_name" : module_name, |
||
| 92 | "module_id" : module_id, |
||
| 93 | "group" : false |
||
| 94 | }); |
||
| 95 | |||
| 96 | switch (command) |
||
| 97 | { |
||
| 98 | case "Flow": |
||
| 99 | { |
||
| 100 | for (var alias_name in aliases_data) |
||
| 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["Flow"] = { "value" : variables["Flow"], "timestamp" : get_time() }; |
||
| 111 | last_value["Volume"] = { "value" : variables["Volume"], "timestamp" : get_time() }; |
||
| 112 | |||
| 113 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
||
| 114 | } |
||
| 115 | |||
| 116 | break; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | Module_RegisterToOnMessage("all", Water_OnMessage); |