Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1765 | linlun | 1 | |
| 2 | Output_ModuleNames = [ "output","multiSwitch"]; |
||
| 3 | Output_ModuleNamesOutput = [ "output"]; |
||
| 4 | Output_ModuleNamesSwitch = [ "multiSwitch"]; |
||
| 5 | Output_Aliases = function() { return Module_GetAliasNames(Output_ModuleNames); }; |
||
| 6 | Output_Aliases_output = function() { return Module_GetAliasNames(Output_ModuleNamesOutput); }; |
||
| 7 | Output_Aliases_Switch = function() { return Module_GetAliasNames(Output_ModuleNamesSwitch); }; |
||
| 8 | Output_SwitchState = function() { return [ "0", "1", "2", "3" ]; }; |
||
| 9 | Output_AvailableIds = function() { return Module_GetAvailableIds(Output_ModuleNames); }; |
||
| 10 | Output_AvailableIdsOutput = function() { return Module_GetAvailableIds(Output_ModuleNamesOutput); }; |
||
| 11 | Output_AvailableIdsSwitch = function() { return Module_GetAvailableIds(Output_ModuleNamesSwitch); }; |
||
| 12 | Output_pin = function() { return [ "High", "Low" ]; }; |
||
| 13 | |||
| 14 | //Log("\033[31mStarting input.\033[0m\n"); |
||
| 15 | |||
| 16 | |||
| 17 | function Output_OnMessage(module_name, module_id, command, variables) |
||
| 18 | { |
||
| 19 | //Log("\033[31mGot message "+module_name + " "+ module_id+" "+command+ "if: "+in_array(Output_ModuleNames, module_name)+".\033[0m\n"); |
||
| 20 | |||
| 21 | if (in_array(Output_ModuleNames, module_name)) |
||
| 22 | { |
||
| 23 | // Log("\033[31mGot message2 "+command+".\033[0m\n"); |
||
| 24 | var aliases_data = Module_LookupAliases({ |
||
| 25 | "module_name" : module_name, |
||
| 26 | "module_id" : module_id, |
||
| 27 | "group" : false |
||
| 28 | }); |
||
| 29 | |||
| 30 | switch (command) |
||
| 31 | { |
||
| 32 | case "SetPin": |
||
| 33 | { |
||
| 34 | // Log("\033[31mGot PinStatus message.\033[0m\n"); |
||
| 35 | for (var alias_name in aliases_data) |
||
| 36 | { |
||
| 37 | if (aliases_data[alias_name]["module_name"] == "multiSwitch") |
||
| 38 | { |
||
| 39 | var last_value = {}; |
||
| 40 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
||
| 41 | |||
| 42 | if (last_value_string) |
||
| 43 | { |
||
| 44 | last_value = eval("(" + last_value_string + ")"); |
||
| 45 | } |
||
| 46 | |||
| 47 | last_value["Status"] = { "value" : variables["PinId"], "timestamp" : get_time() }; |
||
| 48 | |||
| 49 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
||
| 50 | continue; |
||
| 51 | } |
||
| 52 | if (aliases_data[alias_name]["specific"]["PinId"] != variables["PinId"]) |
||
| 53 | { |
||
| 54 | continue; |
||
| 55 | } |
||
| 56 | // Log("\033[31mMessage from: "+alias_name+".\033[0m\n"); |
||
| 57 | var last_value = {}; |
||
| 58 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
||
| 59 | |||
| 60 | if (last_value_string) |
||
| 61 | { |
||
| 62 | last_value = eval("(" + last_value_string + ")"); |
||
| 63 | } |
||
| 64 | |||
| 65 | last_value["Status"] = { "value" : variables["Status"], "timestamp" : get_time() }; |
||
| 66 | |||
| 67 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
||
| 68 | } |
||
| 69 | |||
| 70 | break; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | Module_RegisterToOnMessage("all", Output_OnMessage); |
||
| 76 | |||
| 77 | function Output_SetPin(alias_name, status) |
||
| 78 | { |
||
| 79 | if (arguments.length < 2) |
||
| 80 | { |
||
| 81 | Log("\033[31mNot enough parameters given.\033[0m\n"); |
||
| 82 | return false; |
||
| 83 | } |
||
| 84 | |||
| 85 | var aliases_data = Module_ResolveAlias(alias_name, Output_ModuleNamesOutput); |
||
| 86 | var found = false; |
||
| 87 | |||
| 88 | for (var name in aliases_data) |
||
| 89 | { |
||
| 90 | var variables = { |
||
| 91 | "PinId" : aliases_data[name]["specific"]["PinId"], |
||
| 92 | "Status" : status}; |
||
| 93 | |||
| 94 | if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "SetPin", variables)) |
||
| 95 | { |
||
| 96 | //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n"); |
||
| 97 | } |
||
| 98 | else |
||
| 99 | { |
||
| 100 | Log("\033[31mFailed to send command to " + name + ".\033[0m\n"); |
||
| 101 | } |
||
| 102 | |||
| 103 | found = true; |
||
| 104 | } |
||
| 105 | |||
| 106 | if (!found) |
||
| 107 | { |
||
| 108 | Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n"); |
||
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | return true; |
||
| 113 | } |
||
| 114 | Console_RegisterCommand(Output_SetPin, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Output_Aliases_output(), Output_pin()); }); |
||
| 115 | |||
| 116 | |||
| 117 | function Output_SetSwitch(alias_name, position) |
||
| 118 | { |
||
| 119 | if (arguments.length < 2) |
||
| 120 | { |
||
| 121 | Log("\033[31mNot enough parameters given.\033[0m\n"); |
||
| 122 | return false; |
||
| 123 | } |
||
| 124 | |||
| 125 | var aliases_data = Module_ResolveAlias(alias_name, Output_ModuleNamesSwitch); |
||
| 126 | var found = false; |
||
| 127 | |||
| 128 | for (var name in aliases_data) |
||
| 129 | { |
||
| 130 | var variables = { |
||
| 131 | "PinId" : position, |
||
| 132 | "Status" : "High"}; |
||
| 133 | |||
| 134 | if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "SetPin", variables)) |
||
| 135 | { |
||
| 136 | //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n"); |
||
| 137 | } |
||
| 138 | else |
||
| 139 | { |
||
| 140 | Log("\033[31mFailed to send command to " + name + ".\033[0m\n"); |
||
| 141 | } |
||
| 142 | |||
| 143 | found = true; |
||
| 144 | } |
||
| 145 | |||
| 146 | if (!found) |
||
| 147 | { |
||
| 148 | Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n"); |
||
| 149 | return false; |
||
| 150 | } |
||
| 151 | |||
| 152 | return true; |
||
| 153 | } |
||
| 154 | Console_RegisterCommand(Output_SetSwitch, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Output_Aliases_Switch(), Output_SwitchState()); }); |