thermferm/devices.c

changeset 645
49eb753a958b
parent 644
07cc86900473
child 646
e3edc783006b
equal deleted inserted replaced
644:07cc86900473 645:49eb753a958b
27 #include "xutil.h" 27 #include "xutil.h"
28 28
29 29
30 extern sys_config Config; 30 extern sys_config Config;
31 extern int my_shutdown; 31 extern int my_shutdown;
32 extern pthread_mutex_t mutexes[5];
32 33
33 #ifdef USE_SIMULATOR 34 #ifdef USE_SIMULATOR
34 35
35 extern int SIMcooling; 36 extern int SIMcooling;
36 extern int SIMheating; 37 extern int SIMheating;
37 extern int SIMfan; 38 extern int SIMfan;
38 extern int SIMlight; 39 extern int SIMlight;
39 40
40 #endif 41 #endif
42
41 43
42 #ifdef HAVE_WIRINGPI_H 44 #ifdef HAVE_WIRINGPI_H
43 45
44 #define MAXTIMINGS 100 46 #define MAXTIMINGS 100
45 47
297 299
298 if (uuid == NULL) 300 if (uuid == NULL)
299 return 0; 301 return 0;
300 302
301 now = time(NULL); 303 now = time(NULL);
302 304 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
303 #ifdef HAVE_WIRINGPI_H
304 piLock(LOCK_DEVICES);
305 #endif
306 305
307 for (device = Config.devices; device; device = device->next) { 306 for (device = Config.devices; device; device = device->next) {
308 if (! strcmp(uuid, device->uuid)) { 307 if (! strcmp(uuid, device->uuid)) {
309 /* 308 /*
310 * Execute command if different then the old value. But also 309 * Execute command if different then the old value. But also
327 if ((device->type == DEVTYPE_RC433) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { 326 if ((device->type == DEVTYPE_RC433) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) {
328 snprintf(buf, 39, "%s,%d", device->address, value ? 1:0); 327 snprintf(buf, 39, "%s,%d", device->address, value ? 1:0);
329 for (i = 0; i < strlen(buf); i++) 328 for (i = 0; i < strlen(buf); i++)
330 if (buf[i] == '-') 329 if (buf[i] == '-')
331 buf[i] = ','; 330 buf[i] = ',';
332 piUnlock(LOCK_DEVICES); 331 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
333 enableTransmit(device->gpiopin); 332 enableTransmit(device->gpiopin);
334 rc = toggleSwitch(buf); 333 rc = toggleSwitch(buf);
335 disableTransmit(); 334 disableTransmit();
336 piLock(LOCK_DEVICES); 335 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
337 syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc); 336 syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc);
338 device->value = value; 337 device->value = value;
339 device->timestamp = time(NULL); 338 device->timestamp = time(NULL);
340 piUnlock(LOCK_DEVICES); 339 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
341 return rc; 340 return rc;
342 } 341 }
343 342
344 if ((device->type == DEVTYPE_GPIO) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { 343 if ((device->type == DEVTYPE_GPIO) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) {
345 344
396 SIMlight = value; 395 SIMlight = value;
397 } 396 }
398 } 397 }
399 #endif 398 #endif
400 } else { 399 } else {
401 #ifdef HAVE_WIRINGPI_H 400 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
402 piUnlock(LOCK_DEVICES);
403 #endif
404 return 0; 401 return 0;
405 } // if ((test_value != my_value) || (((int)now - (int)my_timestamp) >= 120)) 402 } // if ((test_value != my_value) || (((int)now - (int)my_timestamp) >= 120))
406 } // if (! strcmp(uuid, device->uuid)) 403 } // if (! strcmp(uuid, device->uuid))
407 } // for (device = Config.devices; device; device = device->next) 404 } // for (device = Config.devices; device; device = device->next)
408 #ifdef HAVE_WIRINGPI_H 405
409 piUnlock(LOCK_DEVICES); 406 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
410 #endif
411
412 return 0; 407 return 0;
413 } 408 }
414 409
415 410
416 /* 411 /*
423 int tmp, present; 418 int tmp, present;
424 419
425 if (uuid == NULL) 420 if (uuid == NULL)
426 return 0; 421 return 0;
427 422
428 #ifdef HAVE_WIRINGPI_H 423 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
429 piLock(LOCK_DEVICES);
430 #endif
431 424
432 for (device = Config.devices; device; device = device->next) { 425 for (device = Config.devices; device; device = device->next) {
433 if (! strcmp(uuid, device->uuid)) { 426 if (! strcmp(uuid, device->uuid)) {
434 present = device->present; 427 present = device->present;
435 if (present == DEVPRESENT_YES) { 428 if (present == DEVPRESENT_YES) {
436 tmp = device->value + device->offset; 429 tmp = device->value + device->offset;
437 } else { 430 } else {
438 tmp = 0; 431 tmp = 0;
439 } 432 }
440 #ifdef HAVE_WIRINGPI_H 433 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
441 piUnlock(LOCK_DEVICES);
442 #endif
443 *value = tmp; 434 *value = tmp;
444 return present; 435 return present;
445 } 436 }
446 } 437 }
447 438
448 #ifdef HAVE_WIRINGPI_H 439 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
449 piUnlock(LOCK_DEVICES);
450 #endif
451
452 return DEVPRESENT_NO; 440 return DEVPRESENT_NO;
453 } 441 }
454 442
455 443
456 444
550 } 538 }
551 } 539 }
552 closedir(fd); 540 closedir(fd);
553 } 541 }
554 542
543 /*
544 * DHT11 as kernel module.
545 */
546
555 #ifdef HAVE_WIRINGPI_H 547 #ifdef HAVE_WIRINGPI_H
556 if (piBoardRev() == 2) { 548 if (piBoardRev() == 2) {
557 /* 549 /*
558 * Support rev B and newer boards only 550 * Support rev B and newer boards only
559 */ 551 */
595 ndev->inuse = 1; 587 ndev->inuse = 1;
596 ndev->comment = xstrcpy((char *)"433 Mhz transmitter"); 588 ndev->comment = xstrcpy((char *)"433 Mhz transmitter");
597 } else if (i == 3) { 589 } else if (i == 3) {
598 ndev->direction = DEVDIR_INTERN; 590 ndev->direction = DEVDIR_INTERN;
599 ndev->inuse = 1; 591 ndev->inuse = 1;
600 ndev->comment = xstrcpy((char *)"DHT11 room sensor"); 592 ndev->comment = xstrcpy((char *)"DHT11 sensor");
601 } else if (i == PANEL_LED) { 593 } else if (i == PANEL_LED) {
602 ndev->direction = DEVDIR_OUT_BIN; 594 ndev->direction = DEVDIR_OUT_BIN;
603 ndev->inuse = 1; 595 ndev->inuse = 1;
604 ndev->comment = xstrcpy((char *)"Frontpanel LED"); 596 ndev->comment = xstrcpy((char *)"Frontpanel LED");
605 } else if (i == PANEL_ENTER) { 597 } else if (i == PANEL_ENTER) {
723 return rc; 715 return rc;
724 } 716 }
725 717
726 718
727 719
728 #ifdef HAVE_WIRINGPI_H
729 PI_THREAD (my_devices_loop)
730 #else
731 void *my_devices_loop(void *threadid) 720 void *my_devices_loop(void *threadid)
732 #endif
733 { 721 {
734 devices_list *device; 722 devices_list *device;
735 #ifdef USE_SIMULATOR 723 #ifdef USE_SIMULATOR
736 simulator_list *simulator; 724 simulator_list *simulator;
737 #endif 725 #endif
813 line1[strlen(line1)-1] = '\0'; 801 line1[strlen(line1)-1] = '\0';
814 if ((line1[36] == 'Y') && (line1[37] == 'E')) { 802 if ((line1[36] == 'Y') && (line1[37] == 'E')) {
815 /* CRC is Ok, continue */ 803 /* CRC is Ok, continue */
816 if (device->present != DEVPRESENT_YES) { 804 if (device->present != DEVPRESENT_YES) {
817 syslog(LOG_NOTICE, "sensor %s is Ok", device->address); 805 syslog(LOG_NOTICE, "sensor %s is Ok", device->address);
818 #ifdef HAVE_WIRINGPI_H 806 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
819 piLock(LOCK_DEVICES);
820 #endif
821 device->present = DEVPRESENT_YES; 807 device->present = DEVPRESENT_YES;
822 #ifdef HAVE_WIRINGPI_H 808 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
823 piUnlock(LOCK_DEVICES);
824 #endif
825 } 809 }
826 810
827 line2[strlen(line2)-1] = '\0'; 811 line2[strlen(line2)-1] = '\0';
828 strtok(line2, (char *)"="); 812 strtok(line2, (char *)"=");
829 p = strtok(NULL, (char *)"="); 813 p = strtok(NULL, (char *)"=");
830 rc = sscanf(p, "%d", &temp); 814 rc = sscanf(p, "%d", &temp);
831 if ((rc == 1) && (device->value != temp)) { 815 if ((rc == 1) && (device->value != temp)) {
832 #ifdef HAVE_WIRINGPI_H 816 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
833 piLock(LOCK_DEVICES);
834 #endif
835 if (temp < -55000) { 817 if (temp < -55000) {
836 syslog(LOG_NOTICE, "sensor %s value error '%d` '%s`", device->address, temp, line1); 818 syslog(LOG_NOTICE, "sensor %s value error '%d` '%s`", device->address, temp, line1);
837 device->present = DEVPRESENT_ERROR; 819 device->present = DEVPRESENT_ERROR;
838 } else { 820 } else {
839 device->value = temp; 821 device->value = temp;
840 device->timestamp = time(NULL); 822 device->timestamp = time(NULL);
841 device->present = DEVPRESENT_YES; 823 device->present = DEVPRESENT_YES;
842 } 824 }
843 #ifdef HAVE_WIRINGPI_H 825 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
844 piUnlock(LOCK_DEVICES);
845 #endif
846 } 826 }
847 } else { 827 } else {
848 syslog(LOG_NOTICE, "sensor %s CRC error '%s`", device->address, line1); 828 syslog(LOG_NOTICE, "sensor %s CRC error '%s`", device->address, line1);
849 #ifdef HAVE_WIRINGPI_H 829 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
850 piLock(LOCK_DEVICES);
851 #endif
852 device->present = DEVPRESENT_ERROR; 830 device->present = DEVPRESENT_ERROR;
853 #ifdef HAVE_WIRINGPI_H 831 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
854 piUnlock(LOCK_DEVICES);
855 #endif
856 } 832 }
857 } else { 833 } else {
858 if (device->present != DEVPRESENT_NO) { 834 if (device->present != DEVPRESENT_NO) {
859 syslog(LOG_NOTICE, "sensor %s is missing", device->address); 835 syslog(LOG_NOTICE, "sensor %s is missing", device->address);
860 #ifdef HAVE_WIRINGPI_H 836 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
861 piLock(LOCK_DEVICES);
862 #endif
863 device->present = DEVPRESENT_NO; 837 device->present = DEVPRESENT_NO;
864 #ifdef HAVE_WIRINGPI_H 838 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
865 piUnlock(LOCK_DEVICES);
866 #endif
867 } 839 }
868 } 840 }
869 free(addr); 841 free(addr);
870 addr = NULL; 842 addr = NULL;
871 } /* if temperature sensor */ 843 } /* if temperature sensor */
878 addr = xstrcat(addr, (char *)"/state"); 850 addr = xstrcat(addr, (char *)"/state");
879 851
880 if ((access(addr, R_OK)) == 0) { 852 if ((access(addr, R_OK)) == 0) {
881 if (device->present != DEVPRESENT_YES) { 853 if (device->present != DEVPRESENT_YES) {
882 syslog(LOG_NOTICE, "DS2413 %s is back", device->address); 854 syslog(LOG_NOTICE, "DS2413 %s is back", device->address);
883 #ifdef HAVE_WIRINGPI_H 855 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
884 piLock(LOCK_DEVICES);
885 #endif
886 device->present = DEVPRESENT_YES; 856 device->present = DEVPRESENT_YES;
887 #ifdef HAVE_WIRINGPI_H 857 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
888 piUnlock(LOCK_DEVICES);
889 #endif
890 } 858 }
891 /* 859 /*
892 * First make sure that if this device is configured as input 860 * First make sure that if this device is configured as input
893 * to drive the output high. 861 * to drive the output high.
894 */ 862 */
909 } 877 }
910 write_w1(device->address, (char *)"output", output); 878 write_w1(device->address, (char *)"output", output);
911 } 879 }
912 } 880 }
913 if ((rc = read_w1(device->address, (char *)"state")) >= 0) { 881 if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
914 #ifdef HAVE_WIRINGPI_H 882 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
915 piLock(LOCK_DEVICES);
916 #endif
917 /* 883 /*
918 * Read PIOA or PIOB pin state bits 884 * Read PIOA or PIOB pin state bits
919 */ 885 */
920 if (device->subdevice == 0) 886 if (device->subdevice == 0)
921 device->value = (rc & 0x01) ? 0 : 1; 887 device->value = (rc & 0x01) ? 0 : 1;
922 else if (device->subdevice == 1) 888 else if (device->subdevice == 1)
923 device->value = (rc & 0x04) ? 0 : 1; 889 device->value = (rc & 0x04) ? 0 : 1;
924 device->timestamp = time(NULL); 890 device->timestamp = time(NULL);
925 #ifdef HAVE_WIRINGPI_H 891 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
926 piUnlock(LOCK_DEVICES);
927 #endif
928 } 892 }
929 } else { 893 } else {
930 if (device->present != DEVPRESENT_NO) { 894 if (device->present != DEVPRESENT_NO) {
931 syslog(LOG_NOTICE, "DS2413 %s is missing", device->address); 895 syslog(LOG_NOTICE, "DS2413 %s is missing", device->address);
932 #ifdef HAVE_WIRINGPI_H 896 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
933 piLock(LOCK_DEVICES);
934 #endif
935 device->present = DEVPRESENT_NO; 897 device->present = DEVPRESENT_NO;
936 #ifdef HAVE_WIRINGPI_H 898 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
937 piUnlock(LOCK_DEVICES);
938 #endif
939 } 899 }
940 } 900 }
941 free(addr); 901 free(addr);
942 addr = NULL; 902 addr = NULL;
943 } 903 }
954 if ((int)(now - dht11_last) > 30) { 914 if ((int)(now - dht11_last) > 30) {
955 if (device->subdevice == 0) { 915 if (device->subdevice == 0) {
956 /* Read once during subdevice 0 */ 916 /* Read once during subdevice 0 */
957 dht11_pin = device->gpiopin; 917 dht11_pin = device->gpiopin;
958 dht11Read(); 918 dht11Read();
959 piLock(LOCK_DEVICES); 919 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
960 if (dht11_valid) { 920 if (dht11_valid) {
961 device->value = dht11_temperature * 1000; 921 device->value = dht11_temperature * 1000;
962 device->timestamp = time(NULL); 922 device->timestamp = time(NULL);
963 device->present = DEVPRESENT_YES; 923 device->present = DEVPRESENT_YES;
964 } else { 924 } else {
965 device->present = DEVPRESENT_ERROR; 925 device->present = DEVPRESENT_ERROR;
966 } 926 }
967 piUnlock(LOCK_DEVICES); 927 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
968 } else if (device->subdevice == 1) { 928 } else if (device->subdevice == 1) {
969 /* Data already present */ 929 /* Data already present */
970 piLock(LOCK_DEVICES); 930 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
971 if (dht11_valid) { 931 if (dht11_valid) {
972 device->value = dht11_humidity * 1000; 932 device->value = dht11_humidity * 1000;
973 device->timestamp = time(NULL); 933 device->timestamp = time(NULL);
974 device->present = DEVPRESENT_YES; 934 device->present = DEVPRESENT_YES;
975 } else { 935 } else {
976 device->present = DEVPRESENT_ERROR; 936 device->present = DEVPRESENT_ERROR;
977 } 937 }
978 piUnlock(LOCK_DEVICES); 938 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
979 dht11_last = now; /* Okay for a new reading. */ 939 dht11_last = now; /* Okay for a new reading. */
980 } 940 }
981 } 941 }
982 break; 942 break;
983 943
984 case DEVTYPE_GPIO: 944 case DEVTYPE_GPIO:
985 if (device->direction == DEVDIR_IN_BIN) { 945 if (device->direction == DEVDIR_IN_BIN) {
986 piLock(LOCK_DEVICES); 946 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
987 device->value = digitalRead(device->gpiopin); 947 device->value = digitalRead(device->gpiopin);
988 device->offset = 0; 948 device->offset = 0;
989 device->timestamp = time(NULL); 949 device->timestamp = time(NULL);
990 piUnlock(LOCK_DEVICES); 950 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
991 } 951 }
992 break; 952 break;
993 953
994 #endif 954 #endif
995 #ifdef USE_SIMULATOR 955 #ifdef USE_SIMULATOR
996 case DEVTYPE_SIM: 956 case DEVTYPE_SIM:
997 #ifdef HAVE_WIRINGPI_H 957 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
998 piLock(LOCK_DEVICES);
999 #endif
1000 if (Config.simulators) { 958 if (Config.simulators) {
1001 simulator = Config.simulators; 959 simulator = Config.simulators;
1002 if (device->subdevice == 0) { 960 if (device->subdevice == 0) {
1003 device->value = (int)((int)(simulator->room_temperature * 1000) / 500) * 500; 961 device->value = (int)((int)(simulator->room_temperature * 1000) / 500) * 500;
1004 device->timestamp = time(NULL); 962 device->timestamp = time(NULL);
1014 } else if (device->subdevice == 6) { 972 } else if (device->subdevice == 6) {
1015 device->value = (int)((int)(simulator->chiller_temperature * 1000) / 62.5) * 62.5; 973 device->value = (int)((int)(simulator->chiller_temperature * 1000) / 62.5) * 62.5;
1016 device->timestamp = time(NULL); 974 device->timestamp = time(NULL);
1017 } 975 }
1018 } 976 }
1019 #ifdef HAVE_WIRINGPI_H 977 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
1020 piUnlock(LOCK_DEVICES);
1021 #endif
1022 break; 978 break;
1023 #endif 979 #endif
1024 default: 980 default:
1025 break; 981 break;
1026 } 982 }

mercurial