Rev 1765 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1765 | linlun | 1 | |
| 2 | //Require("plugin/module.js"); |
||
| 3 | |||
| 2027 | arune | 4 | Power_ModuleNames = [ "power", "heatPower" ]; |
| 1765 | linlun | 5 | Power_Intervals = function() { return [ 1, 5, 10, 15, 20 ]; }; |
| 6 | Power_Energy = function() { return [ 0, 500, 1000, 5000, 10000 ]; }; |
||
| 7 | Power_Aliases = function() { return Module_GetAliasNames(Power_ModuleNames); }; |
||
| 8 | Power_AvailableIds = function() { return Module_GetAvailableIds(Power_ModuleNames); }; |
||
| 9 | |||
| 10 | function Power_SetEnergy(alias_name, energy) |
||
| 11 | { |
||
| 12 | if (arguments.length < 2) |
||
| 13 | { |
||
| 14 | Log("\033[31mNot enough parameters given.\033[0m\n"); |
||
| 15 | return false; |
||
| 16 | } |
||
| 17 | |||
| 18 | var aliases_data = Module_ResolveAlias(alias_name, Power_ModuleNames); |
||
| 19 | var found = false; |
||
| 20 | |||
| 21 | for (var name in aliases_data) |
||
| 22 | { |
||
| 23 | var variables = {"Energy" : energy }; |
||
| 24 | |||
| 25 | if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "setEnergy", 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(Power_SetEnergy, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Power_Aliases(), Power_Energy()); }); |
||
| 46 | |||
| 47 | function Power_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, Power_ModuleNames); |
||
| 56 | var found = false; |
||
| 57 | |||
| 58 | for (var name in aliases_data) |
||
| 59 | { |
||
| 60 | var variables = {"Time" : interval }; |
||
| 61 | |||
| 62 | if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Report_Interval", variables)) |
||
| 63 | { |
||
| 64 | Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n"); |
||
| 65 | } |
||
| 66 | else |
||
| 67 | { |
||
| 68 | Log("\033[31mFailed to send command to " + name + ".\033[0m\n"); |
||
| 69 | } |
||
| 70 | |||
| 71 | found = true; |
||
| 72 | } |
||
| 73 | |||
| 74 | if (!found) |
||
| 75 | { |
||
| 76 | Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n"); |
||
| 77 | return false; |
||
| 78 | } |
||
| 79 | |||
| 80 | return true; |
||
| 81 | } |
||
| 82 | Console_RegisterCommand(Power_SetReportInterval, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Power_Aliases(), Power_Intervals()); }); |
||
| 83 | |||
| 84 | function Power_OnMessage(module_name, module_id, command, variables) |
||
| 85 | { |
||
| 86 | if (in_array(Power_ModuleNames, module_name)) |
||
| 87 | { |
||
| 88 | var aliases_data = Module_LookupAliases({ |
||
| 89 | "module_name" : module_name, |
||
| 90 | "module_id" : module_id, |
||
| 91 | "group" : false |
||
| 92 | }); |
||
| 93 | |||
| 94 | switch (command) |
||
| 95 | { |
||
| 96 | /* |
||
| 97 | case "avgPower": |
||
| 98 | { |
||
| 99 | for (var alias_name in aliases_data) |
||
| 100 | { |
||
| 101 | var last_value = {}; |
||
| 102 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
||
| 103 | |||
| 104 | if (last_value_string) |
||
| 105 | { |
||
| 106 | last_value = eval("(" + last_value_string + ")"); |
||
| 107 | } |
||
| 108 | |||
| 109 | last_value["Energy"] = { "value" : variables["Energy"], "timestamp" : get_time() }; |
||
| 110 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
||
| 111 | } |
||
| 112 | |||
| 113 | break; |
||
| 114 | } |
||
| 115 | */ |
||
| 116 | case "ElectricPower": |
||
| 117 | { |
||
| 118 | for (var alias_name in aliases_data) |
||
| 119 | { |
||
| 120 | var last_value = {}; |
||
| 121 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
||
| 122 | |||
| 123 | if (last_value_string) |
||
| 124 | { |
||
| 125 | last_value = eval("(" + last_value_string + ")"); |
||
| 126 | } |
||
| 127 | |||
| 128 | last_value["Power"] = { "value" : variables["Power"], "timestamp" : get_time() }; |
||
| 129 | last_value["EnergySum"] = { "value" : variables["EnergySum"], "timestamp" : get_time() }; |
||
| 130 | last_value["Power32"] = { "value" : variables["Power32"], "timestamp" : get_time() }; |
||
| 131 | |||
| 132 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
||
| 133 | } |
||
| 134 | |||
| 135 | break; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | Module_RegisterToOnMessage("all", Power_OnMessage); |