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