mirror of
https://github.com/eddyem/small_tel.git
synced 2026-01-31 20:35:09 +03:00
Add some usefull functions from lib SOFA
This commit is contained in:
parent
1a7a4db46e
commit
1250d0642b
10
.gitignore
vendored
10
.gitignore
vendored
@ -19,7 +19,15 @@
|
||||
*.la
|
||||
*.lo
|
||||
|
||||
# Shared objects (inc. Windows DLLs)
|
||||
# Shared objects
|
||||
*.so
|
||||
*.so.*
|
||||
|
||||
# qt-creator
|
||||
*.config
|
||||
*.cflags
|
||||
*.cxxflags
|
||||
*.creator*
|
||||
*.files
|
||||
*.includes
|
||||
|
||||
|
||||
56
Auxiliary_utils/sofa_functions/Makefile
Normal file
56
Auxiliary_utils/sofa_functions/Makefile
Normal file
@ -0,0 +1,56 @@
|
||||
# run `make DEF=...` to add extra defines
|
||||
PROGRAM := sofa
|
||||
LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all
|
||||
LDFLAGS += -lsofa_c
|
||||
SRCS := $(wildcard *.c)
|
||||
DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111
|
||||
OBJDIR := mk
|
||||
CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu99
|
||||
OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o))
|
||||
DEPS := $(OBJS:.o=.d)
|
||||
CC = gcc
|
||||
#CXX = g++
|
||||
TARGET := RELEASE
|
||||
|
||||
all: $(OBJDIR)/RELEASE
|
||||
all: $(PROGRAM)
|
||||
release: all
|
||||
|
||||
debug: CFLAGS += -DEBUG -Werror
|
||||
debug: TARGET := DEBUG
|
||||
debug: $(OBJDIR)/DEBUG
|
||||
debug: $(PROGRAM)
|
||||
|
||||
$(OBJDIR)/DEBUG:
|
||||
@make clean
|
||||
$(OBJDIR)/RELEASE:
|
||||
@make clean
|
||||
|
||||
$(PROGRAM) : $(OBJDIR) $(OBJS)
|
||||
@echo -e "\t\tTARGET: $(TARGET)"
|
||||
@> $(OBJDIR)/$(TARGET)
|
||||
@echo -e "\t\tLD $(PROGRAM)"
|
||||
$(CC) $(LDFLAGS) $(OBJS) -o $(PROGRAM)
|
||||
|
||||
$(OBJDIR):
|
||||
@mkdir $(OBJDIR)
|
||||
|
||||
ifneq ($(MAKECMDGOALS),clean)
|
||||
-include $(DEPS)
|
||||
endif
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
@echo -e "\t\tCC $<"
|
||||
$(CC) -MD -c $(LDFLAGS) $(CFLAGS) $(DEFINES) -o $@ $<
|
||||
|
||||
clean:
|
||||
@echo -e "\t\tCLEAN"
|
||||
@rm -rf $(OBJDIR) 2>/dev/null || true
|
||||
|
||||
xclean: clean
|
||||
@rm -f $(PROGRAM)
|
||||
|
||||
gentags:
|
||||
CFLAGS="$(CFLAGS) $(DEFINES)" geany -g $(PROGRAM).c.tags *[hc] 2>/dev/null
|
||||
|
||||
.PHONY: gentags clean xclean
|
||||
1
Auxiliary_utils/sofa_functions/Readme
Normal file
1
Auxiliary_utils/sofa_functions/Readme
Normal file
@ -0,0 +1 @@
|
||||
Some usefull functions of SOFA library
|
||||
291
Auxiliary_utils/sofa_functions/sofa.c
Normal file
291
Auxiliary_utils/sofa_functions/sofa.c
Normal file
@ -0,0 +1,291 @@
|
||||
#include <sofa.h>
|
||||
#include <sofam.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
typedef struct{
|
||||
double utc1; double utc2; // UTC JD, commonly used MJD = utc1+utc2-2400000.5
|
||||
double MJD;
|
||||
double tai1; double tai2; // TAI JD
|
||||
double tt1; double tt2; // TT JD
|
||||
} sMJD;
|
||||
|
||||
// polar coordinates & equation of origins (all in radians)
|
||||
typedef struct{
|
||||
double ha; // hour angle
|
||||
double dec; // declination
|
||||
double ra; // right ascension
|
||||
double eo; // equation of origins
|
||||
} polarCrds;
|
||||
|
||||
// horizontal coordinates (all in radians)
|
||||
typedef struct{
|
||||
double az; // azimuth, 0 @ south, positive clockwise
|
||||
double zd; // zenith distance
|
||||
} horizCrds;
|
||||
|
||||
// observational place coordinates and altitude; all coordinates are in radians!
|
||||
typedef struct{
|
||||
double slong; // longitude
|
||||
double slat; // lattitude
|
||||
double salt; // altitude, m
|
||||
} placeData;
|
||||
|
||||
// place weather data
|
||||
typedef struct{
|
||||
double relhum; // rel. humidity, 0..1
|
||||
double php; // atm. pressure (hectopascales)
|
||||
double tc; // temperature, degrC
|
||||
} placeWeather;
|
||||
|
||||
// DUT/polar almanach data
|
||||
typedef struct{
|
||||
double DUT1; // UT1-UTC, sec
|
||||
double px; // polar coordinates, arcsec
|
||||
double py;
|
||||
} almDut;
|
||||
|
||||
static placeData *pldata = NULL;
|
||||
// temporal stubs for weather/place/DUT1 data; return 0 if all OK
|
||||
placeData *getPlace(){
|
||||
if(pldata) return pldata;
|
||||
pldata = malloc(sizeof(placeData));
|
||||
/* Site longitude, latitude (radians) and height above the geoid (m). */
|
||||
pldata->slong = 0.7232763200;
|
||||
pldata->slat = 0.7618977414;
|
||||
pldata->salt = 2070.0; // altitude
|
||||
return pldata;
|
||||
}
|
||||
int getWeath(placeWeather *w){
|
||||
if(!w) return 0;
|
||||
w->relhum = 0.7;
|
||||
w->tc = 1.;
|
||||
w->php = 780.;
|
||||
return 0;
|
||||
}
|
||||
int getDUT(almDut *a){
|
||||
if(!a) return 0;
|
||||
a->px = a->py = 0;
|
||||
a->DUT1 = -0.25080;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *radtodeg(double r){
|
||||
static char buf[128];
|
||||
int i[4]; char pm;
|
||||
r = iauAnpm(r);
|
||||
iauA2af (2, r, &pm, i);
|
||||
snprintf(buf, 128, "%c%02d %02d %02d.%02d", pm, i[0],i[1],i[2],i[3]);
|
||||
return buf;
|
||||
}
|
||||
|
||||
char *radtohrs(double r){
|
||||
static char buf[128];
|
||||
int i[4]; char pm;
|
||||
r = iauAnp(r);
|
||||
iauA2tf(2, r, &pm, i);
|
||||
snprintf(buf, 128, "%02d:%02d:%02d.%02d", i[0],i[1],i[2],i[3]);
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief get_MJDt - calculate MJD of date from argument
|
||||
* @param tval (i) - given date (or NULL for current)
|
||||
* @param MJD (o) - time (or NULL just to check)
|
||||
* @return 0 if all OK
|
||||
*/
|
||||
int get_MJDt(struct timeval *tval, sMJD *MJD){
|
||||
struct tm tms;
|
||||
double tSeconds;
|
||||
if(!tval){
|
||||
//DBG("MJD for current time");
|
||||
struct timeval currentTime;
|
||||
gettimeofday(¤tTime, NULL);
|
||||
gmtime_r(¤tTime.tv_sec, &tms);
|
||||
tSeconds = tms.tm_sec + ((double)currentTime.tv_usec)/1e6;
|
||||
}else{
|
||||
gmtime_r(&tval->tv_sec, &tms);
|
||||
tSeconds = tms.tm_sec + ((double)tval->tv_usec)/1e6;
|
||||
}
|
||||
int y, m, d;
|
||||
y = 1900 + tms.tm_year;
|
||||
m = tms.tm_mon + 1;
|
||||
d = tms.tm_mday;
|
||||
double utc1, utc2;
|
||||
/* UTC date. */
|
||||
if(iauDtf2d("UTC", y, m, d, tms.tm_hour, tms.tm_min, tSeconds, &utc1, &utc2) < 0) return -1;
|
||||
if(!MJD) return 0;
|
||||
MJD->MJD = utc1 - 2400000.5 + utc2;
|
||||
MJD->utc1 = utc1;
|
||||
MJD->utc2 = utc2;
|
||||
//DBG("UTC(m): %g, %.8f\n", utc1 - 2400000.5, utc2);
|
||||
if(iauUtctai(utc1, utc2, &MJD->tai1, &MJD->tai2)) return -1;
|
||||
//DBG("TAI");
|
||||
if(iauTaitt(MJD->tai1, MJD->tai2, &MJD->tt1, &MJD->tt2)) return -1;
|
||||
//DBG("TT");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief get_LST - calculate local siderial time
|
||||
* @param mjd (i) - date/time for LST (utc1 & tt used)
|
||||
* @param dUT1 - (UT1-UTC)
|
||||
* @param slong - site longitude (radians)
|
||||
* @param LST (o) - local sidereal time (radians)
|
||||
* @return 0 if all OK
|
||||
*/
|
||||
double get_LST(sMJD *mjd, double dUT1, double slong, double *LST){
|
||||
double ut11, ut12;
|
||||
if(iauUtcut1(mjd->utc1, mjd->utc2, dUT1, &ut11, &ut12)) return 1;
|
||||
double ST = iauGst06a(ut11, ut12, mjd->tt1, mjd->tt2);
|
||||
ST += slong;
|
||||
if(ST > D2PI) ST -= D2PI;
|
||||
if(LST) *LST = ST;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief hor2eq - convert horizontal coordinates to polar
|
||||
* @param h (i) - horizontal coordinates
|
||||
* @param pc (o) - polar coordinates
|
||||
* @param sidTime - sidereal time
|
||||
*/
|
||||
void hor2eq(horizCrds *h, polarCrds *pc, double sidTime){
|
||||
if(!h || !pc) return;
|
||||
placeData *p = getPlace();
|
||||
iauAe2hd(h->az, DPI/2. - h->zd, p->slat, &pc->ha, &pc->dec); // A,H -> HA,DEC; phi - site latitude
|
||||
pc->ra = sidTime - pc->ha;
|
||||
pc->eo = 0.;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief eq2horH - convert polar coordinates to horizontal
|
||||
* @param pc (i) - polar coordinates (only HA used)
|
||||
* @param h (o) - horizontal coordinates
|
||||
* @param sidTime - sidereal time
|
||||
*/
|
||||
void eq2horH(polarCrds *pc, horizCrds *h){
|
||||
if(!h || !pc) return;
|
||||
placeData *p = getPlace();
|
||||
double alt;
|
||||
iauHd2ae(pc->ha, pc->dec, p->slat, &h->az, &alt);
|
||||
h->zd = DPI/2. - alt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief eq2hor - convert polar coordinates to horizontal
|
||||
* @param pc (i) - polar coordinates (only RA used)
|
||||
* @param h (o) - horizontal coordinates
|
||||
* @param sidTime - sidereal time
|
||||
*/
|
||||
void eq2hor(polarCrds *pc, horizCrds *h, double sidTime){
|
||||
if(!h || !pc) return;
|
||||
double ha = sidTime - pc->ra + pc->eo;
|
||||
placeData *p = getPlace();
|
||||
double alt;
|
||||
iauHd2ae(ha, pc->dec, p->slat, &h->az, &alt);
|
||||
h->zd = DPI/2. - alt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief get_ObsPlace - calculate observed place (without PM etc) for given date @550nm
|
||||
* @param tval (i) - time
|
||||
* @param p2000 (i) - polar coordinates for J2000 (only ra/dec used), ICRS (catalog)
|
||||
* @param pnow (o) - polar coordinates for given epoch (or NULL)
|
||||
* @param hnow (o) - horizontal coordinates for given epoch (or NULL)
|
||||
* @return 0 if all OK
|
||||
*/
|
||||
int get_ObsPlace(struct timeval *tval, polarCrds *p2000, polarCrds *pnow, horizCrds *hnow){
|
||||
double pr = 0.0; // RA proper motion (radians/year; Note 2)
|
||||
double pd = 0.0; // Dec proper motion (radians/year)
|
||||
double px = 0.0; // parallax (arcsec)
|
||||
double rv = 0.0; // radial velocity (km/s, positive if receding)
|
||||
sMJD MJD;
|
||||
if(get_MJDt(tval, &MJD)) return -1;
|
||||
if(!p2000) return -1;
|
||||
placeData *p = getPlace();
|
||||
placeWeather w;
|
||||
almDut d;
|
||||
if(!p) return -1;
|
||||
if(getWeath(&w)) return -1;
|
||||
if(getDUT(&d)) return -1;
|
||||
/* Effective wavelength (microns) */
|
||||
double wl = 0.55;
|
||||
/* ICRS to observed. */
|
||||
double aob, zob, hob, dob, rob, eo;
|
||||
if(iauAtco13(p2000->ra, p2000->dec,
|
||||
pr, pd, px, rv,
|
||||
MJD.utc1, MJD.utc2,
|
||||
d.DUT1,
|
||||
p->slong, p->slat, p->salt,
|
||||
d.px, d.py,
|
||||
w.php, w.tc, w.relhum,
|
||||
wl,
|
||||
&aob, &zob,
|
||||
&hob, &dob, &rob, &eo)) return -1;
|
||||
if(pnow){
|
||||
pnow->eo = eo;
|
||||
pnow->ha = hob;
|
||||
pnow->ra = rob;
|
||||
pnow->dec = dob;
|
||||
}
|
||||
if(hnow){
|
||||
hnow->az = aob;
|
||||
hnow->zd = zob;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Y M D MJD x(arcsec) y(arcsec) UT1-UTC(sec)
|
||||
// 2020 5 15 58984 0.0986 0.4466 -0.25080
|
||||
|
||||
int main(){
|
||||
sMJD mjd;
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
if(get_MJDt(&tv, &mjd)) return 1;
|
||||
double ST;
|
||||
almDut adut;
|
||||
if(getDUT(&adut)) return 1;
|
||||
placeData *place = getPlace();
|
||||
if(!place) return 1;
|
||||
if(get_LST(&mjd, adut.DUT1, place->slong, &ST)) return 1;
|
||||
printf("ST = %s\n", radtohrs(ST));
|
||||
horizCrds htest = {.az = DD2R*91., .zd = DPI/4.}; // Z=45degr, A=1degr from south to west
|
||||
printf("hzd=%g\n", htest.zd);
|
||||
polarCrds ptest;
|
||||
hor2eq(&htest, &ptest, ST);
|
||||
printf("A=%s, ", radtodeg(htest.az));
|
||||
printf("Z=%s; ", radtodeg(htest.zd));
|
||||
printf("HOR->EQ: HA=%s, ", radtohrs(ptest.ha));
|
||||
printf("RA=%s, ", radtohrs(ptest.ra));
|
||||
printf("DEC=%s\n", radtodeg(ptest.dec));
|
||||
horizCrds h2;
|
||||
eq2hor(&ptest, &h2, ST);
|
||||
printf("Back conversion EQ->HOR: A=%s, ", radtodeg(h2.az));
|
||||
printf("Z=%s\n", radtodeg(h2.zd));
|
||||
polarCrds pnow;
|
||||
if(!get_ObsPlace(&tv, &ptest, &pnow, &h2)){
|
||||
printf("\nApparent place, RA=%s, ", radtohrs(pnow.ra-pnow.eo));
|
||||
printf("HA=%s, ", radtohrs(pnow.ha));
|
||||
printf("ST-RA=%s, ", radtohrs(ST-pnow.ra+pnow.eo));
|
||||
printf("DEC=%s; ", radtodeg(pnow.dec));
|
||||
printf("A=%s, ", radtodeg(h2.az));
|
||||
printf("Z=%s\n", radtodeg(h2.zd));
|
||||
polarCrds h2p;
|
||||
hor2eq(&h2, &h2p, ST);
|
||||
printf("\tHOR->EQ: RA=%s, ", radtohrs(h2p.ra-h2p.eo));
|
||||
printf("HA=%s, ", radtohrs(h2p.ha));
|
||||
printf("ST-RA=%s, ", radtohrs(ST-h2p.ra+h2p.eo));
|
||||
printf("DEC=%s\n", radtodeg(h2p.dec));
|
||||
eq2hor(&pnow, &h2, ST);
|
||||
//eq2horH(&pnow, &h2);
|
||||
printf("\tEQ->HOR: A=%s, ", radtodeg(h2.az));
|
||||
printf("Z=%s\n", radtodeg(h2.zd));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
1
Auxiliary_utils/sofa_functions/sofa.cflags
Normal file
1
Auxiliary_utils/sofa_functions/sofa.cflags
Normal file
@ -0,0 +1 @@
|
||||
-std=c17
|
||||
2
Auxiliary_utils/sofa_functions/sofa.config
Normal file
2
Auxiliary_utils/sofa_functions/sofa.config
Normal file
@ -0,0 +1,2 @@
|
||||
// Add predefined macros for your project here. For example:
|
||||
// #define THE_ANSWER 42
|
||||
1
Auxiliary_utils/sofa_functions/sofa.creator
Normal file
1
Auxiliary_utils/sofa_functions/sofa.creator
Normal file
@ -0,0 +1 @@
|
||||
[General]
|
||||
165
Auxiliary_utils/sofa_functions/sofa.creator.user
Normal file
165
Auxiliary_utils/sofa_functions/sofa.creator.user
Normal file
@ -0,0 +1,165 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE QtCreatorProject>
|
||||
<!-- Written by QtCreator 4.10.1, 2020-05-19T00:12:06. -->
|
||||
<qtcreator>
|
||||
<data>
|
||||
<variable>EnvironmentId</variable>
|
||||
<value type="QByteArray">{7bd84e39-ca37-46d3-be9d-99ebea85bc0d}</value>
|
||||
</data>
|
||||
<data>
|
||||
<variable>ProjectExplorer.Project.ActiveTarget</variable>
|
||||
<value type="int">0</value>
|
||||
</data>
|
||||
<data>
|
||||
<variable>ProjectExplorer.Project.EditorSettings</variable>
|
||||
<valuemap type="QVariantMap">
|
||||
<value type="bool" key="EditorConfiguration.AutoIndent">true</value>
|
||||
<value type="bool" key="EditorConfiguration.AutoSpacesForTabs">false</value>
|
||||
<value type="bool" key="EditorConfiguration.CamelCaseNavigation">true</value>
|
||||
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.0">
|
||||
<value type="QString" key="language">Cpp</value>
|
||||
<valuemap type="QVariantMap" key="value">
|
||||
<value type="QByteArray" key="CurrentPreferences">CppGlobal</value>
|
||||
</valuemap>
|
||||
</valuemap>
|
||||
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.1">
|
||||
<value type="QString" key="language">QmlJS</value>
|
||||
<valuemap type="QVariantMap" key="value">
|
||||
<value type="QByteArray" key="CurrentPreferences">QmlJSGlobal</value>
|
||||
</valuemap>
|
||||
</valuemap>
|
||||
<value type="int" key="EditorConfiguration.CodeStyle.Count">2</value>
|
||||
<value type="QByteArray" key="EditorConfiguration.Codec">KOI8-R</value>
|
||||
<value type="bool" key="EditorConfiguration.ConstrainTooltips">false</value>
|
||||
<value type="int" key="EditorConfiguration.IndentSize">4</value>
|
||||
<value type="bool" key="EditorConfiguration.KeyboardTooltips">false</value>
|
||||
<value type="int" key="EditorConfiguration.MarginColumn">80</value>
|
||||
<value type="bool" key="EditorConfiguration.MouseHiding">true</value>
|
||||
<value type="bool" key="EditorConfiguration.MouseNavigation">true</value>
|
||||
<value type="int" key="EditorConfiguration.PaddingMode">1</value>
|
||||
<value type="bool" key="EditorConfiguration.ScrollWheelZooming">true</value>
|
||||
<value type="bool" key="EditorConfiguration.ShowMargin">false</value>
|
||||
<value type="int" key="EditorConfiguration.SmartBackspaceBehavior">0</value>
|
||||
<value type="bool" key="EditorConfiguration.SmartSelectionChanging">true</value>
|
||||
<value type="bool" key="EditorConfiguration.SpacesForTabs">true</value>
|
||||
<value type="int" key="EditorConfiguration.TabKeyBehavior">0</value>
|
||||
<value type="int" key="EditorConfiguration.TabSize">8</value>
|
||||
<value type="bool" key="EditorConfiguration.UseGlobal">true</value>
|
||||
<value type="int" key="EditorConfiguration.Utf8BomBehavior">1</value>
|
||||
<value type="bool" key="EditorConfiguration.addFinalNewLine">true</value>
|
||||
<value type="bool" key="EditorConfiguration.cleanIndentation">false</value>
|
||||
<value type="bool" key="EditorConfiguration.cleanWhitespace">true</value>
|
||||
<value type="bool" key="EditorConfiguration.inEntireDocument">false</value>
|
||||
</valuemap>
|
||||
</data>
|
||||
<data>
|
||||
<variable>ProjectExplorer.Project.PluginSettings</variable>
|
||||
<valuemap type="QVariantMap">
|
||||
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey"/>
|
||||
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
|
||||
</valuemap>
|
||||
</data>
|
||||
<data>
|
||||
<variable>ProjectExplorer.Project.Target.0</variable>
|
||||
<valuemap type="QVariantMap">
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{65a14f9e-e008-4c1b-89df-4eaa4774b6e3}</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
|
||||
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/Big/Data/00__Small_tel/C-sources/sofa</value>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
|
||||
<valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets">
|
||||
<value type="QString">all</value>
|
||||
</valuelist>
|
||||
<value type="bool" key="GenericProjectManager.GenericMakeStep.Clean">false</value>
|
||||
<value type="QString" key="GenericProjectManager.GenericMakeStep.MakeArguments"></value>
|
||||
<value type="QString" key="GenericProjectManager.GenericMakeStep.MakeCommand"></value>
|
||||
<value type="bool" key="GenericProjectManager.GenericMakeStep.OverrideMakeflags">false</value>
|
||||
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Сборка</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Сборка</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
|
||||
</valuemap>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
|
||||
<valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets">
|
||||
<value type="QString">clean</value>
|
||||
</valuelist>
|
||||
<value type="bool" key="GenericProjectManager.GenericMakeStep.Clean">false</value>
|
||||
<value type="QString" key="GenericProjectManager.GenericMakeStep.MakeArguments"></value>
|
||||
<value type="QString" key="GenericProjectManager.GenericMakeStep.MakeCommand"></value>
|
||||
<value type="bool" key="GenericProjectManager.GenericMakeStep.OverrideMakeflags">false</value>
|
||||
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Сборка</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Очистка</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
|
||||
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
|
||||
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">По умолчанию</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">По умолчанию</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericBuildConfiguration</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.Target.BuildConfigurationCount">1</value>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
|
||||
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">0</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Развёртывание</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">1</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Конфигурация развёртывания</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.Target.DeployConfigurationCount">1</value>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.Target.PluginSettings"/>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0">
|
||||
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
|
||||
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
|
||||
<value type="QString" key="ProjectExplorer.CustomExecutableRunConfiguration.Executable"></value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Особая программа</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
|
||||
<value type="QString" key="RunConfiguration.Arguments"></value>
|
||||
<value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>
|
||||
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
|
||||
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
|
||||
<value type="bool" key="RunConfiguration.UseMultiProcess">false</value>
|
||||
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
|
||||
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
|
||||
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
|
||||
<value type="QString" key="RunConfiguration.WorkingDirectory.default"></value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
|
||||
</valuemap>
|
||||
</data>
|
||||
<data>
|
||||
<variable>ProjectExplorer.Project.TargetCount</variable>
|
||||
<value type="int">1</value>
|
||||
</data>
|
||||
<data>
|
||||
<variable>ProjectExplorer.Project.Updater.FileVersion</variable>
|
||||
<value type="int">22</value>
|
||||
</data>
|
||||
<data>
|
||||
<variable>Version</variable>
|
||||
<value type="int">22</value>
|
||||
</data>
|
||||
</qtcreator>
|
||||
1
Auxiliary_utils/sofa_functions/sofa.cxxflags
Normal file
1
Auxiliary_utils/sofa_functions/sofa.cxxflags
Normal file
@ -0,0 +1 @@
|
||||
-std=c++17
|
||||
1
Auxiliary_utils/sofa_functions/sofa.files
Normal file
1
Auxiliary_utils/sofa_functions/sofa.files
Normal file
@ -0,0 +1 @@
|
||||
sofa.c
|
||||
0
Auxiliary_utils/sofa_functions/sofa.includes
Normal file
0
Auxiliary_utils/sofa_functions/sofa.includes
Normal file
@ -45,7 +45,7 @@ void radtodeg(double r){
|
||||
#define REP(a,b,c)
|
||||
#endif
|
||||
|
||||
// temporal stubs for weather/place data; return 0 if all OK
|
||||
// temporal stubs for weather/place/DUT1 data; return 0 if all OK
|
||||
int getPlace(placeData *p){
|
||||
if(!p) return 0;
|
||||
/* Site longitude, latitude (radians) and height above the geoid (m). */
|
||||
@ -63,7 +63,7 @@ int getWeath(placeWeather *w){
|
||||
if(!w) return 0;
|
||||
w->relhum = 0.7;
|
||||
w->tc = 1.;
|
||||
w->php = 78.; // temporary, to fix bug of firmware
|
||||
w->php = 780.;
|
||||
return 0;
|
||||
}
|
||||
int getDUT(almDut *a){
|
||||
@ -75,7 +75,7 @@ int getDUT(almDut *a){
|
||||
/**
|
||||
* @brief get_MJDt - calculate MJD of date from argument
|
||||
* @param tval (i) - given date (or NULL for current)
|
||||
* @param MJD (o) - time
|
||||
* @param MJD (o) - time (or NULL just to check)
|
||||
* @return 0 if all OK
|
||||
*/
|
||||
int get_MJDt(struct timeval *tval, sMJD *MJD){
|
||||
@ -113,7 +113,7 @@ int get_MJDt(struct timeval *tval, sMJD *MJD){
|
||||
/**
|
||||
* @brief get_ObsPlace - calculate observed place (without PM etc) for given date @550nm
|
||||
* @param tval (i) - time
|
||||
* @param p2000 (i) - polar coordinates for J2000 (only ra/dec used)
|
||||
* @param p2000 (i) - polar coordinates for J2000 (only ra/dec used), ICRS (catalog)
|
||||
* @param pnow (o) - polar coordinates for given epoch (or NULL)
|
||||
* @param hnow (o) - horizontal coordinates for given epoch (or NULL)
|
||||
* @return 0 if all OK
|
||||
@ -171,3 +171,96 @@ int get_ObsPlace(struct timeval *tval, polarCrds *p2000, polarCrds *pnow, horizC
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
// azimuth: north=zero, east=90deg
|
||||
|
||||
// parallactic angle: iauHd2pa ( ha, dec, phi );
|
||||
|
||||
// refraction coefficients: iauRefco
|
||||
|
||||
// iauAe2hd ( az, el, phi, &ha, &dec ); A,H -> HA,DEC; phi - site latitude
|
||||
// iauHd2ae ( ha, dec, phi, &az, &el ); HA,DEC -> A,H
|
||||
|
||||
// iauAtoc13 - obs->ICRS(catalog)
|
||||
// iauAtoi13 - obs->CIRS
|
||||
|
||||
// iauAtio13 - CIRS->observed
|
||||
|
||||
#if 0
|
||||
/**
|
||||
* convert geocentric coordinates (nowadays, CIRS) to mean (JD2000, ICRS)
|
||||
* appRA, appDecl in seconds
|
||||
* r, d in seconds
|
||||
*/
|
||||
void JnowtoJ2000(double appRA, double appDecl, double *r, double *dc){
|
||||
double ra=0., dec=0., utc1, utc2, tai1, tai2, tt1, tt2, fd, eo, ri;
|
||||
int y, m, d, H, M;
|
||||
DBG("appRa: %g'', appDecl'': %g", appRA, appDecl);
|
||||
appRA *= DS2R;
|
||||
appDecl *= DAS2R;
|
||||
#define SOFA(f, ...) do{if(f(__VA_ARGS__)){WARNX("Error in " #f); goto rtn;}}while(0)
|
||||
// 1. convert system JDate to UTC
|
||||
SOFA(iauJd2cal, JDate, 0., &y, &m, &d, &fd);
|
||||
fd *= 24.;
|
||||
H = (int)fd;
|
||||
fd = (fd - H)*60.;
|
||||
M = (int)fd;
|
||||
fd = (fd - M)*60.;
|
||||
SOFA(iauDtf2d, "UTC", y, m, d, H, M, fd, &utc1, &utc2);
|
||||
SOFA(iauUtctai, utc1, utc2, &tai1, &tai2);
|
||||
SOFA(iauTaitt, tai1, tai2, &tt1, &tt2);
|
||||
iauAtic13(appRA, appDecl, tt1, tt2, &ri, &dec, &eo);
|
||||
ra = iauAnp(ri + eo);
|
||||
ra *= DR2S;
|
||||
dec *= DR2AS;
|
||||
DBG("SOFA: r=%g'', d=%g''", ra, dec);
|
||||
#undef SOFA
|
||||
rtn:
|
||||
if(r) *r = ra;
|
||||
if(dc) *dc = dec;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief J2000toJnow - convert ra/dec between epochs
|
||||
* @param in - J2000 (degrees)
|
||||
* @param out - Jnow (degrees)
|
||||
* @return
|
||||
*/
|
||||
int J2000toJnow(const polar *in, polar *out){
|
||||
if(!out) return 1;
|
||||
double utc1, utc2;
|
||||
time_t tsec;
|
||||
struct tm *ts;
|
||||
tsec = time(0); // number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC)
|
||||
ts = gmtime(&tsec);
|
||||
int result = 0;
|
||||
result = iauDtf2d ( "UTC", ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec, &utc1, &utc2 );
|
||||
if (result != 0) {
|
||||
fprintf(stderr, "iauDtf2d call failed\n");
|
||||
return 1;
|
||||
}
|
||||
// Make TT julian date for Atci13 call
|
||||
double tai1, tai2;
|
||||
double tt1, tt2;
|
||||
result = iauUtctai(utc1, utc2, &tai1, &tai2);
|
||||
if(result){
|
||||
fprintf(stderr, "iauUtctai call failed\n");
|
||||
return 1;
|
||||
}
|
||||
result = iauTaitt(tai1, tai2, &tt1, &tt2);
|
||||
if(result){
|
||||
fprintf(stderr, "iauTaitt call failed\n");
|
||||
return 1;
|
||||
}
|
||||
double pr = 0.0; // RA proper motion (radians/year; Note 2)
|
||||
double pd = 0.0; // Dec proper motion (radians/year)
|
||||
double px = 0.0; // parallax (arcsec)
|
||||
double rv = 0.0; // radial velocity (km/s, positive if receding)
|
||||
double rc = DD2R * in->ra, dc = DD2R * in->dec; // convert into radians
|
||||
double ri, di, eo;
|
||||
iauAtci13(rc, dc, pr, pd, px, rv, tt1, tt2, &ri, &di, &eo);
|
||||
out->ra = iauAnp(ri - eo) * DR2D;
|
||||
out->dec = di * DR2D;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -53,10 +53,8 @@ volatile int global_quit = 0;
|
||||
// quit by signal
|
||||
void signals(int sig){
|
||||
signal(sig, SIG_IGN);
|
||||
unlink(GP->crdsfile); // remove header file
|
||||
if(childpid){ // parent process
|
||||
restore_console();
|
||||
restore_tty();
|
||||
restore_tty(); // restore all parameters
|
||||
unlink(GP->pidfile); // and remove pidfile
|
||||
}
|
||||
DBG("Get signal %d, quit.\n", sig);
|
||||
@ -190,7 +188,12 @@ int setCoords(double ra, double dec){
|
||||
return pointfunction(ra, dec);
|
||||
}
|
||||
|
||||
// return 1 if all OK
|
||||
/**
|
||||
* @brief proc_data - process data received from Stellarium
|
||||
* @param data - raw data
|
||||
* @param len - its length
|
||||
* @return 1 if all OK
|
||||
*/
|
||||
int proc_data(uint8_t *data, ssize_t len){
|
||||
FNAME();
|
||||
if(len != sizeof(indata)){
|
||||
@ -315,7 +318,7 @@ static void *hdrthread(_U_ void *buf){
|
||||
// write FITS-header at most once per second
|
||||
while(!global_quit){
|
||||
wrhdr();
|
||||
usleep(1000); // give a chanse to write/read for others
|
||||
usleep(1000); // give a chance to write/read for others
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -141,7 +141,11 @@ static int makecorr(){
|
||||
ret = 1;
|
||||
}
|
||||
DBG("curtime: %s", write_cmd(":GUDT#", ibuff));
|
||||
placeWeather w;
|
||||
sprintf(buf, ":SREF0#"); // turn off 2-coord guiding & refraction
|
||||
write_cmd(buf, ibuff);
|
||||
sprintf(buf, ":Sdat0#"); // turn off dual-axis tracking
|
||||
write_cmd(buf, ibuff);
|
||||
/*placeWeather w;
|
||||
if(getWeath(&w)) putlog("Can't determine weather data");
|
||||
else{ // set refraction model data
|
||||
snprintf(buf, 64, ":SRPRS%.1f#", w.php);
|
||||
@ -152,7 +156,7 @@ static int makecorr(){
|
||||
ans = write_cmd(buf, ibuff);
|
||||
if(!ans || *ans != '1') putlog("Can't set temperature data of refraction model");
|
||||
else putlog("Correct temperature to %g", w.tc);
|
||||
}
|
||||
}*/
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -388,7 +392,7 @@ static void getplace(){
|
||||
|
||||
static const char *statuses[12] = {
|
||||
[0] = "'Tracking'",
|
||||
[1] = "'Stoped or homing'",
|
||||
[1] = "'Stopped or homing'",
|
||||
[2] = "'Slewing to park'",
|
||||
[3] = "'Unparking'",
|
||||
[4] = "'Slewing to home'",
|
||||
@ -446,6 +450,7 @@ void wrhdr(){
|
||||
}
|
||||
failcounter = 0;
|
||||
tlast = time(NULL);
|
||||
// check it here, not in the beginning of function - to check connection with mount first
|
||||
if(!hdname){
|
||||
DBG("hdname not given!");
|
||||
return;
|
||||
@ -467,19 +472,19 @@ void wrhdr(){
|
||||
mountstatus = atoi(ans);
|
||||
//DBG("Status: %d", mountstatus);
|
||||
}
|
||||
#define WRHDR(k, v, c) do{if(printhdr(hdrfd, k, v, c)){close(hdrfd); return;}}while(0)
|
||||
int l = strlen(hdname) + 7;
|
||||
char *aname = MALLOC(char, l);
|
||||
snprintf(aname, l, "%sXXXXXX", hdname);
|
||||
int fd = mkstemp(aname);
|
||||
if(fd < 0){
|
||||
WARN("Can't write header file: mkstemp()");
|
||||
FREE(aname);
|
||||
FREE(jd); FREE(lst); FREE(date); FREE(pS);
|
||||
return;
|
||||
}
|
||||
fchmod(fd, 0644);
|
||||
char val[22];
|
||||
if(unlink(hdname) && errno != ENOENT){ // can't unlink existng file
|
||||
WARN("unlink(%s)", hdname);
|
||||
FREE(jd); FREE(lst); FREE(date); FREE(pS);
|
||||
return;
|
||||
}
|
||||
int hdrfd = open(hdname, O_WRONLY | O_TRUNC | O_CREAT, 0644);
|
||||
if(hdrfd < 0){
|
||||
WARN("Can't open %s", hdname);
|
||||
FREE(jd); FREE(lst); FREE(date); FREE(pS);
|
||||
return;
|
||||
}
|
||||
#define WRHDR(k, v, c) do{if(printhdr(fd, k, v, c)){goto returning;}}while(0)
|
||||
WRHDR("TIMESYS", "'UTC'", "Time system");
|
||||
WRHDR("ORIGIN", "'SAO RAS'", "Organization responsible for the data");
|
||||
WRHDR("TELESCOP", "'Astrosib-500'", "Telescope name");
|
||||
@ -508,10 +513,13 @@ void wrhdr(){
|
||||
if(latitude) WRHDR("LATITUDE", latitude, "Geo latitude of site (south negative)");
|
||||
if(lst) WRHDR("LSTEND", lst, "Local sidereal time of observations end");
|
||||
if(date) WRHDR("DATE-END", date, "Date (UTC) of observations end");
|
||||
FREE(jd); FREE(lst); FREE(date); FREE(pS);
|
||||
// WRHDR("", , "");
|
||||
#undef WRHDR
|
||||
close(hdrfd);
|
||||
returning:
|
||||
FREE(jd); FREE(lst); FREE(date); FREE(pS);
|
||||
close(fd);
|
||||
rename(aname, hdname);
|
||||
FREE(aname);
|
||||
}
|
||||
|
||||
// terminal thread: allows to work with terminal through socket
|
||||
|
||||
@ -393,9 +393,9 @@ int str2double(double *num, const char *str){
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
FILE *Flog = NULL; // log file descriptor
|
||||
char *logname = NULL;
|
||||
time_t log_open_time = 0;
|
||||
static FILE *Flog = NULL; // log file descriptor
|
||||
static char *logname = NULL;
|
||||
// static time_t log_open_time = 0;
|
||||
/**
|
||||
* Try to open log file
|
||||
* if failed show warning message
|
||||
@ -411,8 +411,7 @@ void openlogfile(char *name){
|
||||
WARN(_("Can't open log file"));
|
||||
return;
|
||||
}
|
||||
DBG("here");
|
||||
log_open_time = time(NULL);
|
||||
// log_open_time = time(NULL);
|
||||
logname = name;
|
||||
}
|
||||
|
||||
@ -422,7 +421,7 @@ void openlogfile(char *name){
|
||||
int putlog(const char *fmt, ...){
|
||||
if(!Flog) return 0;
|
||||
time_t t_now = time(NULL);
|
||||
if(t_now - log_open_time > 86400){ // rotate log
|
||||
/*if(t_now - log_open_time > 86400){ // rotate log
|
||||
fprintf(Flog, "\n\t\t%sRotate log\n", ctime(&t_now));
|
||||
fclose(Flog);
|
||||
char newname[PATH_MAX];
|
||||
@ -430,7 +429,7 @@ int putlog(const char *fmt, ...){
|
||||
if(rename(logname, newname)) WARN("rename()");
|
||||
openlogfile(logname);
|
||||
if(!Flog) return 0;
|
||||
}
|
||||
}*/
|
||||
int i = fprintf(Flog, "\n\t\t%s", ctime(&t_now));
|
||||
va_list ar;
|
||||
va_start(ar, fmt);
|
||||
|
||||
@ -22,17 +22,20 @@
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h> // wait
|
||||
#include <sys/prctl.h> //prctl
|
||||
#include <time.h>
|
||||
#include "cmdlnopts.h"
|
||||
#include "socket.h"
|
||||
|
||||
// dome @ /dev/ttyS2
|
||||
|
||||
glob_pars *GP;
|
||||
static pid_t childpid = 0;
|
||||
|
||||
void signals(int signo){
|
||||
restore_console();
|
||||
if(childpid){ // parent process
|
||||
restore_tty();
|
||||
putlog("exit with status %d", signo);
|
||||
}
|
||||
exit(signo);
|
||||
}
|
||||
|
||||
@ -56,13 +59,16 @@ int main(int argc, char **argv){
|
||||
if(daemon(1, 0)){
|
||||
ERR("daemon()");
|
||||
}
|
||||
time_t lastd = 0;
|
||||
while(1){ // guard for dead processes
|
||||
pid_t childpid = fork();
|
||||
childpid = fork();
|
||||
if(childpid){
|
||||
putlog("create child with PID %d\n", childpid);
|
||||
DBG("Created child with PID %d\n", childpid);
|
||||
wait(NULL);
|
||||
time_t t = time(NULL);
|
||||
if(t - lastd > 600) // at least 10 minutes of work
|
||||
putlog("child %d died\n", childpid);
|
||||
lastd = t;
|
||||
WARNX("Child %d died\n", childpid);
|
||||
sleep(1);
|
||||
}else{
|
||||
@ -74,9 +80,9 @@ int main(int argc, char **argv){
|
||||
|
||||
if(GP->device) try_connect(GP->device);
|
||||
if(ping()){
|
||||
putlog("Can't write command PING");
|
||||
ERRX(_("Can't write command PING"));
|
||||
}
|
||||
putlog("Child %d connected to %s", getpid(), GP->device);
|
||||
daemonize(GP->port);
|
||||
signals(0); // newer reached
|
||||
return 0;
|
||||
|
||||
@ -390,9 +390,9 @@ int str2double(double *num, const char *str){
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
FILE *Flog = NULL; // log file descriptor
|
||||
char *logname = NULL;
|
||||
time_t log_open_time = 0;
|
||||
static FILE *Flog = NULL; // log file descriptor
|
||||
static char *logname = NULL;
|
||||
|
||||
/**
|
||||
* Try to open log file
|
||||
* if failed show warning message
|
||||
@ -407,7 +407,6 @@ void openlogfile(char *name){
|
||||
WARN(_("Can't open log file"));
|
||||
return;
|
||||
}
|
||||
log_open_time = time(NULL);
|
||||
logname = name;
|
||||
}
|
||||
|
||||
@ -417,15 +416,6 @@ void openlogfile(char *name){
|
||||
int putlog(const char *fmt, ...){
|
||||
if(!Flog) return 0;
|
||||
time_t t_now = time(NULL);
|
||||
if(t_now - log_open_time > 86400){ // rotate log
|
||||
fprintf(Flog, "\n\t\t%sRotate log\n", ctime(&t_now));
|
||||
fclose(Flog);
|
||||
char newname[PATH_MAX];
|
||||
snprintf(newname, PATH_MAX, "%s.old", logname);
|
||||
if(rename(logname, newname)) WARN("rename()");
|
||||
openlogfile(logname);
|
||||
if(!Flog) return 0;
|
||||
}
|
||||
int i = fprintf(Flog, "\n\t\t%s", ctime(&t_now));
|
||||
va_list ar;
|
||||
va_start(ar, fmt);
|
||||
|
||||
@ -22,6 +22,7 @@
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h> // wait
|
||||
#include <sys/prctl.h> //prctl
|
||||
#include <time.h> // time
|
||||
#include "cmdlnopts.h"
|
||||
#include "socket.h"
|
||||
|
||||
@ -29,10 +30,13 @@
|
||||
|
||||
glob_pars *GP;
|
||||
|
||||
pid_t childpid = 0;
|
||||
|
||||
void signals(int signo){
|
||||
restore_console();
|
||||
if(childpid){ // parent process
|
||||
restore_tty();
|
||||
putlog("exit with status %d", signo);
|
||||
}
|
||||
exit(signo);
|
||||
}
|
||||
|
||||
@ -56,13 +60,16 @@ int main(int argc, char **argv){
|
||||
if(daemon(1, 0)){
|
||||
ERR("daemon()");
|
||||
}
|
||||
time_t lastd = 0;
|
||||
while(1){ // guard for dead processes
|
||||
pid_t childpid = fork();
|
||||
childpid = fork();
|
||||
if(childpid){
|
||||
putlog("create child with PID %d\n", childpid);
|
||||
DBG("Created child with PID %d\n", childpid);
|
||||
wait(NULL);
|
||||
time_t t = time(NULL);
|
||||
if(t - lastd > 600) // at least 10 minutes of work
|
||||
putlog("child %d died\n", childpid);
|
||||
lastd = t;
|
||||
WARNX("Child %d died\n", childpid);
|
||||
sleep(1);
|
||||
}else{
|
||||
@ -74,9 +81,9 @@ int main(int argc, char **argv){
|
||||
|
||||
if(GP->device) try_connect(GP->device);
|
||||
if(ping()){
|
||||
putlog("Can't write command PING");
|
||||
ERRX(_("Can't write command PING"));
|
||||
}
|
||||
putlog("Child %d connected to %s", getpid(), GP->device);
|
||||
daemonize(GP->port);
|
||||
signals(0); // newer reached
|
||||
return 0;
|
||||
|
||||
@ -75,7 +75,6 @@ void try_connect(char *device){
|
||||
fflush(stdout);
|
||||
tty_init(device);
|
||||
read_tty(tmpbuf, 4096); // clear rbuf
|
||||
putlog("Connected to %s", device);
|
||||
DBG("connected");
|
||||
}
|
||||
|
||||
|
||||
@ -390,9 +390,8 @@ int str2double(double *num, const char *str){
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
FILE *Flog = NULL; // log file descriptor
|
||||
char *logname = NULL;
|
||||
time_t log_open_time = 0;
|
||||
static FILE *Flog = NULL; // log file descriptor
|
||||
static char *logname = NULL;
|
||||
/**
|
||||
* Try to open log file
|
||||
* if failed show warning message
|
||||
@ -407,7 +406,6 @@ void openlogfile(char *name){
|
||||
WARN(_("Can't open log file"));
|
||||
return;
|
||||
}
|
||||
log_open_time = time(NULL);
|
||||
logname = name;
|
||||
}
|
||||
|
||||
@ -417,15 +415,6 @@ void openlogfile(char *name){
|
||||
int putlog(const char *fmt, ...){
|
||||
if(!Flog) return 0;
|
||||
time_t t_now = time(NULL);
|
||||
if(t_now - log_open_time > 86400){ // rotate log
|
||||
fprintf(Flog, "\n\t\t%sRotate log\n", ctime(&t_now));
|
||||
fclose(Flog);
|
||||
char newname[PATH_MAX];
|
||||
snprintf(newname, PATH_MAX, "%s.old", logname);
|
||||
if(rename(logname, newname)) WARN("rename()");
|
||||
openlogfile(logname);
|
||||
if(!Flog) return 0;
|
||||
}
|
||||
int i = fprintf(Flog, "\n\t\t%s", ctime(&t_now));
|
||||
va_list ar;
|
||||
va_start(ar, fmt);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user