thermferm/devices.c

changeset 660
a28ef4d9afa4
parent 658
b10d0f6337a3
child 661
8c1e7a52e24f
equal deleted inserted replaced
659:bfab45f4d5cd 660:a28ef4d9afa4
26 #include "rc-switch.h" 26 #include "rc-switch.h"
27 #include "panel.h" 27 #include "panel.h"
28 #include "xutil.h" 28 #include "xutil.h"
29 29
30 30
31 int my_devices_shutdown = 0;
31 int my_devices_state = 0; 32 int my_devices_state = 0;
32 33
33 extern sys_config Config; 34 extern sys_config Config;
34 extern int my_shutdown;
35 extern pthread_mutex_t mutexes[5]; 35 extern pthread_mutex_t mutexes[5];
36 extern w1_list *w1_devices; 36 extern w1_list *w1_devices;
37 37
38 38
39 #ifdef USE_SIMULATOR 39 #ifdef USE_SIMULATOR
312 output = 0xff; 312 output = 0xff;
313 } 313 }
314 314
315 if ((write_w1(device->address, (char *)"output", output)) == 0) { 315 if ((write_w1(device->address, (char *)"output", output)) == 0) {
316 syslog(LOG_NOTICE, "DS2413 PIO%c value=%d (%s)", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1, device->comment); 316 syslog(LOG_NOTICE, "DS2413 PIO%c value=%d (%s)", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1, device->comment);
317 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
317 device->value = (value == 0) ? 0 : 1; 318 device->value = (value == 0) ? 0 : 1;
318 device->timestamp = time(NULL); 319 device->timestamp = time(NULL);
320 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
319 } 321 }
320 } 322 }
321 } 323 }
322 } 324 }
323 325
417 break; 419 break;
418 } 420 }
419 } 421 }
420 422
421 if (found == FALSE) { 423 if (found == FALSE) {
422 for (i = 0; i < dev_w1->subdevices; i++) { 424 subdevices = (strcmp(dev_w1->family, (char *)"3a") == 0) ? 2:1;
425 for (i = 0; i < subdevices; i++) {
423 ndev = (devices_list *)malloc(sizeof(devices_list)); 426 ndev = (devices_list *)malloc(sizeof(devices_list));
424 ndev->next = NULL; 427 ndev->next = NULL;
425 ndev->uuid = malloc(37); 428 ndev->uuid = malloc(37);
426 uuid_generate(uu); 429 uuid_generate(uu);
427 uuid_unparse(uu, ndev->uuid); 430 uuid_unparse(uu, ndev->uuid);
712 715
713 716
714 void *my_devices_loop(void *threadid) 717 void *my_devices_loop(void *threadid)
715 { 718 {
716 devices_list *device; 719 devices_list *device;
720 w1_list *dev_w1;
717 #ifdef USE_SIMULATOR 721 #ifdef USE_SIMULATOR
718 simulator_list *simulator; 722 simulator_list *simulator;
719 #endif 723 #endif
720 char *addr = NULL, line1[60], line2[60], *p = NULL; 724 // char *addr = NULL, line1[60], line2[60], *p = NULL;
721 FILE *fp; 725 // FILE *fp;
722 int temp, rc; 726 int rc, found;
723 time_t now; 727 time_t now;
724 728
725 my_devices_state = 1; 729 my_devices_state = 1;
726 syslog(LOG_NOTICE, "Thread my_devices_loop started"); 730 syslog(LOG_NOTICE, "Thread my_devices_loop started");
727 731
733 dht11_next = time(NULL); 737 dht11_next = time(NULL);
734 738
735 /* 739 /*
736 * Set the temperature sensors to 12 bits resolution and write it in EEPROM 740 * Set the temperature sensors to 12 bits resolution and write it in EEPROM
737 */ 741 */
738 for (device = Config.devices; device; device = device->next) { 742 // for (device = Config.devices; device; device = device->next) {
739 if ((device->type == DEVTYPE_W1) && 743 // if ((device->type == DEVTYPE_W1) &&
740 ((strncmp(device->address, (char *)"10", 2) == 0) || 744 // ((strncmp(device->address, (char *)"10", 2) == 0) ||
741 (strncmp(device->address, (char *)"22", 2) == 0) || 745 // (strncmp(device->address, (char *)"22", 2) == 0) ||
742 (strncmp(device->address, (char *)"28", 2) == 0) || 746 // (strncmp(device->address, (char *)"28", 2) == 0) ||
743 (strncmp(device->address, (char *)"3b", 2) == 0) || 747 // (strncmp(device->address, (char *)"3b", 2) == 0) ||
744 (strncmp(device->address, (char *)"42", 2) == 0))) { 748 // (strncmp(device->address, (char *)"42", 2) == 0))) {
745 addr = xstrcpy((char *)"/sys/bus/w1/devices/"); 749 // addr = xstrcpy((char *)"/sys/bus/w1/devices/");
746 addr = xstrcat(addr, device->address); 750 // addr = xstrcat(addr, device->address);
747 addr = xstrcat(addr, (char *)"/w1_slave"); 751 // addr = xstrcat(addr, (char *)"/w1_slave");
748 if ((fp = fopen(addr, "w"))) { 752 // if ((fp = fopen(addr, "w"))) {
749 rc = fprintf(fp, "12\n0\n"); // According to the kernel documentation. Seems to work. 753 // rc = fprintf(fp, "12\n0\n"); // According to the kernel documentation. Seems to work.
750 fclose(fp); 754 // fclose(fp);
751 if (rc != 5) { 755 // if (rc != 5) {
752 syslog(LOG_NOTICE, "Program 12 bits resolution error rc=%d for %s", rc, addr); 756 // syslog(LOG_NOTICE, "Program 12 bits resolution error rc=%d for %s", rc, addr);
753 } 757 // }
754 } 758 // }
755 free(addr); 759 // free(addr);
756 addr = NULL; 760 // addr = NULL;
757 } 761 // }
758 } 762 // }
759 763
760 /* 764 /*
761 * Loop forever until the external shutdown variable is set. 765 * Loop forever until the external shutdown variable is set.
762 */ 766 */
763 for (;;) { 767 for (;;) {
765 /* 769 /*
766 * Process all devices. 770 * Process all devices.
767 */ 771 */
768 for (device = Config.devices; device; device = device->next) { 772 for (device = Config.devices; device; device = device->next) {
769 773
770 if (my_shutdown) 774 if (my_devices_shutdown)
771 break; 775 break;
772 776
773 switch (device->type) { 777 switch (device->type) {
774 case DEVTYPE_W1: 778 case DEVTYPE_W1:
775 /* 779 /*
779 if ((strncmp(device->address, (char *)"10", 2) == 0) || 783 if ((strncmp(device->address, (char *)"10", 2) == 0) ||
780 (strncmp(device->address, (char *)"22", 2) == 0) || 784 (strncmp(device->address, (char *)"22", 2) == 0) ||
781 (strncmp(device->address, (char *)"28", 2) == 0) || 785 (strncmp(device->address, (char *)"28", 2) == 0) ||
782 (strncmp(device->address, (char *)"3b", 2) == 0) || 786 (strncmp(device->address, (char *)"3b", 2) == 0) ||
783 (strncmp(device->address, (char *)"42", 2) == 0)) { 787 (strncmp(device->address, (char *)"42", 2) == 0)) {
784 addr = xstrcpy((char *)"/sys/bus/w1/devices/"); 788 found = FALSE;
785 addr = xstrcat(addr, device->address); 789 for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
786 addr = xstrcat(addr, (char *)"/w1_slave"); 790 if (strcmp(device->address, dev_w1->address) == 0) {
787 if ((fp = fopen(addr, "r"))) { 791 syslog(LOG_NOTICE, "sensor %s value %d", dev_w1->address, dev_w1->value);
788 fgets(line1, 50, fp); // Read 2 lines 792 found = TRUE;
789 fgets(line2, 50, fp); 793 if ((dev_w1->value == -1) || (dev_w1->value < -55000)) {
790 fclose(fp); 794 syslog(LOG_NOTICE, "sensor %s value error %d", device->address, dev_w1->value);
791 /* 795 device->present = DEVPRESENT_ERROR;
792 * The output looks like: 796 } else {
793 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES 797 device->value = dev_w1->value;
794 * 72 01 4b 46 7f ff 0e 10 57 t=23125 798 device->timestamp = time(NULL);
795 */
796 line1[strlen(line1)-1] = '\0';
797 if ((line1[36] == 'Y') && (line1[37] == 'E')) {
798 /* CRC is Ok, continue */
799 if (device->present != DEVPRESENT_YES) {
800 syslog(LOG_NOTICE, "sensor %s is Ok", device->address);
801 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
802 device->present = DEVPRESENT_YES; 799 device->present = DEVPRESENT_YES;
803 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
804 } 800 }
805
806 line2[strlen(line2)-1] = '\0';
807 strtok(line2, (char *)"=");
808 p = strtok(NULL, (char *)"=");
809 rc = sscanf(p, "%d", &temp);
810 if ((rc == 1) && (device->value != temp)) {
811 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
812 if (temp < -55000) {
813 syslog(LOG_NOTICE, "sensor %s value error '%d` '%s`", device->address, temp, line1);
814 device->present = DEVPRESENT_ERROR;
815 } else {
816 device->value = temp;
817 device->timestamp = time(NULL);
818 device->present = DEVPRESENT_YES;
819 }
820 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
821 }
822 } else {
823 syslog(LOG_NOTICE, "sensor %s CRC error '%s`", device->address, line1);
824 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
825 device->present = DEVPRESENT_ERROR;
826 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
827 } 801 }
828 } else { 802 }
803
804 if (found == FALSE) {
829 if (device->present != DEVPRESENT_NO) { 805 if (device->present != DEVPRESENT_NO) {
830 syslog(LOG_NOTICE, "sensor %s is missing", device->address); 806 syslog(LOG_NOTICE, "sensor %s is missing", device->address);
831 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 807 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
832 device->present = DEVPRESENT_NO; 808 device->present = DEVPRESENT_NO;
833 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 809 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
834 } 810 }
835 } 811 }
836 free(addr); 812
837 addr = NULL;
838 } /* if temperature sensor */ 813 } /* if temperature sensor */
839 814
840 break; 815 break;
841 816
842 case DEVTYPE_DHT: 817 case DEVTYPE_DHT:
912 #endif 887 #endif
913 default: 888 default:
914 break; 889 break;
915 } 890 }
916 891
892 if (my_devices_shutdown)
893 break;
917 /* 894 /*
918 * Delay a bit after procesing a device. 895 * Delay a bit after procesing a device.
919 */ 896 */
920 mDelay(10); 897 mDelay(10);
921 } 898 }
922 if (my_shutdown) 899 if (my_devices_shutdown)
923 break; 900 break;
924 /* 901 /*
925 * Delay a bit after all devices 902 * Delay a bit after all devices
926 */ 903 */
927 mDelay(100); 904 mDelay(100);

mercurial