some fixes

This commit is contained in:
2026-04-10 14:51:40 +03:00
parent 987cf022fe
commit af33a036c8
6 changed files with 92 additions and 48 deletions

View File

@@ -1,3 +1,21 @@
/*
* This file is part of the weather_proxy project.
* Copyright 2026 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "weather_data.h"
#include <stdio.h>
#include <stdlib.h>

View File

@@ -1,10 +1,28 @@
/*
* This file is part of the weather_proxy project.
* Copyright 2026 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "weather_data.h"
#include <stdio.h>
int main() {
weather_data_t wd;
if (get_weather_data(&wd) == 0) {
printf("Weather: %d, Max wind: %.1f, Wind: %.1f, Temp: %.1f; updated @%.1f\n",
printf("Weather: %d, Max wind: %.1f, Wind: %.1f, Temp: %.1f; updated @%zd\n",
wd.weather, wd.windmax, wd.wind, wd.exttemp, wd.last_update);
} else {
fprintf(stderr, "Failed to get weather data\n");

View File

@@ -1,4 +1,21 @@
#include "weather_data.h"
/*
* This file is part of the weather_proxy project.
* Copyright 2026 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -17,10 +34,11 @@
#include <time.h>
#include <stdarg.h>
#include "weather_data.h"
#define DEAD_TMOUT 15
#define RECONN_TMOUT 5
#define STAT_TMOUT 60
#define WEAT_TMOUT 5
#define WEAT_TMOUT 1
#define SHM_NAME "/weather_shm"
#define SEM_NAME "/weather_sem"
@@ -108,39 +126,36 @@ static void parse_line(const char *line, weather_data_t *data) {
char key[64];
char value[256];
if (sscanf(line, "%63[^=]=%255s", key, value) == 2) {
if (strcmp(key, "weather") == 0) {
if (strcmp(value, "good") == 0)
data->weather = WEATHER_GOOD;
else if (strcmp(value, "bad") == 0)
data->weather = WEATHER_BAD;
else if (strcmp(value, "terrible") == 0)
data->weather = WEATHER_TERRIBLE;
if (strcmp(key, "WEATHER") == 0) {
data->weather = (weather_condition_t) atoi(value);
printf("got weather: %d\n", data->weather);
} else if (strcmp(key, "Windmax") == 0) {
} else if (strcmp(key, "WINDMAX1") == 0) {
data->windmax = atof(value);
printf("got windmax: %g\n", data->windmax);
} else if (strcmp(key, "Rain") == 0) {
} else if (strcmp(key, "PRECIP") == 0) {
data->rain = atoi(value);
printf("got rain: %d\n", data->rain);
} else if (strcmp(key, "Clouds") == 0) {
} else if (strcmp(key, "CLOUDS") == 0) {
data->clouds = atof(value);
printf("got clouds: %g\n", data->clouds);
} else if (strcmp(key, "Wind") == 0) {
} else if (strcmp(key, "WIND") == 0) {
data->wind = atof(value);
printf("got wind: %g\n", data->wind);
} else if (strcmp(key, "Temperature") == 0) {
} else if (strcmp(key, "EXTTEMP") == 0) {
data->exttemp = atof(value);
printf("got temp: %g\n", data->exttemp);
} else if (strcmp(key, "Pressure") == 0) {
} else if (strcmp(key, "PRESSURE") == 0) {
data->pressure = atof(value);
printf("got pressure: %g\n", data->pressure);
} else if (strcmp(key, "Humidity") == 0) {
} else if (strcmp(key, "HUMIDITY") == 0) {
data->humidity = atof(value);
printf("got humidity: %g\n", data->humidity);
} else if (strcmp(key, "prohibited") == 0) {
} else if (strcmp(key, "PROHIBIT") == 0) {
data->prohibited = atoi(value);
} else if (strcmp(key, "Time") == 0) {
} else if (strcmp(key, "TMEAS") == 0) {
data->last_update = atof(value);
if(data->weather == WEATHER_PROHIBITED) data->prohibited = 1;
else if(data->weather < WEATHER_TERRIBLE) data->prohibited = 0;
// update all
if (sem_wait(sem) == -1) {
log_error("sem_wait failed: %s", strerror(errno));
@@ -199,15 +214,12 @@ static int opensock(const char *server_ip, int port){
}
static int request_weather_data() {
static time_t tstat = 0, tcur = 0;
static time_t tcur = 0;
char *request = "\n";
char *request = "get\n";
time_t tnow = time(NULL);
if(tnow - tcur >= WEAT_TMOUT){
tcur = tnow;
}else if(tnow - tstat >= STAT_TMOUT){
tstat = tnow;
request = "stat60\n";
}else return 1; // not now
printf("try to send request: '%s", request);
@@ -259,16 +271,10 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE);
}
//daemonize();
log_message("Starting weather daemon, server %s:%d", server_ip, port);
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = signal_handler;
sigemptyset(&sa.sa_mask);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGPIPE, SIG_IGN);
if (init_ipc() != 0) {

View File

@@ -6,20 +6,21 @@
typedef enum {
WEATHER_GOOD = 0,
WEATHER_BAD = 1,
WEATHER_TERRIBLE = 2
WEATHER_TERRIBLE = 2,
WEATHER_PROHIBITED = 3,
} weather_condition_t;
typedef struct {
weather_condition_t weather;
float windmax;
int rain;
float clouds;
float wind;
float exttemp;
float pressure;
float humidity;
int prohibited;
double last_update;
weather_condition_t weather; // conditions: field "WEATHER"
float windmax; // maximal wind for last hour, m/s: "WINDMAX1"
float wind; // current wind speed, m/s: "WIND"
float clouds; // sky "quality" (>2500 - OK): "CLOUDS"
float exttemp; // external temperature, degC: "EXTTEMP"
float pressure; // atm. pressure, mmHg: "PRESSURE"
float humidity; // humidity, percents: "HUMIDITY"
int rain; // ==1 when rainy: "PRECIP"
int prohibited; // ==1 if "weather == prohibited" or rain == 1
time_t last_update; // value of "TMEAS"
} weather_data_t;
int get_weather_data(weather_data_t *data);

View File

@@ -240,12 +240,12 @@ int format_sensval(const val_t *v, char *buf, int buflen, int Np){
// the same for measurement time formatting
int format_msrmttm(time_t t, char *buf, int buflen){
--buflen; // for trailing zero
if(!buf || buflen < FULL_LEN) return -1;
if(!buf || buflen < 1) return -1;
char cmt[COMMENT_LEN+1];
struct tm *T = localtime(&t);
strftime(cmt, COMMENT_LEN, "%F %T", T);
return snprintf(buf, buflen, "TMEAS=%zd / Last measurement time: %s", t, cmt);
int got = snprintf(buf, buflen, "TMEAS=%zd / Last measurement time: %s", t, cmt);
return (got < buflen) ? got : buflen;
}
// find sensor's value by its name; @return index or -1 if not found

View File

@@ -119,6 +119,7 @@ static void showdata(sl_sock_t *client){
sl_sock_sendbyte(client, '\n');
++nsum; Tsum += v.time;
}
DBG("nsum=%d", nsum);
if(nsum > 0){
oldest = (time_t)(Tsum / nsum);
if(0 < format_msrmttm(oldest, buf, FULL_LEN)){ // send mean measuring time