Subversion Repositories HomeAutomation

Rev

Rev 2275 | Rev 2298 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. Sensor_ModuleNames    = [ "DS18x20", "FOST02", "BusVoltage", "SimpleDTMF", "DHT11", "VoltageCurrent", "ultrasonic", "flower", "NTC", "TC1047A", "LM335", "TCN75A"];
  3. SensorRf_ModuleNames    = [ "rfTransceive" ];
  4. Sensor_Intervals      = function() { return [ 1, 5, 10, 15, 20 ]; };
  5. Sensor_Aliases        = function() { return Module_GetAliasNames(Sensor_ModuleNames); };
  6. Sensor_AvailableIds   = function() { return Module_GetAvailableIds(Sensor_ModuleNames); };
  7.  
  8. function Sensor_SetReportInterval(alias_name, interval)
  9. {
  10.     if (arguments.length < 2)
  11.     {
  12.         Log("\033[31mNot enough parameters given.\033[0m\n");
  13.         return false;
  14.     }
  15.  
  16.     var aliases_data = Module_ResolveAlias(alias_name, Sensor_ModuleNames);
  17.     var found = false;
  18.  
  19.     for (var name in aliases_data)
  20.     {
  21.         var variables = {"Time"     : interval };
  22.         if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Report_Interval", variables))
  23.         {
  24.             Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
  25.         }
  26.         else
  27.         {
  28.             Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
  29.         }
  30.  
  31.         found = true;
  32.     }
  33.  
  34.     if (!found)
  35.     {
  36.         Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
  37.         return false;
  38.     }
  39.  
  40.     return true;
  41. }
  42. Console_RegisterCommand(Sensor_SetReportInterval, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Sensor_Aliases(), Sensor_Intervals()); });
  43.  
  44. Sensor_OnNewPhonenumberFunctions = [];
  45.  
  46. function Sensor_RegisterToNewPhonenumber(alias_name, callback)
  47. {
  48.   if (!Sensor_OnNewPhonenumberFunctions[alias_name])
  49.   {
  50.     Sensor_OnNewPhonenumberFunctions[alias_name] = [];
  51.   }
  52.  
  53.   Sensor_OnNewPhonenumberFunctions[alias_name].push(callback);
  54. }
  55.  
  56. Sensor_OnDeviceStateChangeFunctions = [];
  57.  
  58. function Sensor_RegisterToDeviceStateChange(alias_name, callback)
  59. {
  60.   if (!Sensor_OnDeviceStateChangeFunctions[alias_name])
  61.   {
  62.     Sensor_OnDeviceStateChangeFunctions[alias_name] = [];
  63.   }
  64.  
  65.   Sensor_OnDeviceStateChangeFunctions[alias_name].push(callback);
  66. }
  67.  
  68.  
  69. function Sensor_OnMessage(module_name, module_id, command, variables)
  70. {
  71.   if (in_array(Sensor_ModuleNames, module_name))
  72.   {
  73.     var aliases_data = Module_LookupAliases({
  74.       "module_name" : module_name,
  75.       "module_id"   : module_id,
  76.       "group"       : false
  77.     });
  78.  
  79.     switch (command)
  80.     {
  81.       case "Phonenumber":
  82.       {
  83.         for (var alias_name in aliases_data)
  84.         {
  85.           var number = variables["Number"];
  86.           var incommingCall = true;
  87.           var checkName = true;
  88.  
  89.           Log("received from DTMF:\"" + number + "\"");
  90.  
  91.           if (variables["Number"].length >= 6)
  92.           {
  93.             /* A seems to mean it is an incomming call! */
  94.             if (number.charAt(0) === 'A' || number.charAt(0) === 'D')
  95.             {
  96.               incommingCall = true;
  97.  
  98.               /* Remove leading A */
  99.               number = number.substring(1);
  100.             }
  101.             else /* Outgoing call */
  102.             {
  103.               incommingCall = false;
  104.             }
  105.  
  106.             /* A number can have a trailing C */
  107.             var endIndex = number.indexOf('C');
  108.  
  109.             if (endIndex !== -1)
  110.             {
  111.               /* Remove trailing C */
  112.               number = number.substring(0, endIndex);
  113.             }
  114.           }
  115.           else if (number == "B00C")
  116.           {
  117.             /* Unknown number */
  118.             number = "Unknown";
  119.             incommingCall = true;
  120.             checkName = false;
  121.           }
  122.           else if (number == "B10C")
  123.           {
  124.             /* Secret/protected number */
  125.             number = "Protected";
  126.             incommingCall = true;
  127.             checkName = false;
  128.           }
  129.           else
  130.           {
  131.             Log("DTMF decoded number is to short, number: \"" + number + "\", length: " + number.length);
  132.             return;
  133.           }
  134.          
  135.           /* Adding area code prefix if needed */
  136.           if (number.charAt(0) != '0' && checkName)
  137.           {
  138.             if (typeof sensorDefaultPhoneAreaCode === 'undefined')
  139.             {
  140.               Log("\033[31mOnlinePhonebook: please add sensorDefaultPhoneAreaCode = yourAreaCode to autostart.js, defaulting to 031\033[0m\n");
  141.               number = "031" + number;
  142.             }
  143.             else
  144.             {
  145.                 number = sensorDefaultPhoneAreaCode + number;
  146.             }
  147.           }
  148.  
  149.           /* Store number and direction in last values */
  150.           var lastValue = {};
  151.           var lastValueString = Storage_GetParameter("LastValues", alias_name);
  152.  
  153.           if (lastValueString)
  154.           {
  155.             lastValue = JSON.parse(lastValueString);
  156.           }
  157.  
  158.           var timestamp = get_time();
  159.  
  160.           lastValue["Phonenumber"] = { "value" : number, "timestamp" : timestamp };
  161.           lastValue["Direction"] = { "value" : incommingCall ? "in" : "out", "timestamp" : timestamp };
  162.  
  163.           Storage_SetParameter("LastValues", alias_name, JSON.stringify(lastValue));
  164.  
  165.  
  166.           /* Store number in phone call history */
  167.           var newValue = { "number" : number, "direction" : incommingCall ? "in" : "out", "module" : alias_name, "names" : [], "timestamp" : timestamp };
  168.  
  169.           Storage_SetParameter("PhoneCalls", timestamp, JSON.stringify(newValue));
  170.  
  171.  
  172.           /* Log the number */
  173.           Log("New number: " + number + ", direction: " + (incommingCall ? "in" : "out"));
  174.          
  175.           /* Lookup name */
  176.           if (checkName)
  177.           {
  178.             Sensor_StoreNumberInPhonebook(number, timestamp);
  179.           }
  180.  
  181.           var phonebookNumbers = Storage_GetJsonParamter("PhoneBook", number);
  182.           if (!phonebookNumbers)
  183.           {
  184.             phonebookNumbers = [];
  185.           }
  186.          
  187.           /* Call any potential subscribers */
  188.           if (Sensor_OnNewPhonenumberFunctions[alias_name])
  189.           {
  190.             for (var n in Sensor_OnNewPhonenumberFunctions[alias_name])
  191.             {
  192.               /* Call callback with arguments alias, number, direction */
  193.               Sensor_OnNewPhonenumberFunctions[alias_name][n](alias_name, number, incommingCall ? "in" : "out", phonebookNumbers);
  194.             }
  195.           }
  196.         }
  197.         break;
  198.       }
  199.       case "Voltage":
  200.       case "Temperature_Celsius":
  201.       {
  202.         if ((module_name == "DS18x20") && (variables["Value"] == 85))
  203.         {
  204.           Log("\033[31mDS18x20: Temperature conversion error: " + module_name + ":" + module_id + ", SensorId=" + variables["SensorId"] + "\033[0m");
  205.           break;
  206.         }
  207.       }
  208.       case "Humidity_Percent":
  209.       case "Percent":
  210.       case "Distance":
  211.       {
  212.         for (var alias_name in aliases_data)
  213.         {
  214.           if (aliases_data[alias_name]["specific"]["SensorId"] != variables["SensorId"])
  215.           {
  216.             continue;
  217.           }
  218.  
  219.           var last_value = {};
  220.           var last_value_string = Storage_GetParameter("LastValues", alias_name);
  221.  
  222.           if (last_value_string)
  223.           {
  224.             last_value = eval("(" + last_value_string + ")");
  225.           }
  226.       last_value[command] = { "value" : variables["Value"], "timestamp" : get_time() };
  227.           Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
  228.         }
  229.  
  230.         break;
  231.       }
  232.     }
  233.   }
  234. }
  235. Module_RegisterToOnMessage("all", Sensor_OnMessage);
  236.  
  237. function SensorRf_OnMessage(module_name, module_id, command, variables)
  238. {
  239.  
  240.     if (in_array(SensorRf_ModuleNames, module_name))
  241.     {
  242.         var aliases_data = Module_LookupAliases({
  243.         "module_name" : module_name,
  244.         "module_id"   : module_id,
  245.         "group"       : false   });
  246.        
  247.         /* Must check sensor here in case no alias is stored the adress needs to be printed */
  248.         var RubicsonSuccess = "NoRubicsonSensor";
  249.         var OregonRainSuccess = "NoSensor";
  250.         var OregonTempHumSuccess = "NoSensor";
  251.         var OregonWindSuccess = "NoSensor";
  252.         var VikingSuccess = "NoVikingSensor";
  253.         var VikingSteakSuccess = "NoVikingSteakSensor";
  254.         //Log("found sensor: "+variables["Protocol"]);
  255.         if (variables["Protocol"] == "OregonRain")
  256.         {
  257.             /* ----------- Data order proto-data ------
  258.             * cc bb bb bb aa aa
  259.             * a   = Rain in 0.01 inches per hour
  260.             * b   = Total Rain in 0.001 inches
  261.             * cc   = 0xddefffff
  262.             *   dd = Channel
  263.             *   e  = Battery low flag
  264.             *   fffff = Lower part of rolling code
  265.             * -----------------------------*/
  266.             var data = variables["IRdata"];
  267.             var inchPerHour = (data)&0xFFFF;
  268.             inchPerHour = inchPerHour/100;
  269.             var totInch = (data>>>16)&0xFFFFFF;
  270.             totInch = totInch/1000;
  271.             var totMM = totInch * 25.4;
  272.             var mmPerHour = inchPerHour * 25.4;
  273.            
  274.             var addr = rshift(data,46)&0x3;
  275.             var bat = rshift(data,45)&0x1;
  276.             var unknown = rshift(data,40)&0x1F;
  277.  
  278.             OregonRainSuccess = "NotFound";
  279.            
  280.         } else if (variables["Protocol"] == "OregonTempHum")
  281.         {
  282.                /* ----------- Data order proto-data ------
  283.             * cc 00 00 bb bb aa
  284.             * aa   = Humidity in %
  285.             * bbbb = Temperature*10 in celcius
  286.             * cc   = 0xddefffff
  287.             *   dd = Channel
  288.             *   e  = Battery low flag
  289.             *   fffff = Lower part of rolling code
  290.             * -----------------------------*/  
  291.             var data = variables["IRdata"];
  292.             var humidity = (data)&0xFF;
  293.             var temp = (data>>>8)&0x7FFF;
  294.             var sign = (temp>>>23)&1;
  295.             if (sign > 0)
  296.             {
  297.                 temp = temp^0x7FFF;
  298.                 temp += 1;
  299.                 temp = -temp;
  300.             }
  301.             temp = temp/10;
  302.             var addr = rshift(data,46)&0x3;
  303.             var bat = rshift(data,45)&0x1;
  304.             var unknown = rshift(data,40)&0x1F;
  305.  
  306.             OregonTempHumSuccess = "NotFound";
  307.            
  308.         } else if (variables["Protocol"] == "OregonWind")
  309.         {
  310.             /* ----------- Data order proto-data ------
  311.             * cc 00 0d bb ba aa
  312.             * aaa  = wind speed average (in 0.1m/s)
  313.             * bbb  = wind speed current (in 0.1m/s)
  314.             * d    = Direction in 22.5 degrees
  315.             * cc   = 0xddefffff
  316.             *   dd = Channel
  317.             *   e  = Battery low flag
  318.             *   fffff = Lower part of rolling code
  319.             * -----------------------------*/
  320.             var data = variables["IRdata"];
  321.             var average = (data)&0xFFF;
  322.             average = average/10;
  323.            
  324.             var current = rshift(data,12)&0xFFF;
  325.             current = current/10;
  326.            
  327.             var direction = rshift(data,24)&0xF;
  328.             direction = direction*22.5;
  329.            
  330.             var addr = rshift(data,46)&0x3;
  331.             var bat = rshift(data,45)&0x1;
  332.             var unknown = rshift(data,40)&0x1F;
  333.  
  334.             OregonWindSuccess = "NotFound";
  335.            
  336.         } else if (variables["Protocol"] == "Rubicson")
  337.         {
  338.             /*
  339.                       ??? aaaaaaaa sttttttttttt hhhhhhhh cccccccc
  340.             341731651126    0b100 11111001 000011001011 00101110 00110110   20.3 46%
  341.             a = address, s = sign, t = temperature, h = humidity, c = crc
  342.             */
  343.             var data = variables["IRdata"];
  344.             var unknown = rshift(data,26)&0x3FF;
  345.             //var crc = data&0xFF;
  346.             //var humidity = (data>>>8)&0xFF;
  347.             var temp = (data>>>12)&0xFFF;
  348.             var addr = rshift(data,24)&0x3;
  349.             //var calccrc=crc8(rshift(data,8), 32);
  350.                    
  351.             var sign = (temp>>>11)&1;
  352.             if (sign > 0)
  353.             {
  354.                 temp = temp^0xFFF;
  355.                 temp += 1;
  356.                 temp = -temp;
  357.             }
  358.             temp = temp/10;
  359.  
  360.             RubicsonSuccess = "RubicsonNotFound";
  361.            
  362.         } else if (variables["Protocol"] == "Viking")
  363.         {
  364.             /*
  365.                               ppp aaaaaaaa sttttttttttt hhhhhhhh cccccccc
  366.             341731651126    0b100 11111001 000011001011 00101110 00110110   20.3 46%
  367.             p = protocol type, a = address, s = sign, t = temperature, h = humidity, c = crc
  368.             */
  369.             var data = variables["IRdata"];
  370.             var crc = data&0xFF;
  371.             var humidity = (data>>>8)&0xFF;
  372.             var temp = (data>>>16)&0xFFF;
  373.             var addr = rshift(data,28)&0xFF;
  374.             var type = rshift(data,36)&0xF;
  375.             var calccrc=crc8(rshift(data,8), 32);
  376.  
  377.             var sign = (temp>>>11)&1;
  378.             temp = temp&0x7FF;
  379.             if (sign > 0)
  380.             {
  381.                 temp = -temp;
  382.             }
  383.             temp = temp/10;
  384.  
  385.             VikingSuccess = "VikingNotFound";
  386.            
  387.             if (type != 4 && type != 3)
  388.             {
  389.                 Log("\033[33mWarning: VikingSensor, type="+type+", data="+data+"\033[0m");
  390.             }
  391.         } else if (variables["Protocol"] == "VikingSteak")
  392.         {
  393.             var data = variables["IRdata"];
  394.             var addr = (data>>>4)&0xFF;
  395.             var byte0 = rshift(data,32)&0xF;
  396.             var byte1 = rshift(data,28)&0xF;
  397.             var byte2 = rshift(data,24)&0xF;
  398.             var temp = Math.round((((((byte1^byte2)<<8)+((byte0^byte1)<<4)+byte0^10)-122)*5)/9);
  399.             VikingSteakSuccess = "VikingNotFound";
  400.         }
  401.                
  402.         loopAliases:    /* Label to break nested */
  403.         for (var alias_name in aliases_data)
  404.         {
  405.             if (aliases_data[alias_name]["specific"]["Channel"] == variables["Channel"] &&
  406.                 aliases_data[alias_name]["specific"]["Proto"] == variables["Protocol"])
  407.             {
  408.                 switch (variables["Protocol"])
  409.                 {
  410.                     case "Viking":
  411.                     {   /* Add alias with these specifics: Channel=0,Proto=Viking,Address=<address of sensor> */
  412.                         if (type == 4)
  413.                         {
  414.                             if (crc != calccrc)
  415.                             {
  416.                                 Log("\033[31mError: VikingSensor, incorrect CRC, data="+data+", CRC="+crc+", calcCRC="+calccrc+"\033[0m");
  417.                                 break loopAliases;
  418.                             }
  419.                             if (addr == aliases_data[alias_name]["specific"]["Address"])
  420.                             {
  421.                                 VikingSuccess = "VikingFound";
  422.  
  423.                                 var last_value = {};
  424.                                 var last_value_string = Storage_GetParameter("LastValues", alias_name);
  425.  
  426.                                 if (last_value_string)
  427.                                 {
  428.                                     last_value = eval("(" + last_value_string + ")");
  429.                                 }
  430.  
  431.                                 var timestamp = get_time();
  432.                                 last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
  433.                                 if (humidity<101)
  434.                                 {
  435.                                     last_value["Humidity_Percent"] = { "value" : humidity.toString(), "timestamp" : timestamp };
  436.                                 }
  437.  
  438.                                 Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
  439.                                
  440.                                 break loopAliases;
  441.                             }
  442.                         }
  443.                         else if (type == 3)
  444.                         {
  445.                             /* Don't check crc since all bits did not fit in CAN-frame */
  446.                             if (addr == aliases_data[alias_name]["specific"]["Address"])
  447.                             {
  448.                                 VikingSuccess = "VikingFound";
  449.  
  450.                                 var last_value = {};
  451.                                 var last_value_string = Storage_GetParameter("LastValues", alias_name);
  452.  
  453.                                 if (last_value_string)
  454.                                 {
  455.                                     last_value = eval("(" + last_value_string + ")");
  456.                                 }
  457.  
  458.                                 var timestamp = get_time();
  459.                                 temp = temp-40.0;
  460.                                 waterlevel = (crc*256 + humidity)*0.3;
  461.                                 last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
  462.                                 last_value["WaterAbsolute_mm"] = { "value" : waterlevel.toString(), "timestamp" : timestamp };
  463.  
  464.                                 Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
  465.                                
  466.                                 break loopAliases;
  467.                             }
  468.                         }
  469.                         break;
  470.                     }
  471.                     case "Rubicson":
  472.                     {   /* Add alias with these specifics: Channel=0,Proto=Rubicson,Address=<address of sensor> */
  473.                         /*if (crc != calccrc)
  474.                         {
  475.                             Log("\033[31mError: VikingSensor, incorrect CRC, data="+data+", CRC="+crc+", calcCRC="+calccrc+"\033[0m");
  476.                             break loopAliases;
  477.                         }*/
  478.                         if (addr == aliases_data[alias_name]["specific"]["Address"])
  479.                         {
  480.                             RubicsonSuccess = "RubicsonFound";
  481.  
  482.                             var last_value = {};
  483.                             var last_value_string = Storage_GetParameter("LastValues", alias_name);
  484.  
  485.                             if (last_value_string)
  486.                             {
  487.                                 last_value = eval("(" + last_value_string + ")");
  488.                             }
  489.  
  490.                             var timestamp = get_time();
  491.                             last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
  492.                             /*if (humidity<101)
  493.                             {
  494.                                 last_value["Humidity_Percent"] = { "value" : humidity.toString(), "timestamp" : timestamp };
  495.                             }
  496.                             */
  497.                             Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
  498.                            
  499.                             break loopAliases;
  500.                         }
  501.                         break;
  502.                     }
  503.                     case "VikingSteak":
  504.                     {   /* Add alias with these specifics: Channel=0,Proto=VikingSteak,Address=<address of sensor> */
  505.                         /* if no Address is added to the alias, all vikingSteaksensors will match*/
  506.                         if (aliases_data[alias_name]["specific"]["Address"] == undefined)
  507.                         {
  508.                             VikingSteakSuccess = "VikingFound";
  509.                             var last_value = {};
  510.                             var last_value_string = Storage_GetParameter("LastValues", alias_name);
  511.  
  512.                             if (last_value_string)
  513.                             {
  514.                                 last_value = eval("(" + last_value_string + ")");
  515.                             }
  516.  
  517.                             var timestamp = get_time();
  518.                             last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
  519.                             last_value["Address"] = { "value" : addr, "timestamp" : timestamp };
  520.                             Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
  521.                             break loopAliases;
  522.                         }
  523.                         else if (addr == aliases_data[alias_name]["specific"]["Address"])
  524.                         {
  525.                             VikingSteakSuccess = "VikingFound";
  526.                             var last_value = {};
  527.                             var last_value_string = Storage_GetParameter("LastValues", alias_name);
  528.  
  529.                             if (last_value_string)
  530.                             {
  531.                                 last_value = eval("(" + last_value_string + ")");
  532.                             }
  533.  
  534.                             var timestamp = get_time();
  535.                             last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
  536.                             last_value["Address"] = { "value" : addr, "timestamp" : timestamp };
  537.                             Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
  538.                            
  539.                             break loopAliases;
  540.                         }
  541.                         break;
  542.                     }
  543.                     case "Nexa2":
  544.                     {   /* Add alias with these specifics: Channel=0,Proto=Nexa2,OnData=<data for on>,OffData=<data for off> */
  545.                         var deviceStateCommand = "";
  546.                         if (variables["IRdata"] == aliases_data[alias_name]["specific"]["OnData"] && variables["Status"] == "Pressed")
  547.                         {
  548.                             deviceStateCommand = "On";
  549.                         }
  550.                         else if (variables["IRdata"] == aliases_data[alias_name]["specific"]["OffData"] && variables["Status"] == "Pressed")
  551.                         {
  552.                             deviceStateCommand = "Off";
  553.                         }
  554.                         else
  555.                         {
  556.                             break;
  557.                         }
  558.                        
  559.                         var last_value = {};
  560.                         var last_value_string = Storage_GetParameter("LastValues", alias_name);
  561.  
  562.                         if (last_value_string)
  563.                         {
  564.                             last_value = eval("(" + last_value_string + ")");
  565.                         }
  566.                        
  567.                         if (( typeof(last_value["State"]) == "undefined" ) ||
  568.                             ( deviceStateCommand == "Off" && last_value["State"]["value"] == "On" ) ||
  569.                             ( deviceStateCommand == "On" && last_value["State"]["value"] == "Off" ))
  570.                         {
  571.                             last_value["State"] = { "value" : deviceStateCommand, "timestamp" : get_time() };
  572.  
  573.                             Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
  574.  
  575.                             /* Call any potential subscribers */
  576.                             if (Sensor_OnDeviceStateChangeFunctions[alias_name])
  577.                             {
  578.                                 for (var n in Sensor_OnDeviceStateChangeFunctions[alias_name])
  579.                                 {
  580.                                     /* Call callback with arguments alias, state */
  581.                                     Sensor_OnDeviceStateChangeFunctions[alias_name][n](alias_name, deviceStateCommand);
  582.                                 }
  583.                             }
  584.                         }
  585.                         break;
  586.                     }
  587.                 }
  588.             }
  589.         }
  590.         if (VikingSuccess == "VikingNotFound")
  591.         {
  592.             Log("Found unknown vikingsensor address="+addr+" temperature="+temp+" humidity="+humidity+" protocol type="+type);
  593.         }
  594.         if (VikingSteakSuccess == "VikingNotFound")
  595.         {
  596.             Log("Found unknown vikingSteaksensor address="+addr+" temperature="+temp);
  597.         }
  598.         if (RubicsonSuccess == "RubicsonNotFound")
  599.         {
  600.             Log("Found unknown Rubicson sensor address="+addr+" temperature="+temp);
  601.         }
  602.         if (OregonTempHumSuccess == "NotFound")
  603.         {
  604.             Log("Found unknown oregon temp/humidity sensor address="+addr+" temperature="+temp +" humidity="+humidity+" unknown="+unknown + " bat="+bat);
  605.         }
  606.         if (OregonRainSuccess == "NotFound")
  607.         {
  608.             Log("Found unknown oregon rain sensor address="+addr+" tot inch="+totInch +" totMM="+totMM+" inch/h="+inchPerHour + " mm/h="+mmPerHour +" bat="+bat);
  609.         }
  610.         if (OregonWindSuccess == "NotFound")
  611.         {
  612.             Log("Found unknown oregon wind sensor address="+addr+" Average="+average +" current="+current+" direction="+direction +" bat="+bat);
  613.         }
  614.     }
  615. }
  616. Module_RegisterToOnMessage("all", SensorRf_OnMessage);
  617.  
  618. function Sensor_StoreNumberInPhonebook(number, timestamp)
  619. {
  620.   var phonebookNumbers = Storage_GetJsonParamter("PhoneBook", number);
  621.  
  622.   if (!phonebookNumbers)
  623.   {
  624.     phonebookNumbers = [];
  625.  
  626.     url = "wap.eniro.se/query?search_word=" + number + "&what=mobwp";
  627.     //Log("\033[31mURL: http://"+url+"\033[0m\n");
  628.  
  629.     Http_Request(url, function(socket_id, result, header, content_data)
  630.     {
  631.       //Log("\033[31mOnlinePhonebook-eniro: result was: " + result + "\033[0m\n");
  632.       //Log("\033[31mOnlinePhonebook-eniro: content was: " + content_data + "\033[0m\n");
  633.  
  634.       if (result.indexOf("200 OK") != -1)
  635.       {
  636.  
  637.         var lines = content_data.split("\n");
  638.  
  639.         var endString = "<anchor>Tillbaka<prev/></anchor>";
  640.         var nameStartString = "<td class=\"hTd2\">";
  641.         var nameEndString = "</table>";
  642.  
  643.         for (var n = 1; n < lines.length; n++)
  644.         {
  645.           var line = lines[n].trim("\n");
  646.  
  647.           if (line.indexOf(endString) != -1)
  648.           {
  649.             break;
  650.           }
  651.  
  652.           if (line.indexOf(nameStartString) != -1)
  653.           {
  654.             line = lines[n + 1].trim("\n");
  655.             splitted = line.split("b>");
  656.  
  657.             phonebookNumbers.push(splitted[1].substr(0, splitted[1].length - 2));
  658.             break;
  659.           }
  660.         }
  661.  
  662.         //Log("\033[31mOnlinePhonebook: Line: " + lines[1] + "\033[0m\n");
  663.         //Log("\033[31mOnlinePhonebook: Line: " + lines[2] + "\033[0m\n");
  664.         //Log("\033[31mOnlinePhonebook: Line: " + lines[3] + "\033[0m\n");
  665.         if (phonebookNumbers.length > 0)
  666.         {
  667.           Storage_SetJsonParameter("PhoneBook", number, phonebookNumbers);
  668.  
  669.           Sensor_UpdateNameInPhonecalls(timestamp, phonebookNumbers);
  670.  
  671.           Log("\033[31mOnlinePhonebook-eniro: Found: " + JSON.stringify(phonebookNumbers) + "\033[0m\n");
  672.         }
  673.       }
  674.       else
  675.       {
  676.         Log("\033[31mOnlinePhonebook-eniro: Failed to do name lookup, result was: " + result + "\033[0m\n");
  677.       }
  678.  
  679.       if (phonebookNumbers.length == 0)
  680.       {
  681.         if (typeof sensorRKSEEK_API !== 'undefined')
  682.         {
  683.           url = "rkseek.oblivioncreations.se/?client="+sensorRKSEEK_API+"&n=" + number + "&out=text";
  684.           Log("\033[31mURL: http://"+url+"\033[0m\n");
  685.  
  686.           Http_Request(url, function(socket_id, result, header, content_data)
  687.           {
  688.             phonebookNumbers = [];
  689.  
  690.             if (result.indexOf("200 OK") != -1)
  691.             {
  692.               //Log("\033[31mOnlinePhonebook: result was: " + content_data + "\033[0m\n");
  693.               var lines = content_data.split("\n");
  694.               //Log("\033[31mOnlinePhonebook: Line: " + lines[1] + "\033[0m\n");
  695.               //Log("\033[31mOnlinePhonebook: Line: " + lines[2] + "\033[0m\n");
  696.               //Log("\033[31mOnlinePhonebook: Line: " + lines[3] + "\033[0m\n");
  697.               if (lines[2].length > 0)
  698.               {
  699.                 phonebookNumbers = [ lines[2] ]; // TODO Is this really correct?!
  700.                 Storage_SetJsonParameter("PhoneBook", number, phonebookNumbers);
  701.                
  702.                 Sensor_UpdateNameInPhonecalls(timestamp, phonebookNumbers);
  703.  
  704.                 Log("\033[31mOnlinePhonebook-rseek: Found: " + JSON.stringify(phonebookNumbers) + "\033[0m\n");
  705.               }
  706.             }
  707.             else
  708.             {
  709.               Log("\033[31mOnlinePhonebook-rseek: Failed to do name lookup, result was: " + result + "\033[0m\n");
  710.             }
  711.           });
  712.         }
  713.       }
  714.     });
  715.         /*
  716.         Http_Request("wap.hitta.se/default.aspx?Who=" + number + "&Where=&PageAction=White",
  717.             function(socket_id, result, header, content_data) {
  718.                 var persons = new Array();
  719.                 if (result.indexOf("200 OK") != -1)
  720.                 {
  721.                     var endString = "<anchor>Tillbaka<prev/></anchor>";
  722.                     var nameStartString = " title=\"Link\">";
  723.                     var nameEndString = "</a>";
  724.  
  725.                     var lines = content_data.split("<br/>");
  726.  
  727.                     for (var n = 1; n < lines.length; n++)
  728.                     {
  729.                         var line = lines[n].trim(" \n");
  730.  
  731.                         if (line.indexOf(endString) != -1)
  732.                         {
  733.                             break;
  734.                         }
  735.                         var pos = line.indexOf(nameStartString + (persons.length+1) + ".");
  736.  
  737.                         if (pos != -1)
  738.                         {
  739.                             pos += nameStartString.length + 2
  740.                             persons[persons.length] = line.substr(pos, line.length-pos-nameEndString.length).html_entity_decode().replace("  ", " ");
  741.                         }
  742.                     }
  743.                     if (persons.length > 0) {
  744.                         Storage_SetJsonParameter("PhoneBook", number, persons);
  745.  
  746.                         //CAll all listeners!
  747.                         //....
  748.  
  749.                         Log("\033[31mOnlinePhonebook: Found: " + JSON.stringify(persons) + "\033[0m\n");
  750.                     }
  751.                 }
  752.                 else
  753.                 {
  754.                     Log("\033[31mOnlinePhonebook: Failed to do name lookup, result was: " + result + "\033[0m\n");
  755.                 }
  756.             }
  757.         );
  758.         */
  759.  
  760.   }
  761.   else
  762.   {
  763.     //Log("Number already stored\n");
  764.  
  765.     Sensor_UpdateNameInPhonecalls(timestamp, phonebookNumbers);
  766.   }
  767.  
  768.   return phonebookNumbers;
  769. }
  770. Console_RegisterCommand(Sensor_StoreNumberInPhonebook, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args); });
  771.  
  772. function Sensor_StoreNumberInPhonebookManual(number, name)
  773. {
  774.     if (arguments.length < 2)
  775.     {
  776.         Log("\033[31mNot enough parameters given.\033[0m\n");
  777.         return false;
  778.     }
  779.    
  780.     var completename = arguments[1];
  781.     for (var i=2; i<arguments.length; i++)
  782.     {
  783.         completename = completename+" "+arguments[i];
  784.     }
  785.    
  786.     phonebookNumbers = [];
  787.     phonebookNumbers.push(completename);
  788.     Storage_SetJsonParameter("PhoneBook", number, phonebookNumbers);
  789. }
  790. Console_RegisterCommand(Sensor_StoreNumberInPhonebookManual, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args); });
  791.  
  792. function Sensor_UpdateNameInPhonecalls(timestamp, numbers)
  793. {
  794.   if (numbers.length > 0)
  795.   {
  796.     var lastValueString = Storage_GetParameter("PhoneCalls", timestamp);
  797.  
  798.     if (lastValueString)
  799.     {
  800.       var lastValue = JSON.parse(lastValueString);
  801.  
  802.       lastValue.names = numbers;
  803.  
  804.       Storage_SetParameter("PhoneCalls", timestamp, JSON.stringify(lastValue));
  805.     }
  806.   }
  807. }
  808.  
  809. function Sensor_SetUltrasonicMode(alias_name, BottomLimit, TopLimit, mode)
  810. {
  811.     if (arguments.length < 4)
  812.     {
  813.         Log("\033[31mNot enough parameters given.\033[0m\n");
  814.         return false;
  815.     }
  816.  
  817.     var aliases_data = Module_ResolveAlias(alias_name, Sensor_ModuleNames);
  818.     var found = false;
  819.  
  820.     for (var name in aliases_data)
  821.     {
  822.         var variables = {   "0Percent"     : BottomLimit,
  823.                     "100Percent"   : TopLimit,
  824.                     "SensorMode"   : mode,
  825.                     "SensorId"  : aliases_data[name]["specific"]["Channel"],
  826.         };
  827.         if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "UltrasonicConfig", variables))
  828.         {
  829.             Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
  830.         }
  831.         else
  832.         {
  833.             Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
  834.         }
  835.  
  836.         found = true;
  837.     }
  838.  
  839.     if (!found)
  840.     {
  841.         Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
  842.         return false;
  843.     }
  844.  
  845.     return true;
  846. }
  847. Console_RegisterCommand(Sensor_SetUltrasonicMode, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Sensor_Aliases()); });
  848.