From c70ce5216af1bfe71daba8b22939aac1d36c74e7 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Tue, 23 May 2023 16:03:45 +0300 Subject: [PATCH] fixed some small bugs --- Daemons/weatherdaemon_newmeteo/Makefile | 2 +- Daemons/weatherdaemon_newmeteo/socket.c | 9 ++- Daemons/weatherdaemon_newmeteo/stat.c | 24 ++++---- Daemons/weatherdaemon_newmeteo/term.c | 5 +- .../weatherdaemon.creator.user | 55 ++++++++++--------- 5 files changed, 54 insertions(+), 41 deletions(-) diff --git a/Daemons/weatherdaemon_newmeteo/Makefile b/Daemons/weatherdaemon_newmeteo/Makefile index 7504aaf..1da0763 100644 --- a/Daemons/weatherdaemon_newmeteo/Makefile +++ b/Daemons/weatherdaemon_newmeteo/Makefile @@ -4,7 +4,7 @@ LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-a LDFLAGS += -flto `pkg-config --libs usefull_macros` -lm SRCS := $(wildcard *.c) DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111 -DEFINES += -DEBUG +#DEFINES += -DEBUG OBJDIR := mk CFLAGS += `pkg-config --cflags usefull_macros` -O3 -Wall -Werror -Wextra -Wno-trampolines -flto OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o)) diff --git a/Daemons/weatherdaemon_newmeteo/socket.c b/Daemons/weatherdaemon_newmeteo/socket.c index 7c927ae..e254ccb 100644 --- a/Daemons/weatherdaemon_newmeteo/socket.c +++ b/Daemons/weatherdaemon_newmeteo/socket.c @@ -200,12 +200,18 @@ static int handle_socket(int sock){ format_t format = FORMAT_CURDFULL; // text format - default for web-queries if(0 == strncmp(buff, "GET", 3)){ - DBG("GET"); + DBG("GET, buff=%s", buff); // GET web query have format GET /some.resource webquery = 1; char *slash = strchr(buff, '/'); if(slash){ found = slash + 1; + if(strncmp(found, "stat", 4) == 0){ + format = FORMAT_STATFULL; + double dt = getpar(found + 4); + if(dt < 1.) dt = 900.; + if(stat_for(dt, &wstat) < 1.) format = FORMAT_ERROR; + } char *eol = strstr(found, "HTTP"); if(eol) *eol = 0; } @@ -308,6 +314,7 @@ static void *weatherpolling(_U_ void *notused){ addtobuf(&w); pthread_mutex_unlock(&mutex); } + usleep(100000); // not more than 10 messages per second } return NULL; } diff --git a/Daemons/weatherdaemon_newmeteo/stat.c b/Daemons/weatherdaemon_newmeteo/stat.c index e41816b..82433cf 100644 --- a/Daemons/weatherdaemon_newmeteo/stat.c +++ b/Daemons/weatherdaemon_newmeteo/stat.c @@ -77,13 +77,15 @@ double stat_for(double Tsec, weatherstat_t *wstat){ double dt = 0., tlast = buf[lastidx].tmeasure; size_t startfrom = lastidx; pthread_mutex_lock(&mutex); - if(tdiffmax <= Tsec) startfrom = firstidx; // use all data - else while(dt < Tsec && startfrom != firstidx){ // search from which index we should start + if(tdiffmax <= Tsec){ // use all data + startfrom = firstidx; + dt = tdiffmax; + }else while(dt < Tsec && startfrom != firstidx){ // search from which index we should start if(startfrom == 0) startfrom = buflen - 1; else --startfrom; + dt = tlast - buf[startfrom].tmeasure; } - dt = tlast - buf[startfrom].tmeasure; - // DBG("got indexes: start=%zd, end=%zd, dt=%.2f", startfrom, lastidx, dt); + DBG("got indexes: start=%zd, end=%zd, dt=%.2f (dtiffmax=%.1f)", startfrom, lastidx, dt, tdiffmax); weather_t min = {0}, max = {0}, sum = {0}, sum2 = {0}; size_t amount = 0; memcpy(&min, &buf[lastidx], sizeof(weather_t)); @@ -100,18 +102,20 @@ double stat_for(double Tsec, weatherstat_t *wstat){ else ++south; if(++st == buflen) st = 0; } - double wminus = 0.; - if(north > 2*south) wminus = 360.; + int minus = 0; + if(north > 2*south) minus = 1; while(startfrom != lastidx){ weather_t *curw = &buf[startfrom]; #define CHK(field) do{register double d = curw->field; if(d > max.field) max.field = d; if(d < min.field) min.field = d; \ sum.field += d; sum2.field += d*d;}while(0) CHK(windspeed); - register double s = curw->windspeed, sd = (curw->winddir - wminus) * s; - if(s > max.winddir) max.winddir = s; - if(s < min.winddir) min.winddir = s; + register double dir = curw->winddir, s = curw->windspeed; + if(minus && dir > 180.) dir -= 360.; + register double sd = dir * s; // weighted sum + if(dir > max.winddir) max.winddir = dir; + if(dir < min.winddir) min.winddir = dir; sum.winddir += sd; - sum2.winddir += sd * s; // v * dir^2 + sum2.winddir += sd * dir; // v * dir^2 CHK(pressure); CHK(temperature); CHK(humidity); diff --git a/Daemons/weatherdaemon_newmeteo/term.c b/Daemons/weatherdaemon_newmeteo/term.c index b8a6f93..5b4459f 100644 --- a/Daemons/weatherdaemon_newmeteo/term.c +++ b/Daemons/weatherdaemon_newmeteo/term.c @@ -42,7 +42,6 @@ static const char *emultemplate = "0R0,S=1.9,D=217.2,P=787.7,T=10.8,H=69.0,R=31. static char *read_string(){ //static int done = 0; if(GP->emul){ - usleep(100000); strncpy(buf, emultemplate, BUFLEN); return buf; } @@ -55,14 +54,14 @@ static char *read_string(){ if((l = read_tty(ttydescr))){ strncpy(ptr, ttydescr->buf, LL); r += l; LL -= l; ptr += l; - DBG("l=%zd, r=%zd, LL=%d", l, r, LL); + //DBG("l=%zd, r=%zd, LL=%d", l, r, LL); d0 = dtime(); if(r > 2 && ptr[-1] == '\n') break; } }while(dtime() - d0 < WAIT_TMOUT && LL); if(r){ //buf[r] = 0; - DBG("buf: %s", buf); + //DBG("buf: %s", buf); return buf; } return NULL; diff --git a/Daemons/weatherdaemon_newmeteo/weatherdaemon.creator.user b/Daemons/weatherdaemon_newmeteo/weatherdaemon.creator.user index e4de2e7..fd150b7 100644 --- a/Daemons/weatherdaemon_newmeteo/weatherdaemon.creator.user +++ b/Daemons/weatherdaemon_newmeteo/weatherdaemon.creator.user @@ -1,10 +1,10 @@ - + EnvironmentId - {7bd84e39-ca37-46d3-be9d-99ebea85bc0d} + {cf63021e-ef53-49b0-b03b-2f2570cdf3b6} ProjectExplorer.Project.ActiveTarget @@ -37,45 +37,57 @@ true true 1 - true + false + false false - 0 + 1 true true 0 8 true - 1 + false + 2 true - false + true true - false + *.md, *.MD, Makefile + true + true ProjectExplorer.Project.PluginSettings - + + + true + true + Builtin.DefaultTidyAndClazy + 4 + + + + true + + ProjectExplorer.Project.Target.0 + Desktop Desktop Desktop - {65a14f9e-e008-4c1b-89df-4eaa4774b6e3} + {91347f2c-5221-46a7-80b1-0a054ca02f79} 0 0 0 - /Big/Data/00__Small_tel/C-sources/netdaemon + /home/eddy/Docs/SAO/10micron/C-sources/weatherdaemon_newmeteo all - false - - - false true GenericProjectManager.GenericMakeStep @@ -89,10 +101,6 @@ clean - false - - - false true GenericProjectManager.GenericMakeStep @@ -103,6 +111,8 @@ 2 false + + false По умолчанию GenericProjectManager.GenericBuildConfiguration @@ -121,23 +131,16 @@ ProjectExplorer.DefaultDeployConfiguration 1 - + 2 - ProjectExplorer.CustomExecutableRunConfiguration - - false - false true - false false true - - 1