thermferm/devices.c

changeset 716
5c30c8ef83a8
parent 715
f5d85af156ab
child 724
01e3936f62d4
equal deleted inserted replaced
715:f5d85af156ab 716:5c30c8ef83a8
38 38
39 extern const char DEVTYPE[8][6]; 39 extern const char DEVTYPE[8][6];
40 extern const char DEVPRESENT[4][6]; 40 extern const char DEVPRESENT[4][6];
41 extern const char DEVDIR[7][11]; 41 extern const char DEVDIR[7][11];
42 42
43 #ifdef USE_SIMULATOR
44 extern int my_simulator_state;
45 #endif
43 46
44 47
45 /* 48 /*
46 * Since kernel version 4 there is a module, and a dtoverlay so that the 49 * Since kernel version 4 there is a module, and a dtoverlay so that the
47 * temperature and humidity can simply read from the /sys filesystem. 50 * temperature and humidity can simply read from the /sys filesystem.
304 } 307 }
305 } 308 }
306 309
307 #ifdef USE_SIMULATOR 310 #ifdef USE_SIMULATOR
308 if ((device->type == DEVTYPE_SIM) && (device->direction == DEVDIR_OUT_BIN)) { 311 if ((device->type == DEVTYPE_SIM) && (device->direction == DEVDIR_OUT_BIN)) {
312 while (my_simulator_state == THREAD_PAUSE) { mDelay(20); };
309 for (simulator = Config.simulators; simulator; simulator = simulator->next) { 313 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
310 if ((strcmp(device->address, simulator->cooler_address) == 0) || 314 if ((strcmp(device->address, simulator->cooler_address) == 0) ||
311 (strcmp(device->address, simulator->heater_address) == 0) || 315 (strcmp(device->address, simulator->heater_address) == 0) ||
312 (strcmp(device->address, simulator->fan_address) == 0) || 316 (strcmp(device->address, simulator->fan_address) == 0) ||
313 (strcmp(device->address, simulator->light_address) == 0)) { 317 (strcmp(device->address, simulator->light_address) == 0)) {
713 #endif 717 #endif
714 718
715 #ifdef USE_SIMULATOR 719 #ifdef USE_SIMULATOR
716 720
717 for (simulator = Config.simulators; simulator; simulator = simulator->next) { 721 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
718 for (i = 0; i < 10; i++) { 722 for (i = 0; i < 12; i++) {
719 723
720 found = FALSE; 724 found = FALSE;
721 for (device = Config.devices; device; device = device->next) { 725 for (device = Config.devices; device; device = device->next) {
722 if ((i == 0 && strcmp(device->address, simulator->room_tempaddress) == 0) || 726 if ((i == 0 && strcmp(device->address, simulator->room_tempaddress) == 0) ||
723 (i == 1 && strcmp(device->address, simulator->air_address) == 0) || 727 (i == 1 && strcmp(device->address, simulator->air_address) == 0) ||
724 (i == 2 && strcmp(device->address, simulator->beer_address) == 0) || 728 (i == 2 && strcmp(device->address, simulator->beer_address) == 0) ||
725 (i == 3 && strcmp(device->address, simulator->heater_address) == 0) || 729 (i == 3 && strcmp(device->address, simulator->heater_address) == 0) ||
726 (i == 4 && strcmp(device->address, simulator->cooler_address) == 0) || 730 (i == 4 && strcmp(device->address, simulator->cooler_address) == 0) ||
727 (i == 5 && strcmp(device->address, simulator->room_humaddress) == 0) || 731 (i == 5 && strcmp(device->address, simulator->room_humaddress) == 0) ||
728 (i == 6 && strcmp(device->address, simulator->chiller_address) == 0) || 732 (i == 6 && strcmp(device->address, simulator->chiller_address) == 0) ||
729 (i == 7 && strcmp(device->address, simulator->fan_address) == 0) || 733 (i == 7 && strcmp(device->address, simulator->fan_address) == 0) ||
730 (i == 8 && strcmp(device->address, simulator->light_address) == 0) || 734 (i == 8 && strcmp(device->address, simulator->light_address) == 0) ||
731 (i == 9 && strcmp(device->address, simulator->beer_address2) == 0)) { 735 (i == 9 && strcmp(device->address, simulator->beer_address2) == 0) ||
736 (i == 10 && strcmp(device->address, simulator->door_address) == 0) ||
737 (i == 11 && strcmp(device->address, simulator->psu_address) == 0)) {
732 found = TRUE; 738 found = TRUE;
733 break; 739 break;
734 } 740 }
735 } 741 }
736 742
797 break; 803 break;
798 case 9: ndev->direction = DEVDIR_IN_ANALOG; 804 case 9: ndev->direction = DEVDIR_IN_ANALOG;
799 ndev->address = xstrcpy(simulator->beer_address2); 805 ndev->address = xstrcpy(simulator->beer_address2);
800 ndev->description = xstrcpy((char *)"Simulated beer temperature (alt)"); 806 ndev->description = xstrcpy((char *)"Simulated beer temperature (alt)");
801 ndev->present = simulator->beer_present2; 807 ndev->present = simulator->beer_present2;
808 break;
809 case 10: ndev->direction = DEVDIR_IN_BIN;
810 ndev->address = xstrcpy(simulator->door_address);
811 ndev->description = xstrcpy((char *)"Simulated fridge door");
812 ndev->present = simulator->door_present;
813 break;
814 case 11: ndev->direction = DEVDIR_IN_BIN;
815 ndev->address = xstrcpy(simulator->psu_address);
816 ndev->description = xstrcpy((char *)"Simulated PSU status");
817 ndev->present = simulator->psu_present;
802 break; 818 break;
803 } 819 }
804 820
805 syslog(LOG_NOTICE, "New Simulator device %s, subdevice %d", ndev->address, ndev->subdevice); 821 syslog(LOG_NOTICE, "New Simulator device %s, subdevice %d", ndev->address, ndev->subdevice);
806 822
958 break; 974 break;
959 975
960 #endif 976 #endif
961 #ifdef USE_SIMULATOR 977 #ifdef USE_SIMULATOR
962 case DEVTYPE_SIM: 978 case DEVTYPE_SIM:
979 while (my_simulator_state == THREAD_PAUSE) { mDelay(20); };
963 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 980 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
964 if (Config.simulators) { 981 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
965 int val; 982 int val;
966 983
967 simulator = Config.simulators; 984 if (strcmp(simulator->room_tempaddress, device->address) == 0) {
968 if (device->subdevice == 0) {
969 val = (int)((int)(simulator->room_temperature * 1000) / 500) * 500; 985 val = (int)((int)(simulator->room_temperature * 1000) / 500) * 500;
970 if (device->value != val) { 986 if (device->value != val) {
971 device->value = val; 987 device->value = val;
972 device->timestamp = time(NULL); 988 device->timestamp = time(NULL);
973 changed = true; 989 changed = true;
974 } 990 }
975 } else if (device->subdevice == 1) { 991 } else if (strcmp(simulator->air_address, device->address) == 0) {
976 val = (int)((int)(simulator->air_temperature * 1000) / 62.5) * 62.5; 992 val = (int)((int)((simulator->air_temperature * 1000) + 31.25) / 62.5) * 62.5;
977 if (device->value != val) { 993 if (device->value != val) {
978 device->value = val; 994 device->value = val;
979 device->timestamp = time(NULL); 995 device->timestamp = time(NULL);
980 changed = true; 996 changed = true;
981 } 997 }
982 } else if (device->subdevice == 2) { 998 } else if (strcmp(simulator->beer_address, device->address) == 0) {
983 val = (int)((int)(simulator->beer_temperature * 1000) / 62.5) * 62.5; 999 val = (int)((int)((simulator->beer_temperature * 1000) + 31.25) / 62.5) * 62.5;
984 if (device->value != val) { 1000 if (device->value != val) {
985 device->value = val; 1001 device->value = val;
986 device->timestamp = time(NULL); 1002 device->timestamp = time(NULL);
987 changed = true; 1003 changed = true;
988 } 1004 }
989 } else if (device->subdevice == 5) { 1005 } else if (strcmp(simulator->room_humaddress, device->address) == 0) {
990 val = (int)((int)(simulator->room_humidity * 1000) / 500) * 500; 1006 val = (int)((int)(simulator->room_humidity * 1000) / 500) * 500;
991 if (device->value != val) { 1007 if (device->value != val) {
992 device->value = val; 1008 device->value = val;
993 device->timestamp = time(NULL); 1009 device->timestamp = time(NULL);
994 changed = true; 1010 changed = true;
995 } 1011 }
996 } else if (device->subdevice == 6) { 1012 } else if (strcmp(simulator->chiller_address, device->address) == 0) {
997 val = (int)((int)(simulator->chiller_temperature * 1000) / 62.5) * 62.5; 1013 val = (int)((int)(simulator->chiller_temperature * 1000) / 62.5) * 62.5;
998 if (device->value != val) { 1014 if (device->value != val) {
999 device->value = val; 1015 device->value = val;
1000 device->timestamp = time(NULL); 1016 device->timestamp = time(NULL);
1001 changed = true; 1017 changed = true;
1002 } 1018 }
1019 } else if (strcmp(simulator->door_address, device->address) == 0) {
1020 val = simulator->door_value;
1021 if (device->value != val) {
1022 device->value = val;
1023 device->timestamp = time(NULL);
1024 changed = true;
1025 }
1026 } else if (strcmp(simulator->psu_address, device->address) == 0) {
1027 val = simulator->psu_value;
1028 if (device->value != val) {
1029 device->value = val;
1030 device->timestamp = time(NULL);
1031 changed = true;
1032 }
1003 } 1033 }
1004 } 1034 }
1005 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 1035 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
1006 break; 1036 break;
1007 #endif 1037 #endif

mercurial