mirror of
https://github.com/eddyem/small_tel.git
synced 2026-03-21 01:01:02 +03:00
some fixes, sill have troubles with dummy socket/fd
This commit is contained in:
@@ -16,94 +16,72 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// dummy meteostation sending data each `dt` seconds
|
||||
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <usefull_macros.h>
|
||||
// dummy meteostation sending data each `tpoll` seconds
|
||||
|
||||
#include "weathlib.h"
|
||||
|
||||
#define NS (6)
|
||||
|
||||
extern sensordata_t sensor;
|
||||
static double dt = 1.;
|
||||
|
||||
static void (*freshdatahandler)(const struct sensordata_t* const) = NULL; // do nothing with fresh data
|
||||
static pthread_t thread;
|
||||
|
||||
static val_t values[NS] = { // fields `name` and `comment` have no sense until value meaning is `IS_OTHER`
|
||||
{.name = "WIND", .comment = "wind speed, m/s", .sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_WIND},
|
||||
{.name = "WINDDIR", .comment = "wind direction azimuth (from south over west), deg", .sense = VAL_RECOMMENDED, .type = VALT_FLOAT, .meaning = IS_WINDDIR},
|
||||
{.name = "EXTTEMP", .comment = "external temperature, degC", .sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_AMB_TEMP},
|
||||
{.name = "PRESSURE", .comment = "atmospheric pressure, hPa", .sense = VAL_RECOMMENDED, .type = VALT_FLOAT, .meaning = IS_PRESSURE},
|
||||
{.name = "HUMIDITY", .comment = "air relative humidity, %%", .sense = VAL_RECOMMENDED, .type = VALT_FLOAT, .meaning = IS_HUMIDITY},
|
||||
{.name = "PRECIP", .comment = "precipitations flag (0 - no, 1 - yes)", .sense = VAL_OBLIGATORY, .type = VALT_UINT, .meaning = IS_PRECIP},
|
||||
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_RECOMMENDED, .type = VALT_FLOAT, .meaning = IS_WINDDIR},
|
||||
{.sense = VAL_OBLIGATORY, .type = VALT_FLOAT, .meaning = IS_AMB_TEMP},
|
||||
{.sense = VAL_RECOMMENDED, .type = VALT_FLOAT, .meaning = IS_PRESSURE},
|
||||
{.sense = VAL_RECOMMENDED, .type = VALT_FLOAT, .meaning = IS_HUMIDITY},
|
||||
{.sense = VAL_OBLIGATORY, .type = VALT_UINT, .meaning = IS_PRECIP},
|
||||
};
|
||||
|
||||
static void *mainthread(void _U_ *U){
|
||||
FNAME();
|
||||
double t0 = sl_dtime();
|
||||
while(1){
|
||||
float f = values[0].value.f + (drand48() - 0.5) / 2.;
|
||||
if(f >= 0.) values[0].value.f = f;
|
||||
f = values[1].value.f + (drand48() - 0.5) * 4.;
|
||||
if(f > 160. && f < 200.) values[1].value.f = f;
|
||||
f = values[2].value.f + (drand48() - 0.5) / 20.;
|
||||
if(f > 13. && f < 21.) values[2].value.f = f;
|
||||
f = values[3].value.f + (drand48() - 0.5) / 100.;
|
||||
if(f > 585. && f < 615.) values[3].value.f = f;
|
||||
f = values[4].value.f + (drand48() - 0.5) / 10.;
|
||||
if(f > 60. && f <= 100.) values[4].value.f = f;
|
||||
values[5].value.u = (f > 98.) ? 1 : 0;
|
||||
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) / 20.;
|
||||
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) values[i].time = cur;
|
||||
if(freshdatahandler) freshdatahandler(&sensor);
|
||||
while(sl_dtime() - t0 < dt) usleep(500);
|
||||
for(int i = 0; i < NS; ++i) sensor.values[i].time = cur;
|
||||
if(sensor.freshdatahandler) sensor.freshdatahandler(&sensor);
|
||||
while(sl_dtime() - t0 < sensor.tpoll) usleep(500);
|
||||
t0 = sl_dtime();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int init(int N, time_t pollt, int _U_ fd){
|
||||
static int init(struct sensordata_t* s, int N, time_t pollt, int _U_ fd){
|
||||
FNAME();
|
||||
values[0].value.f = 1.;
|
||||
values[1].value.f = 180.;
|
||||
values[2].value.f = 17.;
|
||||
values[3].value.f = 600.;
|
||||
values[4].value.f = 80.;
|
||||
values[5].value.u = 0;
|
||||
if(pthread_create(&thread, NULL, mainthread, NULL)) return 0;
|
||||
if(pthread_create(&s->thread, NULL, mainthread, NULL)) return 0;
|
||||
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;
|
||||
if(pollt) dt = (double) pollt; // refresh each `pollt` seconds
|
||||
return NS;
|
||||
}
|
||||
|
||||
static int onrefresh(void (*handler)(const struct sensordata_t* const)){
|
||||
FNAME();
|
||||
if(!handler) return FALSE;
|
||||
freshdatahandler = handler;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void die(){
|
||||
FNAME();
|
||||
if(0 == pthread_kill(thread, 9)){
|
||||
DBG("Killed, join");
|
||||
pthread_join(thread, NULL);
|
||||
DBG("Done");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief getval - value's getter
|
||||
* @param o (o) - value
|
||||
* @param N - it's index
|
||||
* @return FALSE if failed
|
||||
*/
|
||||
static int getval(val_t *o, int N){
|
||||
static int getval(struct sensordata_t* s, val_t *o, int N){
|
||||
if(N < 0 || N >= NS) return FALSE;
|
||||
if(o) *o = values[N];
|
||||
if(o) *o = s->values[N];
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@@ -111,7 +89,7 @@ sensordata_t sensor = {
|
||||
.name = "Dummy weatherstation",
|
||||
.Nvalues = NS,
|
||||
.init = init,
|
||||
.onrefresh = onrefresh,
|
||||
.onrefresh = common_onrefresh,
|
||||
.get_value = getval,
|
||||
.die = die,
|
||||
.kill = common_kill,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user