thermferm/devices.c

changeset 661
8c1e7a52e24f
parent 660
a28ef4d9afa4
child 671
b2e2cbb13cb3
equal deleted inserted replaced
660:a28ef4d9afa4 661:8c1e7a52e24f
244 244
245 if (uuid == NULL) 245 if (uuid == NULL)
246 return 0; 246 return 0;
247 247
248 now = time(NULL); 248 now = time(NULL);
249 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 249 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
250 250
251 for (device = Config.devices; device; device = device->next) { 251 for (device = Config.devices; device; device = device->next) {
252 if (! strcmp(uuid, device->uuid)) { 252 if (! strcmp(uuid, device->uuid)) {
253 /* 253 /*
254 * Execute command if different then the old value. But also 254 * Execute command if different then the old value. But also
271 if ((device->type == DEVTYPE_RC433) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { 271 if ((device->type == DEVTYPE_RC433) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) {
272 snprintf(buf, 39, "%s,%d", device->address, value ? 1:0); 272 snprintf(buf, 39, "%s,%d", device->address, value ? 1:0);
273 for (i = 0; i < strlen(buf); i++) 273 for (i = 0; i < strlen(buf); i++)
274 if (buf[i] == '-') 274 if (buf[i] == '-')
275 buf[i] = ','; 275 buf[i] = ',';
276 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 276 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
277 enableTransmit(device->gpiopin); 277 enableTransmit(device->gpiopin);
278 rc = toggleSwitch(buf); 278 rc = toggleSwitch(buf);
279 disableTransmit(); 279 disableTransmit();
280 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 280 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
281 syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc); 281 syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc);
282 device->value = value; 282 device->value = value;
283 device->timestamp = time(NULL); 283 device->timestamp = time(NULL);
284 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 284 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
285 return rc; 285 return rc;
286 } 286 }
287 287
288 if ((device->type == DEVTYPE_GPIO) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { 288 if ((device->type == DEVTYPE_GPIO) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) {
289 289
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 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
318 device->value = (value == 0) ? 0 : 1; 318 device->value = (value == 0) ? 0 : 1;
319 device->timestamp = time(NULL); 319 device->timestamp = time(NULL);
320 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 320 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
321 } 321 }
322 } 322 }
323 } 323 }
324 } 324 }
325 325
342 SIMlight = value; 342 SIMlight = value;
343 } 343 }
344 } 344 }
345 #endif 345 #endif
346 } else { 346 } else {
347 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 347 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
348 return 0; 348 return 0;
349 } // if ((test_value != my_value) || (((int)now - (int)my_timestamp) >= 120)) 349 } // if ((test_value != my_value) || (((int)now - (int)my_timestamp) >= 120))
350 } // if (! strcmp(uuid, device->uuid)) 350 } // if (! strcmp(uuid, device->uuid))
351 } // for (device = Config.devices; device; device = device->next) 351 } // for (device = Config.devices; device; device = device->next)
352 352
353 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 353 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
354 return 0; 354 return 0;
355 } 355 }
356 356
357 357
358 /* 358 /*
365 int tmp, present; 365 int tmp, present;
366 366
367 if (uuid == NULL) 367 if (uuid == NULL)
368 return 0; 368 return 0;
369 369
370 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 370 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
371 371
372 for (device = Config.devices; device; device = device->next) { 372 for (device = Config.devices; device; device = device->next) {
373 if (! strcmp(uuid, device->uuid)) { 373 if (! strcmp(uuid, device->uuid)) {
374 present = device->present; 374 present = device->present;
375 if (present == DEVPRESENT_YES) { 375 if (present == DEVPRESENT_YES) {
376 tmp = device->value + device->offset; 376 tmp = device->value + device->offset;
377 } else { 377 } else {
378 tmp = 0; 378 tmp = 0;
379 } 379 }
380 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 380 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
381 *value = tmp; 381 *value = tmp;
382 return present; 382 return present;
383 } 383 }
384 } 384 }
385 385
386 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 386 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
387 return DEVPRESENT_NO; 387 return DEVPRESENT_NO;
388 } 388 }
389 389
390 390
391 391
721 #ifdef USE_SIMULATOR 721 #ifdef USE_SIMULATOR
722 simulator_list *simulator; 722 simulator_list *simulator;
723 #endif 723 #endif
724 // char *addr = NULL, line1[60], line2[60], *p = NULL; 724 // char *addr = NULL, line1[60], line2[60], *p = NULL;
725 // FILE *fp; 725 // FILE *fp;
726 int rc, found; 726 int found;
727 time_t now; 727 time_t now;
728 728
729 my_devices_state = 1; 729 my_devices_state = 1;
730 syslog(LOG_NOTICE, "Thread my_devices_loop started"); 730 syslog(LOG_NOTICE, "Thread my_devices_loop started");
731 731
732 #ifdef HAVE_WIRINGPI_H 732 #ifdef HAVE_WIRINGPI_H
733 if ((rc = piHiPri(50))) 733 // if ((rc = piHiPri(50)))
734 syslog(LOG_NOTICE, "my_devices_loop: piHiPri(50) rc=%d", rc); 734 // syslog(LOG_NOTICE, "my_devices_loop: piHiPri(50) rc=%d", rc);
735 #endif 735 #endif
736 736
737 dht11_next = time(NULL); 737 dht11_next = time(NULL);
738 738
739 /* 739 /*
771 */ 771 */
772 for (device = Config.devices; device; device = device->next) { 772 for (device = Config.devices; device; device = device->next) {
773 773
774 if (my_devices_shutdown) 774 if (my_devices_shutdown)
775 break; 775 break;
776 //syslog(LOG_NOTICE, "device type %d address %s-%d comment %s", device->type, device->address, device->subdevice, device->comment);
776 777
777 switch (device->type) { 778 switch (device->type) {
778 case DEVTYPE_W1: 779 case DEVTYPE_W1:
779 /* 780 /*
780 * Only tested with DS18B20 but from the kernel source this 781 * Only tested with DS18B20 but from the kernel source this
786 (strncmp(device->address, (char *)"3b", 2) == 0) || 787 (strncmp(device->address, (char *)"3b", 2) == 0) ||
787 (strncmp(device->address, (char *)"42", 2) == 0)) { 788 (strncmp(device->address, (char *)"42", 2) == 0)) {
788 found = FALSE; 789 found = FALSE;
789 for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) { 790 for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
790 if (strcmp(device->address, dev_w1->address) == 0) { 791 if (strcmp(device->address, dev_w1->address) == 0) {
791 syslog(LOG_NOTICE, "sensor %s value %d", dev_w1->address, dev_w1->value); 792 // syslog(LOG_NOTICE, "sensor %s value %d", dev_w1->address, dev_w1->value);
792 found = TRUE; 793 found = TRUE;
793 if ((dev_w1->value == -1) || (dev_w1->value < -55000)) { 794 if ((dev_w1->value == -1) || (dev_w1->value < -55000)) {
794 syslog(LOG_NOTICE, "sensor %s value error %d", device->address, dev_w1->value); 795 syslog(LOG_NOTICE, "sensor %s value error %d", device->address, dev_w1->value);
795 device->present = DEVPRESENT_ERROR; 796 device->present = DEVPRESENT_ERROR;
796 } else { 797 } else {
802 } 803 }
803 804
804 if (found == FALSE) { 805 if (found == FALSE) {
805 if (device->present != DEVPRESENT_NO) { 806 if (device->present != DEVPRESENT_NO) {
806 syslog(LOG_NOTICE, "sensor %s is missing", device->address); 807 syslog(LOG_NOTICE, "sensor %s is missing", device->address);
807 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 808 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
808 device->present = DEVPRESENT_NO; 809 device->present = DEVPRESENT_NO;
809 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 810 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
810 } 811 }
811 } 812 }
812 813
813 } /* if temperature sensor */ 814 } /* if temperature sensor */
814 815
823 now = time(NULL); 824 now = time(NULL);
824 if ((int)(now >= dht11_next)) { 825 if ((int)(now >= dht11_next)) {
825 if (device->subdevice == 0) { 826 if (device->subdevice == 0) {
826 /* Read once during subdevice 0 */ 827 /* Read once during subdevice 0 */
827 dht11Read(device->address); 828 dht11Read(device->address);
828 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 829 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
829 device->present = dht11_state; 830 device->present = dht11_state;
830 if (dht11_state == DEVPRESENT_YES) { 831 if (dht11_state == DEVPRESENT_YES) {
831 device->value = dht11_temperature; 832 device->value = dht11_temperature;
832 device->timestamp = time(NULL); 833 device->timestamp = time(NULL);
833 } 834 }
834 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 835 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
835 } else if (device->subdevice == 1) { 836 } else if (device->subdevice == 1) {
836 /* Data already present, valid or not. */ 837 /* Data already present, valid or not. */
837 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 838 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
838 device->present = dht11_state; 839 device->present = dht11_state;
839 if (dht11_state == DEVPRESENT_YES) { 840 if (dht11_state == DEVPRESENT_YES) {
840 device->value = dht11_humidity; 841 device->value = dht11_humidity;
841 device->timestamp = time(NULL); 842 device->timestamp = time(NULL);
842 dht11_next = now + 29; 843 dht11_next = now + 29;
843 } else { 844 } else {
844 dht11_next = now + 1; 845 dht11_next = now + 1;
845 } 846 }
846 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 847 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
847 } 848 }
848 } 849 }
849 break; 850 break;
850 851
851 #ifdef HAVE_WIRINGPI_H 852 #ifdef HAVE_WIRINGPI_H
852 case DEVTYPE_GPIO: 853 case DEVTYPE_GPIO:
853 if (device->direction == DEVDIR_IN_BIN) { 854 if (device->direction == DEVDIR_IN_BIN) {
854 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 855 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
855 device->value = digitalRead(device->gpiopin); 856 device->value = digitalRead(device->gpiopin);
856 device->offset = 0; 857 device->offset = 0;
857 device->timestamp = time(NULL); 858 device->timestamp = time(NULL);
858 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 859 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
859 } 860 }
860 break; 861 break;
861 862
862 #endif 863 #endif
863 #ifdef USE_SIMULATOR 864 #ifdef USE_SIMULATOR
864 case DEVTYPE_SIM: 865 case DEVTYPE_SIM:
865 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 866 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
866 if (Config.simulators) { 867 if (Config.simulators) {
867 simulator = Config.simulators; 868 simulator = Config.simulators;
868 if (device->subdevice == 0) { 869 if (device->subdevice == 0) {
869 device->value = (int)((int)(simulator->room_temperature * 1000) / 500) * 500; 870 device->value = (int)((int)(simulator->room_temperature * 1000) / 500) * 500;
870 device->timestamp = time(NULL); 871 device->timestamp = time(NULL);
880 } else if (device->subdevice == 6) { 881 } else if (device->subdevice == 6) {
881 device->value = (int)((int)(simulator->chiller_temperature * 1000) / 62.5) * 62.5; 882 device->value = (int)((int)(simulator->chiller_temperature * 1000) / 62.5) * 62.5;
882 device->timestamp = time(NULL); 883 device->timestamp = time(NULL);
883 } 884 }
884 } 885 }
885 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 886 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
886 break; 887 break;
887 #endif 888 #endif
888 default: 889 default:
889 break; 890 break;
890 } 891 }

mercurial