Rev 1657 | Rev 1765 | 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 | |
| 1649 | runge | 4 | function Module_CreateAliasAutoComplete(arg_index, args) |
| 1647 | runge | 5 | { |
| 6 | if (arg_index == 0) |
||
| 7 | { |
||
| 8 | return Module_GetAliasNames(); |
||
| 9 | } |
||
| 10 | else if (arg_index == 1) |
||
| 11 | { |
||
| 12 | return Module_GetNames(); |
||
| 13 | } |
||
| 14 | else if (arg_index == 2) |
||
| 15 | { |
||
| 1649 | runge | 16 | return Module_GetAvailableIds([ args[1] ]) |
| 1647 | runge | 17 | } |
| 18 | |||
| 19 | return []; |
||
| 20 | } |
||
| 21 | |||
| 22 | function Module_CreateAlias(alias_name, module_name, module_id, specific) |
||
| 23 | { |
||
| 1649 | runge | 24 | if (arguments.length < 3) |
| 25 | { |
||
| 26 | Log("\033[31mNot enough parameters given.\033[0m\n"); |
||
| 27 | return false; |
||
| 28 | } |
||
| 1647 | runge | 29 | |
| 1649 | runge | 30 | if (alias_name == "all") |
| 31 | { |
||
| 32 | Log("\033[31m\"all\" is not allowed as alias name.\033[0m\n"); |
||
| 33 | return false; |
||
| 34 | } |
||
| 35 | |||
| 1647 | runge | 36 | var specific_list = {}; |
| 37 | |||
| 38 | if (specific) |
||
| 39 | { |
||
| 1649 | runge | 40 | var specific_parts = specific.split(","); |
| 1647 | runge | 41 | |
| 42 | // TODO: Check length |
||
| 43 | |||
| 44 | for (var index in specific_parts) |
||
| 45 | { |
||
| 1649 | runge | 46 | var parts = specific_parts[index].split("="); |
| 1647 | runge | 47 | |
| 48 | // TODO: Check length |
||
| 49 | |||
| 50 | specific_list[parts[0]] = parts[1]; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | var alias_data = { |
||
| 55 | "group" : false, |
||
| 56 | "name" : alias_name, |
||
| 57 | "module_name" : module_name, |
||
| 58 | "module_id" : module_id, |
||
| 59 | "specific" : specific_list |
||
| 60 | }; |
||
| 61 | |||
| 62 | return Storage_SetParameter("alias", alias_name, JSON.stringify(alias_data)); |
||
| 63 | } |
||
| 64 | Console_RegisterCommand(Module_CreateAlias, Module_CreateAliasAutoComplete); |
||
| 65 | |||
| 1649 | runge | 66 | function Module_CreateAliasGroupAutoComplete(arg_index, args) |
| 1624 | runge | 67 | { |
| 1644 | runge | 68 | if (arg_index == 0) |
| 1603 | runge | 69 | { |
| 1644 | runge | 70 | return Module_GetAliasNames(); |
| 1603 | runge | 71 | } |
| 1644 | runge | 72 | else if (arg_index == 1) |
| 73 | { |
||
| 1647 | runge | 74 | return Module_GetNames(); |
| 1644 | runge | 75 | } |
| 76 | else if (arg_index >= 2) |
||
| 77 | { |
||
| 1649 | runge | 78 | return Module_GetAliasNames([ args[1] ]) |
| 1644 | runge | 79 | } |
| 1603 | runge | 80 | |
| 1644 | runge | 81 | return []; |
| 1603 | runge | 82 | } |
| 83 | |||
| 1644 | runge | 84 | function Module_CreateAliasGroup(alias_group_name, module_type, alias_name1, alias_name2) |
| 1602 | runge | 85 | { |
| 1644 | runge | 86 | if (arguments.length < 3) |
| 1604 | runge | 87 | { |
| 1649 | runge | 88 | Log("\033[31mNot enough parameters given.\033[0m\n"); |
| 89 | return false; |
||
| 1604 | runge | 90 | } |
| 91 | |||
| 1649 | runge | 92 | if (alias_group_name == "all") |
| 93 | { |
||
| 94 | Log("\033[31m\"all\" is not allowed as alias name.\033[0m\n"); |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | |||
| 1644 | runge | 98 | alias_group_name = arguments[0]; |
| 99 | module_type = arguments[1]; |
||
| 100 | var aliases = []; |
||
| 1603 | runge | 101 | |
| 1644 | runge | 102 | for (var n = 2; n < arguments.length; n++) |
| 1603 | runge | 103 | { |
| 1644 | runge | 104 | if (arguments[n].length > 0) |
| 105 | { |
||
| 106 | var aliases_data = Module_ResolveAlias(arguments[n]); |
||
| 107 | aliases.push(arguments[n]); |
||
| 108 | } |
||
| 1603 | runge | 109 | } |
| 1602 | runge | 110 | |
| 1644 | runge | 111 | if (aliases.length == 0) |
| 1604 | runge | 112 | { |
| 1649 | runge | 113 | Log("\033[31mNo valid aliases were found, aborting creation of group alias.\033[0m\n"); |
| 114 | return false; |
||
| 1604 | runge | 115 | } |
| 116 | |||
| 1644 | runge | 117 | var alias_data = { |
| 118 | "group" : true, |
||
| 119 | "name" : alias_group_name, |
||
| 120 | "module_name" : module_type, |
||
| 121 | "aliases" : aliases |
||
| 122 | }; |
||
| 1603 | runge | 123 | |
| 1644 | runge | 124 | return Storage_SetParameter("alias", alias_group_name, JSON.stringify(alias_data)); |
| 1602 | runge | 125 | } |
| 1644 | runge | 126 | Console_RegisterCommand(Module_CreateAliasGroup, Module_CreateAliasGroupAutoComplete); |
| 1602 | runge | 127 | |
| 1644 | runge | 128 | function Module_ResolveAlias(alias_name, filter_module_names) |
| 1602 | runge | 129 | { |
| 1644 | runge | 130 | var aliases_data = {}; |
| 131 | var alias_data_string = Storage_GetParameter("alias", alias_name); |
||
| 1602 | runge | 132 | |
| 1644 | runge | 133 | if (alias_data_string) |
| 1602 | runge | 134 | { |
| 1644 | runge | 135 | var alias_data = eval("(" + alias_data_string + ")"); |
| 1603 | runge | 136 | |
| 1644 | runge | 137 | if (alias_data["group"]) |
| 1603 | runge | 138 | { |
| 1644 | runge | 139 | for (var n in alias_data["aliases"]) |
| 140 | { |
||
| 1646 | runge | 141 | var aliases_data_sub = Module_ResolveAlias(alias_data["aliases"][n], filter_module_names); |
| 1644 | runge | 142 | |
| 143 | for (var name in aliases_data_sub) |
||
| 144 | { |
||
| 145 | aliases_data[name] = aliases_data_sub[name]; |
||
| 146 | } |
||
| 147 | } |
||
| 1603 | runge | 148 | } |
| 1644 | runge | 149 | else |
| 150 | { |
||
| 1650 | runge | 151 | if (!filter_module_names || in_array(filter_module_names, alias_data["module_name"])) |
| 1644 | runge | 152 | { |
| 153 | aliases_data[alias_name] = alias_data; |
||
| 154 | } |
||
| 155 | } |
||
| 1602 | runge | 156 | } |
| 157 | |||
| 1644 | runge | 158 | return aliases_data; |
| 1602 | runge | 159 | } |
| 1603 | runge | 160 | |
| 1644 | runge | 161 | function Module_LookupAliases(match_filter) |
| 1603 | runge | 162 | { |
| 1644 | runge | 163 | var aliases_data_filtered = {}; |
| 164 | var aliases_data = Storage_GetParameters("alias"); |
||
| 1603 | runge | 165 | |
| 1644 | runge | 166 | for (var name in aliases_data) |
| 167 | { |
||
| 168 | var alias_data = eval("(" + aliases_data[name] + ")"); |
||
| 169 | |||
| 170 | var valid = true; |
||
| 171 | |||
| 172 | for (var key in match_filter) |
||
| 173 | { |
||
| 174 | if (alias_data[key] != match_filter[key]) |
||
| 175 | { |
||
| 176 | valid = false; |
||
| 177 | break; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | if (valid) |
||
| 182 | { |
||
| 183 | aliases_data_filtered[name] = alias_data; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | return aliases_data_filtered; |
||
| 1603 | runge | 188 | } |
| 189 | |||
| 1644 | runge | 190 | function Module_GetAliasNames(filter_module_names) |
| 1603 | runge | 191 | { |
| 1644 | runge | 192 | var aliases_name_filtered = []; |
| 193 | var aliases_data = Storage_GetParameters("alias"); |
||
| 194 | |||
| 195 | for (var name in aliases_data) |
||
| 1603 | runge | 196 | { |
| 1644 | runge | 197 | var alias_data = eval("(" + aliases_data[name] + ")"); |
| 198 | |||
| 199 | if (!filter_module_names || in_array(filter_module_names, alias_data["module_name"])) |
||
| 200 | { |
||
| 201 | aliases_name_filtered.push(name) |
||
| 202 | } |
||
| 1603 | runge | 203 | } |
| 1644 | runge | 204 | |
| 205 | return aliases_name_filtered; |
||
| 1603 | runge | 206 | } |
| 207 | |||
| 1644 | runge | 208 | function Module_GetAvailableIds(filter_module_names) |
| 1603 | runge | 209 | { |
| 1644 | runge | 210 | var result_list = []; |
| 211 | var available_modules = ModuleExport_GetAvailableModules(); |
||
| 1603 | runge | 212 | |
| 1644 | runge | 213 | for (var n in available_modules) |
| 1603 | runge | 214 | { |
| 1644 | runge | 215 | var id_parts = available_modules[n].split(":", 2); |
| 216 | |||
| 217 | if (in_array(filter_module_names, id_parts[0])) |
||
| 1603 | runge | 218 | { |
| 1644 | runge | 219 | result_list.push(id_parts[1]); |
| 1603 | runge | 220 | } |
| 221 | } |
||
| 222 | |||
| 1644 | runge | 223 | return result_list; |
| 1603 | runge | 224 | } |
| 225 | |||
| 1647 | runge | 226 | function Module_GetNames() |
| 227 | { |
||
| 1650 | runge | 228 | return [ "Dimmer230", "irTransmit", "irReceive", "SimpleDTMF", "BusVoltage", "DS18x20", "FOST02", "HD44789" ]; |
| 1647 | runge | 229 | } |
| 230 | |||
| 1644 | runge | 231 | function Module_SendMessage(module_name, module_id, command, variables) |
| 1614 | runge | 232 | { |
| 1644 | runge | 233 | var args = []; |
| 1614 | runge | 234 | |
| 1644 | runge | 235 | args.push(module_name + ":" + module_id); |
| 236 | args.push(command); |
||
| 1619 | runge | 237 | |
| 1644 | runge | 238 | var length = 0; |
| 239 | for (var key in variables) |
||
| 1614 | runge | 240 | { |
| 1644 | runge | 241 | length++; |
| 1614 | runge | 242 | } |
| 243 | |||
| 1644 | runge | 244 | args.push(length); |
| 245 | |||
| 246 | for (var key in variables) |
||
| 247 | { |
||
| 248 | args.push(key); |
||
| 249 | args.push(variables[key]); |
||
| 250 | } |
||
| 251 | |||
| 252 | return ModuleExport_SendMessage.apply(null, Array.prototype.slice.call(args, 0)); |
||
| 1614 | runge | 253 | } |
| 254 | |||
| 1644 | runge | 255 | var Module_OnChangeFunctions = {}; |
| 256 | var Module_OnMessageFunctions = {}; |
||
| 257 | |||
| 258 | function Module_RegisterToOnChange(alias_name, callback) |
||
| 259 | { |
||
| 260 | if (!Module_OnChangeFunctions[alias_name]) |
||
| 1603 | runge | 261 | { |
| 1644 | runge | 262 | Module_OnChangeFunctions[alias_name] = []; |
| 1603 | runge | 263 | } |
| 264 | |||
| 1644 | runge | 265 | Module_OnChangeFunctions[alias_name].push(callback); |
| 1603 | runge | 266 | } |
| 267 | |||
| 1644 | runge | 268 | function Module_RegisterToOnMessage(alias_name, callback) |
| 1603 | runge | 269 | { |
| 1644 | runge | 270 | if (!Module_OnMessageFunctions[alias_name]) |
| 1603 | runge | 271 | { |
| 1644 | runge | 272 | Module_OnMessageFunctions[alias_name] = []; |
| 1603 | runge | 273 | } |
| 1644 | runge | 274 | |
| 275 | Module_OnMessageFunctions[alias_name].push(callback); |
||
| 1603 | runge | 276 | } |
| 1609 | runge | 277 | |
| 1644 | runge | 278 | function Module_OnMessage(full_id, command, variables) |
| 1609 | runge | 279 | { |
| 1644 | runge | 280 | var id_parts = full_id.split(":", 2); |
| 281 | var aliases_data = {}; |
||
| 1647 | runge | 282 | |
| 283 | var aliases_data = Module_LookupAliases({ |
||
| 284 | "module_name" : id_parts[0], |
||
| 285 | "module_id" : id_parts[1], |
||
| 286 | "group" : false |
||
| 287 | }); |
||
| 1609 | runge | 288 | |
| 1647 | runge | 289 | for (var alias_name in aliases_data) |
| 1609 | runge | 290 | { |
| 1647 | runge | 291 | var ok = true; |
| 292 | |||
| 293 | if (aliases_data[alias_name]["specific"]) |
||
| 1644 | runge | 294 | { |
| 1647 | runge | 295 | var ok = true; |
| 296 | |||
| 297 | for (var name in variables) |
||
| 298 | { |
||
| 299 | if (aliases_data[alias_name]["specific"][name] && aliases_data[alias_name]["specific"][name] != variables[name]) |
||
| 300 | { |
||
| 301 | ok = false; |
||
| 302 | break; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | if (Module_OnMessageFunctions[alias_name] && ok) |
||
| 308 | { |
||
| 1644 | runge | 309 | for (var n in Module_OnMessageFunctions[alias_name]) |
| 310 | { |
||
| 311 | Module_OnMessageFunctions[alias_name][n](alias_name, command, variables); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | } |
||
| 1649 | runge | 315 | |
| 316 | if (Module_OnMessageFunctions["all"]) |
||
| 317 | { |
||
| 318 | for (var n in Module_OnMessageFunctions["all"]) |
||
| 319 | { |
||
| 320 | Module_OnMessageFunctions["all"][n](id_parts[0], id_parts[1], command, variables); |
||
| 321 | } |
||
| 322 | } |
||
| 1609 | runge | 323 | } |
| 324 | |||
| 1644 | runge | 325 | function Module_OnChange(full_id, available) |
| 1642 | runge | 326 | { |
| 1644 | runge | 327 | var id_parts = full_id.split(":", 2); |
| 1647 | runge | 328 | var aliases_data = {}; |
| 1644 | runge | 329 | |
| 1647 | runge | 330 | var aliases_data_all = Module_LookupAliases({ |
| 1644 | runge | 331 | "module_name" : id_parts[0], |
| 332 | "module_id" : id_parts[1], |
||
| 1647 | runge | 333 | "group" : false |
| 1644 | runge | 334 | }); |
| 335 | |||
| 1647 | runge | 336 | for (var alias_name in aliases_data) |
| 1644 | runge | 337 | { |
| 1647 | runge | 338 | var ok = true; |
| 339 | |||
| 340 | if (aliases_data[alias_name]["specific"]) |
||
| 1644 | runge | 341 | { |
| 1647 | runge | 342 | var ok = true; |
| 343 | |||
| 344 | for (var name in variables) |
||
| 345 | { |
||
| 346 | if (aliases_data[alias_name]["specific"][name] && aliases_data[alias_name]["specific"][name] != variables[name]) |
||
| 347 | { |
||
| 348 | ok = false; |
||
| 349 | break; |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | if (Module_OnChangeFunctions[alias_name] && ok) |
||
| 355 | { |
||
| 1644 | runge | 356 | for (var n in Module_OnChangeFunctions[alias_name]) |
| 357 | { |
||
| 358 | Module_OnChangeFunctions[alias_name][n](alias_name, available); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | } |
||
| 1649 | runge | 362 | |
| 363 | if (Module_OnChangeFunctions["all"]) |
||
| 364 | { |
||
| 365 | for (var n in Module_OnChangeFunctions["all"]) |
||
| 366 | { |
||
| 367 | Module_OnChangeFunctions["all"][n](id_parts[0], id_parts[1], available); |
||
| 368 | } |
||
| 369 | } |
||
| 1642 | runge | 370 | } |
| 1650 | runge | 371 | |
| 372 | function Module_GetLastValue(alias_name) |
||
| 373 | { |
||
| 374 | if (arguments.length < 1) |
||
| 375 | { |
||
| 376 | Log("\033[31mNot enough parameters given.\033[0m\n"); |
||
| 377 | return false; |
||
| 378 | } |
||
| 379 | |||
| 380 | var aliases_data = Module_ResolveAlias(alias_name); |
||
| 381 | var found = false; |
||
| 382 | |||
| 383 | for (var name in aliases_data) |
||
| 384 | { |
||
| 385 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
||
| 386 | |||
| 387 | if (last_value_string) |
||
| 388 | { |
||
| 389 | var last_value = eval("(" + last_value_string + ")"); |
||
| 390 | |||
| 391 | for (var type_name in last_value) |
||
| 392 | { |
||
| 393 | var date = new Date(last_value[type_name]["timestamp"] * 1000); |
||
| 394 | |||
| 395 | Log("\033[96m" + type_name + ": \033[0;1m" + last_value[type_name]["value"] + "\033[0m at " + date.toString() + "\n"); |
||
| 396 | } |
||
| 397 | } |
||
| 398 | else |
||
| 399 | { |
||
| 400 | Log("\033[32mNo value is stored for " + name + ".\033[0m\n"); |
||
| 401 | } |
||
| 402 | |||
| 403 | found = true; |
||
| 404 | } |
||
| 405 | |||
| 406 | if (!found) |
||
| 407 | { |
||
| 408 | Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n"); |
||
| 409 | return false; |
||
| 410 | } |
||
| 411 | |||
| 412 | return true; |
||
| 413 | } |
||
| 414 | Console_RegisterCommand(Module_GetLastValue, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Module_GetAliasNames()); }); |
||
| 1657 | runge | 415 | |
| 416 | function Module_ListAvailable() |
||
| 417 | { |
||
| 418 | var available_modules = ModuleExport_GetAvailableModules(); |
||
| 419 | |||
| 1660 | runge | 420 | var lines = [["Id", "Name", "Aliases"]]; |
| 421 | |||
| 1657 | runge | 422 | for (var n in available_modules) |
| 423 | { |
||
| 424 | var id_parts = available_modules[n].split(":", 2); |
||
| 1660 | runge | 425 | var alias = "<none>"; |
| 1657 | runge | 426 | |
| 1660 | runge | 427 | var alias_names = get_keys(Module_LookupAliases({ |
| 1657 | runge | 428 | "module_name" : id_parts[0], |
| 429 | "module_id" : id_parts[1], |
||
| 430 | "group" : false |
||
| 1660 | runge | 431 | })); |
| 1657 | runge | 432 | |
| 1660 | runge | 433 | if (alias_names.length > 0) |
| 1657 | runge | 434 | { |
| 1660 | runge | 435 | alias = alias_names.join(", "); |
| 1657 | runge | 436 | } |
| 437 | |||
| 1660 | runge | 438 | lines.push([id_parts[1], id_parts[0], alias]); |
| 1657 | runge | 439 | } |
| 440 | |||
| 1660 | runge | 441 | var table_lines = create_table(lines); |
| 442 | |||
| 443 | Log("\033[29;1m" + table_lines[0] + "\033[0m\n"); |
||
| 444 | |||
| 445 | for (var n = 1; n < table_lines.length; n++) |
||
| 446 | { |
||
| 447 | Log(table_lines[n] + "\n"); |
||
| 448 | } |
||
| 449 | |||
| 1657 | runge | 450 | return true; |
| 451 | } |
||
| 452 | Console_RegisterCommand(Module_ListAvailable); |