Rev 1807 | Rev 1900 | 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'); |
||
| 64 | if (endIndex =! -1) { |
||
| 65 | variables["Number"] = variables["Number"].subString(1,endIndex); |
||
| 66 | } |
||
| 67 | } else { |
||
| 68 | direction = "out"; |
||
| 69 | } |
||
| 70 | var last_value = {}; |
||
| 71 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
||
| 1649 | runge | 72 | |
| 1899 | linlun | 73 | if (last_value_string) |
| 74 | { |
||
| 75 | last_value = eval("(" + last_value_string + ")"); |
||
| 76 | } |
||
| 77 | |||
| 78 | last_value[command] = { "value" : variables["Number"], "timestamp" : get_time(), "direction" : direction }; |
||
| 79 | |||
| 80 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
||
| 1807 | linlun | 81 | |
| 1899 | linlun | 82 | var all_numbers = Storage_GetParameters("PhoneCalls"); |
| 83 | var newString = "{\"number\":\"" + variables["Number"] + "\",\"module\":\"" + alias_name + "\",\"direction\":\"" + direction + "\"}"; |
||
| 84 | Storage_SetParameter("PhoneCalls", get_time(), newString); |
||
| 85 | Log("New number: "+newString +"\n"); |
||
| 86 | Sensor_StoreNumberInPhonebook(variables["Number"]); |
||
| 87 | } |
||
| 1649 | runge | 88 | } |
| 89 | break; |
||
| 90 | } |
||
| 91 | case "Voltage": |
||
| 92 | case "Temperature_Celsius": |
||
| 93 | case "Humidity_Percent": |
||
| 94 | { |
||
| 95 | for (var alias_name in aliases_data) |
||
| 96 | { |
||
| 97 | if (aliases_data[alias_name]["specific"]["SensorId"] != variables["SensorId"]) |
||
| 98 | { |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | |||
| 102 | var last_value = {}; |
||
| 1650 | runge | 103 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
| 1649 | runge | 104 | |
| 105 | if (last_value_string) |
||
| 106 | { |
||
| 107 | last_value = eval("(" + last_value_string + ")"); |
||
| 108 | } |
||
| 109 | |||
| 110 | last_value[command] = { "value" : variables["Value"], "timestamp" : get_time() }; |
||
| 111 | |||
| 1650 | runge | 112 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
| 1649 | runge | 113 | } |
| 114 | |||
| 115 | break; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | Module_RegisterToOnMessage("all", Sensor_OnMessage); |
||
| 1899 | linlun | 121 | |
| 122 | function Sensor_StoreNumberInPhonebook(number) |
||
| 123 | { |
||
| 124 | var phonebook = Storage_GetJsonParamter("PhoneBook",number) |
||
| 125 | if (!phonebook) { |
||
| 126 | Http_Request("wap.hitta.se/default.aspx?Who=" + number + "&Where=&PageAction=White", |
||
| 127 | function(socket_id, result, header, content_data) { |
||
| 128 | var persons = new Array(); |
||
| 129 | if (result.indexOf("200 OK") != -1) |
||
| 130 | { |
||
| 131 | var endString = "<anchor>Tillbaka<prev/></anchor>"; |
||
| 132 | var nameStartString = " title=\"Link\">"; |
||
| 133 | var nameEndString = "</a>"; |
||
| 134 | |||
| 135 | var lines = content_data.split("<br/>"); |
||
| 136 | |||
| 137 | for (var n = 1; n < lines.length; n++) |
||
| 138 | { |
||
| 139 | var line = lines[n].trim(" \n"); |
||
| 140 | |||
| 141 | if (line.indexOf(endString) != -1) |
||
| 142 | { |
||
| 143 | break; |
||
| 144 | } |
||
| 145 | var pos = line.indexOf(nameStartString + (persons.length+1) + "."); |
||
| 146 | |||
| 147 | if (pos != -1) |
||
| 148 | { |
||
| 149 | pos += nameStartString.length + 2 |
||
| 150 | persons[persons.length] = line.substr(pos, line.length-pos-nameEndString.length).html_entity_decode().replace(" ", " "); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | if (persons.length > 0) { |
||
| 154 | Storage_SetJsonParameter("PhoneBook", number, persons); |
||
| 155 | |||
| 156 | //CAll all listeners! |
||
| 157 | //.... |
||
| 158 | |||
| 159 | Log("\033[31mOnlinePhonebook: Found: " + JSON.stringify(persons) + "\033[0m\n"); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | else |
||
| 163 | { |
||
| 164 | Log("\033[31mOnlinePhonebook: Failed to do name lookup, result was: " + result + "\033[0m\n"); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | ); |
||
| 168 | } else { |
||
| 169 | Log("Number already stored\n"); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | Console_RegisterCommand(Sensor_StoreNumberInPhonebook, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args); }); |
||
| 173 | |||
| 174 | |||
| 175 |