Wed, 01 May 2024 14:38:37 +0200
Added device_present() function to easy update device present from one-wire and simulator devices. When a simulator temperature sensor present is changed, the device table is changed too. Controlling simulator relays is now for each simulator. The simulator runs under the state machine. If something changed in the running simulator, all data is broadcasted over websocket. Completed the web editor.
thermferm/devices.c | file | annotate | diff | comparison | revisions | |
thermferm/devices.h | file | annotate | diff | comparison | revisions | |
thermferm/one-wire.c | file | annotate | diff | comparison | revisions | |
thermferm/server.c | file | annotate | diff | comparison | revisions | |
thermferm/simulator.c | file | annotate | diff | comparison | revisions | |
thermferm/simulator.h | file | annotate | diff | comparison | revisions | |
www/js/set_simulators.js | file | annotate | diff | comparison | revisions |
--- a/thermferm/devices.c Tue Apr 30 17:26:41 2024 +0200 +++ b/thermferm/devices.c Wed May 01 14:38:37 2024 +0200 @@ -41,18 +41,6 @@ extern const char DEVDIR[7][11]; -#ifdef USE_SIMULATOR - -extern int SIMcooling; -extern int SIMheating; -extern int SIMfan; -extern int SIMlight; - -#endif - - -void devices_ws(void); - /* * Since kernel version 4 there is a module, and a dtoverlay so that the @@ -389,6 +377,28 @@ } +int device_present(char *address, int present) +{ + devices_list *device; + + if (address == NULL || present < DEVPRESENT_UNDEF || present > DEVPRESENT_ERROR) { + return -1; + } + + for (device = Config.devices; device; device = device->next) { + if (! strcmp(address, device->address)) { + if (device->present != present) { + device->present = present; + device->timestamp = time(NULL); + return 1; + } + return 0; + } + } + return -1; +} + + /* * Return json data for one device. */
--- a/thermferm/devices.h Tue Apr 30 17:26:41 2024 +0200 +++ b/thermferm/devices.h Wed May 01 14:38:37 2024 +0200 @@ -1,14 +1,33 @@ #ifndef MY_DEVICES_H #define MY_DEVICES_H + +/** + * @brief Read one byte from a 1-wire device like a DS2413 + * @param address The device address string + * @param file The file in the address space + * @return The read value or negative value if error + */ int read_w1(char *address, char *file); int write_w1(char *address, char *file, uint8_t val); int device_out(char *uuid, int value); int device_in(char *uuid, int *value); +/** + * @brief Set present status + * @param address The device address string + * @param present The desired present value + * @return -1 if error. 0 if ok and 1 if ok and changed. + */ +int device_present(char *address, int present); + + char *device_json(devices_list *device); + +void devices_ws(void); + int devices_detect(void); void *my_devices_loop(void *);
--- a/thermferm/one-wire.c Tue Apr 30 17:26:41 2024 +0200 +++ b/thermferm/one-wire.c Wed May 01 14:38:37 2024 +0200 @@ -181,13 +181,7 @@ dev_w1->timestamp = time(NULL); pthread_mutex_unlock(&mutexes[LOCK_ONE_WIRE]); changed = true; - for (device = Config.devices; device; device = device->next) { - if (strcmp(dev_w1->address, device->address) == 0) { -// pthread_mutex_lock(&mutexes[LOCK_DEVICES]); - device->present = DEVPRESENT_YES; -// pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); - } - } + device_present(dev_w1->address, DEVPRESENT_YES); } break; } @@ -248,14 +242,7 @@ dev_w1->timestamp = time(NULL); pthread_mutex_unlock(&mutexes[LOCK_ONE_WIRE]); changed = true; - for (device = Config.devices; device; device = device->next) { - if (strcmp(dev_w1->address, device->address) == 0) { -// pthread_mutex_lock(&mutexes[LOCK_DEVICES]); - device->present = DEVPRESENT_NO; - syslog(LOG_NOTICE, "One-wire device %s marked device %s not present", dev_w1->address, device->address); -// pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); - } - } + device_present(dev_w1->address, DEVPRESENT_NO); } free(devfile); devfile = NULL;
--- a/thermferm/server.c Tue Apr 30 17:26:41 2024 +0200 +++ b/thermferm/server.c Wed May 01 14:38:37 2024 +0200 @@ -1284,6 +1284,7 @@ if (simulator->air_present != i) syslog(LOG_NOTICE, "Simulator %s air_present %s to %s", simulator->uuid, DEVPRESENT[simulator->air_present], DEVPRESENT[i]); simulator->air_present = i; + device_present(simulator->air_address, i); break; } } @@ -1301,6 +1302,7 @@ if (simulator->beer_present != i) syslog(LOG_NOTICE, "Simulator %s beer_present %s to %s", simulator->uuid, DEVPRESENT[simulator->beer_present], DEVPRESENT[i]); simulator->beer_present = i; + device_present(simulator->beer_address, i); break; } } @@ -1318,6 +1320,7 @@ if (simulator->beer_present2 != i) syslog(LOG_NOTICE, "Simulator %s beer_present2 %s to %s", simulator->uuid, DEVPRESENT[simulator->beer_present2], DEVPRESENT[i]); simulator->beer_present2 = i; + device_present(simulator->beer_address2, i); break; } } @@ -1335,6 +1338,7 @@ if (simulator->chiller_present != i) syslog(LOG_NOTICE, "Simulator %s chiller_present %s to %s", simulator->uuid, DEVPRESENT[simulator->chiller_present], DEVPRESENT[i]); simulator->chiller_present = i; + device_present(simulator->chiller_address, i); break; } } @@ -1366,6 +1370,7 @@ if (simulator->cooler_present != i) syslog(LOG_NOTICE, "Simulator %s cooler_present %s to %s", simulator->uuid, DEVPRESENT[simulator->cooler_present], DEVPRESENT[i]); simulator->cooler_present = i; + device_present(simulator->cooler_address, i); break; } } @@ -1404,6 +1409,7 @@ if (simulator->heater_present != i) syslog(LOG_NOTICE, "Simulator %s heater_present %s to %s", simulator->uuid, DEVPRESENT[simulator->heater_present], DEVPRESENT[i]); simulator->heater_present = i; + device_present(simulator->heater_address, i); break; } } @@ -1421,6 +1427,7 @@ if (simulator->fan_present != i) syslog(LOG_NOTICE, "Simulator %s fan_present %s to %s", simulator->uuid, DEVPRESENT[simulator->fan_present], DEVPRESENT[i]); simulator->fan_present = i; + device_present(simulator->fan_address, i); break; } } @@ -1438,6 +1445,7 @@ if (simulator->light_present != i) syslog(LOG_NOTICE, "Simulator %s light_present %s to %s", simulator->uuid, DEVPRESENT[simulator->light_present], DEVPRESENT[i]); simulator->light_present = i; + device_present(simulator->light_address, i); break; } }
--- a/thermferm/simulator.c Tue Apr 30 17:26:41 2024 +0200 +++ b/thermferm/simulator.c Wed May 01 14:38:37 2024 +0200 @@ -20,7 +20,10 @@ * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *****************************************************************************/ +extern int debug; + #include "thermferm.h" +#include "statetbl.h" #include "delay.h" #include "xutil.h" #include "websocket.h" @@ -34,10 +37,9 @@ extern const char DEVPRESENT[4][6]; int my_simulator_shutdown = 0; -int SIMcooling = 0; -int SIMheating = 0; -int SIMfan = 0; -int SIMlight = 0; + + +static int simulate(void); /* @@ -193,14 +195,43 @@ void *my_simulator_loop(void *threadid) { + my_simulator_state = 1; + syslog(LOG_NOTICE, "Thread my_simulator_loop started"); + + /* + * Run the state machine + */ + simulate(); + + syslog(LOG_NOTICE, "Thread my_simulator_loop stopped"); + my_simulator_state = 0; + return 0; +} + + +SM_DECL(simulate, (char *)"simulator") +SM_STATES + Init, + Waiting, + Run, + Websocket +SM_NAMES + (char *)"Init", + (char *)"Waiting", + (char *)"run", + (char *)"Websocket" +SM_EDECL simulator_list *simulator; time_t now, last = (time_t)0; int seconds = 0; double k_room_air, sqm_room_air, thick_room_air, air_heat_transfer; double air_change, vhc_air = 0.00121; + double air_temp, beer_temp, chiller_temp; + bool changed = false; - my_simulator_state = 1; - syslog(LOG_NOTICE, "Thread my_simulator_loop started"); +SM_START(Init) + +SM_STATE(Init) for (simulator = Config.simulators; simulator; simulator = simulator->next) { /* @@ -208,86 +239,125 @@ */ simulator->s_heat_temp = simulator->s_cool_temp = simulator->room_temperature; } + SM_PROCEED(Waiting); - for (;;) { - if (my_simulator_shutdown) - break; +SM_STATE(Waiting) - now = time(NULL); - if (now != last) { - last = now; - /* - * Each second - */ - seconds++; + if (my_simulator_shutdown) { + SM_SUCCESS; + } + now = time(NULL); + if (now != last) { + last = now; + seconds++; + SM_PROCEED(Run); + } + mDelay(50L); + +SM_STATE(Run) - for (simulator = Config.simulators; simulator; simulator = simulator->next) { - if (my_simulator_shutdown) - break; + changed = false; + for (simulator = Config.simulators; simulator; simulator = simulator->next) { + if (my_simulator_shutdown) { + SM_SUCCESS; + } + + /* + * Copy to duplicates + */ + air_temp = simulator->air_temperature; + beer_temp = simulator->beer_temperature; + chiller_temp = simulator->chiller_temperature; - /* - * First, calculate temperature difference between the room and the air in the - * fridge. We use the volume air to roughly calculate the total area between - * the in and outside. Calculate the effect and shift the air temperature towards - * the room temperature. - */ - sqm_room_air = (cbrtl(simulator->volume_air) * cbrtl(simulator->volume_air) * 6) / 100; /* square meters all fridge sides */ - thick_room_air = 0.04; /* 4 cm walls */ - k_room_air = 0.03; /* Polystrene */ - air_heat_transfer=(k_room_air * sqm_room_air * (simulator->room_temperature - simulator->air_temperature)) / thick_room_air; - air_change = (air_heat_transfer / (vhc_air * ((simulator->volume_air - simulator->volume_beer) * 1000))) / 60.0; - simulator->air_temperature += air_change; + /* + * First, calculate temperature difference between the room and the air in the + * fridge. We use the volume air to roughly calculate the total area between + * the in and outside. Calculate the effect and shift the air temperature towards + * the room temperature. + */ + sqm_room_air = (cbrtl(simulator->volume_air) * cbrtl(simulator->volume_air) * 6) / 100; /* square meters all fridge sides */ + thick_room_air = 0.04; /* 4 cm walls */ + k_room_air = 0.03; /* Polystrene */ + air_heat_transfer=(k_room_air * sqm_room_air * (simulator->room_temperature - air_temp)) / thick_room_air; + air_change = (air_heat_transfer / (vhc_air * ((simulator->volume_air - simulator->volume_beer) * 1000))) / 60.0; + air_temp += air_change; - /* - * If heating, calculate temperature of the heating plate. If heating is off but - * the plate is warmer then the air, calculate the cooling down temperature. - * Finally, calculate the new air and plate temperature. - */ - if (SIMheating) { - if (simulator->s_heat_temp < simulator->heater_temp) { - simulator->s_heat_temp += 0.05; - if (simulator->s_heat_temp > simulator->air_temperature) - simulator->air_temperature += ((simulator->s_heat_temp - simulator->air_temperature) / 100.0); - } - } else { - /* - * Follow the air temperature - */ - simulator->s_heat_temp -= (simulator->s_heat_temp - simulator->air_temperature) / 25.0; - } + /* + * If heating, calculate temperature of the heating plate. If heating is off but + * the plate is warmer then the air, calculate the cooling down temperature. + * Finally, calculate the new air and plate temperature. + */ + if (simulator->heater_present == DEVPRESENT_YES && simulator->heater_power >= 50) { + if (simulator->s_heat_temp < simulator->heater_temp) { + simulator->s_heat_temp += 0.05; + if (simulator->s_heat_temp > simulator->air_temperature) + air_temp += ((simulator->s_heat_temp - air_temp) / 100.0); + } + } else { + /* + * Follow the air temperature + */ + simulator->s_heat_temp -= (simulator->s_heat_temp - air_temp) / 25.0; + } + + /* + * If cooling, calculate temperature of the cooling plate. If cooling is off but + * the plate is colder then the air, calculate the warming up temperature. + * Finsally, calculate the new air and plate temperature. + */ + if (simulator->cooler_present == DEVPRESENT_YES && simulator->cooler_power >= 50) { + if (simulator->s_cool_temp > simulator->cooler_temp) { + simulator->s_cool_temp -= 0.05; + if (simulator->s_cool_temp < air_temp) + air_temp -= ((air_temp - simulator->s_cool_temp) / 100.0); + } + } else { + simulator->s_cool_temp -= (simulator->s_cool_temp - air_temp) / 25.0; + } - /* - * If cooling, calculate temperature of the cooling plate. If cooling is off but - * the plate is colder then the air, calculate the warming up temperature. - * Finsally, calculate the new air and plate temperature. - */ - if (SIMcooling) { - if (simulator->s_cool_temp > simulator->cooler_temp) { - simulator->s_cool_temp -= 0.05; - if (simulator->s_cool_temp < simulator->air_temperature) - simulator->air_temperature -= ((simulator->air_temperature - simulator->s_cool_temp) / 100.0); - } - } else { - simulator->s_cool_temp -= (simulator->s_cool_temp - simulator->air_temperature) / 25.0; - } + /* + * Calculate final temperature of the beer and the air. + */ + // Cheap trick, just follow slowly the air temp. + beer_temp += ((air_temp - beer_temp) / 500.0); + air_temp += ((beer_temp - air_temp) / 2500.0); + chiller_temp = simulator->cooler_temp; // Link these - /* - * Calculate final temperature of the beer and the air. - */ - // Cheap trick, just follow slowly the air temp. - simulator->beer_temperature += ((simulator->air_temperature - simulator->beer_temperature) / 500.0); - simulator->air_temperature += ((simulator->beer_temperature - simulator->air_temperature) / 2500.0); - simulator->chiller_temperature = simulator->cooler_temp; // Libk these - } - mDelay(100L); + /* + * Finally update simulated sensors with the new values. + * The devices_loop will pickup the values and sets the resolution. + */ + if (air_temp != simulator->air_temperature) { +// syslog(LOG_NOTICE, "SIM %d: air %f to %f", simulator->simno, simulator->air_temperature, air_temp); + simulator->air_temperature = air_temp; + changed = true; + } + if (beer_temp != simulator->beer_temperature) { +// syslog(LOG_NOTICE, "SIM %d: beer %f to %f", simulator->simno, simulator->beer_temperature, beer_temp); + simulator->beer_temperature = beer_temp; + changed = true; } - mDelay(50L); + if (chiller_temp != simulator->chiller_temperature) { +// syslog(LOG_NOTICE, "SIM %d: chiller %f to %f", simulator->simno, simulator->chiller_temperature, chiller_temp); + simulator->chiller_temperature = chiller_temp; + changed = true; + } } + SM_PROCEED(Websocket); + +SM_STATE(Websocket) - syslog(LOG_NOTICE, "Thread my_simulator_loop stopped"); - my_simulator_state = 0; - return 0; -} + if (my_simulator_shutdown) { + SM_SUCCESS; + } + if (changed) { + simulator_ws(); + changed = false; + } + SM_PROCEED(Waiting); + +SM_END +SM_RETURN #endif
--- a/thermferm/simulator.h Tue Apr 30 17:26:41 2024 +0200 +++ b/thermferm/simulator.h Wed May 01 14:38:37 2024 +0200 @@ -3,9 +3,23 @@ #ifdef USE_SIMULATOR +/** + * @brief Return a json string with one simulator record + * @param simulator The simulator record + * @return Json string + */ char *simulator_json(simulator_list *simulator); + + +/** + * @brief Send all simulator records to the websocket + */ void simulator_ws(void); + +/** + * @brief The simulator thread. + */ void *my_simulator_loop(void *); #endif
--- a/www/js/set_simulators.js Tue Apr 30 17:26:41 2024 +0200 +++ b/www/js/set_simulators.js Wed May 01 14:38:37 2024 +0200 @@ -271,7 +271,7 @@ modalOpacity: 0.40 }); $('#popupWindow').on('open', function() { - $('#dev_description').jqxInput('selectAll'); + $('#name').jqxInput('selectAll'); }); $('#Delete').jqxButton({ template: 'danger', width: '90px', theme: theme }); $('#Delete').click(function() { @@ -308,16 +308,30 @@ var data, row = { uuid: dataRecord.uuid, - type: $('#dev_type').val(), - direction: $('#dev_direction').val(), - value: parseInt($('#dev_value').jqxNumberInput('decimal')), - offset: parseInt($('#dev_offset').jqxNumberInput('decimal')), - present: $('#dev_present').val(), - address: $('#dev_address').val(), - subdevice: parseInt($('#dev_subdevice').jqxNumberInput('decimal')), - gpiopin: parseInt($('#dev_gpiopin').jqxNumberInput('val')), - description: $('#dev_description').val(), - comment: $('#dev_comment').val() + name: $('#name').val(), + volume_air: parseInt($('#volume_air').jqxNumberInput('decimal')), + volume_beer: parseInt($('#volume_beer').jqxNumberInput('decimal')), + room_temperature: parseFloat($('#room_temperature').jqxNumberInput('decimal')), + room_humidity: parseFloat($('#room_humidity').jqxNumberInput('decimal')), + air_present: $('#air_present').val(), + beer_present: $('#beer_present').val(), + beer_present2: $('#beer_present2').val(), + chiller_present: $('#chiller_present').val(), + cooler_temp: parseFloat($('#cooler_temp').jqxNumberInput('decimal')), + cooler_time: parseInt($('#cooler_time').jqxNumberInput('decimal')), + cooler_size: parseFloat($('#cooler_size').jqxNumberInput('decimal')), + cooler_present: $('#cooler_present').val(), + cooler_power: parseInt($('#cooler_power').jqxNumberInput('decimal')), + heater_temp: parseFloat($('#heater_temp').jqxNumberInput('decimal')), + heater_time: parseInt($('#heater_time').jqxNumberInput('decimal')), + heater_size: parseFloat($('#heater_size').jqxNumberInput('decimal')), + heater_present: $('#heater_present').val(), + heater_power: parseInt($('#heater_power').jqxNumberInput('decimal')), + fan_present: $('#fan_present').val(), + fan_power: parseInt($('#fan_power').jqxNumberInput('decimal')), + light_present: $('#light_present').val(), + light_power: parseInt($('#light_power').jqxNumberInput('decimal')), + frigo_isolation: parseFloat($('#frigo_isolation').jqxNumberInput('decimal')) }; data = 'update=true&' + $.param(row); console.log(data); @@ -333,6 +347,7 @@ alert('Error: ' + data.msg); } else { console.log('update: success'); + $('#jqxgrid').jqxGrid('updatebounddata'); } }, error: function(jqXHR, textStatus, errorThrown) {}