Subversion Repositories HomeAutomation

Rev

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
            /*
2296 arune 365
                              ppp aaaaaaaa sttttttttttt hhhhhhhh cccccccc
2211 arune 366
            341731651126    0b100 11111001 000011001011 00101110 00110110   20.3 46%
2296 arune 367
            p = protocol type, a = address, s = sign, t = temperature, h = humidity, c = crc
2211 arune 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;
2296 arune 374
            var type = rshift(data,36)&0xF;
2211 arune 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
 
2296 arune 387
            if (type != 4 && type != 3)
2211 arune 388
            {
2296 arune 389
                Log("\033[33mWarning: VikingSensor, type="+type+", data="+data+"\033[0m");
2211 arune 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> */
2296 arune 412
                        if (type == 4)
2205 arune 413
                        {
2296 arune 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
                            }
2205 arune 442
                        }
2296 arune 443
                        else if (type == 3)
2205 arune 444
                        {
2296 arune 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";
2205 arune 449
 
2296 arune 450
                                var last_value = {};
451
                                var last_value_string = Storage_GetParameter("LastValues", alias_name);
2205 arune 452
 
2296 arune 453
                                if (last_value_string)
454
                                {
455
                                    last_value = eval("(" + last_value_string + ")");
456
                                }
2205 arune 457
 
2296 arune 458
                                var timestamp = get_time();
459
                                temp = temp-40.0;
460
                                waterlevel = (crc*256 + humidity)*0.3;
2298 arune 461
                                /* Using COUNTER as DS in rrdtool does only work if the data is integer,
462
                                   to work around this let's create a second variable with the water level
463
                                   multiplied with 10 and rounded to integer.
464
                                   Then when generating graph, the data must be divided by 10 */
465
                                var waterlevel10xInt = Math.round(waterlevel*10);
466
 
2296 arune 467
                                last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
468
                                last_value["WaterAbsolute_mm"] = { "value" : waterlevel.toString(), "timestamp" : timestamp };
2298 arune 469
                                last_value["WaterAbsolute10xInt_mm"] = { "value" : waterlevel10xInt.toString(), "timestamp" : timestamp };
2296 arune 470
 
471
                                Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
472
 
473
                                break loopAliases;
2218 arune 474
                            }
2205 arune 475
                        }
476
                        break;
477
                    }
2296 arune 478
                    case "Rubicson":
479
                    {   /* Add alias with these specifics: Channel=0,Proto=Rubicson,Address=<address of sensor> */
2240 linlun 480
                        /*if (crc != calccrc)
481
                        {
482
                            Log("\033[31mError: VikingSensor, incorrect CRC, data="+data+", CRC="+crc+", calcCRC="+calccrc+"\033[0m");
483
                            break loopAliases;
484
                        }*/
485
                        if (addr == aliases_data[alias_name]["specific"]["Address"])
486
                        {
487
                            RubicsonSuccess = "RubicsonFound";
488
 
489
                            var last_value = {};
490
                            var last_value_string = Storage_GetParameter("LastValues", alias_name);
491
 
492
                            if (last_value_string)
493
                            {
494
                                last_value = eval("(" + last_value_string + ")");
495
                            }
496
 
497
                            var timestamp = get_time();
498
                            last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
499
                            /*if (humidity<101)
500
                            {
501
                                last_value["Humidity_Percent"] = { "value" : humidity.toString(), "timestamp" : timestamp };
502
                            }
503
                            */
504
                            Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
505
 
506
                            break loopAliases;
507
                        }
508
                        break;
509
                    }
2237 linlun 510
                    case "VikingSteak":
511
                    {   /* Add alias with these specifics: Channel=0,Proto=VikingSteak,Address=<address of sensor> */
512
                        /* if no Address is added to the alias, all vikingSteaksensors will match*/
513
                        if (aliases_data[alias_name]["specific"]["Address"] == undefined)
514
                        {
515
                            VikingSteakSuccess = "VikingFound";
516
                            var last_value = {};
517
                            var last_value_string = Storage_GetParameter("LastValues", alias_name);
518
 
519
                            if (last_value_string)
520
                            {
521
                                last_value = eval("(" + last_value_string + ")");
522
                            }
523
 
524
                            var timestamp = get_time();
525
                            last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
526
                            last_value["Address"] = { "value" : addr, "timestamp" : timestamp };
527
                            Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
528
                            break loopAliases;
529
                        }
530
                        else if (addr == aliases_data[alias_name]["specific"]["Address"])
531
                        {
532
                            VikingSteakSuccess = "VikingFound";
533
                            var last_value = {};
534
                            var last_value_string = Storage_GetParameter("LastValues", alias_name);
535
 
536
                            if (last_value_string)
537
                            {
538
                                last_value = eval("(" + last_value_string + ")");
539
                            }
540
 
541
                            var timestamp = get_time();
542
                            last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
543
                            last_value["Address"] = { "value" : addr, "timestamp" : timestamp };
544
                            Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
545
 
546
                            break loopAliases;
547
                        }
548
                        break;
549
                    }
2207 arune 550
                    case "Nexa2":
2211 arune 551
                    {   /* Add alias with these specifics: Channel=0,Proto=Nexa2,OnData=<data for on>,OffData=<data for off> */
2207 arune 552
                        var deviceStateCommand = "";
553
                        if (variables["IRdata"] == aliases_data[alias_name]["specific"]["OnData"] && variables["Status"] == "Pressed")
554
                        {
555
                            deviceStateCommand = "On";
556
                        }
557
                        else if (variables["IRdata"] == aliases_data[alias_name]["specific"]["OffData"] && variables["Status"] == "Pressed")
558
                        {
559
                            deviceStateCommand = "Off";
560
                        }
561
                        else
562
                        {
563
                            break;
564
                        }
565
 
566
                        var last_value = {};
567
                        var last_value_string = Storage_GetParameter("LastValues", alias_name);
568
 
569
                        if (last_value_string)
570
                        {
571
                            last_value = eval("(" + last_value_string + ")");
572
                        }
573
 
574
                        if (( typeof(last_value["State"]) == "undefined" ) ||
575
                            ( deviceStateCommand == "Off" && last_value["State"]["value"] == "On" ) ||
576
                            ( deviceStateCommand == "On" && last_value["State"]["value"] == "Off" ))
577
                        {
578
                            last_value["State"] = { "value" : deviceStateCommand, "timestamp" : get_time() };
579
 
580
                            Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
581
 
582
                            /* Call any potential subscribers */
583
                            if (Sensor_OnDeviceStateChangeFunctions[alias_name])
584
                            {
585
                                for (var n in Sensor_OnDeviceStateChangeFunctions[alias_name])
586
                                {
587
                                    /* Call callback with arguments alias, state */
588
                                    Sensor_OnDeviceStateChangeFunctions[alias_name][n](alias_name, deviceStateCommand);
589
                                }
590
                            }
591
                        }
592
                        break;
593
                    }
2205 arune 594
                }
595
            }
596
        }
597
        if (VikingSuccess == "VikingNotFound")
598
        {
2296 arune 599
            Log("Found unknown vikingsensor address="+addr+" temperature="+temp+" humidity="+humidity+" protocol type="+type);
2205 arune 600
        }
2237 linlun 601
        if (VikingSteakSuccess == "VikingNotFound")
602
        {
603
            Log("Found unknown vikingSteaksensor address="+addr+" temperature="+temp);
604
        }
2240 linlun 605
        if (RubicsonSuccess == "RubicsonNotFound")
606
        {
607
            Log("Found unknown Rubicson sensor address="+addr+" temperature="+temp);
608
        }
2257 linlun 609
        if (OregonTempHumSuccess == "NotFound")
610
        {
611
            Log("Found unknown oregon temp/humidity sensor address="+addr+" temperature="+temp +" humidity="+humidity+" unknown="+unknown + " bat="+bat);
612
        }
613
        if (OregonRainSuccess == "NotFound")
614
        {
615
            Log("Found unknown oregon rain sensor address="+addr+" tot inch="+totInch +" totMM="+totMM+" inch/h="+inchPerHour + " mm/h="+mmPerHour +" bat="+bat);
616
        }
617
        if (OregonWindSuccess == "NotFound")
618
        {
619
            Log("Found unknown oregon wind sensor address="+addr+" Average="+average +" current="+current+" direction="+direction +" bat="+bat);
620
        }
2205 arune 621
    }
622
}
623
Module_RegisterToOnMessage("all", SensorRf_OnMessage);
624
 
2091 runge 625
function Sensor_StoreNumberInPhonebook(number, timestamp)
1899 linlun 626
{
2152 arune 627
  var phonebookNumbers = Storage_GetJsonParamter("PhoneBook", number);
2091 runge 628
 
629
  if (!phonebookNumbers)
630
  {
631
    phonebookNumbers = [];
632
 
633
    url = "wap.eniro.se/query?search_word=" + number + "&what=mobwp";
634
    //Log("\033[31mURL: http://"+url+"\033[0m\n");
635
 
636
    Http_Request(url, function(socket_id, result, header, content_data)
637
    {
638
      //Log("\033[31mOnlinePhonebook-eniro: result was: " + result + "\033[0m\n");
639
      //Log("\033[31mOnlinePhonebook-eniro: content was: " + content_data + "\033[0m\n");
640
 
641
      if (result.indexOf("200 OK") != -1)
642
      {
643
 
644
        var lines = content_data.split("\n");
645
 
646
        var endString = "<anchor>Tillbaka<prev/></anchor>";
647
        var nameStartString = "<td class=\"hTd2\">";
648
        var nameEndString = "</table>";
649
 
650
        for (var n = 1; n < lines.length; n++)
651
        {
652
          var line = lines[n].trim("\n");
653
 
654
          if (line.indexOf(endString) != -1)
655
          {
656
            break;
657
          }
658
 
659
          if (line.indexOf(nameStartString) != -1)
660
          {
661
            line = lines[n + 1].trim("\n");
662
            splitted = line.split("b>");
663
 
664
            phonebookNumbers.push(splitted[1].substr(0, splitted[1].length - 2));
665
            break;
666
          }
667
        }
668
 
669
        //Log("\033[31mOnlinePhonebook: Line: " + lines[1] + "\033[0m\n");
670
        //Log("\033[31mOnlinePhonebook: Line: " + lines[2] + "\033[0m\n");
671
        //Log("\033[31mOnlinePhonebook: Line: " + lines[3] + "\033[0m\n");
672
        if (phonebookNumbers.length > 0)
673
        {
674
          Storage_SetJsonParameter("PhoneBook", number, phonebookNumbers);
675
 
676
          Sensor_UpdateNameInPhonecalls(timestamp, phonebookNumbers);
677
 
678
          Log("\033[31mOnlinePhonebook-eniro: Found: " + JSON.stringify(phonebookNumbers) + "\033[0m\n");
679
        }
680
      }
681
      else
682
      {
683
        Log("\033[31mOnlinePhonebook-eniro: Failed to do name lookup, result was: " + result + "\033[0m\n");
684
      }
685
 
2094 runge 686
      if (phonebookNumbers.length == 0)
2091 runge 687
      {
2094 runge 688
        if (typeof sensorRKSEEK_API !== 'undefined')
2091 runge 689
        {
2094 runge 690
          url = "rkseek.oblivioncreations.se/?client="+sensorRKSEEK_API+"&n=" + number + "&out=text";
691
          Log("\033[31mURL: http://"+url+"\033[0m\n");
2091 runge 692
 
2094 runge 693
          Http_Request(url, function(socket_id, result, header, content_data)
2091 runge 694
          {
2094 runge 695
            phonebookNumbers = [];
696
 
697
            if (result.indexOf("200 OK") != -1)
2091 runge 698
            {
2094 runge 699
              //Log("\033[31mOnlinePhonebook: result was: " + content_data + "\033[0m\n");
700
              var lines = content_data.split("\n");
701
              //Log("\033[31mOnlinePhonebook: Line: " + lines[1] + "\033[0m\n");
702
              //Log("\033[31mOnlinePhonebook: Line: " + lines[2] + "\033[0m\n");
703
              //Log("\033[31mOnlinePhonebook: Line: " + lines[3] + "\033[0m\n");
704
              if (lines[2].length > 0)
705
              {
706
                phonebookNumbers = [ lines[2] ]; // TODO Is this really correct?!
707
                Storage_SetJsonParameter("PhoneBook", number, phonebookNumbers);
708
 
709
                Sensor_UpdateNameInPhonecalls(timestamp, phonebookNumbers);
2152 arune 710
 
2094 runge 711
                Log("\033[31mOnlinePhonebook-rseek: Found: " + JSON.stringify(phonebookNumbers) + "\033[0m\n");
712
              }
2091 runge 713
            }
2094 runge 714
            else
715
            {
716
              Log("\033[31mOnlinePhonebook-rseek: Failed to do name lookup, result was: " + result + "\033[0m\n");
717
            }
718
          });
719
        }
2091 runge 720
      }
2094 runge 721
    });
2014 linlun 722
        /*
2091 runge 723
        Http_Request("wap.hitta.se/default.aspx?Who=" + number + "&Where=&PageAction=White",
1899 linlun 724
            function(socket_id, result, header, content_data) {
725
                var persons = new Array();
726
                if (result.indexOf("200 OK") != -1)
727
                {
728
                    var endString = "<anchor>Tillbaka<prev/></anchor>";
729
                    var nameStartString = " title=\"Link\">";
730
                    var nameEndString = "</a>";
2091 runge 731
 
1899 linlun 732
                    var lines = content_data.split("<br/>");
2091 runge 733
 
1899 linlun 734
                    for (var n = 1; n < lines.length; n++)
735
                    {
736
                        var line = lines[n].trim(" \n");
2091 runge 737
 
1899 linlun 738
                        if (line.indexOf(endString) != -1)
739
                        {
740
                            break;
741
                        }
742
                        var pos = line.indexOf(nameStartString + (persons.length+1) + ".");
2091 runge 743
 
1899 linlun 744
                        if (pos != -1)
745
                        {
746
                            pos += nameStartString.length + 2
747
                            persons[persons.length] = line.substr(pos, line.length-pos-nameEndString.length).html_entity_decode().replace("  ", " ");
748
                        }
749
                    }
750
                    if (persons.length > 0) {
751
                        Storage_SetJsonParameter("PhoneBook", number, persons);
2091 runge 752
 
1899 linlun 753
                        //CAll all listeners!
754
                        //....
2091 runge 755
 
1899 linlun 756
                        Log("\033[31mOnlinePhonebook: Found: " + JSON.stringify(persons) + "\033[0m\n");
757
                    }
758
                }
759
                else
760
                {
761
                    Log("\033[31mOnlinePhonebook: Failed to do name lookup, result was: " + result + "\033[0m\n");
2091 runge 762
                }
1899 linlun 763
            }
764
        );
2014 linlun 765
        */
2091 runge 766
 
767
  }
768
  else
769
  {
770
    //Log("Number already stored\n");
771
 
772
    Sensor_UpdateNameInPhonecalls(timestamp, phonebookNumbers);
773
  }
2152 arune 774
 
775
  return phonebookNumbers;
1899 linlun 776
}
777
Console_RegisterCommand(Sensor_StoreNumberInPhonebook, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args); });
778
 
2182 arune 779
function Sensor_StoreNumberInPhonebookManual(number, name)
780
{
781
    if (arguments.length < 2)
782
    {
783
        Log("\033[31mNot enough parameters given.\033[0m\n");
784
        return false;
785
    }
786
 
787
    var completename = arguments[1];
788
    for (var i=2; i<arguments.length; i++)
789
    {
790
        completename = completename+" "+arguments[i];
791
    }
792
 
793
    phonebookNumbers = [];
794
    phonebookNumbers.push(completename);
795
    Storage_SetJsonParameter("PhoneBook", number, phonebookNumbers);
796
}
797
Console_RegisterCommand(Sensor_StoreNumberInPhonebookManual, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args); });
798
 
2091 runge 799
function Sensor_UpdateNameInPhonecalls(timestamp, numbers)
800
{
801
  if (numbers.length > 0)
802
  {
803
    var lastValueString = Storage_GetParameter("PhoneCalls", timestamp);
1899 linlun 804
 
2091 runge 805
    if (lastValueString)
806
    {
807
      var lastValue = JSON.parse(lastValueString);
1899 linlun 808
 
2091 runge 809
      lastValue.names = numbers;
810
 
811
      Storage_SetParameter("PhoneCalls", timestamp, JSON.stringify(lastValue));
812
    }
813
  }
814
}
815
 
2232 linlun 816
function Sensor_SetUltrasonicMode(alias_name, BottomLimit, TopLimit, mode)
817
{
818
    if (arguments.length < 4)
819
    {
820
        Log("\033[31mNot enough parameters given.\033[0m\n");
821
        return false;
822
    }
823
 
824
    var aliases_data = Module_ResolveAlias(alias_name, Sensor_ModuleNames);
825
    var found = false;
826
 
827
    for (var name in aliases_data)
828
    {
829
        var variables = {   "0Percent"     : BottomLimit,
830
                    "100Percent"   : TopLimit,
831
                    "SensorMode"   : mode,
832
                    "SensorId"  : aliases_data[name]["specific"]["Channel"],
833
        };
834
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "UltrasonicConfig", variables))
835
        {
836
            Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
837
        }
838
        else
839
        {
840
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
841
        }
842
 
843
        found = true;
844
    }
845
 
846
    if (!found)
847
    {
848
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
849
        return false;
850
    }
851
 
852
    return true;
853
}
854
Console_RegisterCommand(Sensor_SetUltrasonicMode, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Sensor_Aliases()); });