thermferm/simulator.c

changeset 714
24749c296a50
parent 660
a28ef4d9afa4
child 715
f5d85af156ab
equal deleted inserted replaced
713:ea24b4ce02b1 714:24749c296a50
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *****************************************************************************/ 21 *****************************************************************************/
22 22
23 #include "thermferm.h" 23 #include "thermferm.h"
24 #include "delay.h" 24 #include "delay.h"
25 #include "xutil.h"
26 #include "websocket.h"
25 #include "simulator.h" 27 #include "simulator.h"
26 28
27 int my_simulator_state = 0; 29 int my_simulator_state = 0;
28 30
29 #ifdef USE_SIMULATOR 31 #ifdef USE_SIMULATOR
30 32
31 extern sys_config Config; 33 extern sys_config Config;
34 extern const char DEVPRESENT[4][6];
32 35
33 int my_simulator_shutdown = 0; 36 int my_simulator_shutdown = 0;
34 int SIMcooling = 0; 37 int SIMcooling = 0;
35 int SIMheating = 0; 38 int SIMheating = 0;
36 int SIMfan = 0; 39 int SIMfan = 0;
37 int SIMlight = 0; 40 int SIMlight = 0;
41
42
43 /*
44 * Return json data for one simulator
45 */
46 char *simulator_json(simulator_list *simulator)
47 {
48 char *payload, buf[64];
49
50 payload = xstrcpy((char *)"{\"uuid\":\"");
51 payload = xstrcat(payload, simulator->uuid);
52 payload = xstrcat(payload, (char *)"\",\"name\":\"");
53 payload = xstrcat(payload, simulator->name);
54 payload = xstrcat(payload, (char *)"\",\"simno\":");
55 sprintf(buf, "%d", simulator->simno);
56 payload = xstrcat(payload, buf);
57 payload = xstrcat(payload, (char *)",\"volume_air\":");
58 sprintf(buf, "%d", simulator->volume_air);
59 payload = xstrcat(payload, buf);
60 payload = xstrcat(payload, (char *)",\"volume_beer\":");
61 sprintf(buf, "%d", simulator->volume_beer);
62 payload = xstrcat(payload, buf);
63 payload = xstrcat(payload, (char *)",\"room\":{\"tempaddress\":\"");
64 payload = xstrcat(payload, simulator->room_tempaddress);
65 payload = xstrcat(payload, (char *)"\",\"temperature\":");
66 sprintf(buf, "%.1f", simulator->room_temperature);
67 payload = xstrcat(payload, buf);
68 payload = xstrcat(payload, (char *)",\"humaddress\":\"");
69 payload = xstrcat(payload, simulator->room_humaddress);
70 payload = xstrcat(payload, (char *)"\",\"humidity\":");
71 sprintf(buf, "%.1f", simulator->room_humidity);
72 payload = xstrcat(payload, buf);
73
74 payload = xstrcat(payload, (char *)"},\"air\":{\"address\":\"");
75 payload = xstrcat(payload, simulator->air_address);
76 payload = xstrcat(payload, (char *)"\",\"temperature\":");
77 sprintf(buf, "%.4f", simulator->air_temperature);
78 payload = xstrcat(payload, buf);
79 payload = xstrcat(payload, (char *)",\"present\":\"");
80 payload = xstrcat(payload, (char *)DEVPRESENT[simulator->air_present]);
81 payload = xstrcat(payload, (char *)"\"},\"beer\":{\"address\":\"");
82
83 payload = xstrcat(payload, simulator->beer_address);
84 payload = xstrcat(payload, (char *)"\",\"temperature\":");
85 sprintf(buf, "%.4f", simulator->beer_temperature);
86 payload = xstrcat(payload, buf);
87 payload = xstrcat(payload, (char *)",\"present\":\"");
88 payload = xstrcat(payload, (char *)DEVPRESENT[simulator->beer_present]);
89 payload = xstrcat(payload, (char *)"\"},\"beer2\":{\"address\":\"");
90
91 payload = xstrcat(payload, simulator->beer_address2);
92 payload = xstrcat(payload, (char *)"\",\"temperature\":");
93 sprintf(buf, "%.4f", simulator->beer_temperature2);
94 payload = xstrcat(payload, buf);
95 payload = xstrcat(payload, (char *)",\"present\":\"");
96 payload = xstrcat(payload, (char *)DEVPRESENT[simulator->beer_present2]);
97 payload = xstrcat(payload, (char *)"\"},\"chiller\":{\"address\":\"");
98
99 payload = xstrcat(payload, simulator->chiller_address);
100 payload = xstrcat(payload, (char *)"\",\"temperature\":");
101 sprintf(buf, "%.4f", simulator->chiller_temperature);
102 payload = xstrcat(payload, buf);
103 payload = xstrcat(payload, (char *)",\"present\":\"");
104 payload = xstrcat(payload, (char *)DEVPRESENT[simulator->chiller_present]);
105 payload = xstrcat(payload, (char *)"\"},\"cooler\":{\"address\":\"");
106
107 payload = xstrcat(payload, simulator->cooler_address);
108 payload = xstrcat(payload, (char *)"\",\"temperature\":");
109 sprintf(buf, "%.4f", simulator->cooler_temp);
110 payload = xstrcat(payload, buf);
111 payload = xstrcat(payload, (char *)",\"time\":");
112 sprintf(buf, "%d", simulator->cooler_time);
113 payload = xstrcat(payload, buf);
114 payload = xstrcat(payload, (char *)",\"size\":");
115 sprintf(buf, "%.4f", simulator->cooler_size);
116 payload = xstrcat(payload, buf);
117 payload = xstrcat(payload, (char *)",\"present\":\"");
118 payload = xstrcat(payload, (char *)DEVPRESENT[simulator->cooler_present]);
119 payload = xstrcat(payload, (char *)"\",\"power\":");
120 sprintf(buf, "%d", simulator->cooler_power);
121 payload = xstrcat(payload, buf);
122 payload = xstrcat(payload, (char *)"},\"heater\":{\"address\":\"");
123
124 payload = xstrcat(payload, simulator->heater_address);
125 payload = xstrcat(payload, (char *)"\",\"temperature\":");
126 sprintf(buf, "%.4f", simulator->heater_temp);
127 payload = xstrcat(payload, buf);
128 payload = xstrcat(payload, (char *)",\"time\":");
129 sprintf(buf, "%d", simulator->heater_time);
130 payload = xstrcat(payload, buf);
131 payload = xstrcat(payload, (char *)",\"size\":");
132 sprintf(buf, "%.4f", simulator->heater_size);
133 payload = xstrcat(payload, buf);
134 payload = xstrcat(payload, (char *)",\"present\":\"");
135 payload = xstrcat(payload, (char *)DEVPRESENT[simulator->heater_present]);
136 payload = xstrcat(payload, (char *)"\",\"power\":");
137 sprintf(buf, "%d", simulator->heater_power);
138 payload = xstrcat(payload, buf);
139 payload = xstrcat(payload, (char *)"},\"fan\":{\"address\":\"");
140
141 payload = xstrcat(payload, simulator->fan_address);
142 payload = xstrcat(payload, (char *)"\",\"present\":\"");
143 payload = xstrcat(payload, (char *)DEVPRESENT[simulator->fan_present]);
144 payload = xstrcat(payload, (char *)"\",\"power\":");
145 sprintf(buf, "%d", simulator->fan_power);
146 payload = xstrcat(payload, buf);
147 payload = xstrcat(payload, (char *)"},\"light\":{\"address\":\"");
148
149 payload = xstrcat(payload, simulator->light_address);
150 payload = xstrcat(payload, (char *)"\",\"present\":\"");
151 payload = xstrcat(payload, (char *)DEVPRESENT[simulator->light_present]);
152 payload = xstrcat(payload, (char *)"\",\"power\":");
153 sprintf(buf, "%d", simulator->light_power);
154 payload = xstrcat(payload, buf);
155 payload = xstrcat(payload, (char *)"},\"frigo_isolation\":");
156
157 sprintf(buf, "%.4f", simulator->frigo_isolation);
158 payload = xstrcat(payload, buf);
159
160 payload = xstrcat(payload, (char *)",\"timestamp\":");
161 sprintf(buf, "%ld", (long)simulator->timestamp);
162 payload = xstrcat(payload, buf);
163 payload = xstrcat(payload, (char *)"}");
164
165 return payload;
166 }
167
168
169
170 void simulator_ws(void)
171 {
172 bool comma = false;
173 char *payload = NULL, *payloadu = NULL;
174 simulator_list *simulator;
175
176 payload = xstrcpy((char *)"{\"type\":\"simulator\",\"metric\":[");
177 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
178 if (comma)
179 payload = xstrcat(payload, (char *)",");
180 payloadu = simulator_json(simulator);
181 payload = xstrcat(payload, payloadu);
182 comma = true;
183 free(payloadu);
184 payloadu = NULL;
185 }
186 payload = xstrcat(payload, (char *)"]}");
187 ws_broadcast(payload);
188 free(payload);
189 payload = NULL;
190 }
191
38 192
39 193
40 void *my_simulator_loop(void *threadid) 194 void *my_simulator_loop(void *threadid)
41 { 195 {
42 simulator_list *simulator; 196 simulator_list *simulator;
57 211
58 for (;;) { 212 for (;;) {
59 if (my_simulator_shutdown) 213 if (my_simulator_shutdown)
60 break; 214 break;
61 215
62 for (simulator = Config.simulators; simulator; simulator = simulator->next) { 216 now = time(NULL);
63 if (my_simulator_shutdown) 217 if (now != last) {
64 break; 218 last = now;
65 219 /*
66 now = time(NULL); 220 * Each second
67 if (now != last) { 221 */
68 last = now; 222 seconds++;
69 /* 223
70 * Each second 224 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
71 */ 225 if (my_simulator_shutdown)
72 seconds++; 226 break;
73 227
74 /* 228 /*
75 * First, calculate temperature difference between the room and the air in the 229 * First, calculate temperature difference between the room and the air in the
76 * fridge. We use the volume air to roughly calculate the total area between 230 * fridge. We use the volume air to roughly calculate the total area between
77 * the in and outside. Calculate the effect and shift the air temperature towards 231 * the in and outside. Calculate the effect and shift the air temperature towards
78 * the room temperature. 232 * the room temperature.
79 */ 233 */
80 sqm_room_air = (cbrtl(simulator->volume_air) * cbrtl(simulator->volume_air) * 6) / 100; /* square meters all fridge sides */ 234 sqm_room_air = (cbrtl(simulator->volume_air) * cbrtl(simulator->volume_air) * 6) / 100; /* square meters all fridge sides */
81 thick_room_air = 0.04; /* 4 cm walls */ 235 thick_room_air = 0.04; /* 4 cm walls */
82 k_room_air = 0.03; /* Polystrene */ 236 k_room_air = 0.03; /* Polystrene */
83 air_heat_transfer=(k_room_air * sqm_room_air * (simulator->room_temperature - simulator->air_temperature)) / thick_room_air; 237 air_heat_transfer=(k_room_air * sqm_room_air * (simulator->room_temperature - simulator->air_temperature)) / thick_room_air;
84 air_change = (air_heat_transfer / (vhc_air * ((simulator->volume_air - simulator->volume_beer) * 1000))) / 60.0; 238 air_change = (air_heat_transfer / (vhc_air * ((simulator->volume_air - simulator->volume_beer) * 1000))) / 60.0;
85 simulator->air_temperature += air_change; 239 simulator->air_temperature += air_change;
86 240
87 /* 241 /*
115 } 269 }
116 } else { 270 } else {
117 simulator->s_cool_temp -= (simulator->s_cool_temp - simulator->air_temperature) / 25.0; 271 simulator->s_cool_temp -= (simulator->s_cool_temp - simulator->air_temperature) / 25.0;
118 } 272 }
119 273
120 /* 274 /*
121 * Calculate final temperature of the beer and the air. 275 * Calculate final temperature of the beer and the air.
122 */ 276 */
123 // Cheap trick, just follow slowly the air temp. 277 // Cheap trick, just follow slowly the air temp.
124 simulator->beer_temperature += ((simulator->air_temperature - simulator->beer_temperature) / 500.0); 278 simulator->beer_temperature += ((simulator->air_temperature - simulator->beer_temperature) / 500.0);
125 simulator->air_temperature += ((simulator->beer_temperature - simulator->air_temperature) / 2500.0); 279 simulator->air_temperature += ((simulator->beer_temperature - simulator->air_temperature) / 2500.0);

mercurial