Add Weather_chk

This commit is contained in:
eddyem
2020-08-27 16:28:10 +03:00
parent b0bba12a9d
commit 2c8b8d97df
11 changed files with 368 additions and 5126 deletions

View File

@@ -0,0 +1,69 @@
cmake_minimum_required(VERSION 3.0)
set(PROJ chkweather)
set(MINOR_VERSION "1")
set(MID_VERSION "0")
set(MAJOR_VERSION "0")
set(VERSION "${MAJOR_VERSION}.${MID_VERSION}.${MINOR_VERSION}")
project(${PROJ} VERSION ${PROJ_VERSION} LANGUAGES C)
#enable_language(C)
message("VER: ${VERSION}")
# default flags
set(CMAKE_C_FLAGS_RELEASE "")
set(CMAKE_C_FLAGS_DEBUG "")
set(CMAKE_C_FLAGS "-O2 -std=gnu99")
set(CMAKE_COLOR_MAKEFILE ON)
# here is one of two variants: all .c in directory or .c files in list
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SOURCES)
# cmake -DDEBUG=1 -> debugging
if(DEFINED EBUG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra -Wall -Werror -W")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra -Wall -Werror -W")
set(CMAKE_BUILD_TYPE DEBUG)
set(CMAKE_VERBOSE_MAKEFILE "ON")
add_definitions(-DEBUG)
else()
set(CMAKE_BUILD_TYPE RELEASE)
endif()
###### pkgconfig ######
# pkg-config modules (for pkg-check-modules)
set(MODULES usefull_macros)
# find packages:
find_package(PkgConfig REQUIRED)
pkg_check_modules(${PROJ} REQUIRED ${MODULES})
###### additional flags ######
#list(APPEND ${PROJ}_LIBRARIES "-lfftw3_threads")
# change wrong behaviour with install prefix
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND CMAKE_INSTALL_PREFIX MATCHES "/usr/local")
else()
message("Change default install path to /usr/local")
set(CMAKE_INSTALL_PREFIX "/usr/local")
endif()
message("Install dir prefix: ${CMAKE_INSTALL_PREFIX}")
# exe file
add_executable(${PROJ} ${SOURCES})
# -I
include_directories(${${PROJ}_INCLUDE_DIRS})
# -L
link_directories(${${PROJ}_LIBRARY_DIRS})
# -D
add_definitions(${CFLAGS} -DLOCALEDIR=\"${LOCALEDIR}\"
-DPACKAGE_VERSION=\"${VERSION}\" -DGETTEXT_PACKAGE=\"${PROJ}\"
-DMINOR_VERSION=\"${MINOR_VERSION}\" -DMID_VERSION=\"${MID_VERSION}\"
-DMAJOR_VERSION=\"${MAJOR_VESION}\")
# -l
target_link_libraries(${PROJ} ${${PROJ}_LIBRARIES} -lm)
# Installation of the program
INSTALL(TARGETS ${PROJ} DESTINATION "bin")

View File

@@ -0,0 +1,18 @@
Check meteostation parameters
===========================
Usage: chkweather [args]
Where args are:
-d, --devname=arg serial device name
-h, --help show this help
-s, --speed=arg baudrate (default: 9600)
Output:
Rain=0/1
Clouds=0/1
Return value:
0 if no rain, 1 if there's rainy

View File

@@ -0,0 +1,73 @@
/*
* This file is part of the ttyterm project.
* Copyright 2020 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 <assert.h> // assert
#include <stdio.h> // printf
#include <string.h> // memcpy
#include <usefull_macros.h>
#include "cmdlnopts.h"
/*
* here are global parameters initialisation
*/
static int help;
static glob_pars G;
// DEFAULTS
// default global parameters
glob_pars const Gdefault = {
.speed = 9600,
.ttyname = "/dev/ttyUSB0",
};
/*
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
static myoption cmdlnopts[] = {
// set 1 to param despite of its repeating number:
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
{"speed", NEED_ARG, NULL, 's', arg_int, APTR(&G.speed), _("baudrate (default: 9600)")},
{"devname", NEED_ARG, NULL, 'd', arg_string, APTR(&G.ttyname), _("serial device name")},
end_option
};
/**
* Parse command line options and return dynamically allocated structure
* to global parameters
* @param argc - copy of argc from main
* @param argv - copy of argv from main
* @return allocated structure with global parameters
*/
glob_pars *parse_args(int argc, char **argv){
void *ptr = memcpy(&G, &Gdefault, sizeof(G)); assert(ptr);
// format of help: "Usage: progname [args]\n"
change_helpstring(_("Usage: %s [args]\n\n\tWhere args are:\n"));
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(argc > 0){
WARNX("Wrong arguments:\n");
for(int i = 0; i < argc; i++)
fprintf(stderr, "\t%s\n", argv[i]);
signals(9);
}
return &G;
}

View File

@@ -0,0 +1,36 @@
/*
* This file is part of the ttyterm project.
* Copyright 2020 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/>.
*/
#pragma once
#ifndef CMDLNOPTS_H__
#define CMDLNOPTS_H__
/*
* here are some typedef's for global data
*/
typedef struct{
int speed; // baudrate
char *ttyname; // device name
} glob_pars;
// default & global parameters
extern glob_pars const Gdefault;
glob_pars *parse_args(int argc, char **argv);
#endif // CMDLNOPTS_H__

View File

@@ -0,0 +1,89 @@
/*
* This file is part of the ttyterm project.
* Copyright 2020 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 <ctype.h>
#include <signal.h>
#include <stdio.h>
#include <string.h> // strcmp
#include <usefull_macros.h>
#include "cmdlnopts.h"
#define BUFLEN 2048
/**
* @brief getpar - get parameter value
* @param string (i) - string where to search
* @param Val (o) - value found
* @param Name - parameter name
* @return 0 if found
*/
static int getpar(char *string, double *Val, char *Name){
char *p = strstr(string, Name);
if(!p) return 1;
p += strlen(Name);
DBG("search %s: %s", Name, p);
if(!Val) return 0;
char *endptr;
*Val = strtod(p, &endptr);
DBG("eptr=%s, val=%g", endptr, *Val);
if(endptr == string){
WARNX("Double value not found");
return 2;
}
return 0;
}
int main(int argc, char **argv){
glob_pars *G = NULL; // default parameters see in cmdlnopts.c
initial_setup();
G = parse_args(argc, argv);
TTY_descr *dev = new_tty(G->ttyname, G->speed, 64);
if(!dev || !(dev = tty_open(dev, 1))) return 1; // open exclusively
while(read_tty(dev)); // clear buffer
if(write_tty(dev->comfd, "?U\r\n", 3)) ERR("write_tty()");
size_t got, L = 0;
char buff[BUFLEN], *ptr = buff;
double t0 = dtime();
while(dtime() - t0 < 1.){ // timeout - 1s
got = read_tty(dev);
if(got == 0) continue;
t0 = dtime();
L += got;
if(BUFLEN > L){
strncpy(ptr, dev->buf, dev->buflen);
ptr += got;
}else break;
}
buff[L] = 0;
if(L == 0) ERRX("Got nothing from TTY");
if(strncmp(buff, "<?U>", 4)) ERRX("Wrong answer: %s", buff);
ptr = &buff[4];
for(size_t i = 4; i < L; ++i){
char c = *ptr;
if(isspace(c)) ++ptr;
}
char *eol = strchr(ptr, '\n');
if(eol) *eol = 0;
DBG("Now: %s\n", ptr);
double rain = 1., clouds = 1.;
if(!getpar(ptr, &rain, "RT")) printf("Rain=%g\n", rain);
if(!getpar(ptr, &clouds, "WK")) printf("Clouds=%g\n", clouds);
close_tty(&dev);
if(rain > 0.1) return 1;
return 0;
}