mirror of
https://github.com/eddyem/small_tel.git
synced 2026-05-07 13:27:06 +03:00
some fixes
This commit is contained in:
@@ -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 "weather_data.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|||||||
@@ -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 "weather_data.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
weather_data_t wd;
|
weather_data_t wd;
|
||||||
if (get_weather_data(&wd) == 0) {
|
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);
|
wd.weather, wd.windmax, wd.wind, wd.exttemp, wd.last_update);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Failed to get weather data\n");
|
fprintf(stderr, "Failed to get weather data\n");
|
||||||
|
|||||||
@@ -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 <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -17,10 +34,11 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#include "weather_data.h"
|
||||||
|
|
||||||
#define DEAD_TMOUT 15
|
#define DEAD_TMOUT 15
|
||||||
#define RECONN_TMOUT 5
|
#define RECONN_TMOUT 5
|
||||||
#define STAT_TMOUT 60
|
#define WEAT_TMOUT 1
|
||||||
#define WEAT_TMOUT 5
|
|
||||||
|
|
||||||
#define SHM_NAME "/weather_shm"
|
#define SHM_NAME "/weather_shm"
|
||||||
#define SEM_NAME "/weather_sem"
|
#define SEM_NAME "/weather_sem"
|
||||||
@@ -108,39 +126,36 @@ static void parse_line(const char *line, weather_data_t *data) {
|
|||||||
char key[64];
|
char key[64];
|
||||||
char value[256];
|
char value[256];
|
||||||
if (sscanf(line, "%63[^=]=%255s", key, value) == 2) {
|
if (sscanf(line, "%63[^=]=%255s", key, value) == 2) {
|
||||||
if (strcmp(key, "weather") == 0) {
|
if (strcmp(key, "WEATHER") == 0) {
|
||||||
if (strcmp(value, "good") == 0)
|
data->weather = (weather_condition_t) atoi(value);
|
||||||
data->weather = WEATHER_GOOD;
|
|
||||||
else if (strcmp(value, "bad") == 0)
|
|
||||||
data->weather = WEATHER_BAD;
|
|
||||||
else if (strcmp(value, "terrible") == 0)
|
|
||||||
data->weather = WEATHER_TERRIBLE;
|
|
||||||
printf("got weather: %d\n", data->weather);
|
printf("got weather: %d\n", data->weather);
|
||||||
} else if (strcmp(key, "Windmax") == 0) {
|
} else if (strcmp(key, "WINDMAX1") == 0) {
|
||||||
data->windmax = atof(value);
|
data->windmax = atof(value);
|
||||||
printf("got windmax: %g\n", data->windmax);
|
printf("got windmax: %g\n", data->windmax);
|
||||||
} else if (strcmp(key, "Rain") == 0) {
|
} else if (strcmp(key, "PRECIP") == 0) {
|
||||||
data->rain = atoi(value);
|
data->rain = atoi(value);
|
||||||
printf("got rain: %d\n", data->rain);
|
printf("got rain: %d\n", data->rain);
|
||||||
} else if (strcmp(key, "Clouds") == 0) {
|
} else if (strcmp(key, "CLOUDS") == 0) {
|
||||||
data->clouds = atof(value);
|
data->clouds = atof(value);
|
||||||
printf("got clouds: %g\n", data->clouds);
|
printf("got clouds: %g\n", data->clouds);
|
||||||
} else if (strcmp(key, "Wind") == 0) {
|
} else if (strcmp(key, "WIND") == 0) {
|
||||||
data->wind = atof(value);
|
data->wind = atof(value);
|
||||||
printf("got wind: %g\n", data->wind);
|
printf("got wind: %g\n", data->wind);
|
||||||
} else if (strcmp(key, "Temperature") == 0) {
|
} else if (strcmp(key, "EXTTEMP") == 0) {
|
||||||
data->exttemp = atof(value);
|
data->exttemp = atof(value);
|
||||||
printf("got temp: %g\n", data->exttemp);
|
printf("got temp: %g\n", data->exttemp);
|
||||||
} else if (strcmp(key, "Pressure") == 0) {
|
} else if (strcmp(key, "PRESSURE") == 0) {
|
||||||
data->pressure = atof(value);
|
data->pressure = atof(value);
|
||||||
printf("got pressure: %g\n", data->pressure);
|
printf("got pressure: %g\n", data->pressure);
|
||||||
} else if (strcmp(key, "Humidity") == 0) {
|
} else if (strcmp(key, "HUMIDITY") == 0) {
|
||||||
data->humidity = atof(value);
|
data->humidity = atof(value);
|
||||||
printf("got humidity: %g\n", data->humidity);
|
printf("got humidity: %g\n", data->humidity);
|
||||||
} else if (strcmp(key, "prohibited") == 0) {
|
} else if (strcmp(key, "PROHIBIT") == 0) {
|
||||||
data->prohibited = atoi(value);
|
data->prohibited = atoi(value);
|
||||||
} else if (strcmp(key, "Time") == 0) {
|
} else if (strcmp(key, "TMEAS") == 0) {
|
||||||
data->last_update = atof(value);
|
data->last_update = atof(value);
|
||||||
|
if(data->weather == WEATHER_PROHIBITED) data->prohibited = 1;
|
||||||
|
else if(data->weather < WEATHER_TERRIBLE) data->prohibited = 0;
|
||||||
// update all
|
// update all
|
||||||
if (sem_wait(sem) == -1) {
|
if (sem_wait(sem) == -1) {
|
||||||
log_error("sem_wait failed: %s", strerror(errno));
|
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 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);
|
time_t tnow = time(NULL);
|
||||||
if(tnow - tcur >= WEAT_TMOUT){
|
if(tnow - tcur >= WEAT_TMOUT){
|
||||||
tcur = tnow;
|
tcur = tnow;
|
||||||
}else if(tnow - tstat >= STAT_TMOUT){
|
|
||||||
tstat = tnow;
|
|
||||||
request = "stat60\n";
|
|
||||||
}else return 1; // not now
|
}else return 1; // not now
|
||||||
|
|
||||||
printf("try to send request: '%s", request);
|
printf("try to send request: '%s", request);
|
||||||
@@ -259,16 +271,10 @@ int main(int argc, char *argv[]) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
//daemonize();
|
|
||||||
|
|
||||||
log_message("Starting weather daemon, server %s:%d", server_ip, port);
|
log_message("Starting weather daemon, server %s:%d", server_ip, port);
|
||||||
|
|
||||||
struct sigaction sa;
|
signal(SIGTERM, signal_handler);
|
||||||
memset(&sa, 0, sizeof(sa));
|
signal(SIGINT, signal_handler);
|
||||||
sa.sa_handler = signal_handler;
|
|
||||||
sigemptyset(&sa.sa_mask);
|
|
||||||
sigaction(SIGTERM, &sa, NULL);
|
|
||||||
sigaction(SIGINT, &sa, NULL);
|
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
|
|
||||||
if (init_ipc() != 0) {
|
if (init_ipc() != 0) {
|
||||||
|
|||||||
@@ -5,21 +5,22 @@
|
|||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
WEATHER_GOOD = 0,
|
WEATHER_GOOD = 0,
|
||||||
WEATHER_BAD = 1,
|
WEATHER_BAD = 1,
|
||||||
WEATHER_TERRIBLE = 2
|
WEATHER_TERRIBLE = 2,
|
||||||
|
WEATHER_PROHIBITED = 3,
|
||||||
} weather_condition_t;
|
} weather_condition_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
weather_condition_t weather;
|
weather_condition_t weather; // conditions: field "WEATHER"
|
||||||
float windmax;
|
float windmax; // maximal wind for last hour, m/s: "WINDMAX1"
|
||||||
int rain;
|
float wind; // current wind speed, m/s: "WIND"
|
||||||
float clouds;
|
float clouds; // sky "quality" (>2500 - OK): "CLOUDS"
|
||||||
float wind;
|
float exttemp; // external temperature, degC: "EXTTEMP"
|
||||||
float exttemp;
|
float pressure; // atm. pressure, mmHg: "PRESSURE"
|
||||||
float pressure;
|
float humidity; // humidity, percents: "HUMIDITY"
|
||||||
float humidity;
|
int rain; // ==1 when rainy: "PRECIP"
|
||||||
int prohibited;
|
int prohibited; // ==1 if "weather == prohibited" or rain == 1
|
||||||
double last_update;
|
time_t last_update; // value of "TMEAS"
|
||||||
} weather_data_t;
|
} weather_data_t;
|
||||||
|
|
||||||
int get_weather_data(weather_data_t *data);
|
int get_weather_data(weather_data_t *data);
|
||||||
|
|||||||
@@ -240,12 +240,12 @@ int format_sensval(const val_t *v, char *buf, int buflen, int Np){
|
|||||||
|
|
||||||
// the same for measurement time formatting
|
// the same for measurement time formatting
|
||||||
int format_msrmttm(time_t t, char *buf, int buflen){
|
int format_msrmttm(time_t t, char *buf, int buflen){
|
||||||
--buflen; // for trailing zero
|
if(!buf || buflen < 1) return -1;
|
||||||
if(!buf || buflen < FULL_LEN) return -1;
|
|
||||||
char cmt[COMMENT_LEN+1];
|
char cmt[COMMENT_LEN+1];
|
||||||
struct tm *T = localtime(&t);
|
struct tm *T = localtime(&t);
|
||||||
strftime(cmt, COMMENT_LEN, "%F %T", 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
|
// find sensor's value by its name; @return index or -1 if not found
|
||||||
|
|||||||
@@ -119,6 +119,7 @@ static void showdata(sl_sock_t *client){
|
|||||||
sl_sock_sendbyte(client, '\n');
|
sl_sock_sendbyte(client, '\n');
|
||||||
++nsum; Tsum += v.time;
|
++nsum; Tsum += v.time;
|
||||||
}
|
}
|
||||||
|
DBG("nsum=%d", nsum);
|
||||||
if(nsum > 0){
|
if(nsum > 0){
|
||||||
oldest = (time_t)(Tsum / nsum);
|
oldest = (time_t)(Tsum / nsum);
|
||||||
if(0 < format_msrmttm(oldest, buf, FULL_LEN)){ // send mean measuring time
|
if(0 < format_msrmttm(oldest, buf, FULL_LEN)){ // send mean measuring time
|
||||||
|
|||||||
Reference in New Issue
Block a user