Subversion Repositories HomeAutomation

Rev

Rev 2257 | Rev 2296 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1648 runge 1
 
2275 linlun 2
Sensor_ModuleNames    = [ "DS18x20", "FOST02", "BusVoltage", "SimpleDTMF", "DHT11", "VoltageCurrent", "ultrasonic", "flower", "NTC", "TC1047A", "LM335", "TCN75A"];
2205 arune 3
SensorRf_ModuleNames    = [ "rfTransceive" ];
1648 runge 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
    {
1649 runge 12
        Log("\033[31mNot enough parameters given.\033[0m\n");
1648 runge 13
        return false;
14
    }
2091 runge 15
 
1648 runge 16
    var aliases_data = Module_ResolveAlias(alias_name, Sensor_ModuleNames);
17
    var found = false;
2091 runge 18
 
1648 runge 19
    for (var name in aliases_data)
20
    {
1765 linlun 21
        var variables = {"Time"     : interval };
1649 runge 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
        }
2091 runge 30
 
1649 runge 31
        found = true;
32
    }
2091 runge 33
 
1649 runge 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
    }
2091 runge 39
 
1649 runge 40
    return true;
41
}
42
Console_RegisterCommand(Sensor_SetReportInterval, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Sensor_Aliases(), Sensor_Intervals()); });
43
 
2091 runge 44
Sensor_OnNewPhonenumberFunctions = [];
2050 arune 45
 
46
function Sensor_RegisterToNewPhonenumber(alias_name, callback)
47
{
2091 runge 48
  if (!Sensor_OnNewPhonenumberFunctions[alias_name])
49
  {
50
    Sensor_OnNewPhonenumberFunctions[alias_name] = [];
51
  }
52
 
53
  Sensor_OnNewPhonenumberFunctions[alias_name].push(callback);
2050 arune 54
}
55
 
2207 arune 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
 
1649 runge 69
function Sensor_OnMessage(module_name, module_id, command, variables)
70
{
2091 runge 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
    });
1807 linlun 78
 
2091 runge 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;
2098 runge 87
          var checkName = true;
1649 runge 88
 
2098 runge 89
          Log("received from DTMF:\"" + number + "\"");
90
 
2122 runge 91
          if (variables["Number"].length >= 6)
2091 runge 92
          {
2092 runge 93
            /* A seems to mean it is an incomming call! */
94
            if (number.charAt(0) === 'A' || number.charAt(0) === 'D')
2091 runge 95
            {
2092 runge 96
              incommingCall = true;
2091 runge 97
 
98
              /* Remove leading A */
99
              number = number.substring(1);
100
            }
2092 runge 101
            else /* Outgoing call */
2091 runge 102
            {
2092 runge 103
              incommingCall = false;
2091 runge 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;
2098 runge 120
            checkName = false;
2091 runge 121
          }
122
          else if (number == "B10C")
123
          {
124
            /* Secret/protected number */
125
            number = "Protected";
126
            incommingCall = true;
2098 runge 127
            checkName = false;
2091 runge 128
          }
129
          else
130
          {
131
            Log("DTMF decoded number is to short, number: \"" + number + "\", length: " + number.length);
132
            return;
133
          }
2181 arune 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
          }
2091 runge 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"));
2152 arune 174
 
2091 runge 175
          /* Lookup name */
2098 runge 176
          if (checkName)
177
          {
2181 arune 178
            Sensor_StoreNumberInPhonebook(number, timestamp);
2098 runge 179
          }
2091 runge 180
 
2181 arune 181
          var phonebookNumbers = Storage_GetJsonParamter("PhoneBook", number);
2152 arune 182
          if (!phonebookNumbers)
183
          {
184
            phonebookNumbers = [];
185
          }
186
 
2091 runge 187
          /* Call any potential subscribers */
188
          if (Sensor_OnNewPhonenumberFunctions[alias_name])
189
          {
190
            for (var n in Sensor_OnNewPhonenumberFunctions[alias_name])
191
            {
2152 arune 192
              /* Call callback with arguments alias, number, direction */
193
              Sensor_OnNewPhonenumberFunctions[alias_name][n](alias_name, number, incommingCall ? "in" : "out", phonebookNumbers);
2091 runge 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":
2232 linlun 209
      case "Percent":
210
      case "Distance":
2091 runge 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
          }
2257 linlun 226
      last_value[command] = { "value" : variables["Value"], "timestamp" : get_time() };
2091 runge 227
          Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
228
        }
229
 
230
        break;
231
      }
232
    }
233
  }
1649 runge 234
}
235
Module_RegisterToOnMessage("all", Sensor_OnMessage);
1899 linlun 236
 
2205 arune 237
function SensorRf_OnMessage(module_name, module_id, command, variables)
238
{
2257 linlun 239
 
2205 arune 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   });
2237 linlun 246
 
2211 arune 247
        /* Must check sensor here in case no alias is stored the adress needs to be printed */
2240 linlun 248
        var RubicsonSuccess = "NoRubicsonSensor";
2257 linlun 249
        var OregonRainSuccess = "NoSensor";
250
        var OregonTempHumSuccess = "NoSensor";
251
        var OregonWindSuccess = "NoSensor";
2205 arune 252
        var VikingSuccess = "NoVikingSensor";
2237 linlun 253
        var VikingSteakSuccess = "NoVikingSteakSensor";
2257 linlun 254
        //Log("found sensor: "+variables["Protocol"]);
255
        if (variables["Protocol"] == "OregonRain")
2211 arune 256
        {
2257 linlun 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
        {
2211 arune 338
            /*
2240 linlun 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
            /*
2211 arune 365
                              ??? aaaaaaaa sttttttttttt hhhhhhhh cccccccc
366
            341731651126    0b100 11111001 000011001011 00101110 00110110   20.3 46%
367
            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 unknown = 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 (unknown != 4)
388
            {
389
                Log("\033[33mWarning: VikingSensor, unknown part="+unknown+", data="+data+"\033[0m");
390
            }
2237 linlun 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";
2211 arune 400
        }
401
 
2205 arune 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":
2207 arune 411
                    {   /* Add alias with these specifics: Channel=0,Proto=Viking,Address=<address of sensor> */
2205 arune 412
                        if (crc != calccrc)
413
                        {
414
                            Log("\033[31mError: VikingSensor, incorrect CRC, data="+data+", CRC="+crc+", calcCRC="+calccrc+"\033[0m");
415
                            break loopAliases;
416
                        }
417
                        if (addr == aliases_data[alias_name]["specific"]["Address"])
418
                        {
419
                            VikingSuccess = "VikingFound";
420
 
421
                            var last_value = {};
422
                            var last_value_string = Storage_GetParameter("LastValues", alias_name);
423
 
424
                            if (last_value_string)
425
                            {
426
                                last_value = eval("(" + last_value_string + ")");
427
                            }
428
 
429
                            var timestamp = get_time();
430
                            last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
2218 arune 431
                            if (humidity<101)
432
                            {
433
                                last_value["Humidity_Percent"] = { "value" : humidity.toString(), "timestamp" : timestamp };
434
                            }
2205 arune 435
 
436
                            Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
437
 
438
                            break loopAliases;
439
                        }
440
                        break;
441
                    }
2240 linlun 442
                    case "Viking":
443
                    {   /* Add alias with these specifics: Channel=0,Proto=Viking,Address=<address of sensor> */
444
                        /*if (crc != calccrc)
445
                        {
446
                            Log("\033[31mError: VikingSensor, incorrect CRC, data="+data+", CRC="+crc+", calcCRC="+calccrc+"\033[0m");
447
                            break loopAliases;
448
                        }*/
449
                        if (addr == aliases_data[alias_name]["specific"]["Address"])
450
                        {
451
                            RubicsonSuccess = "RubicsonFound";
452
 
453
                            var last_value = {};
454
                            var last_value_string = Storage_GetParameter("LastValues", alias_name);
455
 
456
                            if (last_value_string)
457
                            {
458
                                last_value = eval("(" + last_value_string + ")");
459
                            }
460
 
461
                            var timestamp = get_time();
462
                            last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
463
                            /*if (humidity<101)
464
                            {
465
                                last_value["Humidity_Percent"] = { "value" : humidity.toString(), "timestamp" : timestamp };
466
                            }
467
                            */
468
                            Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
469
 
470
                            break loopAliases;
471
                        }
472
                        break;
473
                    }
2237 linlun 474
                    case "VikingSteak":
475
                    {   /* Add alias with these specifics: Channel=0,Proto=VikingSteak,Address=<address of sensor> */
476
                        /* if no Address is added to the alias, all vikingSteaksensors will match*/
477
                        if (aliases_data[alias_name]["specific"]["Address"] == undefined)
478
                        {
479
                            VikingSteakSuccess = "VikingFound";
480
                            var last_value = {};
481
                            var last_value_string = Storage_GetParameter("LastValues", alias_name);
482
 
483
                            if (last_value_string)
484
                            {
485
                                last_value = eval("(" + last_value_string + ")");
486
                            }
487
 
488
                            var timestamp = get_time();
489
                            last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
490
                            last_value["Address"] = { "value" : addr, "timestamp" : timestamp };
491
                            Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
492
                            break loopAliases;
493
                        }
494
                        else if (addr == aliases_data[alias_name]["specific"]["Address"])
495
                        {
496
                            VikingSteakSuccess = "VikingFound";
497
                            var last_value = {};
498
                            var last_value_string = Storage_GetParameter("LastValues", alias_name);
499
 
500
                            if (last_value_string)
501
                            {
502
                                last_value = eval("(" + last_value_string + ")");
503
                            }
504
 
505
                            var timestamp = get_time();
506
                            last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
507
                            last_value["Address"] = { "value" : addr, "timestamp" : timestamp };
508
                            Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
509
 
510
                            break loopAliases;
511
                        }
512
                        break;
513
                    }
2240 linlun 514
                    case "Rubicson":
515
                    {   /* Add alias with these specifics: Channel=0,Proto=Viking,Address=<address of sensor> */
516
                        if (crc != calccrc)
517
                        {
518
                            Log("\033[31mError: Rubicson, incorrect CRC, data="+data+", CRC="+crc+", calcCRC="+calccrc+"\033[0m");
519
                            break loopAliases;
520
                        }
521
                        if (addr == aliases_data[alias_name]["specific"]["Address"])
522
                        {
523
                            VikingSuccess = "VikingFound";
524
 
525
                            var last_value = {};
526
                            var last_value_string = Storage_GetParameter("LastValues", alias_name);
527
 
528
                            if (last_value_string)
529
                            {
530
                                last_value = eval("(" + last_value_string + ")");
531
                            }
532
 
533
                            var timestamp = get_time();
534
                            last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
535
                            if (humidity<101)
536
                            {
537
                                last_value["Humidity_Percent"] = { "value" : humidity.toString(), "timestamp" : timestamp };
538
                            }
539
 
540
                            Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
541
 
542
                            break loopAliases;
543
                        }
544
                        break;
545
                    }
2207 arune 546
                    case "Nexa2":
2211 arune 547
                    {   /* Add alias with these specifics: Channel=0,Proto=Nexa2,OnData=<data for on>,OffData=<data for off> */
2207 arune 548
                        var deviceStateCommand = "";
549
                        if (variables["IRdata"] == aliases_data[alias_name]["specific"]["OnData"] && variables["Status"] == "Pressed")
550
                        {
551
                            deviceStateCommand = "On";
552
                        }
553
                        else if (variables["IRdata"] == aliases_data[alias_name]["specific"]["OffData"] && variables["Status"] == "Pressed")
554
                        {
555
                            deviceStateCommand = "Off";
556
                        }
557
                        else
558
                        {
559
                            break;
560
                        }
561
 
562
                        var last_value = {};
563
                        var last_value_string = Storage_GetParameter("LastValues", alias_name);
564
 
565
                        if (last_value_string)
566
                        {
567
                            last_value = eval("(" + last_value_string + ")");
568
                        }
569
 
570
                        if (( typeof(last_value["State"]) == "undefined" ) ||
571
                            ( deviceStateCommand == "Off" && last_value["State"]["value"] == "On" ) ||
572
                            ( deviceStateCommand == "On" && last_value["State"]["value"] == "Off" ))
573
                        {
574
                            last_value["State"] = { "value" : deviceStateCommand, "timestamp" : get_time() };
575
 
576
                            Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
577
 
578
                            /* Call any potential subscribers */
579
                            if (Sensor_OnDeviceStateChangeFunctions[alias_name])
580
                            {
581
                                for (var n in Sensor_OnDeviceStateChangeFunctions[alias_name])
582
                                {
583
                                    /* Call callback with arguments alias, state */
584
                                    Sensor_OnDeviceStateChangeFunctions[alias_name][n](alias_name, deviceStateCommand);
585
                                }
586
                            }
587
                        }
588
                        break;
589
                    }
2205 arune 590
                }
591
            }
592
        }
593
        if (VikingSuccess == "VikingNotFound")
594
        {
595
            Log("Found unknown vikingsensor address="+addr+" temperature="+temp+" humidity="+humidity+" unknown="+unknown);
596
        }
2237 linlun 597
        if (VikingSteakSuccess == "VikingNotFound")
598
        {
599
            Log("Found unknown vikingSteaksensor address="+addr+" temperature="+temp);
600
        }
2240 linlun 601
        if (RubicsonSuccess == "RubicsonNotFound")
602
        {
603
            Log("Found unknown Rubicson sensor address="+addr+" temperature="+temp);
604
        }
2257 linlun 605
        if (OregonTempHumSuccess == "NotFound")
606
        {
607
            Log("Found unknown oregon temp/humidity sensor address="+addr+" temperature="+temp +" humidity="+humidity+" unknown="+unknown + " bat="+bat);
608
        }
609
        if (OregonRainSuccess == "NotFound")
610
        {
611
            Log("Found unknown oregon rain sensor address="+addr+" tot inch="+totInch +" totMM="+totMM+" inch/h="+inchPerHour + " mm/h="+mmPerHour +" bat="+bat);
612
        }
613
        if (OregonWindSuccess == "NotFound")
614
        {
615
            Log("Found unknown oregon wind sensor address="+addr+" Average="+average +" current="+current+" direction="+direction +" bat="+bat);
616
        }
2205 arune 617
    }
618
}
619
Module_RegisterToOnMessage("all", SensorRf_OnMessage);
620
 
2091 runge 621
function Sensor_StoreNumberInPhonebook(number, timestamp)
1899 linlun 622
{
2152 arune 623
  var phonebookNumbers = Storage_GetJsonParamter("PhoneBook", number);
2091 runge 624
 
625
  if (!phonebookNumbers)
626
  {
627
    phonebookNumbers = [];
628
 
629
    url = "wap.eniro.se/query?search_word=" + number + "&what=mobwp";
630
    //Log("\033[31mURL: http://"+url+"\033[0m\n");
631
 
632
    Http_Request(url, function(socket_id, result, header, content_data)
633
    {
634
      //Log("\033[31mOnlinePhonebook-eniro: result was: " + result + "\033[0m\n");
635
      //Log("\033[31mOnlinePhonebook-eniro: content was: " + content_data + "\033[0m\n");
636
 
637
      if (result.indexOf("200 OK") != -1)
638
      {
639
 
640
        var lines = content_data.split("\n");
641
 
642
        var endString = "<anchor>Tillbaka<prev/></anchor>";
643
        var nameStartString = "<td class=\"hTd2\">";
644
        var nameEndString = "</table>";
645
 
646
        for (var n = 1; n < lines.length; n++)
647
        {
648
          var line = lines[n].trim("\n");
649
 
650
          if (line.indexOf(endString) != -1)
651
          {
652
            break;
653
          }
654
 
655
          if (line.indexOf(nameStartString) != -1)
656
          {
657
            line = lines[n + 1].trim("\n");
658
            splitted = line.split("b>");
659
 
660
            phonebookNumbers.push(splitted[1].substr(0, splitted[1].length - 2));
661
            break;
662
          }
663
        }
664
 
665
        //Log("\033[31mOnlinePhonebook: Line: " + lines[1] + "\033[0m\n");
666
        //Log("\033[31mOnlinePhonebook: Line: " + lines[2] + "\033[0m\n");
667
        //Log("\033[31mOnlinePhonebook: Line: " + lines[3] + "\033[0m\n");
668
        if (phonebookNumbers.length > 0)
669
        {
670
          Storage_SetJsonParameter("PhoneBook", number, phonebookNumbers);
671
 
672
          Sensor_UpdateNameInPhonecalls(timestamp, phonebookNumbers);
673
 
674
          Log("\033[31mOnlinePhonebook-eniro: Found: " + JSON.stringify(phonebookNumbers) + "\033[0m\n");
675
        }
676
      }
677
      else
678
      {
679
        Log("\033[31mOnlinePhonebook-eniro: Failed to do name lookup, result was: " + result + "\033[0m\n");
680
      }
681
 
2094 runge 682
      if (phonebookNumbers.length == 0)
2091 runge 683
      {
2094 runge 684
        if (typeof sensorRKSEEK_API !== 'undefined')
2091 runge 685
        {
2094 runge 686
          url = "rkseek.oblivioncreations.se/?client="+sensorRKSEEK_API+"&n=" + number + "&out=text";
687
          Log("\033[31mURL: http://"+url+"\033[0m\n");
2091 runge 688
 
2094 runge 689
          Http_Request(url, function(socket_id, result, header, content_data)
2091 runge 690
          {
2094 runge 691
            phonebookNumbers = [];
692
 
693
            if (result.indexOf("200 OK") != -1)
2091 runge 694
            {
2094 runge 695
              //Log("\033[31mOnlinePhonebook: result was: " + content_data + "\033[0m\n");
696
              var lines = content_data.split("\n");
697
              //Log("\033[31mOnlinePhonebook: Line: " + lines[1] + "\033[0m\n");
698
              //Log("\033[31mOnlinePhonebook: Line: " + lines[2] + "\033[0m\n");
699
              //Log("\033[31mOnlinePhonebook: Line: " + lines[3] + "\033[0m\n");
700
              if (lines[2].length > 0)
701
              {
702
                phonebookNumbers = [ lines[2] ]; // TODO Is this really correct?!
703
                Storage_SetJsonParameter("PhoneBook", number, phonebookNumbers);
704
 
705
                Sensor_UpdateNameInPhonecalls(timestamp, phonebookNumbers);
2152 arune 706
 
2094 runge 707
                Log("\033[31mOnlinePhonebook-rseek: Found: " + JSON.stringify(phonebookNumbers) + "\033[0m\n");
708
              }
2091 runge 709
            }
2094 runge 710
            else
711
            {
712
              Log("\033[31mOnlinePhonebook-rseek: Failed to do name lookup, result was: " + result + "\033[0m\n");
713
            }
714
          });
715
        }
2091 runge 716
      }
2094 runge 717
    });
2014 linlun 718
        /*
2091 runge 719
        Http_Request("wap.hitta.se/default.aspx?Who=" + number + "&Where=&PageAction=White",
1899 linlun 720
            function(socket_id, result, header, content_data) {
721
                var persons = new Array();
722
                if (result.indexOf("200 OK") != -1)
723
                {
724
                    var endString = "<anchor>Tillbaka<prev/></anchor>";
725
                    var nameStartString = " title=\"Link\">";
726
                    var nameEndString = "</a>";
2091 runge 727
 
1899 linlun 728
                    var lines = content_data.split("<br/>");
2091 runge 729
 
1899 linlun 730
                    for (var n = 1; n < lines.length; n++)
731
                    {
732
                        var line = lines[n].trim(" \n");
2091 runge 733
 
1899 linlun 734
                        if (line.indexOf(endString) != -1)
735
                        {
736
                            break;
737
                        }
738
                        var pos = line.indexOf(nameStartString + (persons.length+1) + ".");
2091 runge 739
 
1899 linlun 740
                        if (pos != -1)
741
                        {
742
                            pos += nameStartString.length + 2
743
                            persons[persons.length] = line.substr(pos, line.length-pos-nameEndString.length).html_entity_decode().replace("  ", " ");
744
                        }
745
                    }
746
                    if (persons.length > 0) {
747
                        Storage_SetJsonParameter("PhoneBook", number, persons);
2091 runge 748
 
1899 linlun 749
                        //CAll all listeners!
750
                        //....
2091 runge 751
 
1899 linlun 752
                        Log("\033[31mOnlinePhonebook: Found: " + JSON.stringify(persons) + "\033[0m\n");
753
                    }
754
                }
755
                else
756
                {
757
                    Log("\033[31mOnlinePhonebook: Failed to do name lookup, result was: " + result + "\033[0m\n");
2091 runge 758
                }
1899 linlun 759
            }
760
        );
2014 linlun 761
        */
2091 runge 762
 
763
  }
764
  else
765
  {
766
    //Log("Number already stored\n");
767
 
768
    Sensor_UpdateNameInPhonecalls(timestamp, phonebookNumbers);
769
  }
2152 arune 770
 
771
  return phonebookNumbers;
1899 linlun 772
}
773
Console_RegisterCommand(Sensor_StoreNumberInPhonebook, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args); });
774
 
2182 arune 775
function Sensor_StoreNumberInPhonebookManual(number, name)
776
{
777
    if (arguments.length < 2)
778
    {
779
        Log("\033[31mNot enough parameters given.\033[0m\n");
780
        return false;
781
    }
782
 
783
    var completename = arguments[1];
784
    for (var i=2; i<arguments.length; i++)
785
    {
786
        completename = completename+" "+arguments[i];
787
    }
788
 
789
    phonebookNumbers = [];
790
    phonebookNumbers.push(completename);
791
    Storage_SetJsonParameter("PhoneBook", number, phonebookNumbers);
792
}
793
Console_RegisterCommand(Sensor_StoreNumberInPhonebookManual, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args); });
794
 
2091 runge 795
function Sensor_UpdateNameInPhonecalls(timestamp, numbers)
796
{
797
  if (numbers.length > 0)
798
  {
799
    var lastValueString = Storage_GetParameter("PhoneCalls", timestamp);
1899 linlun 800
 
2091 runge 801
    if (lastValueString)
802
    {
803
      var lastValue = JSON.parse(lastValueString);
1899 linlun 804
 
2091 runge 805
      lastValue.names = numbers;
806
 
807
      Storage_SetParameter("PhoneCalls", timestamp, JSON.stringify(lastValue));
808
    }
809
  }
810
}
811
 
2232 linlun 812
function Sensor_SetUltrasonicMode(alias_name, BottomLimit, TopLimit, mode)
813
{
814
    if (arguments.length < 4)
815
    {
816
        Log("\033[31mNot enough parameters given.\033[0m\n");
817
        return false;
818
    }
819
 
820
    var aliases_data = Module_ResolveAlias(alias_name, Sensor_ModuleNames);
821
    var found = false;
822
 
823
    for (var name in aliases_data)
824
    {
825
        var variables = {   "0Percent"     : BottomLimit,
826
                    "100Percent"   : TopLimit,
827
                    "SensorMode"   : mode,
828
                    "SensorId"  : aliases_data[name]["specific"]["Channel"],
829
        };
830
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "UltrasonicConfig", variables))
831
        {
832
            Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
833
        }
834
        else
835
        {
836
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
837
        }
838
 
839
        found = true;
840
    }
841
 
842
    if (!found)
843
    {
844
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
845
        return false;
846
    }
847
 
848
    return true;
849
}
850
Console_RegisterCommand(Sensor_SetUltrasonicMode, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Sensor_Aliases()); });