Subversion Repositories HomeAutomation

Rev

Rev 2298 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 2298 Rev 2300
Line 249... Line 249...
249
        var OregonRainSuccess = "NoSensor";
249
        var OregonRainSuccess = "NoSensor";
250
        var OregonTempHumSuccess = "NoSensor";
250
        var OregonTempHumSuccess = "NoSensor";
251
        var OregonWindSuccess = "NoSensor";
251
        var OregonWindSuccess = "NoSensor";
252
        var VikingSuccess = "NoVikingSensor";
252
        var VikingSuccess = "NoVikingSensor";
253
        var VikingSteakSuccess = "NoVikingSteakSensor";
253
        var VikingSteakSuccess = "NoVikingSteakSensor";
-
 
254
        var RubicsonStationSuccessPkt1 = "NoSensor";
254
        //Log("found sensor: "+variables["Protocol"]);
255
        var RubicsonStationSuccessPkt2 = "NoSensor";
255
        if (variables["Protocol"] == "OregonRain")
256
        if (variables["Protocol"] == "OregonRain")
256
        {
257
        {
257
            /* ----------- Data order proto-data ------
258
            /* ----------- Data order proto-data ------
258
            * cc bb bb bb aa aa
259
            * cc bb bb bb aa aa
259
            * a   = Rain in 0.01 inches per hour
260
            * a   = Rain in 0.01 inches per hour
Line 395... Line 396...
395
            var byte0 = rshift(data,32)&0xF;
396
            var byte0 = rshift(data,32)&0xF;
396
            var byte1 = rshift(data,28)&0xF;
397
            var byte1 = rshift(data,28)&0xF;
397
            var byte2 = rshift(data,24)&0xF;
398
            var byte2 = rshift(data,24)&0xF;
398
            var temp = Math.round((((((byte1^byte2)<<8)+((byte0^byte1)<<4)+byte0^10)-122)*5)/9);
399
            var temp = Math.round((((((byte1^byte2)<<8)+((byte0^byte1)<<4)+byte0^10)-122)*5)/9);
399
            VikingSteakSuccess = "VikingNotFound";
400
            VikingSteakSuccess = "VikingNotFound";
-
 
401
        } else if (variables["Protocol"] == "RubicsonStn")
-
 
402
        {
-
 
403
            var data = variables["IRdata"];
-
 
404
            var pkt = rshift(data,39)&0x1;
-
 
405
            if (pkt == 0)  //This is the first part
-
 
406
            {      
-
 
407
                /*
-
 
408
                      aaaaaaaa 0dddsttt tttttttt hhhhhhhh wwwwwwww wwwwwwww
-
 
409
                    0b11111001 11111001 00001100 10110010 11100011 01101111
-
 
410
                a = address = winddirection, s = sign, t = temperature, h = humidity, w = vindspeed
-
 
411
                */
-
 
412
                var windspeed_avg = (rshift(data,8) & 0xFF)*0.8;
-
 
413
                var windspeed_max = (data & 0xFF)*0.8;
-
 
414
                //var crc = data&0xFF;
-
 
415
                var humidity = rshift(data,16)&0xFF;
-
 
416
                var temp = rshift(data,24)&0xFFF;
-
 
417
                var winddirection = (rshift(data,36)&0x7)*360/8;
-
 
418
               
-
 
419
                var addr = rshift(data,40)&0xFF;
-
 
420
                //var calccrc=crc8(rshift(data,8), 32);
-
 
421
                       
-
 
422
                var sign = (temp>>>11)&1;
-
 
423
                if (sign > 0)
-
 
424
                {
-
 
425
                    temp = temp^0xFFF;
-
 
426
                    temp += 1;
-
 
427
                    temp = -temp;
-
 
428
                }
-
 
429
                temp = temp/10;
-
 
430
 
-
 
431
                RubicsonStationSuccessPkt1 = "NotFound";
-
 
432
               
-
 
433
               
-
 
434
            } else //This is the second part
-
 
435
            {
-
 
436
                /*
-
 
437
                      aaaaaaaa 1------- -------b rrrrrrrr rrrrrrrr rrrrrrrr
-
 
438
                    0b11111001 11111001 00001100 10110010 11100011 01101111
-
 
439
                a = address b = battery empty, r = rain
-
 
440
                */
-
 
441
                var battery = rshift(data,24) & 0x1;
-
 
442
                //var crc = data&0xFF;
-
 
443
                var rain = 0.4*(data&0xFFFFFF);
-
 
444
                var addr = rshift(data,40)&0xFF;
-
 
445
 
-
 
446
                RubicsonStationSuccessPkt2 = "NotFound";
-
 
447
            }
-
 
448
           
400
        }
449
        }
401
               
450
               
402
        loopAliases:    /* Label to break nested */
451
        loopAliases:    /* Label to break nested */
403
        for (var alias_name in aliases_data)
452
        for (var alias_name in aliases_data)
404
        {
453
        {
405
            if (aliases_data[alias_name]["specific"]["Channel"] == variables["Channel"] &&
454
            if (aliases_data[alias_name]["specific"]["Channel"] == variables["Channel"] &&
406
                aliases_data[alias_name]["specific"]["Proto"] == variables["Protocol"])
455
                aliases_data[alias_name]["specific"]["Proto"] == variables["Protocol"])
407
            {
456
            {
408
                switch (variables["Protocol"])
457
                switch (variables["Protocol"])
409
                {
458
                {
410
                    case "Viking":
459
                    case "RubicsonStn":
411
                    {   /* Add alias with these specifics: Channel=0,Proto=Viking,Address=<address of sensor> */
460
                    {   /* Add alias with these specifics: Channel=0,Proto=RubicsonStn,Address=<address of sensor> */
-
 
461
                        /* if no Address is added to the alias, all RubicsonStn will match*/
-
 
462
                        if (aliases_data[alias_name]["specific"]["Address"] == undefined)
-
 
463
                        {
412
                        if (type == 4)
464
                            if (pkt == 0)
413
                        {
465
                            {
-
 
466
                                var last_value = {};
-
 
467
                                var last_value_string = Storage_GetParameter("LastValues", alias_name);
-
 
468
 
414
                            if (crc != calccrc)
469
                                if (last_value_string)
-
 
470
                                {
-
 
471
                                    last_value = eval("(" + last_value_string + ")");
-
 
472
                                }
-
 
473
                                var timestamp = get_time();
-
 
474
                                last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
-
 
475
                                last_value["Humidity_Percent"] = { "value" : humidity.toString(), "timestamp" : timestamp };
-
 
476
                                last_value["Wind_Speed_Avg"] = { "value" : windspeed_avg.toString(), "timestamp" : timestamp };
-
 
477
                                last_value["Wind_Speed_Max"] = { "value" : windspeed_max.toString(), "timestamp" : timestamp };
-
 
478
                                last_value["Wind_Direction"] = { "value" : winddirection.toString(), "timestamp" : timestamp };
-
 
479
                                last_value["Address"] = { "value" : addr, "timestamp" : timestamp };
-
 
480
                                Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
-
 
481
                                RubicsonStationSuccessPkt1 = "RubicsonStnFound";
-
 
482
                                break loopAliases;
-
 
483
                            } else
415
                            {
484
                            {
-
 
485
                              var last_value = {};
416
                                Log("\033[31mError: VikingSensor, incorrect CRC, data="+data+", CRC="+crc+", calcCRC="+calccrc+"\033[0m");
486
                                var last_value_string = Storage_GetParameter("LastValues", alias_name);
-
 
487
 
-
 
488
                                if (last_value_string)
-
 
489
                                {
-
 
490
                                    last_value = eval("(" + last_value_string + ")");
-
 
491
                                }
-
 
492
                                var timestamp = get_time();
-
 
493
                                last_value["WaterAbsolute_mm"] = { "value" : rain.toString(), "timestamp" : timestamp };
-
 
494
                                last_value["Battery_Low"] = { "value" : battery.toString(), "timestamp" : timestamp };
-
 
495
                                last_value["Address"] = { "value" : addr, "timestamp" : timestamp };
-
 
496
                                Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
-
 
497
                                RubicsonStationSuccessPkt2 = "RubicsonStnFound";
417
                                break loopAliases;
498
                                break loopAliases;
-
 
499
                            }
-
 
500
                        }
-
 
501
                        else if (addr == aliases_data[alias_name]["specific"]["Address"])
-
 
502
                        {
-
 
503
                            if (pkt == 0)
-
 
504
                            {
-
 
505
                                var last_value = {};
-
 
506
                                var last_value_string = Storage_GetParameter("LastValues", alias_name);
-
 
507
 
-
 
508
                                if (last_value_string)
-
 
509
                                {
-
 
510
                                    last_value = eval("(" + last_value_string + ")");
418
                            }
511
                                }
-
 
512
                                var timestamp = get_time();
-
 
513
                                last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
-
 
514
                                last_value["Humidity_Percent"] = { "value" : humidity.toString(), "timestamp" : timestamp };
-
 
515
                                last_value["Wind_Speed"] = { "value" : windspeed.toString(), "timestamp" : timestamp };
-
 
516
                                last_value["Wind_Direction"] = { "value" : winddirection.toString(), "timestamp" : timestamp };
-
 
517
                                last_value["Address"] = { "value" : addr, "timestamp" : timestamp };
419
                            if (addr == aliases_data[alias_name]["specific"]["Address"])
518
                                Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
-
 
519
                                RubicsonStationSuccessPkt1 = "RubicsonStnFound";
-
 
520
                                break loopAliases;
-
 
521
                            } else
420
                            {
522
                            {
-
 
523
                              var last_value = {};
-
 
524
                                var last_value_string = Storage_GetParameter("LastValues", alias_name);
-
 
525
 
-
 
526
                                if (last_value_string)
-
 
527
                                {
-
 
528
                                    last_value = eval("(" + last_value_string + ")");
-
 
529
                                }
-
 
530
                                var timestamp = get_time();
-
 
531
                                last_value["WaterAbsolute_mm"] = { "value" : (rain*0.4).toString(), "timestamp" : timestamp };
-
 
532
                                last_value["Battery_Low"] = { "value" : battery.toString(), "timestamp" : timestamp };
-
 
533
                                last_value["Address"] = { "value" : addr, "timestamp" : timestamp };
-
 
534
                                Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
-
 
535
                                RubicsonStationSuccessPkt2 = "RubicsonStnFound";
-
 
536
                                break loopAliases;
-
 
537
                            }
-
 
538
                        }
-
 
539
                        break;
-
 
540
                    }
-
 
541
                    case "Viking":
-
 
542
                    {   /* Add alias with these specifics: Channel=0,Proto=Viking,Address=<address of sensor> */
-
 
543
                        if (type == 4)
-
 
544
                        {
-
 
545
                            if (crc != calccrc)
-
 
546
                            {
-
 
547
                                Log("\033[31mError: VikingSensor, incorrect CRC, data="+data+", CRC="+crc+", calcCRC="+calccrc+"\033[0m");
-
 
548
                                break loopAliases;
-
 
549
                            }
-
 
550
                            if (addr == aliases_data[alias_name]["specific"]["Address"])
-
 
551
                            {
421
                                VikingSuccess = "VikingFound";
552
                                VikingSuccess = "VikingFound";
422
 
553
 
423
                                var last_value = {};
554
                                var last_value = {};
424
                                var last_value_string = Storage_GetParameter("LastValues", alias_name);
555
                                var last_value_string = Storage_GetParameter("LastValues", alias_name);
425
 
556
 
426
                                if (last_value_string)
557
                                if (last_value_string)
427
                                {
558
                                {
Line 429... Line 560...
429
                                }
560
                                }
430
 
561
 
431
                                var timestamp = get_time();
562
                                var timestamp = get_time();
432
                                last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
563
                                last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
433
                                if (humidity<101)
564
                                if (humidity<101)
434
                                {
565
                                {
435
                                    last_value["Humidity_Percent"] = { "value" : humidity.toString(), "timestamp" : timestamp };
566
                                    last_value["Humidity_Percent"] = { "value" : humidity.toString(), "timestamp" : timestamp };
436
                                }
567
                                }
437
 
568
 
438
                                Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
569
                                Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
439
                               
570
                               
440
                                break loopAliases;
571
                                break loopAliases;
441
                            }
572
                            }
442
                        }
573
                        }
443
                        else if (type == 3)
574
                        else if (type == 3)
444
                        {
575
                        {
445
                            /* Don't check crc since all bits did not fit in CAN-frame */
576
                            /* Don't check crc since all bits did not fit in CAN-frame */
446
                            if (addr == aliases_data[alias_name]["specific"]["Address"])
577
                            if (addr == aliases_data[alias_name]["specific"]["Address"])
447
                            {
578
                            {
448
                                VikingSuccess = "VikingFound";
579
                                VikingSuccess = "VikingFound";
449
 
580
 
450
                                var last_value = {};
581
                                var last_value = {};
451
                                var last_value_string = Storage_GetParameter("LastValues", alias_name);
582
                                var last_value_string = Storage_GetParameter("LastValues", alias_name);
452
 
583
 
453
                                if (last_value_string)
584
                                if (last_value_string)
454
                                {
585
                                {
455
                                    last_value = eval("(" + last_value_string + ")");
586
                                    last_value = eval("(" + last_value_string + ")");
456
                                }
587
                                }
457
 
588
 
458
                                var timestamp = get_time();
589
                                var timestamp = get_time();
459
                                temp = temp-40.0;
590
                                temp = temp-40.0;
Line 499... Line 630...
499
                            /*if (humidity<101)
630
                            /*if (humidity<101)
500
                            {
631
                            {
501
                                last_value["Humidity_Percent"] = { "value" : humidity.toString(), "timestamp" : timestamp };
632
                                last_value["Humidity_Percent"] = { "value" : humidity.toString(), "timestamp" : timestamp };
502
                            }
633
                            }
503
                            */
634
                            */
504
                            Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
635
                            Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
505
                           
636
                           
506
                            break loopAliases;
637
                            break loopAliases;
507
                        }
638
                        }
508
                        break;
639
                        break;
509
                    }
640
                    }
510
                    case "VikingSteak":
641
                    case "VikingSteak":
511
                    {   /* Add alias with these specifics: Channel=0,Proto=VikingSteak,Address=<address of sensor> */
642
                    {   /* 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*/
643
                        /* if no Address is added to the alias, all vikingSteaksensors will match*/
513
                        if (aliases_data[alias_name]["specific"]["Address"] == undefined)
644
                        if (aliases_data[alias_name]["specific"]["Address"] == undefined)
514
                        {
645
                        {
515
                            VikingSteakSuccess = "VikingFound";
646
                            VikingSteakSuccess = "VikingFound";
516
                            var last_value = {};
647
                            var last_value = {};
517
                            var last_value_string = Storage_GetParameter("LastValues", alias_name);
648
                            var last_value_string = Storage_GetParameter("LastValues", alias_name);
518
 
649
 
519
                            if (last_value_string)
650
                            if (last_value_string)
520
                            {
651
                            {
521
                                last_value = eval("(" + last_value_string + ")");
652
                                last_value = eval("(" + last_value_string + ")");
522
                            }
653
                            }
523
 
654
 
524
                            var timestamp = get_time();
655
                            var timestamp = get_time();
525
                            last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
656
                            last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
Line 528... Line 659...
528
                            break loopAliases;
659
                            break loopAliases;
529
                        }
660
                        }
530
                        else if (addr == aliases_data[alias_name]["specific"]["Address"])
661
                        else if (addr == aliases_data[alias_name]["specific"]["Address"])
531
                        {
662
                        {
532
                            VikingSteakSuccess = "VikingFound";
663
                            VikingSteakSuccess = "VikingFound";
533
                            var last_value = {};
664
                            var last_value = {};
534
                            var last_value_string = Storage_GetParameter("LastValues", alias_name);
665
                            var last_value_string = Storage_GetParameter("LastValues", alias_name);
535
 
666
 
536
                            if (last_value_string)
667
                            if (last_value_string)
537
                            {
668
                            {
538
                                last_value = eval("(" + last_value_string + ")");
669
                                last_value = eval("(" + last_value_string + ")");
539
                            }
670
                            }
540
 
671
 
541
                            var timestamp = get_time();
672
                            var timestamp = get_time();
542
                            last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
673
                            last_value["Temperature_Celsius"] = { "value" : temp.toString(), "timestamp" : timestamp };
Line 549... Line 680...
549
                    }
680
                    }
550
                    case "Nexa2":
681
                    case "Nexa2":
551
                    {   /* Add alias with these specifics: Channel=0,Proto=Nexa2,OnData=<data for on>,OffData=<data for off> */
682
                    {   /* Add alias with these specifics: Channel=0,Proto=Nexa2,OnData=<data for on>,OffData=<data for off> */
552
                        var deviceStateCommand = "";
683
                        var deviceStateCommand = "";
553
                        if (variables["IRdata"] == aliases_data[alias_name]["specific"]["OnData"] && variables["Status"] == "Pressed")
684
                        if (variables["IRdata"] == aliases_data[alias_name]["specific"]["OnData"] && variables["Status"] == "Pressed")
554
                        {
685
                        {
555
                            deviceStateCommand = "On";
686
                            deviceStateCommand = "On";
556
                        }
687
                        }
557
                        else if (variables["IRdata"] == aliases_data[alias_name]["specific"]["OffData"] && variables["Status"] == "Pressed")
688
                        else if (variables["IRdata"] == aliases_data[alias_name]["specific"]["OffData"] && variables["Status"] == "Pressed")
558
                        {
689
                        {
559
                            deviceStateCommand = "Off";
690
                            deviceStateCommand = "Off";
560
                        }
691
                        }
561
                        else
692
                        else
562
                        {
693
                        {
563
                            break;
694
                            break;
564
                        }
695
                        }
565
                       
696
                       
566
                        var last_value = {};
697
                        var last_value = {};
567
                        var last_value_string = Storage_GetParameter("LastValues", alias_name);
698
                        var last_value_string = Storage_GetParameter("LastValues", alias_name);
568
 
699
 
569
                        if (last_value_string)
700
                        if (last_value_string)
570
                        {
701
                        {
571
                            last_value = eval("(" + last_value_string + ")");
702
                            last_value = eval("(" + last_value_string + ")");
572
                        }
703
                        }
573
                       
704
                       
574
                        if (( typeof(last_value["State"]) == "undefined" ) ||
705
                        if (( typeof(last_value["State"]) == "undefined" ) ||
575
                            ( deviceStateCommand == "Off" && last_value["State"]["value"] == "On" ) ||
706
                            ( deviceStateCommand == "Off" && last_value["State"]["value"] == "On" ) ||
576
                            ( deviceStateCommand == "On" && last_value["State"]["value"] == "Off" ))
707
                            ( deviceStateCommand == "On" && last_value["State"]["value"] == "Off" ))
577
                        {
708
                        {
Line 587... Line 718...
587
                                    /* Call callback with arguments alias, state */
718
                                    /* Call callback with arguments alias, state */
588
                                    Sensor_OnDeviceStateChangeFunctions[alias_name][n](alias_name, deviceStateCommand);
719
                                    Sensor_OnDeviceStateChangeFunctions[alias_name][n](alias_name, deviceStateCommand);
589
                                }
720
                                }
590
                            }
721
                            }
591
                        }
722
                        }
592
                        break;
723
                        break;
593
                    }
724
                    }
594
                }
725
                }
595
            }
726
            }
596
        }
727
        }
597
        if (VikingSuccess == "VikingNotFound")
728
        if (VikingSuccess == "VikingNotFound")
598
        {
729
        {
599
            Log("Found unknown vikingsensor address="+addr+" temperature="+temp+" humidity="+humidity+" protocol type="+type);
730
            Log("Found unknown vikingsensor address="+addr+" temperature="+temp+" humidity="+humidity+" protocol type="+type);
600
        }
731
        }
601
        if (VikingSteakSuccess == "VikingNotFound")
732
        if (VikingSteakSuccess == "VikingNotFound")
602
        {
733
        {
603
            Log("Found unknown vikingSteaksensor address="+addr+" temperature="+temp);
734
            Log("Found unknown vikingSteaksensor address="+addr+" temperature="+temp);
604
        }
735
        }
605
        if (RubicsonSuccess == "RubicsonNotFound")
736
        if (RubicsonSuccess == "RubicsonNotFound")
606
        {
737
        {
607
            Log("Found unknown Rubicson sensor address="+addr+" temperature="+temp);
738
            Log("Found unknown Rubicson sensor address="+addr+" temperature="+temp);
608
        }
739
        }
609
        if (OregonTempHumSuccess == "NotFound")
740
        if (OregonTempHumSuccess == "NotFound")
610
        {
741
        {
611
            Log("Found unknown oregon temp/humidity sensor address="+addr+" temperature="+temp +" humidity="+humidity+" unknown="+unknown + " bat="+bat);
742
            Log("Found unknown oregon temp/humidity sensor address="+addr+" temperature="+temp +" humidity="+humidity+" unknown="+unknown + " bat="+bat);
612
        }
743
        }
613
        if (OregonRainSuccess == "NotFound")
744
        if (OregonRainSuccess == "NotFound")
614
        {
745
        {
615
            Log("Found unknown oregon rain sensor address="+addr+" tot inch="+totInch +" totMM="+totMM+" inch/h="+inchPerHour + " mm/h="+mmPerHour +" bat="+bat);
746
            Log("Found unknown oregon rain sensor address="+addr+" tot inch="+totInch +" totMM="+totMM+" inch/h="+inchPerHour + " mm/h="+mmPerHour +" bat="+bat);
616
        }
747
        }
617
        if (OregonWindSuccess == "NotFound")
748
        if (OregonWindSuccess == "NotFound")
618
        {
749
        {
619
            Log("Found unknown oregon wind sensor address="+addr+" Average="+average +" current="+current+" direction="+direction +" bat="+bat);
750
            Log("Found unknown oregon wind sensor address="+addr+" Average="+average +" current="+current+" direction="+direction +" bat="+bat);
-
 
751
        }
-
 
752
        if (RubicsonStationSuccessPkt1 == "NotFound")
-
 
753
        {
-
 
754
            Log("Found unknown rubicson weather station, Packet 1 address="+addr + " temperature="+temp + " humidity="+humidity+" WindSpeed="+windspeed + " WindDirection="+winddirection);
-
 
755
        }
-
 
756
        if (RubicsonStationSuccessPkt2 == "NotFound")
-
 
757
        {
-
 
758
            Log("Found unknown rubicson weather station, Packet 2 address="+addr + " Rain="+rain + "mm BatteryLow="+battery);
620
        }
759
        }
621
    }
760
    }
622
}
761
}
623
Module_RegisterToOnMessage("all", SensorRf_OnMessage);
762
Module_RegisterToOnMessage("all", SensorRf_OnMessage);
624
 
763