weather support

This commit is contained in:
2026-05-15 12:15:03 +03:00
parent e57052fbb0
commit e24568b0bf
6 changed files with 51 additions and 26 deletions

View File

@@ -24,6 +24,8 @@
#include "header.h"
#include "socket.h"
#define VAL_LEN 22
static char *headername = NULL;
static char *dome_name = NULL;
@@ -66,14 +68,14 @@ void domename(const char *name){
if(name){
int l = strlen(name) + 3;
dome_name = MALLOC(char, l);
snprintf(dome_name, l-1, "'%s'", name);
snprintf(dome_name, l, "'%s'", name);
}
}
void write_header(){
if(!headername) return;
char aname[PATH_MAX];
char val[22];
char val[VAL_LEN];
#define WRHDR(k, v, c) do{if(printhdr(fd, k, v, c)){goto returning;}}while(0)
snprintf(aname, PATH_MAX-1, "%sXXXXXX", headername);
int fd = mkstemp(aname);
@@ -86,11 +88,13 @@ void write_header(){
dome_data_t st;
if(get_dome_data(&st)) WRHDR("OPERATIO", "'FORBIDDEN'", "Observations are forbidden");
if(dome_name) WRHDR("DOME", dome_name, "Dome manufacturer/name");
WRHDR("DOMESTAT", st.status, "Dome status");
WRHDR("DOMEWEAT", st.weather, "Dome weather sensor status");
snprintf(val, 21, "%d", st.errcode);
snprintf(val, VAL_LEN, "'%s'", st.status);
WRHDR("DOMESTAT", val, "Dome status");
snprintf(val, VAL_LEN, "'%s'", st.weather);
WRHDR("DOMEWEAT", val, "Dome weather sensor status");
snprintf(val, VAL_LEN, "%d", st.errcode);
WRHDR("DOMEECOD", val, "Dome error code: Rain|Watchdog|Power");
snprintf(val, 21, "%.3f", st.stattime);
snprintf(val, VAL_LEN, "%.3f", st.stattime);
char timebuf[BUFSIZ];
time_t t = (time_t) st.stattime;
struct tm *tmp;

View File

@@ -62,7 +62,7 @@ int get_dome_data(dome_data_t *d){
pthread_mutex_lock(&Dome.mutex);
*d = *((dome_data_t*)&Dome);
pthread_mutex_unlock(&Dome.mutex);
return ForbidObservations;
return (ForbidObservations || BadWeather);
}
void stopserver(){

View File

@@ -1,7 +1,7 @@
# run `make DEF=...` to add extra defines
PROGRAM := teldaemon
LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all
LDFLAGS += -lusefull_macros
LDFLAGS += -lusefull_macros -lweather
SRCS := $(wildcard *.c)
DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111
OBJDIR := mk

View File

@@ -24,6 +24,8 @@
#include "header.h"
#include "socket.h"
#define VAL_LEN 22
static char *headername = NULL;
static char *telescope_name = NULL;
static header_mask_t header_mask = {0};
@@ -68,7 +70,7 @@ void telname(const char *name){
if(name){
int l = strlen(name) + 3;
telescope_name = MALLOC(char, l);
snprintf(telescope_name, l-1, "'%s'", name);
snprintf(telescope_name, l, "'%s'", name);
}
}
@@ -88,7 +90,7 @@ const char *getheadermaskhelp(){
void write_header(){
if(!headername || !header_mask.flags) return;
char aname[PATH_MAX];
char val[22];
char val[VAL_LEN];
telstatus_t st;
#define WRHDR(k, v, c) do{if(printhdr(fd, k, v, c)){goto returning;}}while(0)
snprintf(aname, PATH_MAX-1, "%sXXXXXX", headername);
@@ -101,34 +103,35 @@ void write_header(){
if(get_telescope_data(&st)) WRHDR("OPERATIO", "'FORBIDDEN'", "Observations are forbidden");
if(header_mask.telname && telescope_name) WRHDR("TELESCOP", telescope_name, "Telescope name");
WRHDR("TELSTAT", st.status, "Telescope shutters' status");
snprintf(val, VAL_LEN, "'%s'", st.status);
WRHDR("TELSTAT", val, "Telescope shutters' status");
if(header_mask.fosuser){
snprintf(val, 21, "%d", st.focuserpos);
snprintf(val, VAL_LEN, "%d", st.focuserpos);
WRHDR("FOCUS", val, "Current focuser position");
}
if(header_mask.cooler){
snprintf(val, 21, "%d", st.cooler);
snprintf(val, VAL_LEN, "%d", st.cooler);
WRHDR("TELCOOLR", val, "Primary mirror cooler status: 0/1 (off/on)");
}
if(header_mask.heater){
snprintf(val, 21, "%d", st.heater);
snprintf(val, VAL_LEN, "%d", st.heater);
WRHDR("TELHEATR", val, "Secondary mirror heater status: 0/1 (off/on)");
}
if(header_mask.exttemp){
snprintf(val, 21, "%.1f", st.ambienttemp);
snprintf(val, VAL_LEN, "%.1f", st.ambienttemp);
WRHDR("TDOME", val, "In-dome temperature, degC");
}
if(header_mask.mirtemp){
snprintf(val, 21, "%.1f", st.mirrortemp);
snprintf(val, VAL_LEN, "%.1f", st.mirrortemp);
WRHDR("TMIRROR", val, "Mirror temperature, degC");
}
if(header_mask.meastime){
snprintf(val, 21, "%.3f", st.stattime);
snprintf(val, VAL_LEN, "%.3f", st.stattime);
char timebuf[BUFSIZ];
time_t t = (time_t) st.stattime;
struct tm *tmp;
tmp = localtime(&t);
if(!tmp || 0 == strftime(timebuf, BUFSIZ, "Measurement time (telescope): %F %T", tmp)){
if(!tmp || 0 == strftime(timebuf, BUFSIZ, "Measurement time: %F %T", tmp)){
LOGERR("localtime() returned NULL");
goto returning;
}

View File

@@ -20,12 +20,16 @@
#include <inttypes.h>
#include <pthread.h>
#include <string.h>
#include <weather_data.h>
#include "commands.h"
#include "header.h"
#include "socket.h"
#include "term.h"
// if there's no signal from weather over `WEATHER_LOST` seconds, close the dome
#define WEATHER_LOST (900.)
typedef enum{
CMD_OPEN,
CMD_CLOSE,
@@ -54,6 +58,8 @@ static tel_t telescope = {0};
// external signal "deny/allow"
static atomic_bool ForbidObservations = 0; // ==1 if all is forbidden -> close dome and not allow to open
// weather don't allow to open
static atomic_bool BadWeather = 0;
static sl_sock_t *locksock = NULL; // local server socket
@@ -75,7 +81,7 @@ bool get_telescope_data(telstatus_t *t){
pthread_mutex_lock(&telescope.mutex);
*t = *((telstatus_t*)&telescope);
pthread_mutex_unlock(&telescope.mutex);
return ForbidObservations;
return (ForbidObservations || BadWeather);
}
// send "measuret=..."
@@ -92,7 +98,7 @@ static sl_sock_hresult_e dtimeh(sl_sock_t *client, _U_ sl_sock_hitem_t *item, _U
return RESULT_SILENCE;
}
#define CHKALLOWED() do{if(ForbidObservations){pthread_mutex_unlock(&telescope.mutex); return RESULT_FAIL;}}while(0)
#define CHKALLOWED() do{if(ForbidObservations || BadWeather){pthread_mutex_unlock(&telescope.mutex); return RESULT_FAIL;}}while(0)
static sl_sock_hresult_e openh(_U_ sl_sock_t *client, _U_ sl_sock_hitem_t *item, _U_ const char *req){
pthread_mutex_lock(&telescope.mutex);
@@ -111,7 +117,6 @@ static sl_sock_hresult_e closeh(_U_ sl_sock_t *client, _U_ sl_sock_hitem_t *item
static sl_sock_hresult_e fstoph(_U_ sl_sock_t *client, _U_ sl_sock_hitem_t *item, _U_ const char *req){
pthread_mutex_lock(&telescope.mutex);
CHKALLOWED();
telescope.cmd = CMD_FOCSTOP;
pthread_mutex_unlock(&telescope.mutex);
return RESULT_OK;
@@ -151,6 +156,7 @@ static sl_sock_hresult_e statush(sl_sock_t *client, _U_ sl_sock_hitem_t *item, _
}
if(ForbidObservations) sl_sock_sendstrmessage(client, "FORBIDDEN\n");
if(BadWeather) sl_sock_sendstrmessage(client, "BADWEATHER\n");
return RESULT_SILENCE;
}
@@ -307,6 +313,7 @@ static int poll_device(){
void runserver(int isunix, const char *node, int maxclients){
char ans[ANSLEN];
int forbidden = 0;
weather_data_t weather;
if(locksock) sl_sock_delete(&locksock);
telescope.errcmd = telescope.cmd = CMD_NONE;
pthread_mutex_init(&telescope.mutex, NULL);
@@ -317,9 +324,9 @@ void runserver(int isunix, const char *node, int maxclients){
sl_sock_connhandler(locksock, connected);
sl_sock_dischandler(locksock, disconnected);
sl_sock_defmsghandler(locksock, defhandler);
double tgot = 0.;
double tgot = 0., tweather = 0.;
while(locksock && locksock->connected){
if(ForbidObservations){
if(ForbidObservations || BadWeather){
if(!forbidden){
if(term_cmdwans(TXT_CLOSE, TXT_ANS_OK, ans)) forbidden = 1;
pthread_mutex_lock(&telescope.mutex);
@@ -335,12 +342,21 @@ void runserver(int isunix, const char *node, int maxclients){
}
double tnow = sl_dtime();
if(tnow - tgot > T_INTERVAL){
if(0 == get_weather_data(&weather)){ // got OK -> check if observations are forbidden
tweather = tnow;
int bad = 0;
if((double)weather.last_update - tnow > WEATHER_LOST) bad = 1;
if(weather.forceoff || weather.rain || weather.weather > WEATHER_TERRIBLE) bad = 1;
if(bad) BadWeather = 1;
else BadWeather = 0;
}else{
if(tweather - tnow > WEATHER_LOST) BadWeather = 1; // lost weather IPC
}
if(poll_device()){
tgot = tnow;
write_header();
}
}
if(ForbidObservations) continue;
pthread_mutex_lock(&telescope.mutex);
tel_commands_t tcmd = telescope.cmd;
pthread_mutex_unlock(&telescope.mutex);
@@ -348,15 +364,16 @@ void runserver(int isunix, const char *node, int maxclients){
switch(tcmd){
case CMD_OPEN:
DBG("received command: open");
if(ForbidObservations || BadWeather) break;
if(term_cmdwans(TXT_OPEN, TXT_ANS_OK, ans)){
LOGMSG("Open dome");
LOGMSG("Open shutters");
tcmd = CMD_NONE;
}
break;
case CMD_CLOSE:
DBG("received command: close");
if(term_cmdwans(TXT_CLOSE, TXT_ANS_OK, ans)){
LOGMSG("Close dome");
LOGMSG("Close shutters");
tcmd = CMD_NONE;
}
break;

View File

@@ -1,3 +1,4 @@
// Add predefined macros for your project here. For example:
// #define THE_ANSWER 42
#define EBUG
#define _XOPEN_SOURCE 666