seems like it works

This commit is contained in:
2026-04-09 18:35:06 +03:00
parent e551b94499
commit 5be6876f9e
14 changed files with 527 additions and 265 deletions

View File

@@ -37,4 +37,9 @@ if(REINHARDT)
list(APPEND LIBS reinhardt)
endif()
if(WXA100)
add_library(wxa100 SHARED wxa100.c)
list(APPEND LIBS wxa100)
endif()
install(TARGETS ${LIBS} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

View File

@@ -19,6 +19,8 @@
#include "bta_shdata.h"
#include "weathlib.h"
#define SENSOR_NAME "BTA 6-m telescope main meteostation"
enum{
NWIND,
NHUMIDITY,
@@ -28,67 +30,66 @@ enum{
NAMOUNT
};
extern sensordata_t sensor;
static const val_t values[NAMOUNT] = {
[NWIND] = {.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_WIND},
[NHUMIDITY] = {.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_HUMIDITY},
[NAMB_TEMP] = {.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_AMB_TEMP},
[NPRESSURE] = {.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_PRESSURE},
[NPRECIP] = {.sense = VAL_OBLIGATORY, .type = VALT_UINT, .meaning = IS_PRECIP},
[NWIND] = {.sense = VAL_RECOMMENDED, .type = VALT_FLOAT, .meaning = IS_WIND},
[NHUMIDITY] = {.sense = VAL_RECOMMENDED, .type = VALT_FLOAT, .meaning = IS_HUMIDITY},
[NAMB_TEMP] = {.sense = VAL_RECOMMENDED, .type = VALT_FLOAT, .meaning = IS_AMB_TEMP},
[NPRESSURE] = {.sense = VAL_RECOMMENDED, .type = VALT_FLOAT, .meaning = IS_PRESSURE},
[NPRECIP] = {.sense = VAL_RECOMMENDED, .type = VALT_UINT, .meaning = IS_PRECIP},
};
static void *mainthread(void _U_ *U){
static void *mainthread(void *s){
FNAME();
sensordata_t *sensor = (sensordata_t *)s;
while(1){
if(check_shm_block(&sdat)){
DBG("Got next");
time_t tnow = time(NULL);
pthread_mutex_lock(&sensor.valmutex);
pthread_mutex_lock(&sensor->valmutex);
for(int i = 0; i < NAMOUNT; ++i)
sensor.values[i].time = tnow;
sensor.values[NWIND].value.f = val_Wnd;
sensor.values[NPRESSURE].value.f = val_B;
sensor.values[NAMB_TEMP].value.f = val_T1;
sensor.values[NHUMIDITY].value.f = val_Hmd;
sensor->values[i].time = tnow;
sensor->values[NWIND].value.f = val_Wnd;
sensor->values[NPRESSURE].value.f = val_B;
sensor->values[NAMB_TEMP].value.f = val_T1;
sensor->values[NHUMIDITY].value.f = val_Hmd;
DBG("Tprecip=%.1f, tnow=%.1f", Precip_time, sl_dtime());
sensor.values[NPRECIP].value.u = (tnow - (time_t)Precip_time < 60) ? 1 : 0;
pthread_mutex_unlock(&sensor.valmutex);
if(sensor.freshdatahandler) sensor.freshdatahandler(&sensor);
sensor->values[NPRECIP].value.u = (tnow - (time_t)Precip_time < 60) ? 1 : 0;
pthread_mutex_unlock(&sensor->valmutex);
if(sensor->freshdatahandler) sensor->freshdatahandler(sensor);
}else break; // no connection?
sleep(1);
}
DBG("Lost connection -> suicide");
common_kill(&sensor);
sensor->kill(sensor);
return NULL;
}
static int init(struct sensordata_t *s, int N, time_t pollt, int _U_ fd){
sensordata_t *sensor_new(int N, time_t pollt, int _U_ fd){
FNAME();
if(!s) return -1;
sensor.PluginNo = N;
sensordata_t *s = common_new();
if(!s) return NULL;
s->PluginNo = N;
if(pollt) s->tpoll = pollt;
if(!get_shm_block(&sdat, ClientSide)){
WARNX("Can't get BTA shared memory block");
return -1;
WARNX("Can't get BTA shared memory block or create main thread");
s->kill(s);
return NULL;
}
if(pthread_create(&s->thread, NULL, mainthread, NULL)) return -1;
s->values = MALLOC(val_t, NAMOUNT);
for(int i = 0; i < NAMOUNT; ++i) s->values[i] = values[i];
if(!(s->ringbuffer = sl_RB_new(BUFSIZ))){
s->Nvalues = NAMOUNT;
strncpy(s->name, SENSOR_NAME, NAME_LEN);
/*if(!(s->ringbuffer = sl_RB_new(BUFSIZ))){
WARNX("Can't init ringbuffer!");
common_kill(s);
return -1;
}*/
if(pthread_create(&s->thread, NULL, mainthread, (void*)s)){
WARN("Can't create main thread");
s->kill(s);
return NULL;
}
return NAMOUNT;
s->fdes = 0;
return s;
}
sensordata_t sensor = {
.name = "BTA 6-m telescope main meteostation",
.Nvalues = NAMOUNT,
.init = init,
.onrefresh = common_onrefresh,
.valmutex = PTHREAD_MUTEX_INITIALIZER,
.get_value = common_getval,
.kill = common_kill,
};

View File

@@ -20,9 +20,9 @@
#include "weathlib.h"
#define NS (6)
#define SENSOR_NAME "Dummy weatherstation"
extern sensordata_t sensor;
#define NS (6)
static const val_t values[NS] = { // fields `name` and `comment` have no sense until value meaning is `IS_OTHER`
{.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_WIND},
@@ -33,56 +33,55 @@ static const val_t values[NS] = { // fields `name` and `comment` have no sense u
{.sense = VAL_OBLIGATORY, .type = VALT_UINT, .meaning = IS_PRECIP},
};
static void *mainthread(void _U_ *U){
static void *mainthread(void *s){
FNAME();
double t0 = sl_dtime();
sensordata_t *sensor = (sensordata_t *)s;
while(1){
DBG("locked");
pthread_mutex_lock(&sensor.valmutex);
float f = sensor.values[0].value.f + (drand48() - 0.5) / 2.;
if(f >= 0.) sensor.values[0].value.f = f;
f = sensor.values[1].value.f + (drand48() - 0.5) * 4.;
if(f > 160. && f < 200.) sensor.values[1].value.f = f;
f = sensor.values[2].value.f + (drand48() - 0.5) / 2.;
if(f > 13. && f < 21.) sensor.values[2].value.f = f;
f = sensor.values[3].value.f + (drand48() - 0.5) / 100.;
if(f > 585. && f < 615.) sensor.values[3].value.f = f;
f = sensor.values[4].value.f + (drand48() - 0.5)*10.;
if(f > 60. && f <= 100.) sensor.values[4].value.f = f;
sensor.values[5].value.u = (f > 98.) ? 1 : 0;
pthread_mutex_lock(&sensor->valmutex);
float f = sensor->values[0].value.f + (drand48() - 0.5) / 2.;
if(f >= 0.) sensor->values[0].value.f = f;
f = sensor->values[1].value.f + (drand48() - 0.5) * 4.;
if(f > 160. && f < 200.) sensor->values[1].value.f = f;
f = sensor->values[2].value.f + (drand48() - 0.5) / 2.;
if(f > 13. && f < 21.) sensor->values[2].value.f = f;
f = sensor->values[3].value.f + (drand48() - 0.5) / 100.;
if(f > 585. && f < 615.) sensor->values[3].value.f = f;
f = sensor->values[4].value.f + (drand48() - 0.5)*10.;
if(f > 60. && f <= 100.) sensor->values[4].value.f = f;
sensor->values[5].value.u = (f > 98.) ? 1 : 0;
time_t cur = time(NULL);
for(int i = 0; i < NS; ++i) sensor.values[i].time = cur;
pthread_mutex_unlock(&sensor.valmutex);
for(int i = 0; i < NS; ++i) sensor->values[i].time = cur;
pthread_mutex_unlock(&sensor->valmutex);
DBG("unlocked");
if(sensor.freshdatahandler) sensor.freshdatahandler(&sensor);
while(sl_dtime() - t0 < sensor.tpoll) usleep(500);
if(sensor->freshdatahandler) sensor->freshdatahandler(sensor);
while(sl_dtime() - t0 < sensor->tpoll) usleep(500);
t0 = sl_dtime();
}
return NULL;
}
static int init(struct sensordata_t* s, int N, time_t pollt, int _U_ fd){
sensordata_t *sensor_new(int N, time_t pollt, int _U_ fd){
FNAME();
if(pthread_create(&s->thread, NULL, mainthread, NULL)) return 0;
sensordata_t *s = common_new();
if(!s) return NULL;
s->Nvalues = NS;
strncpy(s->name, SENSOR_NAME, NAME_LEN);
if(pollt) s->tpoll = pollt;
s->values = MALLOC(val_t, NS);
for(int i = 0; i < NS; ++i) s->values[i] = values[i];
sensor.values[0].value.f = 1.;
sensor.values[1].value.f = 180.;
sensor.values[2].value.f = 17.;
sensor.values[3].value.f = 600.;
sensor.values[4].value.f = 80.;
sensor.values[5].value.u = 0;
sensor.PluginNo = N;
return NS;
s->values[0].value.f = 1.;
s->values[1].value.f = 180.;
s->values[2].value.f = 17.;
s->values[3].value.f = 600.;
s->values[4].value.f = 80.;
s->values[5].value.u = 0;
s->PluginNo = N;
if(pthread_create(&s->thread, NULL, mainthread, (void*)s)){
s->kill(s);
return NULL;
}
s->fdes = 0;
return s;
}
sensordata_t sensor = {
.name = "Dummy weatherstation",
.Nvalues = NS,
.init = init,
.onrefresh = common_onrefresh,
.valmutex = PTHREAD_MUTEX_INITIALIZER,
.get_value = common_getval,
.kill = common_kill,
};

View File

@@ -23,10 +23,9 @@
// dummy example of file descriptors usage
#define SENSOR_NAME "Dummy socket or serial device weatherstation"
#define NS (4)
extern sensordata_t sensor;
static const val_t values[NS] = { // fields `name` and `comment` have no sense until value meaning is `IS_OTHER`
{.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_WIND},
{.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_AMB_TEMP},
@@ -34,23 +33,23 @@ static const val_t values[NS] = { // fields `name` and `comment` have no sense u
{.sense = VAL_RECOMMENDED, .type = VALT_FLOAT, .meaning = IS_HUMIDITY},
};
static int format_values(char *buf){
static int format_values(sensordata_t *sensor, char *buf){
int gotvals = 0;
char *token = strtok(buf, ",");
time_t tnow = time(NULL);
pthread_mutex_lock(&sensor->valmutex);
while(token && gotvals < NS){
double v;
DBG("TOKEN: %s", token);
if(sl_str2d(&v, token)){
DBG("next value: %g", v);
pthread_mutex_lock(&sensor.valmutex);
sensor.values[gotvals].value.f = (float) v;
sensor.values[gotvals].time = tnow;
pthread_mutex_unlock(&sensor.valmutex);
sensor->values[gotvals].value.f = (float) v;
sensor->values[gotvals].time = tnow;
++gotvals;
}
token = strtok(NULL, ",");
}
pthread_mutex_unlock(&sensor->valmutex);
DBG("GOT: %d", gotvals);
return gotvals;
}
@@ -79,72 +78,68 @@ static ssize_t writedata(int fd, const char *str, size_t size){
return sent;
}
static void *mainthread(void _U_ *U){
static void *mainthread(void *s){
FNAME();
time_t task = 0;
const char begging[] = "Enter comma-separated data: wind, exttemp, pressure, humidity\n";
char buf[128];
while(sensor.fdes > -1){
sensordata_t *sensor = (sensordata_t *)s;
while(sensor->fdes > -1){
time_t tnow = time(NULL);
int canread = sl_canread(sensor.fdes);
int canread = sl_canread(sensor->fdes);
if(canread < 0){
WARNX("Disconnected fd %d", sensor.fdes);
WARNX("Disconnected fd %d", sensor->fdes);
break;
}else if(canread == 1){
ssize_t got = read(sensor.fdes, buf, 128);
ssize_t got = read(sensor->fdes, buf, 128);
if(got > 0){
sl_RB_write(sensor.ringbuffer, (uint8_t*)buf, got);
sl_RB_write(sensor->ringbuffer, (uint8_t*)buf, got);
}else if(got < 0){
DBG("Disconnected?");
break;
}
}
if(sl_RB_readline(sensor.ringbuffer, buf, 127) > 0){
if(NS == format_values(buf) && sensor.freshdatahandler)
sensor.freshdatahandler(&sensor);
if(sl_RB_readline(sensor->ringbuffer, buf, 127) > 0){
if(NS == format_values(sensor, buf) && sensor->freshdatahandler)
sensor->freshdatahandler(sensor);
}
if(sensor.tpoll){
if(sensor->tpoll){
if(tnow >= task){
DBG("write %s", begging);
ssize_t got = writedata(sensor.fdes, begging, sizeof(begging)-1);
if(got > 0) task = tnow + sensor.tpoll;
ssize_t got = writedata(sensor->fdes, begging, sizeof(begging)-1);
if(got > 0) task = tnow + sensor->tpoll;
else if(got < 0){
close(sensor.fdes);
sensor.fdes = -1;
close(sensor->fdes);
sensor->fdes = -1;
}
}
}
}
DBG("OOOOps!");
common_kill(&sensor);
sensor->kill(sensor);
return NULL;
}
static int init(struct sensordata_t *s, int N, time_t pollt, int fd){
sensordata_t *sensor_new(int N, time_t pollt, int fd){
FNAME();
if(!s) return -1;
if(fd < 0) return NULL;
sensordata_t *s = common_new();
if(!s) return NULL;
s->fdes = fd;
if(s->fdes < 0) return -1;
sensor.PluginNo = N;
s->PluginNo = N;
if(pollt) s->tpoll = pollt;
if(pthread_create(&s->thread, NULL, mainthread, NULL)) return -1;
strncpy(s->name, SENSOR_NAME, NAME_LEN);
s->values = MALLOC(val_t, NS);
// don't use memcpy, as `values` could be aligned
for(int i = 0; i < NS; ++i) s->values[i] = values[i];
if(!(s->ringbuffer = sl_RB_new(BUFSIZ))){
WARNX("Can't init ringbuffer!");
common_kill(s);
return -1;
s->kill(s);
return NULL;
}
return NS;
if(pthread_create(&s->thread, NULL, mainthread, (void*)s)){
s->kill(s);
return NULL;
}
return s;
}
sensordata_t sensor = {
.name = "Dummy socket or serial device weatherstation",
.Nvalues = NS,
.init = init,
.onrefresh = common_onrefresh,
.valmutex = PTHREAD_MUTEX_INITIALIZER,
.get_value = common_getval,
.kill = common_kill,
};

View File

@@ -23,6 +23,8 @@
// HYDREON rain sensor
#define SENSOR_NAME "Hydreon RG-11 rain sensor"
// amount of datafields
#define RREGNUM 6
#define RGBITNUM 8
@@ -83,8 +85,6 @@ typedef struct{
uint8_t RainThr; // (??? == 12)
} slowregs;
extern sensordata_t sensor;
enum{
NPRECIP = 0,
NPRECIP_LEVEL,
@@ -146,78 +146,71 @@ static int encodepacket(const char *buf, int len, rg11 *Rregs, slowregs *Sregs){
return TRUE;
}
static void *mainthread(void _U_ *U){
static void *mainthread(void *s){
FNAME();
char buf[128];
rg11 Rregs;
slowregs Sregs;
while(sensor.fdes > -1){
sensordata_t *sensor = (sensordata_t *)s;
while(sensor->fdes > -1){
time_t tnow = time(NULL);
int canread = sl_canread(sensor.fdes);
int canread = sl_canread(sensor->fdes);
if(canread < 0){
WARNX("Disconnected fd %d", sensor.fdes);
WARNX("Disconnected fd %d", sensor->fdes);
break;
}else if(canread == 1){
ssize_t got = read(sensor.fdes, buf, 128);
ssize_t got = read(sensor->fdes, buf, 128);
if(got > 0){
//DBG("write into buffer: %s[%zd]", buf, got);
sl_RB_write(sensor.ringbuffer, (uint8_t*)buf, got);
sl_RB_write(sensor->ringbuffer, (uint8_t*)buf, got);
}else if(got < 0){
DBG("Disconnected?");
break;
}
}
int got = sl_RB_readto(sensor.ringbuffer, 's', (uint8_t*)buf, 127);
int got = sl_RB_readto(sensor->ringbuffer, 's', (uint8_t*)buf, 127);
if(got > 0){
buf[--got] = 0;
if(encodepacket(buf, got, &Rregs, &Sregs)){
DBG("refresh...");
pthread_mutex_lock(&sensor.valmutex);
pthread_mutex_lock(&sensor->valmutex);
for(int i = 0; i < NAMOUNT; ++i)
sensor.values[i].time = tnow;
sensor.values[NPRECIP].value.u = (Rregs.RGBits & (Raining | Storm)) ? 1 : 0;
sensor->values[i].time = tnow;
sensor->values[NPRECIP].value.u = (Rregs.RGBits & (Raining | Storm)) ? 1 : 0;
float f = Sregs.Barrel * 256.f + Sregs.Bucket - 14.f;
sensor.values[NPRECIP_LEVEL].value.f = (f > 0.f) ? f : 0.f;
sensor.values[NSINCERN].value.u = Sregs.SinceRn;
sensor.values[NPOW].value.u = Rregs.PeakRS;
sensor.values[NAVG].value.u = Rregs.LRA;
sensor.values[NAMBL].value.u = Sregs.AmbLight;
sensor.values[NFREEZ].value.u = (Rregs.RGBits & Freeze) ? 1 : 0;
pthread_mutex_unlock(&sensor.valmutex);
if(sensor.freshdatahandler) sensor.freshdatahandler(&sensor);
sensor->values[NPRECIP_LEVEL].value.f = (f > 0.f) ? f : 0.f;
sensor->values[NSINCERN].value.u = Sregs.SinceRn;
sensor->values[NPOW].value.u = Rregs.PeakRS;
sensor->values[NAVG].value.u = Rregs.LRA;
sensor->values[NAMBL].value.u = Sregs.AmbLight;
sensor->values[NFREEZ].value.u = (Rregs.RGBits & Freeze) ? 1 : 0;
pthread_mutex_unlock(&sensor->valmutex);
if(sensor->freshdatahandler) sensor->freshdatahandler(sensor);
}
}
}
DBG("OOOOps!");
common_kill(&sensor);
sensor->kill(sensor);
return NULL;
}
static int init(struct sensordata_t *s, int N, time_t pollt, int fd){
sensordata_t *sensor_new(int N, time_t pollt, int fd){
FNAME();
if(!s) return -1;
if(fd < 0) return NULL;
sensordata_t *s = common_new();
if(!s) return NULL;
strncpy(s->name, SENSOR_NAME, NAME_LEN);
s->fdes = fd;
if(s->fdes < 0) return -1;
sensor.PluginNo = N;
s->PluginNo = N;
s->Nvalues = NAMOUNT;
if(pollt) s->tpoll = pollt;
if(pthread_create(&s->thread, NULL, mainthread, NULL)) return -1;
s->values = MALLOC(val_t, NAMOUNT);
// don't use memcpy, as `values` could be aligned
for(int i = 0; i < NAMOUNT; ++i) s->values[i] = values[i];
if(!(s->ringbuffer = sl_RB_new(BUFSIZ))){
WARNX("Can't init ringbuffer!");
common_kill(s);
return -1;
if(!(s->ringbuffer = sl_RB_new(BUFSIZ)) ||
pthread_create(&s->thread, NULL, mainthread, (void*)s)){
s->kill(s);
return NULL;
}
return NAMOUNT;
return s;
}
sensordata_t sensor = {
.name = "Hydreon RG-11 rain sensor",
.Nvalues = NAMOUNT,
.init = init,
.onrefresh = common_onrefresh,
.valmutex = PTHREAD_MUTEX_INITIALIZER,
.get_value = common_getval,
.kill = common_kill,
};

View File

@@ -20,6 +20,8 @@
#include "weathlib.h"
#define SENSOR_NAME "Old Reinhard meteostation"
//static const char *emultemplate = "<?U> 06:50:36, 20.01.00, TE-2.20, DR1405.50, WU2057.68, RT0.00, WK1.00, WR177.80, WT-2.20, FE0.69, RE0.00, WG7.36, WV260.03, TI0.00, FI0.00,";
enum{
@@ -34,17 +36,15 @@ enum{
NAMOUNT
};
extern sensordata_t sensor;
static const val_t values[NAMOUNT] = {
[NWIND] = {.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_WIND},
[NWIND] = {.sense = VAL_RECOMMENDED, .type = VALT_FLOAT, .meaning = IS_WIND},
[NWINDDIR] = {.sense = VAL_RECOMMENDED,.type = VALT_FLOAT, .meaning = IS_WINDDIR},
[NHUMIDITY] = {.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_HUMIDITY},
[NAMB_TEMP] = {.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_AMB_TEMP},
[NPRESSURE] = {.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_PRESSURE},
[NHUMIDITY] = {.sense = VAL_BROKEN, .type = VALT_FLOAT, .meaning = IS_HUMIDITY},
[NAMB_TEMP] = {.sense = VAL_RECOMMENDED, .type = VALT_FLOAT, .meaning = IS_AMB_TEMP},
[NPRESSURE] = {.sense = VAL_BROKEN, .type = VALT_FLOAT, .meaning = IS_PRESSURE},
[NCLOUDS] = {.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_CLOUDS},
[NPRECIP] = {.sense = VAL_OBLIGATORY, .type = VALT_UINT, .meaning = IS_PRECIP},
[NPRECIPLVL]= {.sense = VAL_RECOMMENDED,.type = VALT_FLOAT, .meaning = IS_PRECIP_LEVEL},
[NPRECIP] = {.sense = VAL_RECOMMENDED, .type = VALT_UINT, .meaning = IS_PRECIP},
[NPRECIPLVL]= {.sense = VAL_UNNECESSARY,.type = VALT_FLOAT, .meaning = IS_PRECIP_LEVEL},
};
/**
@@ -70,124 +70,118 @@ static int getpar(char *string, double *Val, char *Name){
return TRUE;
}
static void *mainthread(void _U_ *U){
static void *mainthread(void *s){
FNAME();
char buf[BUFSIZ];
time_t tpoll = 0;
while(sensor.fdes > -1){
sensordata_t *sensor = (sensordata_t *)s;
while(sensor->fdes > -1){
time_t tnow = time(NULL);
if(tnow - tpoll > sensor.tpoll){
if(sl_tty_write(sensor.fdes, "?U\r\n", 4)){
if(tnow - tpoll > sensor->tpoll){
if(sl_tty_write(sensor->fdes, "?U\r\n", 4)){
WARN("Can't ask new data");
break;
}
DBG("poll @%zd, pollt=%zd", tnow, sensor.tpoll);
DBG("poll @%zd, pollt=%zd", tnow, sensor->tpoll);
tpoll = tnow;
}
int canread = sl_canread(sensor.fdes);
int canread = sl_canread(sensor->fdes);
if(canread < 0){
WARNX("Disconnected fd %d", sensor.fdes);
WARNX("Disconnected fd %d", sensor->fdes);
break;
}else if(canread == 1){
ssize_t got = read(sensor.fdes, buf, BUFSIZ);
ssize_t got = read(sensor->fdes, buf, BUFSIZ);
if(got > 0){
sl_RB_write(sensor.ringbuffer, (uint8_t*)buf, got);
sl_RB_write(sensor->ringbuffer, (uint8_t*)buf, got);
}else if(got < 0){
DBG("Disconnected?");
break;
}
}
if(sl_RB_datalen(sensor.ringbuffer) > BUFSIZ-1){
if(sl_RB_datalen(sensor->ringbuffer) > BUFSIZ-1){
WARNX("Overfull? Clear data from ring buffer");
sl_RB_clearbuf(sensor.ringbuffer);
sl_RB_clearbuf(sensor->ringbuffer);
}
if(sl_RB_readto(sensor.ringbuffer, '\n', (uint8_t*)buf, BUFSIZ-1) > 0){
if(sl_RB_readto(sensor->ringbuffer, '\n', (uint8_t*)buf, BUFSIZ-1) > 0){
tnow = time(NULL);
DBG("Got next: %s", buf);
pthread_mutex_lock(&sensor.valmutex);
pthread_mutex_lock(&sensor->valmutex);
double d;
//int Ngot = 0;
if(getpar(buf, &d, "RE")){
//++Ngot;
sensor.values[NPRECIPLVL].value.f = (float) d;
sensor.values[NPRECIPLVL].time = tnow;
sensor->values[NPRECIPLVL].value.f = (float) d;
sensor->values[NPRECIPLVL].time = tnow;
DBG("Got precip. lvl: %g", d);
}
if(getpar(buf, &d, "RT")){
//++Ngot;
sensor.values[NPRECIP].value.u = (d > 0.) ? 1 : 0;
sensor.values[NPRECIP].time = tnow;
sensor->values[NPRECIP].value.u = (d > 0.) ? 1 : 0;
sensor->values[NPRECIP].time = tnow;
DBG("Got precip.: %g", d);
}
if(getpar(buf, &d, "WU")){
//++Ngot;
sensor.values[NCLOUDS].value.f = (float) d;
sensor.values[NCLOUDS].time = tnow;
sensor->values[NCLOUDS].value.f = (float) d;
sensor->values[NCLOUDS].time = tnow;
DBG("Got clouds.: %g", d);
}
if(getpar(buf, &d, "TE")){
//++Ngot;
sensor.values[NAMB_TEMP].value.f = (float) d;
sensor.values[NAMB_TEMP].time = tnow;
sensor->values[NAMB_TEMP].value.f = (float) d;
sensor->values[NAMB_TEMP].time = tnow;
DBG("Got ext. T: %g", d);
}
if(getpar(buf, &d, "WG")){
//++Ngot;
d /= 3.6;
DBG("Wind: %g", d);
sensor.values[NWIND].value.f = (float) d;
sensor.values[NWIND].time = tnow;
sensor->values[NWIND].value.f = (float) d;
sensor->values[NWIND].time = tnow;
}
if(getpar(buf, &d, "WR")){
//++Ngot;
sensor.values[NWINDDIR].value.f = (float) d;
sensor.values[NWINDDIR].time = tnow;
sensor->values[NWINDDIR].value.f = (float) d;
sensor->values[NWINDDIR].time = tnow;
DBG("Winddir: %g", d);
}
if(getpar(buf, &d, "DR")){
//++Ngot;
sensor.values[NPRESSURE].value.f = (float) (d * 0.7500616);
sensor.values[NPRESSURE].time = tnow;
sensor->values[NPRESSURE].value.f = (float) (d * 0.7500616);
sensor->values[NPRESSURE].time = tnow;
DBG("Pressure: %g", d);
}
if(getpar(buf, &d, "FE")){
//++Ngot;
sensor.values[NHUMIDITY].value.f = (float) d;
sensor.values[NHUMIDITY].time = tnow;
sensor->values[NHUMIDITY].value.f = (float) d;
sensor->values[NHUMIDITY].time = tnow;
DBG("Humidity: %g", d);
}
pthread_mutex_unlock(&sensor.valmutex);
if(sensor.freshdatahandler) sensor.freshdatahandler(&sensor);
pthread_mutex_unlock(&sensor->valmutex);
if(sensor->freshdatahandler) sensor->freshdatahandler(sensor);
}
}
common_kill(&sensor);
sensor->kill(sensor);
return NULL;
}
static int init(struct sensordata_t *s, int N, time_t pollt, int fd){
sensordata_t *sensor_new(int N, time_t pollt, int fd){
FNAME();
if(!s || fd < 0) return -1;
sensor.PluginNo = N;
sensor.fdes = fd;
if(fd < 0) return NULL;
sensordata_t *s = common_new();
if(!s) return NULL;
s->Nvalues = NAMOUNT;
s->PluginNo = N;
s->fdes = fd;
strncpy(s->name, SENSOR_NAME, NAME_LEN);
if(pollt) s->tpoll = pollt;
if(pthread_create(&s->thread, NULL, mainthread, NULL)) return -1;
s->values = MALLOC(val_t, NAMOUNT);
for(int i = 0; i < NAMOUNT; ++i) s->values[i] = values[i];
if(!(s->ringbuffer = sl_RB_new(BUFSIZ))){
WARNX("Can't init ringbuffer!");
common_kill(s);
return -1;
if(!(s->ringbuffer = sl_RB_new(BUFSIZ)) ||
pthread_create(&s->thread, NULL, mainthread, (void*)s)){
s->kill(s);
return NULL;
}
return NAMOUNT;
return s;
}
sensordata_t sensor = {
.name = "Old Reinhard meteostation",
.Nvalues = NAMOUNT,
.init = init,
.onrefresh = common_onrefresh,
.valmutex = PTHREAD_MUTEX_INITIALIZER,
.get_value = common_getval,
.kill = common_kill,
};

View File

@@ -0,0 +1,216 @@
/*
* This file is part of the weatherdaemon 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 <math.h>
#include <string.h>
#include "weathlib.h"
#define SENSOR_NAME "WXA100-06 ultrasonic meteostation"
// static const char *emultemplate = "0R0,S=1.9,D=217.2,P=787.7,T=10.8,H=69.0,R=31.0,Ri=0.0,Rs=Y";
enum{
NWIND,
NWINDDIR,
NHUMIDITY,
NAMB_TEMP,
NPRESSURE,
NPRECIP,
NPRECIPLVL,
NPRECIPINT,
NAMOUNT
};
static const val_t values[NAMOUNT] = {
[NWIND] = {.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_WIND},
[NWINDDIR] = {.sense = VAL_RECOMMENDED,.type = VALT_FLOAT, .meaning = IS_WINDDIR},
[NHUMIDITY] = {.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_HUMIDITY},
[NAMB_TEMP] = {.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_AMB_TEMP},
[NPRESSURE] = {.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_PRESSURE},
[NPRECIP] = {.sense = VAL_OBLIGATORY, .type = VALT_UINT, .meaning = IS_PRECIP},
[NPRECIPLVL]= {.sense = VAL_RECOMMENDED,.type = VALT_FLOAT, .meaning = IS_PRECIP_LEVEL},
[NPRECIPINT]= {.sense = VAL_RECOMMENDED,.type = VALT_FLOAT, .meaning = IS_OTHER, .name = "PRECRATE", .comment = "Precipitation rate, mm/h"},
};
typedef struct{
double windspeed; // speed in m/s
double winddir; // direction from north
double pressure; // pressure in hPa
double temperature; // outern temperature in degC
double humidity; // humidity in percents
double rainfall; // cumulative rain level (mm)
double rainrate; // rain rate, mm/h
double israin; // ==1 if it's raining
} weather_t;
typedef struct{
const char *parname; // parameter started value
int isboolean; // ==1 if answer Y/N
int parlen; // parameter length in bytes
double *weatherpar; // data target
} wpair_t;
static weather_t lastweather;
static const wpair_t wpairs[] = {
{"S=", 0, 2, &lastweather.windspeed},
{"D=", 0, 2, &lastweather.winddir},
{"P=", 0, 2, &lastweather.pressure},
{"T=", 0, 2, &lastweather.temperature},
{"H=", 0, 2, &lastweather.humidity},
{"R=", 0, 2, &lastweather.rainfall},
{"Ri=",0, 3, &lastweather.rainrate},
{"Rs=",1, 3, &lastweather.israin},
{NULL, 0, 0, NULL}
};
static int parseans(char *str){
int ncollected = 0;
if(strncmp(str, "0R0,", 4)){
WARNX("Wrong answer");
LOGWARN("poll_device() get wrong answer: %s", str);
return 0;
}
// init with NaNs
const wpair_t *el = wpairs;
while(el->parname){
*el->weatherpar = NAN;
++el;
}
char *token = strtok(str, ",");
while(token){
el = wpairs;
while(el->parname){
if(strncmp(token, el->parname, el->parlen) == 0){ // found next parameter
token += el->parlen;
char *endptr;
if(el->isboolean){
*el->weatherpar = (*token == 'Y') ? 1. : 0.;
++ncollected;
}else{
*el->weatherpar = strtod(token, &endptr);
if(endptr == token){
DBG("Wrong double value %s", token);
}else ++ncollected;
}
break;
}
++el;
}
token = strtok(NULL, ",");
}
DBG("Got %d values", ncollected);
return ncollected;
}
static void *mainthread(void *s){
FNAME();
char buf[BUFSIZ];
time_t tpoll = 0;
sensordata_t *sensor = (sensordata_t *)s;
while(sensor->fdes > -1){
time_t tnow = time(NULL);
if(tnow - tpoll > sensor->tpoll){
if(sl_tty_write(sensor->fdes, "!0R0\r\n", 6)){
WARN("Can't ask new data");
break;
}
DBG("poll @%zd, pollt=%zd", tnow, sensor->tpoll);
tpoll = tnow;
}
int canread = sl_canread(sensor->fdes);
if(canread < 0){
WARNX("Disconnected fd %d", sensor->fdes);
break;
}else if(canread == 1){
ssize_t got = read(sensor->fdes, buf, BUFSIZ);
if(got > 0){
sl_RB_write(sensor->ringbuffer, (uint8_t*)buf, got);
}else if(got < 0){
DBG("Disconnected?");
break;
}
}
if(sl_RB_datalen(sensor->ringbuffer) > BUFSIZ-1){
WARNX("Overfull? Clear data from ring buffer");
sl_RB_clearbuf(sensor->ringbuffer);
}
if(sl_RB_readline(sensor->ringbuffer, buf, BUFSIZ-1) > 0 && parseans(buf) > 0){
tnow = time(NULL);
pthread_mutex_lock(&sensor->valmutex);
if(!isnan(lastweather.rainfall)){
sensor->values[NPRECIPLVL].value.f = (float) lastweather.rainfall;
sensor->values[NPRECIPLVL].time = tnow;
}
if(!isnan(lastweather.rainrate)){
sensor->values[NPRECIPINT].value.f = (float) lastweather.rainrate;
sensor->values[NPRECIPINT].time = tnow;
}
if(!isnan(lastweather.israin)){
sensor->values[NPRECIP].value.u = (lastweather.israin > 0.) ? 1 : 0;
sensor->values[NPRECIP].time = tnow;
}
if(!isnan(lastweather.temperature)){
sensor->values[NAMB_TEMP].value.f = (float) lastweather.temperature;
sensor->values[NAMB_TEMP].time = tnow;
}
if(!isnan(lastweather.windspeed)){
sensor->values[NWIND].value.f = (float) lastweather.windspeed;
sensor->values[NWIND].time = tnow;
}
if(!isnan(lastweather.winddir)){
sensor->values[NWINDDIR].value.f = (float) lastweather.winddir;
sensor->values[NWINDDIR].time = tnow;
}
if(!isnan(lastweather.pressure)){
sensor->values[NPRESSURE].value.f = (float) (lastweather.pressure * 0.7500616); // mmHg instead of hPa!
sensor->values[NPRESSURE].time = tnow;
}
if(!isnan(lastweather.humidity)){
sensor->values[NHUMIDITY].value.f = (float) lastweather.humidity;
sensor->values[NHUMIDITY].time = tnow;
}
pthread_mutex_unlock(&sensor->valmutex);
if(sensor->freshdatahandler) sensor->freshdatahandler(sensor);
}
}
sensor->kill(sensor);
return NULL;
}
sensordata_t *sensor_new(int N, time_t pollt, int fd){
FNAME();
if(fd < 0) return NULL;
sensordata_t *s = common_new();
if(!s) return NULL;
strncpy(s->name, SENSOR_NAME, NAME_LEN);
s->PluginNo = N;
s->fdes = fd;
s->Nvalues = NAMOUNT;
if(pollt) s->tpoll = pollt;
s->values = MALLOC(val_t, NAMOUNT);
for(int i = 0; i < NAMOUNT; ++i) s->values[i] = values[i];
if(!(s->ringbuffer = sl_RB_new(BUFSIZ)) ||
pthread_create(&s->thread, NULL, mainthread, (void*)s)){
s->kill(s);
return NULL;
}
return s;
}