1515#include " AES.h"
1616#include < sys/types.h>
1717#include < sys/stat.h>
18+ #include < sys/time.h>
1819#include < signal.h>
1920#include < mosquitto.h>
2021#include < thread>
2122#include < regex>
2223#include < mutex>
2324
24- #define RSCP2MQTT_VERSION " 3.30 "
25+ #define RSCP2MQTT_VERSION " 3.31 "
2526
2627#define AES_KEY_SIZE 32
2728#define AES_BLOCK_SIZE 32
@@ -93,15 +94,21 @@ void signal_handler(int sig) {
9394 go = false ;
9495}
9596
96- void wsleep (int sec ) {
97- for ( uint8_t i = 0 ; i < sec; i++ ) {
97+ void wsleep (double seconds ) {
98+ while (seconds > 0.0 ) {
9899 mtx.lock ();
99100 if (mqttRcvd || !go) {
100101 mtx.unlock ();
101102 return ;
102103 }
103104 mtx.unlock ();
104- sleep (1 );
105+ if (seconds >= 1.0 ) {
106+ sleep (1 );
107+ } else {
108+ useconds_t usec = (useconds_t )(seconds * 1e6 );
109+ if (usec > 0 ) usleep (usec);
110+ }
111+ seconds = seconds - 1.0 ;
105112 }
106113 return ;
107114}
@@ -218,6 +225,8 @@ int handleSetPower(std::vector<RSCP_MQTT::rec_cache_t> & c, uint32_t container,
218225 char cmd[12 ];
219226 char power[12 ];
220227 char modus[2 ];
228+ time_t now;
229+ time (&now);
221230
222231 if (!strcmp (payload, " auto" )) {
223232 strcpy (cycles, " 0" );
@@ -237,12 +246,12 @@ int handleSetPower(std::vector<RSCP_MQTT::rec_cache_t> & c, uint32_t container,
237246 switch (it->tag ) {
238247 case TAG_EMS_REQ_SET_POWER_MODE: {
239248 strcpy (it->payload , modus);
240- it->refresh_count = abs (atoi (cycles));
249+ it->refresh_until = abs (atoi (cycles)) * cfg. interval + now ;
241250 break ;
242251 }
243252 case TAG_EMS_REQ_SET_POWER_VALUE: {
244253 strcpy (it->payload , power);
245- it->refresh_count = abs (atoi (cycles));
254+ it->refresh_until = abs (atoi (cycles)) * cfg. interval + now ;
246255 break ;
247256 }
248257 }
@@ -411,7 +420,7 @@ void addTopic(uint32_t container, uint32_t tag, char *topic, char *unit, int for
411420}
412421
413422void addSetTopic (uint32_t container, uint32_t tag, int index, char *topic, char *regex_true, char *value_true, char *regex_false, char *value_false, int type, bool finalize) {
414- RSCP_MQTT::rec_cache_t cache = { container, tag, index, " " , " " , " 1" , " " , " 0" , " " , UNIT_NONE, type, - 1 , false , true };
423+ RSCP_MQTT::rec_cache_t cache = { container, tag, index, " " , " " , " 1" , " " , " 0" , " " , UNIT_NONE, type, 0 , false , true };
415424 strcpy (cache.topic , topic);
416425 strcpy (cache.regex_true , regex_true);
417426 strcpy (cache.value_true , value_true);
@@ -1931,10 +1940,11 @@ void createRequest(SRscpFrameBuffer * frameBuffer) {
19311940 SRscpValue ReqContainer;
19321941 int sun_mode = 0 ;
19331942 int max_current = -1 ;
1943+ time_t now;
1944+ time (&now);
19341945
19351946 for (std::vector<RSCP_MQTT::rec_cache_t >::iterator it = RSCP_MQTT::RscpMqttReceiveCache.begin (); it != RSCP_MQTT::RscpMqttReceiveCache.end (); ++it) {
1936- if ((it->done == false ) || (it->refresh_count > 0 )) {
1937- if (it->refresh_count > 0 ) it->refresh_count = it->refresh_count - 1 ;
1947+ if ((it->done == false ) || (it->refresh_until > now)) {
19381948 if (!it->container && !it->tag ) { // system call
19391949 if (!strcmp (it->topic , " set/log" ) || !strcmp (it->topic , " set/log/cache" )) logCache (RSCP_MQTT::RscpMqttCache, cfg.logfile );
19401950 if (!strcmp (it->topic , " set/log/rcache" )) logRecCache (RSCP_MQTT::RscpMqttReceiveCache, cfg.logfile );
@@ -2703,6 +2713,8 @@ static void receiveLoop(bool & bStopExecution) {
27032713static void mainLoop (void ) {
27042714 RscpProtocol protocol;
27052715 bool bStopExecution = false ;
2716+ struct timeval start, end;
2717+ double elapsed;
27062718 int countdown = 3 ;
27072719
27082720 while (go && !bStopExecution) {
@@ -2712,6 +2724,8 @@ static void mainLoop(void) {
27122724 SRscpFrameBuffer frameBuffer;
27132725 memset (&frameBuffer, 0 , sizeof (frameBuffer));
27142726
2727+ gettimeofday (&start, NULL );
2728+
27152729 // create an RSCP frame with requests to some example data
27162730 createRequest (&frameBuffer);
27172731
@@ -2847,7 +2861,10 @@ static void mainLoop(void) {
28472861 }
28482862 sleep (1 );
28492863 } else {
2850- wsleep (cfg.interval );
2864+ gettimeofday (&end, NULL );
2865+ elapsed = end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec ) * 1e-6 ;
2866+ elapsed = (double )cfg.interval - elapsed;
2867+ if (elapsed > 0.0 ) wsleep (elapsed);
28512868 }
28522869 }
28532870 return ;
@@ -3500,7 +3517,7 @@ int main(int argc, char *argv[]) {
35003517 SocketClose (iSocket);
35013518 iSocket = -1 ;
35023519
3503- wsleep (DELAY_BEFORE_RECONNECT);
3520+ wsleep (( double ) DELAY_BEFORE_RECONNECT);
35043521 }
35053522
35063523 // retain some values for a restart of the program
0 commit comments