Rev 1899 | Rev 1905 | Go to most recent revision | Details | Compare with Previous | 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 | { |
||
| 1765 | linlun | 20 | var variables = {"Time" : interval }; |
| 1649 | runge | 21 | if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Report_Interval", variables)) |
| 22 | { |
||
| 23 | Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n"); |
||
| 24 | } |
||
| 25 | else |
||
| 26 | { |
||
| 27 | Log("\033[31mFailed to send command to " + name + ".\033[0m\n"); |
||
| 28 | } |
||
| 29 | |||
| 30 | found = true; |
||
| 31 | } |
||
| 32 | |||
| 33 | if (!found) |
||
| 34 | { |
||
| 35 | Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n"); |
||
| 36 | return false; |
||
| 37 | } |
||
| 38 | |||
| 39 | return true; |
||
| 40 | } |
||
| 41 | Console_RegisterCommand(Sensor_SetReportInterval, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Sensor_Aliases(), Sensor_Intervals()); }); |
||
| 42 | |||
| 43 | function Sensor_OnMessage(module_name, module_id, command, variables) |
||
| 44 | { |
||
| 45 | if (in_array(Sensor_ModuleNames, module_name)) |
||
| 46 | { |
||
| 47 | var aliases_data = Module_LookupAliases({ |
||
| 48 | "module_name" : module_name, |
||
| 49 | "module_id" : module_id, |
||
| 50 | "group" : false |
||
| 51 | }); |
||
| 52 | |||
| 53 | switch (command) |
||
| 54 | { |
||
| 55 | case "Phonenumber": |
||
| 56 | { |
||
| 57 | for (var alias_name in aliases_data) |
||
| 58 | { |
||
| 1899 | linlun | 59 | if (variables["Number"].length > 6) { |
| 60 | var direction; |
||
| 61 | if (variables["Number"].charAt(0) == 'A') { |
||
| 62 | direction = "in"; |
||
| 63 | var endIndex = variables["Number"].indexOf('C'); |
||
| 1900 | linlun | 64 | variables["Number"] = variables["Number"].substring(1,endIndex); |
| 1899 | linlun | 65 | } else { |
| 66 | direction = "out"; |
||
| 67 | } |
||
| 68 | var last_value = {}; |
||
| 69 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
||
| 1649 | runge | 70 | |
| 1899 | linlun | 71 | if (last_value_string) |
| 72 | { |
||
| 73 | last_value = eval("(" + last_value_string + ")"); |
||
| 74 | } |
||
| 75 | |||
| 76 | last_value[command] = { "value" : variables["Number"], "timestamp" : get_time(), "direction" : direction }; |
||
| 77 | |||
| 78 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
||
| 1807 | linlun | 79 | |
| 1899 | linlun | 80 | var all_numbers = Storage_GetParameters("PhoneCalls"); |
| 81 | var newString = "{\"number\":\"" + variables["Number"] + "\",\"module\":\"" + alias_name + "\",\"direction\":\"" + direction + "\"}"; |
||
| 82 | Storage_SetParameter("PhoneCalls", get_time(), newString); |
||
| 83 | Log("New number: "+newString +"\n"); |
||
| 84 | Sensor_StoreNumberInPhonebook(variables["Number"]); |
||
| 85 | } |
||
| 1649 | runge | 86 | } |
| 87 | break; |
||
| 88 | } |
||
| 89 | case "Voltage": |
||
| 90 | case "Temperature_Celsius": |
||
| 91 | case "Humidity_Percent": |
||
| 92 | { |
||
| 93 | for (var alias_name in aliases_data) |
||
| 94 | { |
||
| 95 | if (aliases_data[alias_name]["specific"]["SensorId"] != variables["SensorId"]) |
||
| 96 | { |
||
| 97 | continue; |
||
| 98 | } |
||
| 99 | |||
| 100 | var last_value = {}; |
||
| 1650 | runge | 101 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
| 1649 | runge | 102 | |
| 103 | if (last_value_string) |
||
| 104 | { |
||
| 105 | last_value = eval("(" + last_value_string + ")"); |
||
| 106 | } |
||
| 107 | |||
| 108 | last_value[command] = { "value" : variables["Value"], "timestamp" : get_time() }; |
||
| 109 | |||
| 1650 | runge | 110 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
| 1649 | runge | 111 | } |
| 112 | |||
| 113 | break; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | Module_RegisterToOnMessage("all", Sensor_OnMessage); |
||
| 1899 | linlun | 119 | |
| 120 | function Sensor_StoreNumberInPhonebook(number) |
||
| 121 | { |
||
| 122 | var phonebook = Storage_GetJsonParamter("PhoneBook",number) |
||
| 123 | if (!phonebook) { |
||
| 1900 | linlun | 124 | if (number.charAt(0) != '0') { |
| 125 | number = "031"+number; |
||
| 126 | } |
||
| 1899 | linlun | 127 | Http_Request("wap.hitta.se/default.aspx?Who=" + number + "&Where=&PageAction=White", |
| 128 | function(socket_id, result, header, content_data) { |
||
| 129 | var persons = new Array(); |
||
| 130 | if (result.indexOf("200 OK") != -1) |
||
| 131 | { |
||
| 132 | var endString = "<anchor>Tillbaka<prev/></anchor>"; |
||
| 133 | var nameStartString = " title=\"Link\">"; |
||
| 134 | var nameEndString = "</a>"; |
||
| 135 | |||
| 136 | var lines = content_data.split("<br/>"); |
||
| 137 | |||
| 138 | for (var n = 1; n < lines.length; n++) |
||
| 139 | { |
||
| 140 | var line = lines[n].trim(" \n"); |
||
| 141 | |||
| 142 | if (line.indexOf(endString) != -1) |
||
| 143 | { |
||
| 144 | break; |
||
| 145 | } |
||
| 146 | var pos = line.indexOf(nameStartString + (persons.length+1) + "."); |
||
| 147 | |||
| 148 | if (pos != -1) |
||
| 149 | { |
||
| 150 | pos += nameStartString.length + 2 |
||
| 151 | persons[persons.length] = line.substr(pos, line.length-pos-nameEndString.length).html_entity_decode().replace(" ", " "); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | if (persons.length > 0) { |
||
| 155 | Storage_SetJsonParameter("PhoneBook", number, persons); |
||
| 156 | |||
| 157 | //CAll all listeners! |
||
| 158 | //.... |
||
| 159 | |||
| 160 | Log("\033[31mOnlinePhonebook: Found: " + JSON.stringify(persons) + "\033[0m\n"); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | else |
||
| 164 | { |
||
| 165 | Log("\033[31mOnlinePhonebook: Failed to do name lookup, result was: " + result + "\033[0m\n"); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | ); |
||
| 169 | } else { |
||
| 170 | Log("Number already stored\n"); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | Console_RegisterCommand(Sensor_StoreNumberInPhonebook, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args); }); |
||
| 174 | |||
| 175 | |||
| 176 |