thermferm/devices.c

changeset 553
4091d4fe217f
parent 528
908ef02d5d22
child 570
1e0192b295b9
equal deleted inserted replaced
552:8b56f1b4e7ec 553:4091d4fe217f
1 /***************************************************************************** 1 /*****************************************************************************
2 * Copyright (C) 2014..2017 2 * Copyright (C) 2014..2018
3 * 3 *
4 * Michiel Broek <mbroek at mbse dot eu> 4 * Michiel Broek <mbroek at mbse dot eu>
5 * 5 *
6 * This file is part of the mbsePi-apps 6 * This file is part of the mbsePi-apps
7 * 7 *
33 33
34 #ifdef USE_SIMULATOR 34 #ifdef USE_SIMULATOR
35 35
36 extern int SIMcooling; 36 extern int SIMcooling;
37 extern int SIMheating; 37 extern int SIMheating;
38 extern int SIMfan;
39 extern int SIMlight;
38 40
39 #endif 41 #endif
40 42
41 #ifdef HAVE_WIRINGPI_H 43 #ifdef HAVE_WIRINGPI_H
42 44
357 } 359 }
358 } 360 }
359 361
360 #ifdef USE_SIMULATOR 362 #ifdef USE_SIMULATOR
361 if ((device->type == DEVTYPE_SIM) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) { 363 if ((device->type == DEVTYPE_SIM) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) {
362 if ((strcmp((char *)"SimCooler", device->address) == 0) || (strcmp((char *)"SimHeater", device->address) == 0)) { 364 if ((strcmp((char *)"SimCooler", device->address) == 0) || (strcmp((char *)"SimHeater", device->address) == 0) ||
365 (strcmp((char *)"SimFan" , device->address) == 0) || (strcmp((char *)"SimLight" , device->address) == 0)) {
363 if (value != device->value) { 366 if (value != device->value) {
364 syslog(LOG_NOTICE, "SIM %s value=%d", device->address, value); 367 syslog(LOG_NOTICE, "SIM %s value=%d", device->address, value);
365 if (debug) 368 if (debug)
366 fprintf(stdout, "SIM %s value=%d\n", device->address, value); 369 fprintf(stdout, "SIM %s value=%d\n", device->address, value);
367 } 370 }
369 device->timestamp = time(NULL); 372 device->timestamp = time(NULL);
370 if (strcmp((char *)"SimCooler", device->address) == 0) 373 if (strcmp((char *)"SimCooler", device->address) == 0)
371 SIMcooling = value; 374 SIMcooling = value;
372 if (strcmp((char *)"SimHeater", device->address) == 0) 375 if (strcmp((char *)"SimHeater", device->address) == 0)
373 SIMheating = value; 376 SIMheating = value;
377 if (strcmp((char *)"SimFan", device->address) == 0)
378 SIMfan = value;
379 if (strcmp((char *)"SimLight", device->address) == 0)
380 SIMlight = value;
374 } 381 }
375 } 382 }
376 #endif 383 #endif
377 } else { 384 } else {
378 #ifdef HAVE_WIRINGPI_H 385 #ifdef HAVE_WIRINGPI_H
619 } 626 }
620 } 627 }
621 #endif 628 #endif
622 629
623 #ifdef USE_SIMULATOR 630 #ifdef USE_SIMULATOR
624 found = FALSE; 631 found = 0;
625 for (device = Config.devices; device; device = device->next) { 632 for (device = Config.devices; device; device = device->next) {
626 if (device->type == DEVTYPE_SIM) { 633 if (device->type == DEVTYPE_SIM) {
627 found = TRUE; 634 found++;
628 break; 635 }
629 } 636 }
630 } 637
631 638 /*
632 if (found == FALSE) { 639 * Create simulated devices, or upgrade with new devices.
633 subdevices = 5; 640 */
634 for (i = 0; i < subdevices; i++) { 641 subdevices = 9;
635 ndev = (devices_list *)malloc(sizeof(devices_list)); 642 for (i = found; i < subdevices; i++) {
636 ndev->next = NULL; 643 ndev = (devices_list *)malloc(sizeof(devices_list));
637 ndev->version = 1; 644 ndev->next = NULL;
638 ndev->uuid = malloc(37); 645 ndev->version = 1;
639 uuid_generate(uu); 646 ndev->uuid = malloc(37);
640 uuid_unparse(uu, ndev->uuid); 647 uuid_generate(uu);
641 ndev->type = DEVTYPE_SIM; 648 uuid_unparse(uu, ndev->uuid);
642 ndev->value = ndev->offset = 0; 649 ndev->type = DEVTYPE_SIM;
643 ndev->present = DEVPRESENT_YES; 650 ndev->value = ndev->offset = 0;
644 ndev->subdevice = i; 651 ndev->present = DEVPRESENT_YES;
645 ndev->gpiopin = -1; 652 ndev->subdevice = i;
646 ndev->comment = xstrcpy((char *)"Auto detected device"); 653 ndev->gpiopin = -1;
647 ndev->timestamp = time(NULL); 654 ndev->comment = xstrcpy((char *)"Auto detected device");
648 ndev->inuse = 0; 655 ndev->timestamp = time(NULL);
649 switch (i) { 656 ndev->inuse = 0;
650 case 0: ndev->direction = DEVDIR_IN_ANALOG; 657 switch (i) {
658 case 0: ndev->direction = DEVDIR_IN_ANALOG;
651 ndev->address = xstrcpy((char *)"SimRoomTemp"); 659 ndev->address = xstrcpy((char *)"SimRoomTemp");
652 ndev->description = xstrcpy((char *)"Simulated room temperature"); 660 ndev->description = xstrcpy((char *)"Simulated room temperature");
653 break; 661 break;
654 case 1: ndev->direction = DEVDIR_IN_ANALOG; 662 case 1: ndev->direction = DEVDIR_IN_ANALOG;
655 ndev->address = xstrcpy((char *)"SimAirTemp"); 663 ndev->address = xstrcpy((char *)"SimAirTemp");
656 ndev->description = xstrcpy((char *)"Simulated air temperature"); 664 ndev->description = xstrcpy((char *)"Simulated air temperature");
657 break; 665 break;
658 case 2: ndev->direction = DEVDIR_IN_ANALOG; 666 case 2: ndev->direction = DEVDIR_IN_ANALOG;
659 ndev->address = xstrcpy((char *)"SimBeerTemp"); 667 ndev->address = xstrcpy((char *)"SimBeerTemp");
660 ndev->description = xstrcpy((char *)"Simulated beer temperature"); 668 ndev->description = xstrcpy((char *)"Simulated beer temperature");
661 break; 669 break;
662 case 3: ndev->direction = DEVDIR_OUT_BIN; 670 case 3: ndev->direction = DEVDIR_OUT_BIN;
663 ndev->address = xstrcpy((char *)"SimHeater"); 671 ndev->address = xstrcpy((char *)"SimHeater");
664 ndev->description = xstrcpy((char *)"Simulated heater"); 672 ndev->description = xstrcpy((char *)"Simulated heater");
665 break; 673 break;
666 case 4: ndev->direction = DEVDIR_OUT_BIN; 674 case 4: ndev->direction = DEVDIR_OUT_BIN;
667 ndev->address = xstrcpy((char *)"SimCooler"); 675 ndev->address = xstrcpy((char *)"SimCooler");
668 ndev->description = xstrcpy((char *)"Simulated cooler"); 676 ndev->description = xstrcpy((char *)"Simulated cooler");
669 break; 677 break;
670 } 678 case 5: ndev->direction = DEVDIR_IN_ANALOG;
671 679 ndev->address = xstrcpy((char *)"SimRoomHum");
672 if (Config.devices == NULL) { 680 ndev->description = xstrcpy((char *)"Simulated room humidity");
673 Config.devices = ndev; 681 break;
674 } else { 682 case 6: ndev->direction = DEVDIR_IN_ANALOG;
675 for (device = Config.devices; device; device = device->next) { 683 ndev->address = xstrcpy((char *)"SimChillerTemp");
676 if (device->next == NULL) { 684 ndev->description = xstrcpy((char *)"Simulated Chiller temperature");
677 device->next = ndev; 685 break;
678 break; 686 case 7: ndev->direction = DEVDIR_OUT_BIN;
679 } 687 ndev->address = xstrcpy((char *)"SimFan");
688 ndev->description = xstrcpy((char *)"Simulated fan");
689 break;
690 case 8: ndev->direction = DEVDIR_OUT_BIN;
691 ndev->address = xstrcpy((char *)"SimLight");
692 ndev->description = xstrcpy((char *)"Simulated light");
693 break;
694 }
695
696 if (Config.devices == NULL) {
697 Config.devices = ndev;
698 } else {
699 for (device = Config.devices; device; device = device->next) {
700 if (device->next == NULL) {
701 device->next = ndev;
702 break;
680 } 703 }
681 } 704 }
682 rc++; 705 }
683 } 706 rc++;
684 } 707 }
685 #endif 708 #endif
686 709
687 return rc; 710 return rc;
688 } 711 }
742 addr = xstrcat(addr, (char *)"/w1_slave"); 765 addr = xstrcat(addr, (char *)"/w1_slave");
743 if ((fp = fopen(addr, "r"))) { 766 if ((fp = fopen(addr, "r"))) {
744 fgets(line1, 50, fp); // Read 2 lines 767 fgets(line1, 50, fp); // Read 2 lines
745 fgets(line2, 50, fp); 768 fgets(line2, 50, fp);
746 fclose(fp); 769 fclose(fp);
747 // if (device->present != DEVPRESENT_YES) {
748 // syslog(LOG_NOTICE, "sensor %s is back", device->address);
749 //#ifdef HAVE_WIRINGPI_H
750 // piLock(LOCK_DEVICES);
751 //#endif
752 // device->present = DEVPRESENT_YES;
753 //#ifdef HAVE_WIRINGPI_H
754 // piUnlock(LOCK_DEVICES);
755 //#endif
756 // }
757 /* 770 /*
758 * The output looks like: 771 * The output looks like:
759 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES 772 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
760 * 72 01 4b 46 7f ff 0e 10 57 t=23125 773 * 72 01 4b 46 7f ff 0e 10 57 t=23125
761 */ 774 */
947 device->value = (int)((int)(simulator->air_temperature * 1000) / 62.5) * 62.5; 960 device->value = (int)((int)(simulator->air_temperature * 1000) / 62.5) * 62.5;
948 device->timestamp = time(NULL); 961 device->timestamp = time(NULL);
949 } else if (device->subdevice == 2) { 962 } else if (device->subdevice == 2) {
950 device->value = (int)((int)(simulator->beer_temperature * 1000) / 62.5) * 62.5; 963 device->value = (int)((int)(simulator->beer_temperature * 1000) / 62.5) * 62.5;
951 device->timestamp = time(NULL); 964 device->timestamp = time(NULL);
965 } else if (device->subdevice == 5) {
966 device->value = (int)((int)(simulator->room_humidity * 1000) / 500) * 500;
967 device->timestamp = time(NULL);
968 } else if (device->subdevice == 6) {
969 device->value = (int)((int)(simulator->chiller_temperature * 1000) / 62.5) * 62.5;
970 device->timestamp = time(NULL);
952 } 971 }
953 } 972 }
954 #ifdef HAVE_WIRINGPI_H 973 #ifdef HAVE_WIRINGPI_H
955 piUnlock(LOCK_DEVICES); 974 piUnlock(LOCK_DEVICES);
956 #endif 975 #endif

mercurial