Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1648 | runge | 1 | |
| 1649 | runge | 2 | Sensor_ModuleNames = [ "DS18x20", "FOST02", "BusVoltage", "SimpleDTMF" ]; |
| 1648 | runge | 3 | Sensor_Intervals = function() { return [ 1, 5, 10, 15, 20 ]; }; |
| 4 | Sensor_Aliases = function() { return Module_GetAliasNames(Sensor_ModuleNames); }; |
||
| 5 | Sensor_AvailableIds = function() { return Module_GetAvailableIds(Sensor_ModuleNames); }; |
||
| 6 | |||
| 7 | function Sensor_SetReportInterval(alias_name, interval) |
||
| 8 | { |
||
| 9 | if (arguments.length < 2) |
||
| 10 | { |
||
| 1649 | runge | 11 | Log("\033[31mNot enough parameters given.\033[0m\n"); |
| 1648 | runge | 12 | return false; |
| 13 | } |
||
| 14 | |||
| 15 | var aliases_data = Module_ResolveAlias(alias_name, Sensor_ModuleNames); |
||
| 16 | var found = false; |
||
| 17 | |||
| 18 | for (var name in aliases_data) |
||
| 19 | { |
||
| 20 | var variables = { |
||
| 1649 | runge | 21 | "SensorId" : aliases_data[name]["specific"]["SensorId"], |
| 22 | "time" : interval }; |
||
| 23 | |||
| 24 | if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Report_Interval", variables)) |
||
| 25 | { |
||
| 26 | Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n"); |
||
| 27 | } |
||
| 28 | else |
||
| 29 | { |
||
| 30 | Log("\033[31mFailed to send command to " + name + ".\033[0m\n"); |
||
| 31 | } |
||
| 32 | |||
| 33 | found = true; |
||
| 34 | } |
||
| 35 | |||
| 36 | if (!found) |
||
| 37 | { |
||
| 38 | Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n"); |
||
| 39 | return false; |
||
| 40 | } |
||
| 41 | |||
| 42 | return true; |
||
| 43 | } |
||
| 44 | Console_RegisterCommand(Sensor_SetReportInterval, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Sensor_Aliases(), Sensor_Intervals()); }); |
||
| 45 | |||
| 46 | function Sensor_GetLastValue(alias_name) |
||
| 47 | { |
||
| 48 | if (arguments.length < 1) |
||
| 49 | { |
||
| 50 | Log("\033[31mNot enough parameters given.\033[0m\n"); |
||
| 51 | return false; |
||
| 52 | } |
||
| 53 | |||
| 54 | var aliases_data = Module_ResolveAlias(alias_name, Sensor_ModuleNames); |
||
| 55 | var found = false; |
||
| 56 | |||
| 57 | for (var name in aliases_data) |
||
| 58 | { |
||
| 59 | var last_value_string = Storage_GetParameter("SensorValueList", alias_name); |
||
| 60 | |||
| 61 | if (last_value_string) |
||
| 62 | { |
||
| 63 | var last_value = eval("(" + last_value_string + ")"); |
||
| 1648 | runge | 64 | |
| 1649 | runge | 65 | for (var type_name in last_value) |
| 1648 | runge | 66 | { |
| 1649 | runge | 67 | var date = new Date(last_value[type_name]["timestamp"] * 1000); |
| 68 | |||
| 69 | Log("\033[96m" + type_name + ": \033[0;1m" + last_value[type_name]["value"] + "\033[0m at " + date.toString() + "\n"); |
||
| 1648 | runge | 70 | } |
| 1649 | runge | 71 | } |
| 72 | else |
||
| 73 | { |
||
| 74 | Log("\033[32mNo value is stored for " + name + ".\033[0m\n"); |
||
| 75 | } |
||
| 76 | |||
| 77 | found = true; |
||
| 1648 | runge | 78 | } |
| 79 | |||
| 80 | if (!found) |
||
| 81 | { |
||
| 1649 | runge | 82 | Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n"); |
| 1648 | runge | 83 | return false; |
| 84 | } |
||
| 85 | |||
| 86 | return true; |
||
| 87 | } |
||
| 1649 | runge | 88 | Console_RegisterCommand(Sensor_GetLastValue, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Sensor_Aliases()); }); |
| 89 | |||
| 90 | function Sensor_OnMessage(module_name, module_id, command, variables) |
||
| 91 | { |
||
| 92 | if (in_array(Sensor_ModuleNames, module_name)) |
||
| 93 | { |
||
| 94 | var aliases_data = Module_LookupAliases({ |
||
| 95 | "module_name" : module_name, |
||
| 96 | "module_id" : module_id, |
||
| 97 | "group" : false |
||
| 98 | }); |
||
| 99 | |||
| 100 | switch (command) |
||
| 101 | { |
||
| 102 | case "Phonenumber": |
||
| 103 | { |
||
| 104 | for (var alias_name in aliases_data) |
||
| 105 | { |
||
| 106 | var last_value = {}; |
||
| 107 | var last_value_string = Storage_GetParameter("SensorValueList", alias_name); |
||
| 108 | |||
| 109 | if (last_value_string) |
||
| 110 | { |
||
| 111 | last_value = eval("(" + last_value_string + ")"); |
||
| 112 | } |
||
| 113 | |||
| 114 | last_value[command] = { "value" : variables["Number"], "timestamp" : get_time() }; |
||
| 115 | |||
| 116 | Storage_GetParameter("SensorValueList", alias_name, JSON.stringify(last_value)); |
||
| 117 | } |
||
| 118 | |||
| 119 | break; |
||
| 120 | } |
||
| 121 | case "Voltage": |
||
| 122 | case "Temperature_Celsius": |
||
| 123 | case "Humidity_Percent": |
||
| 124 | { |
||
| 125 | for (var alias_name in aliases_data) |
||
| 126 | { |
||
| 127 | if (aliases_data[alias_name]["specific"]["SensorId"] != variables["SensorId"]) |
||
| 128 | { |
||
| 129 | continue; |
||
| 130 | } |
||
| 131 | |||
| 132 | var last_value = {}; |
||
| 133 | var last_value_string = Storage_GetParameter("SensorValueList", alias_name); |
||
| 134 | |||
| 135 | if (last_value_string) |
||
| 136 | { |
||
| 137 | last_value = eval("(" + last_value_string + ")"); |
||
| 138 | } |
||
| 139 | |||
| 140 | last_value[command] = { "value" : variables["Value"], "timestamp" : get_time() }; |
||
| 141 | |||
| 142 | Storage_SetParameter("SensorValueList", alias_name, JSON.stringify(last_value)); |
||
| 143 | } |
||
| 144 | |||
| 145 | break; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | Module_RegisterToOnMessage("all", Sensor_OnMessage); |