47 extern int debug; |
47 extern int debug; |
48 extern sys_config Config; |
48 extern sys_config Config; |
49 extern int lcdHandle; |
49 extern int lcdHandle; |
50 extern int slcdHandle; |
50 extern int slcdHandle; |
51 extern int my_devices_state; |
51 extern int my_devices_state; |
|
52 extern int my_devices_shutdown; |
52 extern int my_panel_state; |
53 extern int my_panel_state; |
|
54 extern int my_panel_shutdown; |
53 extern int my_server_state; |
55 extern int my_server_state; |
|
56 extern int my_server_shutdown; |
54 extern int my_simulator_state; |
57 extern int my_simulator_state; |
|
58 #ifdef USE_SIMULATOR |
|
59 extern int my_simulator_shutdown; |
|
60 #endif |
55 extern int my_one_wire_state; |
61 extern int my_one_wire_state; |
|
62 extern int my_one_wire_shutdown; |
56 int setupmenu = MENU_NONE; |
63 int setupmenu = MENU_NONE; |
57 units_list *current_unit = NULL; /* In panel editor this points to the current unit. */ |
64 units_list *current_unit = NULL; /* In panel editor this points to the current unit. */ |
58 float temp_temp = 20.0; |
65 float temp_temp = 20.0; |
59 |
66 |
60 pthread_t threads[5]; |
67 pthread_t my_one_wire_thread; |
|
68 pthread_t my_devices_thread; |
|
69 pthread_t my_panel_thread; |
|
70 pthread_t my_server_thread; |
|
71 #ifdef USE_SIMULATOR |
|
72 pthread_t my_simulator_thread; |
|
73 #endif |
|
74 |
61 pthread_mutex_t mutexes[5]; |
75 pthread_mutex_t mutexes[5]; |
62 |
76 |
63 extern const char UNITMODE[5][8]; |
77 extern const char UNITMODE[5][8]; |
64 extern const char PROFSTATE[4][6]; |
78 extern const char PROFSTATE[4][6]; |
65 |
79 |
1038 mqtt_connect(); |
1052 mqtt_connect(); |
1039 |
1053 |
1040 /* |
1054 /* |
1041 * First scan the one-wire bus |
1055 * First scan the one-wire bus |
1042 */ |
1056 */ |
1043 rc = pthread_create(&threads[t], NULL, my_one_wire_loop, (void *)t ); |
1057 rc = pthread_create(&my_one_wire_thread, NULL, my_one_wire_loop, (void *)t ); |
1044 if (rc) { |
1058 if (rc) { |
1045 fprintf(stderr, "my_one_wire_loop thread didn't start rc=%d\n", rc); |
1059 fprintf(stderr, "my_one_wire_loop thread didn't start rc=%d\n", rc); |
1046 syslog(LOG_NOTICE, "my_one_wire_loop thread didn't start rc=%d", rc); |
1060 syslog(LOG_NOTICE, "my_one_wire_loop thread didn't start rc=%d", rc); |
1047 } else { |
1061 } else { |
1048 t++; |
1062 t++; |
1049 mDelay(250); /* Wait a while to detect the devices */ |
1063 mDelay(2500); /* Wait a while to detect the devices */ |
1050 } |
1064 } |
1051 |
1065 |
1052 if ((rc = devices_detect())) { |
1066 if ((rc = devices_detect())) { |
1053 syslog(LOG_NOTICE, "Detected %d new devices", rc); |
1067 syslog(LOG_NOTICE, "Detected %d new devices", rc); |
1054 wrconfig(); |
1068 wrconfig(); |
1055 } |
1069 } |
1056 |
1070 |
1057 rc = pthread_create(&threads[t], NULL, my_devices_loop, (void *)t ); |
1071 rc = pthread_create(&my_devices_thread, NULL, my_devices_loop, (void *)t ); |
1058 if (rc) { |
1072 if (rc) { |
1059 fprintf(stderr, "my_devices_loop thread didn't start rc=%d\n", rc); |
1073 fprintf(stderr, "my_devices_loop thread didn't start rc=%d\n", rc); |
1060 syslog(LOG_NOTICE, "my_devices_loop thread didn't start rc=%d", rc); |
1074 syslog(LOG_NOTICE, "my_devices_loop thread didn't start rc=%d", rc); |
1061 } else { |
1075 } else { |
1062 t++; |
1076 t++; |
1063 } |
1077 } |
1064 |
1078 |
1065 rc = pthread_create(&threads[t], NULL, my_server_loop, (void *)t ); |
1079 rc = pthread_create(&my_server_thread, NULL, my_server_loop, (void *)t ); |
1066 if (rc) { |
1080 if (rc) { |
1067 fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc); |
1081 fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc); |
1068 syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc); |
1082 syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc); |
1069 } else { |
1083 } else { |
1070 t++; |
1084 t++; |
1071 } |
1085 } |
1072 |
1086 |
1073 rc = pthread_create(&threads[t], NULL, my_panel_loop, (void *)t ); |
1087 rc = pthread_create(&my_panel_thread, NULL, my_panel_loop, (void *)t ); |
1074 if (rc) { |
1088 if (rc) { |
1075 fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc); |
1089 fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc); |
1076 syslog(LOG_NOTICE, "my_panel_loop thread didn't start rc=%d", rc); |
1090 syslog(LOG_NOTICE, "my_panel_loop thread didn't start rc=%d", rc); |
1077 } else { |
1091 } else { |
1078 t++; |
1092 t++; |
1079 } |
1093 } |
1080 |
1094 |
1081 #ifdef USE_SIMULATOR |
1095 #ifdef USE_SIMULATOR |
1082 rc = pthread_create(&threads[t], NULL, my_simulator_loop, (void *)t ); |
1096 rc = pthread_create(&my_simulator_thread, NULL, my_simulator_loop, (void *)t ); |
1083 if (rc) { |
1097 if (rc) { |
1084 fprintf(stderr, "my_simulator_loop thread didn't start rc=%d\n", rc); |
1098 fprintf(stderr, "my_simulator_loop thread didn't start rc=%d\n", rc); |
1085 syslog(LOG_NOTICE, "my_simulator_loop thread didn't start rc=%d", rc); |
1099 syslog(LOG_NOTICE, "my_simulator_loop thread didn't start rc=%d", rc); |
1086 } else { |
1100 } else { |
1087 t++; |
1101 t++; |
1923 free(unit->event_msg); |
1937 free(unit->event_msg); |
1924 unit->event_msg = NULL; |
1938 unit->event_msg = NULL; |
1925 } |
1939 } |
1926 syslog(LOG_NOTICE, "Unit `%s' stopped in mode %s", unit->alias, UNITMODE[unit->mode]); |
1940 syslog(LOG_NOTICE, "Unit `%s' stopped in mode %s", unit->alias, UNITMODE[unit->mode]); |
1927 } |
1941 } |
1928 syslog(LOG_NOTICE, "Out of loop"); |
1942 syslog(LOG_NOTICE, "Out of loop, stopping threads.."); |
1929 |
1943 |
1930 /* |
1944 /* |
1931 * Note that we don't care if the command server is stopped, this one |
1945 * Stop threads |
1932 * does almost certain keep running but that doesn't harm. |
|
1933 */ |
1946 */ |
1934 while ((my_devices_state + my_panel_state + my_simulator_state + my_one_wire_state) > 0) { sleep(1); }; |
1947 #ifdef USE_SIMULATOR |
|
1948 my_simulator_shutdown = 1; |
|
1949 while (my_simulator_state) { mDelay(50); }; |
|
1950 #endif |
|
1951 my_panel_shutdown = 1; |
|
1952 while (my_panel_state) { mDelay(50); }; |
|
1953 |
|
1954 /* |
|
1955 * Cancel command and shutdown via variable, one of them |
|
1956 * will stop this thread. Includes a failsafe. |
|
1957 */ |
|
1958 my_server_shutdown = 1; |
|
1959 rc = pthread_cancel(my_server_thread); |
|
1960 rc = 0; |
|
1961 while (my_server_state) { |
|
1962 mDelay(50); |
|
1963 if (rc++ > 20) { |
|
1964 syslog(LOG_NOTICE, "Cannot terminate my_server_loop()"); |
|
1965 break; |
|
1966 } |
|
1967 } |
|
1968 |
|
1969 my_devices_shutdown = 1; |
|
1970 while (my_devices_state) { mDelay(50); }; |
|
1971 my_one_wire_shutdown = 1; |
|
1972 while (my_one_wire_state) { mDelay(50); }; |
|
1973 |
1935 mqtt_disconnect(); |
1974 mqtt_disconnect(); |
1936 |
1975 |
1937 stopLCD(); |
1976 stopLCD(); |
1938 if (sock != -1) { |
1977 if (sock != -1) { |
1939 if (shutdown(sock, SHUT_RDWR)) { |
1978 if (shutdown(sock, SHUT_RDWR)) { |