Rev 2050 | Rev 2092 | 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 | } |
||
| 2091 | runge | 14 | |
| 1648 | runge | 15 | var aliases_data = Module_ResolveAlias(alias_name, Sensor_ModuleNames); |
| 16 | var found = false; |
||
| 2091 | runge | 17 | |
| 1648 | runge | 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 | } |
||
| 2091 | runge | 29 | |
| 1649 | runge | 30 | found = true; |
| 31 | } |
||
| 2091 | runge | 32 | |
| 1649 | runge | 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 | } |
||
| 2091 | runge | 38 | |
| 1649 | runge | 39 | return true; |
| 40 | } |
||
| 41 | Console_RegisterCommand(Sensor_SetReportInterval, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Sensor_Aliases(), Sensor_Intervals()); }); |
||
| 42 | |||
| 2091 | runge | 43 | Sensor_OnNewPhonenumberFunctions = []; |
| 2050 | arune | 44 | |
| 45 | function Sensor_RegisterToNewPhonenumber(alias_name, callback) |
||
| 46 | { |
||
| 2091 | runge | 47 | if (!Sensor_OnNewPhonenumberFunctions[alias_name]) |
| 48 | { |
||
| 49 | Sensor_OnNewPhonenumberFunctions[alias_name] = []; |
||
| 50 | } |
||
| 51 | |||
| 52 | Sensor_OnNewPhonenumberFunctions[alias_name].push(callback); |
||
| 2050 | arune | 53 | } |
| 54 | |||
| 1649 | runge | 55 | function Sensor_OnMessage(module_name, module_id, command, variables) |
| 56 | { |
||
| 2091 | runge | 57 | if (in_array(Sensor_ModuleNames, module_name)) |
| 58 | { |
||
| 59 | var aliases_data = Module_LookupAliases({ |
||
| 60 | "module_name" : module_name, |
||
| 61 | "module_id" : module_id, |
||
| 62 | "group" : false |
||
| 63 | }); |
||
| 1807 | linlun | 64 | |
| 2091 | runge | 65 | switch (command) |
| 66 | { |
||
| 67 | case "Phonenumber": |
||
| 68 | { |
||
| 69 | for (var alias_name in aliases_data) |
||
| 70 | { |
||
| 71 | var number = variables["Number"]; |
||
| 72 | var incommingCall = true; |
||
| 1649 | runge | 73 | |
| 2091 | runge | 74 | if (variables["Number"].length > 6) |
| 75 | { |
||
| 76 | /* A seems to mean it is an outgoing call! */ |
||
| 77 | if (number.charAt(0) === 'A') |
||
| 78 | { |
||
| 79 | incommingCall = false; |
||
| 80 | |||
| 81 | /* Remove leading A */ |
||
| 82 | number = number.substring(1); |
||
| 83 | } |
||
| 84 | else /* Incomming call */ |
||
| 85 | { |
||
| 86 | incommingCall = true; |
||
| 87 | |||
| 88 | /* Remove leading D if needed */ |
||
| 89 | if (number.charAt(0) === 'D') |
||
| 90 | { |
||
| 91 | number = number.substring(1); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /* A number can have a trailing C */ |
||
| 96 | var endIndex = number.indexOf('C'); |
||
| 97 | |||
| 98 | if (endIndex !== -1) |
||
| 99 | { |
||
| 100 | /* Remove trailing C */ |
||
| 101 | number = number.substring(0, endIndex); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | else if (number == "B00C") |
||
| 105 | { |
||
| 106 | /* Unknown number */ |
||
| 107 | number = "Unknown"; |
||
| 108 | incommingCall = true; |
||
| 109 | } |
||
| 110 | else if (number == "B10C") |
||
| 111 | { |
||
| 112 | /* Secret/protected number */ |
||
| 113 | number = "Protected"; |
||
| 114 | incommingCall = true; |
||
| 115 | } |
||
| 116 | else |
||
| 117 | { |
||
| 118 | Log("DTMF decoded number is to short, number: \"" + number + "\", length: " + number.length); |
||
| 119 | return; |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | /* Store number and direction in last values */ |
||
| 124 | var lastValue = {}; |
||
| 125 | var lastValueString = Storage_GetParameter("LastValues", alias_name); |
||
| 126 | |||
| 127 | if (lastValueString) |
||
| 128 | { |
||
| 129 | lastValue = JSON.parse(lastValueString); |
||
| 130 | } |
||
| 131 | |||
| 132 | var timestamp = get_time(); |
||
| 133 | |||
| 134 | lastValue["Phonenumber"] = { "value" : number, "timestamp" : timestamp }; |
||
| 135 | lastValue["Direction"] = { "value" : incommingCall ? "in" : "out", "timestamp" : timestamp }; |
||
| 136 | |||
| 137 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(lastValue)); |
||
| 138 | |||
| 139 | |||
| 140 | /* Store number in phone call history */ |
||
| 141 | var newValue = { "number" : number, "direction" : incommingCall ? "in" : "out", "module" : alias_name, "names" : [], "timestamp" : timestamp }; |
||
| 142 | |||
| 143 | Storage_SetParameter("PhoneCalls", timestamp, JSON.stringify(newValue)); |
||
| 144 | |||
| 145 | |||
| 146 | /* Log the number */ |
||
| 147 | Log("New number: " + number + ", direction: " + (incommingCall ? "in" : "out")); |
||
| 148 | |||
| 149 | |||
| 150 | /* Lookup name */ |
||
| 151 | Sensor_StoreNumberInPhonebook(number, timestamp); |
||
| 152 | |||
| 153 | |||
| 154 | /* Call any potential subscribers */ |
||
| 155 | if (Sensor_OnNewPhonenumberFunctions[alias_name]) |
||
| 156 | { |
||
| 157 | for (var n in Sensor_OnNewPhonenumberFunctions[alias_name]) |
||
| 158 | { |
||
| 159 | /* Call callback with arguments number, direction */ |
||
| 160 | Sensor_OnNewPhonenumberFunctions[alias_name][n](alias_name, number, incommingCall ? "in" : "out"); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | break; |
||
| 165 | } |
||
| 166 | case "Voltage": |
||
| 167 | case "Temperature_Celsius": |
||
| 168 | { |
||
| 169 | if ((module_name == "DS18x20") && (variables["Value"] == 85)) |
||
| 170 | { |
||
| 171 | Log("\033[31mDS18x20: Temperature conversion error: " + module_name + ":" + module_id + ", SensorId=" + variables["SensorId"] + "\033[0m"); |
||
| 172 | break; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | case "Humidity_Percent": |
||
| 176 | { |
||
| 177 | for (var alias_name in aliases_data) |
||
| 178 | { |
||
| 179 | if (aliases_data[alias_name]["specific"]["SensorId"] != variables["SensorId"]) |
||
| 180 | { |
||
| 181 | continue; |
||
| 182 | } |
||
| 183 | |||
| 184 | var last_value = {}; |
||
| 185 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
||
| 186 | |||
| 187 | if (last_value_string) |
||
| 188 | { |
||
| 189 | last_value = eval("(" + last_value_string + ")"); |
||
| 190 | } |
||
| 191 | |||
| 192 | last_value[command] = { "value" : variables["Value"], "timestamp" : get_time() }; |
||
| 193 | |||
| 194 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
||
| 195 | } |
||
| 196 | |||
| 197 | break; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 1649 | runge | 201 | } |
| 202 | Module_RegisterToOnMessage("all", Sensor_OnMessage); |
||
| 1899 | linlun | 203 | |
| 2091 | runge | 204 | function Sensor_StoreNumberInPhonebook(number, timestamp) |
| 1899 | linlun | 205 | { |
| 2091 | runge | 206 | var phonebookNumbers = Storage_GetJsonParamter("PhoneBook", number) |
| 207 | |||
| 208 | if (!phonebookNumbers) |
||
| 209 | { |
||
| 210 | if (number.charAt(0) != '0') |
||
| 211 | { |
||
| 212 | if (typeof sensorDefaultPhoneAreaCode === 'undefined') |
||
| 213 | { |
||
| 214 | Log("\033[31mOnlinePhonebook: please add sensorDefaultPhoneAreaCode = yourAreaCode to autostart.js, defaulting to 031\033[0m\n"); |
||
| 215 | number = "031" + number; |
||
| 216 | } |
||
| 217 | else |
||
| 218 | { |
||
| 219 | number = sensorDefaultPhoneAreaCode + number; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | phonebookNumbers = []; |
||
| 224 | |||
| 225 | url = "wap.eniro.se/query?search_word=" + number + "&what=mobwp"; |
||
| 226 | //Log("\033[31mURL: http://"+url+"\033[0m\n"); |
||
| 227 | |||
| 228 | Http_Request(url, function(socket_id, result, header, content_data) |
||
| 229 | { |
||
| 230 | //Log("\033[31mOnlinePhonebook-eniro: result was: " + result + "\033[0m\n"); |
||
| 231 | //Log("\033[31mOnlinePhonebook-eniro: content was: " + content_data + "\033[0m\n"); |
||
| 232 | |||
| 233 | if (result.indexOf("200 OK") != -1) |
||
| 234 | { |
||
| 235 | |||
| 236 | var lines = content_data.split("\n"); |
||
| 237 | |||
| 238 | var endString = "<anchor>Tillbaka<prev/></anchor>"; |
||
| 239 | var nameStartString = "<td class=\"hTd2\">"; |
||
| 240 | var nameEndString = "</table>"; |
||
| 241 | |||
| 242 | for (var n = 1; n < lines.length; n++) |
||
| 243 | { |
||
| 244 | var line = lines[n].trim("\n"); |
||
| 245 | |||
| 246 | if (line.indexOf(endString) != -1) |
||
| 247 | { |
||
| 248 | break; |
||
| 249 | } |
||
| 250 | |||
| 251 | if (line.indexOf(nameStartString) != -1) |
||
| 252 | { |
||
| 253 | line = lines[n + 1].trim("\n"); |
||
| 254 | splitted = line.split("b>"); |
||
| 255 | |||
| 256 | phonebookNumbers.push(splitted[1].substr(0, splitted[1].length - 2)); |
||
| 257 | break; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | //Log("\033[31mOnlinePhonebook: Line: " + lines[1] + "\033[0m\n"); |
||
| 262 | //Log("\033[31mOnlinePhonebook: Line: " + lines[2] + "\033[0m\n"); |
||
| 263 | //Log("\033[31mOnlinePhonebook: Line: " + lines[3] + "\033[0m\n"); |
||
| 264 | if (phonebookNumbers.length > 0) |
||
| 265 | { |
||
| 266 | Storage_SetJsonParameter("PhoneBook", number, phonebookNumbers); |
||
| 267 | |||
| 268 | Sensor_UpdateNameInPhonecalls(timestamp, phonebookNumbers); |
||
| 269 | |||
| 270 | //CAll all listeners! |
||
| 271 | //.... |
||
| 272 | |||
| 273 | Log("\033[31mOnlinePhonebook-eniro: Found: " + JSON.stringify(phonebookNumbers) + "\033[0m\n"); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | else |
||
| 277 | { |
||
| 278 | Log("\033[31mOnlinePhonebook-eniro: Failed to do name lookup, result was: " + result + "\033[0m\n"); |
||
| 279 | } |
||
| 280 | }); |
||
| 281 | |||
| 282 | if (phonebookNumbers.length == 0) |
||
| 283 | { |
||
| 284 | if (typeof sensorRKSEEK_API !== 'undefined') |
||
| 285 | { |
||
| 286 | url = "rkseek.oblivioncreations.se/?client="+sensorRKSEEK_API+"&n=" + number + "&out=text"; |
||
| 287 | Log("\033[31mURL: http://"+url+"\033[0m\n"); |
||
| 288 | |||
| 289 | Http_Request(url, function(socket_id, result, header, content_data) |
||
| 290 | { |
||
| 291 | phonebookNumbers = []; |
||
| 292 | |||
| 293 | if (result.indexOf("200 OK") != -1) |
||
| 294 | { |
||
| 295 | //Log("\033[31mOnlinePhonebook: result was: " + content_data + "\033[0m\n"); |
||
| 296 | var lines = content_data.split("\n"); |
||
| 297 | //Log("\033[31mOnlinePhonebook: Line: " + lines[1] + "\033[0m\n"); |
||
| 298 | //Log("\033[31mOnlinePhonebook: Line: " + lines[2] + "\033[0m\n"); |
||
| 299 | //Log("\033[31mOnlinePhonebook: Line: " + lines[3] + "\033[0m\n"); |
||
| 300 | if (lines[2].length > 0) |
||
| 301 | { |
||
| 302 | phonebookNumbers = lines[2]; |
||
| 303 | Storage_SetJsonParameter("PhoneBook", number, phonebookNumbers); |
||
| 304 | |||
| 305 | Sensor_UpdateNameInPhonecalls(timestamp, phonebookNumbers); |
||
| 306 | |||
| 307 | Log("\033[31mOnlinePhonebook-rseek: Found: " + JSON.stringify(phonebookNumbers) + "\033[0m\n"); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | else |
||
| 311 | { |
||
| 312 | Log("\033[31mOnlinePhonebook-rseek: Failed to do name lookup, result was: " + result + "\033[0m\n"); |
||
| 313 | } |
||
| 314 | }); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 2014 | linlun | 318 | /* |
| 2091 | runge | 319 | Http_Request("wap.hitta.se/default.aspx?Who=" + number + "&Where=&PageAction=White", |
| 1899 | linlun | 320 | function(socket_id, result, header, content_data) { |
| 321 | var persons = new Array(); |
||
| 322 | if (result.indexOf("200 OK") != -1) |
||
| 323 | { |
||
| 324 | var endString = "<anchor>Tillbaka<prev/></anchor>"; |
||
| 325 | var nameStartString = " title=\"Link\">"; |
||
| 326 | var nameEndString = "</a>"; |
||
| 2091 | runge | 327 | |
| 1899 | linlun | 328 | var lines = content_data.split("<br/>"); |
| 2091 | runge | 329 | |
| 1899 | linlun | 330 | for (var n = 1; n < lines.length; n++) |
| 331 | { |
||
| 332 | var line = lines[n].trim(" \n"); |
||
| 2091 | runge | 333 | |
| 1899 | linlun | 334 | if (line.indexOf(endString) != -1) |
| 335 | { |
||
| 336 | break; |
||
| 337 | } |
||
| 338 | var pos = line.indexOf(nameStartString + (persons.length+1) + "."); |
||
| 2091 | runge | 339 | |
| 1899 | linlun | 340 | if (pos != -1) |
| 341 | { |
||
| 342 | pos += nameStartString.length + 2 |
||
| 343 | persons[persons.length] = line.substr(pos, line.length-pos-nameEndString.length).html_entity_decode().replace(" ", " "); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | if (persons.length > 0) { |
||
| 347 | Storage_SetJsonParameter("PhoneBook", number, persons); |
||
| 2091 | runge | 348 | |
| 1899 | linlun | 349 | //CAll all listeners! |
| 350 | //.... |
||
| 2091 | runge | 351 | |
| 1899 | linlun | 352 | Log("\033[31mOnlinePhonebook: Found: " + JSON.stringify(persons) + "\033[0m\n"); |
| 353 | } |
||
| 354 | } |
||
| 355 | else |
||
| 356 | { |
||
| 357 | Log("\033[31mOnlinePhonebook: Failed to do name lookup, result was: " + result + "\033[0m\n"); |
||
| 2091 | runge | 358 | } |
| 1899 | linlun | 359 | } |
| 360 | ); |
||
| 2014 | linlun | 361 | */ |
| 2091 | runge | 362 | |
| 363 | } |
||
| 364 | else |
||
| 365 | { |
||
| 366 | //Log("Number already stored\n"); |
||
| 367 | |||
| 368 | Sensor_UpdateNameInPhonecalls(timestamp, phonebookNumbers); |
||
| 369 | } |
||
| 1899 | linlun | 370 | } |
| 371 | Console_RegisterCommand(Sensor_StoreNumberInPhonebook, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args); }); |
||
| 372 | |||
| 2091 | runge | 373 | function Sensor_UpdateNameInPhonecalls(timestamp, numbers) |
| 374 | { |
||
| 375 | if (numbers.length > 0) |
||
| 376 | { |
||
| 377 | var lastValueString = Storage_GetParameter("PhoneCalls", timestamp); |
||
| 1899 | linlun | 378 | |
| 2091 | runge | 379 | if (lastValueString) |
| 380 | { |
||
| 381 | var lastValue = JSON.parse(lastValueString); |
||
| 1899 | linlun | 382 | |
| 2091 | runge | 383 | lastValue.names = numbers; |
| 384 | |||
| 385 | Storage_SetParameter("PhoneCalls", timestamp, JSON.stringify(lastValue)); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | } |
||
| 389 |