Rev 1647 | Rev 1649 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1602 | runge | 1 | |
| 1644 | runge | 2 | Require("plugin/storage.js"); |
| 1603 | runge | 3 | |
| 1647 | runge | 4 | function Module_CreateAliasAutoComplete(args) |
| 5 | { |
||
| 6 | // args[0] == command name |
||
| 7 | var arg_index = args.length - 2; |
||
| 8 | |||
| 9 | if (arg_index == 0) |
||
| 10 | { |
||
| 11 | return Module_GetAliasNames(); |
||
| 12 | } |
||
| 13 | else if (arg_index == 1) |
||
| 14 | { |
||
| 15 | return Module_GetNames(); |
||
| 16 | } |
||
| 17 | else if (arg_index == 2) |
||
| 18 | { |
||
| 19 | return Module_GetAvailableIds([ args[2] ]) |
||
| 20 | } |
||
| 21 | |||
| 22 | return []; |
||
| 23 | } |
||
| 24 | |||
| 25 | function Module_CreateAlias(alias_name, module_name, module_id, specific) |
||
| 26 | { |
||
| 27 | // TODO Check arguments |
||
| 28 | |||
| 29 | var specific_list = {}; |
||
| 30 | |||
| 31 | if (specific) |
||
| 32 | { |
||
| 33 | var specific_parts = split(",", specific); |
||
| 34 | |||
| 35 | // TODO: Check length |
||
| 36 | |||
| 37 | for (var index in specific_parts) |
||
| 38 | { |
||
| 39 | var parts = split("=", specific_parts[index]); |
||
| 40 | |||
| 41 | // TODO: Check length |
||
| 42 | |||
| 43 | specific_list[parts[0]] = parts[1]; |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | var alias_data = { |
||
| 48 | "group" : false, |
||
| 49 | "name" : alias_name, |
||
| 50 | "module_name" : module_name, |
||
| 51 | "module_id" : module_id, |
||
| 52 | "specific" : specific_list |
||
| 53 | }; |
||
| 54 | |||
| 55 | return Storage_SetParameter("alias", alias_name, JSON.stringify(alias_data)); |
||
| 56 | } |
||
| 57 | Console_RegisterCommand(Module_CreateAlias, Module_CreateAliasAutoComplete); |
||
| 58 | |||
| 1644 | runge | 59 | function Module_CreateAliasGroupAutoComplete(args) |
| 1624 | runge | 60 | { |
| 1644 | runge | 61 | // args[0] == command name |
| 62 | var arg_index = args.length - 2; |
||
| 63 | |||
| 64 | if (arg_index == 0) |
||
| 1603 | runge | 65 | { |
| 1644 | runge | 66 | return Module_GetAliasNames(); |
| 1603 | runge | 67 | } |
| 1644 | runge | 68 | else if (arg_index == 1) |
| 69 | { |
||
| 1647 | runge | 70 | return Module_GetNames(); |
| 1644 | runge | 71 | } |
| 72 | else if (arg_index >= 2) |
||
| 73 | { |
||
| 74 | return Module_GetAliasNames([ args[2] ]) |
||
| 75 | } |
||
| 1603 | runge | 76 | |
| 1644 | runge | 77 | return []; |
| 1603 | runge | 78 | } |
| 79 | |||
| 1644 | runge | 80 | function Module_CreateAliasGroup(alias_group_name, module_type, alias_name1, alias_name2) |
| 1602 | runge | 81 | { |
| 1644 | runge | 82 | if (arguments.length < 3) |
| 1604 | runge | 83 | { |
| 1644 | runge | 84 | throw("Not enought parameters given"); |
| 1604 | runge | 85 | } |
| 86 | |||
| 1644 | runge | 87 | alias_group_name = arguments[0]; |
| 88 | module_type = arguments[1]; |
||
| 89 | var aliases = []; |
||
| 1603 | runge | 90 | |
| 1644 | runge | 91 | for (var n = 2; n < arguments.length; n++) |
| 1603 | runge | 92 | { |
| 1644 | runge | 93 | if (arguments[n].length > 0) |
| 94 | { |
||
| 95 | var aliases_data = Module_ResolveAlias(arguments[n]); |
||
| 96 | aliases.push(arguments[n]); |
||
| 97 | } |
||
| 1603 | runge | 98 | } |
| 1602 | runge | 99 | |
| 1644 | runge | 100 | if (aliases.length == 0) |
| 1604 | runge | 101 | { |
| 1644 | runge | 102 | throw("No valid aliases were found, aborting creation of group alias"); |
| 1604 | runge | 103 | } |
| 104 | |||
| 1644 | runge | 105 | var alias_data = { |
| 106 | "group" : true, |
||
| 107 | "name" : alias_group_name, |
||
| 108 | "module_name" : module_type, |
||
| 109 | "aliases" : aliases |
||
| 110 | }; |
||
| 1603 | runge | 111 | |
| 1644 | runge | 112 | return Storage_SetParameter("alias", alias_group_name, JSON.stringify(alias_data)); |
| 1602 | runge | 113 | } |
| 1644 | runge | 114 | Console_RegisterCommand(Module_CreateAliasGroup, Module_CreateAliasGroupAutoComplete); |
| 1602 | runge | 115 | |
| 1644 | runge | 116 | function Module_ResolveAlias(alias_name, filter_module_names) |
| 1602 | runge | 117 | { |
| 1644 | runge | 118 | var aliases_data = {}; |
| 119 | var alias_data_string = Storage_GetParameter("alias", alias_name); |
||
| 1602 | runge | 120 | |
| 1644 | runge | 121 | if (alias_data_string) |
| 1602 | runge | 122 | { |
| 1644 | runge | 123 | var alias_data = eval("(" + alias_data_string + ")"); |
| 1603 | runge | 124 | |
| 1644 | runge | 125 | if (alias_data["group"]) |
| 1603 | runge | 126 | { |
| 1644 | runge | 127 | for (var n in alias_data["aliases"]) |
| 128 | { |
||
| 1646 | runge | 129 | var aliases_data_sub = Module_ResolveAlias(alias_data["aliases"][n], filter_module_names); |
| 1644 | runge | 130 | |
| 131 | for (var name in aliases_data_sub) |
||
| 132 | { |
||
| 133 | aliases_data[name] = aliases_data_sub[name]; |
||
| 134 | } |
||
| 135 | } |
||
| 1603 | runge | 136 | } |
| 1644 | runge | 137 | else |
| 138 | { |
||
| 139 | if (in_array(filter_module_names, alias_data["module_name"])) |
||
| 140 | { |
||
| 141 | aliases_data[alias_name] = alias_data; |
||
| 142 | } |
||
| 143 | } |
||
| 1602 | runge | 144 | } |
| 145 | |||
| 1644 | runge | 146 | return aliases_data; |
| 1602 | runge | 147 | } |
| 1603 | runge | 148 | |
| 1644 | runge | 149 | function Module_LookupAliases(match_filter) |
| 1603 | runge | 150 | { |
| 1644 | runge | 151 | var aliases_data_filtered = {}; |
| 152 | var aliases_data = Storage_GetParameters("alias"); |
||
| 1603 | runge | 153 | |
| 1644 | runge | 154 | for (var name in aliases_data) |
| 155 | { |
||
| 156 | var alias_data = eval("(" + aliases_data[name] + ")"); |
||
| 157 | |||
| 158 | var valid = true; |
||
| 159 | |||
| 160 | for (var key in match_filter) |
||
| 161 | { |
||
| 162 | if (alias_data[key] != match_filter[key]) |
||
| 163 | { |
||
| 164 | valid = false; |
||
| 165 | break; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | if (valid) |
||
| 170 | { |
||
| 171 | aliases_data_filtered[name] = alias_data; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | return aliases_data_filtered; |
||
| 1603 | runge | 176 | } |
| 177 | |||
| 1644 | runge | 178 | function Module_GetAliasNames(filter_module_names) |
| 1603 | runge | 179 | { |
| 1644 | runge | 180 | var aliases_name_filtered = []; |
| 181 | var aliases_data = Storage_GetParameters("alias"); |
||
| 182 | |||
| 183 | for (var name in aliases_data) |
||
| 1603 | runge | 184 | { |
| 1644 | runge | 185 | var alias_data = eval("(" + aliases_data[name] + ")"); |
| 186 | |||
| 187 | if (!filter_module_names || in_array(filter_module_names, alias_data["module_name"])) |
||
| 188 | { |
||
| 189 | aliases_name_filtered.push(name) |
||
| 190 | } |
||
| 1603 | runge | 191 | } |
| 1644 | runge | 192 | |
| 193 | return aliases_name_filtered; |
||
| 1603 | runge | 194 | } |
| 195 | |||
| 1644 | runge | 196 | function Module_GetAvailableIds(filter_module_names) |
| 1603 | runge | 197 | { |
| 1644 | runge | 198 | var result_list = []; |
| 199 | var available_modules = ModuleExport_GetAvailableModules(); |
||
| 1603 | runge | 200 | |
| 1644 | runge | 201 | for (var n in available_modules) |
| 1603 | runge | 202 | { |
| 1644 | runge | 203 | var id_parts = available_modules[n].split(":", 2); |
| 204 | |||
| 205 | if (in_array(filter_module_names, id_parts[0])) |
||
| 1603 | runge | 206 | { |
| 1644 | runge | 207 | result_list.push(id_parts[1]); |
| 1603 | runge | 208 | } |
| 209 | } |
||
| 210 | |||
| 1644 | runge | 211 | return result_list; |
| 1603 | runge | 212 | } |
| 213 | |||
| 1647 | runge | 214 | function Module_GetNames() |
| 215 | { |
||
| 1648 | runge | 216 | return [ "Dimmer230", "irTransmit", "irReceive", "SimpleDTMF", "BusVoltage" ]; |
| 1647 | runge | 217 | } |
| 218 | |||
| 1644 | runge | 219 | function Module_SendMessage(module_name, module_id, command, variables) |
| 1614 | runge | 220 | { |
| 1644 | runge | 221 | var args = []; |
| 1614 | runge | 222 | |
| 1644 | runge | 223 | args.push(module_name + ":" + module_id); |
| 224 | args.push(command); |
||
| 1619 | runge | 225 | |
| 1644 | runge | 226 | var length = 0; |
| 227 | for (var key in variables) |
||
| 1614 | runge | 228 | { |
| 1644 | runge | 229 | length++; |
| 1614 | runge | 230 | } |
| 231 | |||
| 1644 | runge | 232 | args.push(length); |
| 233 | |||
| 234 | for (var key in variables) |
||
| 235 | { |
||
| 236 | args.push(key); |
||
| 237 | args.push(variables[key]); |
||
| 238 | } |
||
| 239 | |||
| 240 | return ModuleExport_SendMessage.apply(null, Array.prototype.slice.call(args, 0)); |
||
| 1614 | runge | 241 | } |
| 242 | |||
| 1644 | runge | 243 | var Module_OnChangeFunctions = {}; |
| 244 | var Module_OnMessageFunctions = {}; |
||
| 245 | |||
| 246 | function Module_RegisterToOnChange(alias_name, callback) |
||
| 247 | { |
||
| 248 | if (!Module_OnChangeFunctions[alias_name]) |
||
| 1603 | runge | 249 | { |
| 1644 | runge | 250 | Module_OnChangeFunctions[alias_name] = []; |
| 1603 | runge | 251 | } |
| 252 | |||
| 1644 | runge | 253 | Module_OnChangeFunctions[alias_name].push(callback); |
| 1603 | runge | 254 | } |
| 255 | |||
| 1644 | runge | 256 | function Module_RegisterToOnMessage(alias_name, callback) |
| 1603 | runge | 257 | { |
| 1644 | runge | 258 | if (!Module_OnMessageFunctions[alias_name]) |
| 1603 | runge | 259 | { |
| 1644 | runge | 260 | Module_OnMessageFunctions[alias_name] = []; |
| 1603 | runge | 261 | } |
| 1644 | runge | 262 | |
| 263 | Module_OnMessageFunctions[alias_name].push(callback); |
||
| 1603 | runge | 264 | } |
| 1609 | runge | 265 | |
| 1644 | runge | 266 | function Module_OnMessage(full_id, command, variables) |
| 1609 | runge | 267 | { |
| 1644 | runge | 268 | var id_parts = full_id.split(":", 2); |
| 269 | var aliases_data = {}; |
||
| 1647 | runge | 270 | |
| 271 | var aliases_data = Module_LookupAliases({ |
||
| 272 | "module_name" : id_parts[0], |
||
| 273 | "module_id" : id_parts[1], |
||
| 274 | "group" : false |
||
| 275 | }); |
||
| 1609 | runge | 276 | |
| 1647 | runge | 277 | for (var alias_name in aliases_data) |
| 1609 | runge | 278 | { |
| 1647 | runge | 279 | var ok = true; |
| 280 | |||
| 281 | if (aliases_data[alias_name]["specific"]) |
||
| 1644 | runge | 282 | { |
| 1647 | runge | 283 | var ok = true; |
| 284 | |||
| 285 | for (var name in variables) |
||
| 286 | { |
||
| 287 | if (aliases_data[alias_name]["specific"][name] && aliases_data[alias_name]["specific"][name] != variables[name]) |
||
| 288 | { |
||
| 289 | ok = false; |
||
| 290 | break; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | if (Module_OnMessageFunctions[alias_name] && ok) |
||
| 296 | { |
||
| 1644 | runge | 297 | for (var n in Module_OnMessageFunctions[alias_name]) |
| 298 | { |
||
| 299 | Module_OnMessageFunctions[alias_name][n](alias_name, command, variables); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 1609 | runge | 303 | } |
| 304 | |||
| 1644 | runge | 305 | function Module_OnChange(full_id, available) |
| 1642 | runge | 306 | { |
| 1644 | runge | 307 | var id_parts = full_id.split(":", 2); |
| 1647 | runge | 308 | var aliases_data = {}; |
| 1644 | runge | 309 | |
| 1647 | runge | 310 | var aliases_data_all = Module_LookupAliases({ |
| 1644 | runge | 311 | "module_name" : id_parts[0], |
| 312 | "module_id" : id_parts[1], |
||
| 1647 | runge | 313 | "group" : false |
| 1644 | runge | 314 | }); |
| 315 | |||
| 1647 | runge | 316 | for (var alias_name in aliases_data) |
| 1644 | runge | 317 | { |
| 1647 | runge | 318 | var ok = true; |
| 319 | |||
| 320 | if (aliases_data[alias_name]["specific"]) |
||
| 1644 | runge | 321 | { |
| 1647 | runge | 322 | var ok = true; |
| 323 | |||
| 324 | for (var name in variables) |
||
| 325 | { |
||
| 326 | if (aliases_data[alias_name]["specific"][name] && aliases_data[alias_name]["specific"][name] != variables[name]) |
||
| 327 | { |
||
| 328 | ok = false; |
||
| 329 | break; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | if (Module_OnChangeFunctions[alias_name] && ok) |
||
| 335 | { |
||
| 1644 | runge | 336 | for (var n in Module_OnChangeFunctions[alias_name]) |
| 337 | { |
||
| 338 | Module_OnChangeFunctions[alias_name][n](alias_name, available); |
||
| 339 | } |
||
| 340 | } |
||
| 341 | } |
||
| 1642 | runge | 342 | } |