Rev 1958 | Rev 2050 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1648 | runge | 1 | |
| 1936 | englund | 2 | Sensor_ModuleNames = [ "DS18x20", "FOST02", "BusVoltage", "SimpleDTMF", "DHT11" ]; |
| 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; |
||
| 1905 | linlun | 61 | var start = 0; |
| 1899 | linlun | 62 | if (variables["Number"].charAt(0) == 'A') { |
| 63 | direction = "in"; |
||
| 1905 | linlun | 64 | start = 1; |
| 1899 | linlun | 65 | } else { |
| 66 | direction = "out"; |
||
| 67 | } |
||
| 1905 | linlun | 68 | var endIndex = variables["Number"].indexOf('C'); |
| 69 | if (endIndex == -1) { |
||
| 70 | endIndex = variables["Number"].length; |
||
| 71 | } |
||
| 72 | variables["Number"] = variables["Number"].substring(start,endIndex); |
||
| 1899 | linlun | 73 | var last_value = {}; |
| 74 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
||
| 1649 | runge | 75 | |
| 1899 | linlun | 76 | if (last_value_string) |
| 77 | { |
||
| 78 | last_value = eval("(" + last_value_string + ")"); |
||
| 79 | } |
||
| 80 | |||
| 81 | last_value[command] = { "value" : variables["Number"], "timestamp" : get_time(), "direction" : direction }; |
||
| 82 | |||
| 83 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
||
| 1807 | linlun | 84 | |
| 1899 | linlun | 85 | var all_numbers = Storage_GetParameters("PhoneCalls"); |
| 86 | var newString = "{\"number\":\"" + variables["Number"] + "\",\"module\":\"" + alias_name + "\",\"direction\":\"" + direction + "\"}"; |
||
| 87 | Storage_SetParameter("PhoneCalls", get_time(), newString); |
||
| 88 | Log("New number: "+newString +"\n"); |
||
| 89 | Sensor_StoreNumberInPhonebook(variables["Number"]); |
||
| 90 | } |
||
| 1649 | runge | 91 | } |
| 92 | break; |
||
| 93 | } |
||
| 94 | case "Voltage": |
||
| 95 | case "Temperature_Celsius": |
||
| 1958 | cougar | 96 | { |
| 97 | if ((module_name == "DS18x20") && (variables["Value"] == 85)) |
||
| 98 | { |
||
| 99 | Log("\033[31mDS18x20: Temperature conversion error: " + module_name + ":" + module_id + ", SensorId=" + variables["SensorId"] + "\033[0m"); |
||
| 100 | break; |
||
| 101 | } |
||
| 102 | } |
||
| 1649 | runge | 103 | case "Humidity_Percent": |
| 104 | { |
||
| 105 | for (var alias_name in aliases_data) |
||
| 106 | { |
||
| 107 | if (aliases_data[alias_name]["specific"]["SensorId"] != variables["SensorId"]) |
||
| 108 | { |
||
| 109 | continue; |
||
| 110 | } |
||
| 111 | |||
| 112 | var last_value = {}; |
||
| 1650 | runge | 113 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
| 1649 | runge | 114 | |
| 115 | if (last_value_string) |
||
| 116 | { |
||
| 117 | last_value = eval("(" + last_value_string + ")"); |
||
| 118 | } |
||
| 119 | |||
| 120 | last_value[command] = { "value" : variables["Value"], "timestamp" : get_time() }; |
||
| 121 | |||
| 1650 | runge | 122 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
| 1649 | runge | 123 | } |
| 124 | |||
| 125 | break; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | Module_RegisterToOnMessage("all", Sensor_OnMessage); |
||
| 1899 | linlun | 131 | |
| 132 | function Sensor_StoreNumberInPhonebook(number) |
||
| 133 | { |
||
| 134 | var phonebook = Storage_GetJsonParamter("PhoneBook",number) |
||
| 135 | if (!phonebook) { |
||
| 2014 | linlun | 136 | |
| 1900 | linlun | 137 | if (number.charAt(0) != '0') { |
| 2014 | linlun | 138 | if (typeof sensorDefaultPhoneAreaCode === 'undefined'){ |
| 139 | Log("\033[31mOnlinePhonebook: please add sensorDefaultPhoneAreaCode = yourAreaCode to autostart.js, defaulting to 031\033[0m\n"); |
||
| 140 | number = "031"+number; |
||
| 141 | } else { |
||
| 142 | number = sensorDefaultPhoneAreaCode+number; |
||
| 143 | } |
||
| 1900 | linlun | 144 | } |
| 2014 | linlun | 145 | var persons = new Array(); |
| 146 | url = "wap.eniro.se/query?search_word=" + number + "&what=mobwp"; |
||
| 147 | //Log("\033[31mURL:"+url+"\033[0m\n"); |
||
| 148 | Http_Request(url, |
||
| 149 | function(socket_id, result, header, content_data) { |
||
| 150 | if (result.indexOf("200 OK") != -1) |
||
| 151 | { |
||
| 152 | //Log("\033[31mOnlinePhonebook: result was: " + content_data + "\033[0m\n"); |
||
| 153 | var lines = content_data.split("\n"); |
||
| 154 | |||
| 155 | var endString = "<anchor>Tillbaka<prev/></anchor>"; |
||
| 156 | var nameStartString = "<td class=\"hTd2\">"; |
||
| 157 | var nameEndString = "</table>"; |
||
| 158 | for (var n = 1; n < lines.length; n++) |
||
| 159 | { |
||
| 160 | var line = lines[n].trim("\n"); |
||
| 161 | |||
| 162 | if (line.indexOf(endString) != -1) |
||
| 163 | { |
||
| 164 | break; |
||
| 165 | } |
||
| 166 | if (line.indexOf(nameStartString) != -1) |
||
| 167 | { |
||
| 168 | line = lines[n+1].trim("\n"); |
||
| 169 | splitted = line.split("b>"); |
||
| 170 | persons[persons.length] = splitted[1].substr(0, splitted[1].length -2); |
||
| 171 | break; |
||
| 172 | } |
||
| 173 | |||
| 174 | } |
||
| 175 | |||
| 176 | //Log("\033[31mOnlinePhonebook: Line: " + lines[1] + "\033[0m\n"); |
||
| 177 | //Log("\033[31mOnlinePhonebook: Line: " + lines[2] + "\033[0m\n"); |
||
| 178 | //Log("\033[31mOnlinePhonebook: Line: " + lines[3] + "\033[0m\n"); |
||
| 179 | if (persons.length > 0) { |
||
| 180 | Storage_SetJsonParameter("PhoneBook", number, persons); |
||
| 181 | |||
| 182 | //CAll all listeners! |
||
| 183 | //.... |
||
| 184 | |||
| 185 | Log("\033[31mOnlinePhonebook: Found: " + JSON.stringify(persons) + "\033[0m\n"); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | else |
||
| 189 | { |
||
| 190 | Log("\033[31mOnlinePhonebook: Failed to do name lookup, result was: " + result + "\033[0m\n"); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | ); |
||
| 194 | if (persons.length == 0) { |
||
| 195 | if (typeof SsensorRKSEEK_API === 'undefined') { |
||
| 196 | ; |
||
| 197 | } else { |
||
| 198 | url = "rkseek.oblivioncreations.se/?client="+sensorRKSEEK_API+"&n=" + number + "&out=text"; |
||
| 199 | //Log("\033[31mURL:"+url+"\033[0m\n"); |
||
| 200 | Http_Request(url, |
||
| 201 | function(socket_id, result, header, content_data) { |
||
| 202 | var persons = new Array(); |
||
| 203 | if (result.indexOf("200 OK") != -1) |
||
| 204 | { |
||
| 205 | //Log("\033[31mOnlinePhonebook: result was: " + content_data + "\033[0m\n"); |
||
| 206 | var lines = content_data.split("\n"); |
||
| 207 | //Log("\033[31mOnlinePhonebook: Line: " + lines[1] + "\033[0m\n"); |
||
| 208 | //Log("\033[31mOnlinePhonebook: Line: " + lines[2] + "\033[0m\n"); |
||
| 209 | //Log("\033[31mOnlinePhonebook: Line: " + lines[3] + "\033[0m\n"); |
||
| 210 | if (lines[2].length > 0) { |
||
| 211 | persons = lines[2]; |
||
| 212 | Storage_SetJsonParameter("PhoneBook", number, persons); |
||
| 213 | Log("\033[31mOnlinePhonebook: Found: " + JSON.stringify(persons) + "\033[0m\n"); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | else |
||
| 217 | { |
||
| 218 | Log("\033[31mOnlinePhonebook: Failed to do name lookup, result was: " + result + "\033[0m\n"); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | ); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /* |
||
| 1899 | linlun | 226 | Http_Request("wap.hitta.se/default.aspx?Who=" + number + "&Where=&PageAction=White", |
| 227 | function(socket_id, result, header, content_data) { |
||
| 228 | var persons = new Array(); |
||
| 229 | if (result.indexOf("200 OK") != -1) |
||
| 230 | { |
||
| 231 | var endString = "<anchor>Tillbaka<prev/></anchor>"; |
||
| 232 | var nameStartString = " title=\"Link\">"; |
||
| 233 | var nameEndString = "</a>"; |
||
| 234 | |||
| 235 | var lines = content_data.split("<br/>"); |
||
| 236 | |||
| 237 | for (var n = 1; n < lines.length; n++) |
||
| 238 | { |
||
| 239 | var line = lines[n].trim(" \n"); |
||
| 240 | |||
| 241 | if (line.indexOf(endString) != -1) |
||
| 242 | { |
||
| 243 | break; |
||
| 244 | } |
||
| 245 | var pos = line.indexOf(nameStartString + (persons.length+1) + "."); |
||
| 246 | |||
| 247 | if (pos != -1) |
||
| 248 | { |
||
| 249 | pos += nameStartString.length + 2 |
||
| 250 | persons[persons.length] = line.substr(pos, line.length-pos-nameEndString.length).html_entity_decode().replace(" ", " "); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | if (persons.length > 0) { |
||
| 254 | Storage_SetJsonParameter("PhoneBook", number, persons); |
||
| 255 | |||
| 256 | //CAll all listeners! |
||
| 257 | //.... |
||
| 258 | |||
| 259 | Log("\033[31mOnlinePhonebook: Found: " + JSON.stringify(persons) + "\033[0m\n"); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | else |
||
| 263 | { |
||
| 264 | Log("\033[31mOnlinePhonebook: Failed to do name lookup, result was: " + result + "\033[0m\n"); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | ); |
||
| 2014 | linlun | 268 | */ |
| 1899 | linlun | 269 | } else { |
| 270 | Log("Number already stored\n"); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | Console_RegisterCommand(Sensor_StoreNumberInPhonebook, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args); }); |
||
| 274 | |||
| 275 | |||
| 276 |