console_j2000tracer.c 8.77 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
	Tracer for J2000.0 positions

	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2008, 2009
	Copyright Stichting C.A. Muller Radioastronomiestation, 2008, 2009

	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 <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>
#include <ctype.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <errno.h>

Jeroen Vreeken's avatar
Jeroen Vreeken committed
39
40
41
#include <libnova/transform.h>
#include <libnova/julian_day.h>
#include <libnova/utility.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
42

Jeroen Vreeken's avatar
Jeroen Vreeken committed
43
44
#include <utils/tcp_connect.h>
#include <utils/tcp_listen.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

#define SKIP_TIME 10

char *trace_host = "localhost";
int trace_port = 10000;
int stat_port = 11030;
char *az_trace_name = "Azimuth_Position";
char *el_trace_name = "Elevation_Position";

struct traceval {
	time_t seconds;
	int sample;
	float value;
	int wasnan;
	int new;
	float newval;
};

struct traceval traceval_az = { 0, -1, 0.0, 0, 0, 0.0 };
struct traceval traceval_el = { 0, -1, 0.0, 0, 0, 0.0 };
float traceval_az_deg;

static int fd_trace_az, fd_trace_el;

typedef union {
	uint32_t u;
	float f;
} float32_t;

struct lnh_lnlat_posn dwingeloo;
struct ln_lnlat_posn dwingeloo_pos;
struct ln_equ_posn object;
struct ln_hms track_hms;
struct ln_dms track_dms;

struct stat_client {
	struct stat_client *next;
	int fd;
};

struct stat_client *stat_clients = NULL;


int init_trace(int fd, char *trace_name, struct traceval *trc)
{
	
	trc->seconds = 0;
	
	write(fd, trace_name, strlen(trace_name) + 1);

	return 0;
}


void a2coord(char *ara, char *adec, struct ln_hms *ra, struct ln_dms *dec)
{
	int i, neg;

	if (strlen(adec) && adec[0] == '-') {
		dec->neg = 1;
		i = 1;
	} else {
		dec->neg = 0;
		i = 0;
	}
	dec->degrees = 0;
	dec->minutes = 0;
	dec->seconds = 0.0;
	
	for (; isdigit(adec[i]) && i < strlen(adec); i++) {
		dec->degrees *= 10;
		dec->degrees += adec[i] - '0';
	}
	for (i++; isdigit(adec[i]) && i < strlen(adec); i++) {
		dec->minutes *= 10;
		dec->minutes += adec[i] - '0';
	}
	i++;
	if (i < strlen(adec)) {
		dec->seconds = atof(adec + i);
	}
	
	ra->hours = 0;
	ra->minutes = 0;
	ra->seconds = 0.0;
	
	if (strlen(ara) && ara[0] == '-') {
		neg = -1;
		i = 1;
	} else {
		neg = 1;
		i = 0;
	}
	for (; isdigit(ara[i]) && i < strlen(ara); i++) {
		ra->hours *= 10;
		ra->hours += (ara[i] - '0') * neg;
	}
	for (i++; isdigit(ara[i]) && i < strlen(ara); i++) {
		ra->minutes *= 10;
		ra->minutes += (ara[i] - '0') * neg;
	}
	i++;
	if (i < strlen(ara)) {
		ra->seconds = atof(ara + i) * neg;
	}
	if (neg < 0) {
		ra->hours += 24;
		ra->minutes += 60;
		ra->seconds += 60.0;
	}
}

int handle_trace(int fd, struct traceval *trc)
{
	int ret;
	uint32_t nin;
	float fin;
	float32_t f;
	
	ret = read(fd, &nin, sizeof(uint32_t));
	if (ret != sizeof(uint32_t)) {
		close(fd);
		return -1;
	}
	nin = ntohl(nin);
	f.u = nin;
	fin = f.f;
	if (trc->wasnan) {
		trc->seconds = nin;
		trc->sample = -1;
		trc->wasnan = 0;
		trc->new = 0;
	} else if (isnan(fin)) {
		trc->wasnan = 1;
		trc->new = 0;
	} else {
		trc->value = fin;
		trc->sample++;
		trc->new = 1;
	}
	return 0;
}

void new_stat_client(int fd_stat)
{
	struct stat_client *new_stat;
	int fd;
	
	fd = tcp_accept(fd_stat);
	if (fd < 0)
		return;
	ioctl(fd, FIONBIO, &(int){ 1 });
	
	new_stat = malloc(sizeof(struct stat_client));
	if (!new_stat) {
		close(fd);
		return;
	}
	
	new_stat->fd = fd;
	new_stat->next = stat_clients;
	stat_clients = new_stat;
}


void output(void)
{
	static time_t last = 0;
	time_t now;
	struct ln_hrz_posn hrz;
	struct ln_date date;
	double JD;
	struct ln_equ_posn equ_pos;
	struct ln_hms hms;
	struct ln_dms dms;
	struct stat_client **stat_clientp, **nextp;
	char statline[100];
	int do_output = 1;
	

	if (traceval_az.seconds < traceval_el.seconds)
		now = traceval_az.seconds;
	else
		now = traceval_el.seconds;	
	
	if (now <= last)
		do_output = 0;
	
	if (do_output == 0) {
		time_t localtime;
		
		localtime = time(NULL);
		if (fd_trace_az >= 0 && localtime > (traceval_az.seconds + 5) &&
		    traceval_az.seconds > 0) {
			close(fd_trace_az);
			fd_trace_az = -1;
		}
		if (fd_trace_el >= 0 && localtime > (traceval_el.seconds + 5) &&
		    traceval_el.seconds > 0) {
			close(fd_trace_el);
			fd_trace_el = -1;
		}
		
		if (localtime > now + 10 &&
		    localtime > last) {
			last = localtime;
		} else {
			return;
		}
	} else {
		last = now;
	}
	
	hrz.az = traceval_az.value * 360.0 / (2*M_PI);
	traceval_az_deg = hrz.az;
	hrz.alt = traceval_el.value * 360.0 / (2*M_PI);
	
	ln_get_date_from_timet(&last, &date);
	JD = ln_get_julian_day(&date);

	ln_get_equ_from_hrz (&hrz, &dwingeloo_pos, JD, &equ_pos); 

	ln_deg_to_hms(equ_pos.ra, &hms);
	ln_deg_to_dms(equ_pos.dec, &dms);

	printf("Trace  %f %f : %02dh%02dm%04.1f %s%03dd%02dm%04.1f\n",
	    hrz.az, hrz.alt,
	    hms.hours, hms.minutes, hms.seconds,
	    dms.neg ? "-" : " ", dms.degrees, dms.minutes, dms.seconds);

	sprintf(statline, "%02dh%02dm%04.1f %s%03dd%02dm%04.1f %02dh%02dm%04.1f %s%03dd%02dm%04.1f %ld\n",
	    track_hms.hours, track_hms.minutes, track_hms.seconds,
	    track_dms.neg ? "-" : "", 
	    track_dms.degrees, track_dms.minutes, track_dms.seconds,
	    hms.hours, hms.minutes, hms.seconds,
	    dms.neg ? "-" : "",
	    dms.degrees, dms.minutes, dms.seconds,
	    (unsigned long)last);

	for (stat_clientp = &stat_clients; *stat_clientp; stat_clientp = nextp){
		int ret;
		
		ret = write((*stat_clientp)->fd, statline, strlen(statline));
		if (ret <= 0) {
			struct stat_client *tmp;
			
			tmp = *stat_clientp;
			close((*stat_clientp)->fd);
			*stat_clientp = (*stat_clientp)->next;
			free(tmp);
			nextp = stat_clientp;
		} else {
			nextp = &(*stat_clientp)->next;
		}
	}
}

int main(int argc, char **argv)
{
	int fd_stat;
	int skip = 0;

	printf("libnova version: %s\n", ln_get_version());

	dwingeloo.lng.neg = 0;
	dwingeloo.lng.degrees = 6;
	dwingeloo.lng.minutes = 23;
	dwingeloo.lng.seconds = 46.21;
	dwingeloo.lat.neg = 0;
	dwingeloo.lat.degrees = 52;
	dwingeloo.lat.minutes = 48;
	dwingeloo.lat.seconds = 43.27;
	ln_hlnlat_to_lnlat(&dwingeloo, &dwingeloo_pos);

	track_hms.hours = 0;
	track_hms.minutes = 0;
	track_hms.seconds = 0;
	track_dms.neg = 0;
	track_dms.degrees = 0;
	track_dms.minutes = 0;
	track_dms.seconds = 0.0;

	signal(SIGPIPE, SIG_IGN);

	if (argc == 2) {
		trace_host = argv[argc - 1];
	}
	object.ra = ln_hms_to_deg(&track_hms);
	object.dec = ln_dms_to_deg(&track_dms);

	ln_deg_to_hms(object.ra, &track_hms);
	ln_deg_to_dms(object.dec, &track_dms);

	fd_trace_az = tcp_connect(trace_host, trace_port);
	if (fd_trace_az >= 0) {
		init_trace(fd_trace_az, az_trace_name, &traceval_az);
	}
	fd_trace_el = tcp_connect(trace_host, trace_port);
	if (fd_trace_el >= 0) {
		init_trace(fd_trace_el, el_trace_name, &traceval_el);
	}

	fd_stat = tcp_listen(stat_port, 0, 100);
	if (fd_stat < 0) {
		printf("Could not open listen port for status\n");
	} else {
		ioctl(fd_stat, FIONBIO, &(int){ 1 });
	}

	while (1)
	{
		int high = 0;
		fd_set fdset_rx;
		struct timeval tv;
		
		FD_ZERO(&fdset_rx);
	
		if (fd_stat >= 0) {
			FD_SET(fd_stat, &fdset_rx);
			if (fd_stat >= high)
				high = fd_stat + 1;
		}
		if (skip > 0) {
			if (skip < time(NULL))
				skip = 0;
		} else {
			if (fd_trace_el >= 0) {
				FD_SET(fd_trace_el, &fdset_rx);
				if (fd_trace_el >= high)
					high = fd_trace_el + 1;
			} else {
				fd_trace_el = tcp_connect(trace_host, trace_port);
				if (fd_trace_el >= 0) {
					init_trace(fd_trace_el, el_trace_name,
					    &traceval_el);
					skip = 0;
				} else
					skip = time(NULL) + SKIP_TIME;
			}
			if (fd_trace_az >= 0) {
				FD_SET(fd_trace_az, &fdset_rx);
				if (fd_trace_el >= high)
					high = fd_trace_az + 1;
			} else if (!skip) {
				fd_trace_az = tcp_connect(trace_host, trace_port);
				if (fd_trace_az >= 0) {
					init_trace(fd_trace_az, az_trace_name,
					    &traceval_az);
					skip = 0;
				}
			}
		}
		
		tv.tv_sec = 1;
		tv.tv_usec = 0;

		select(high, &fdset_rx, NULL, NULL, &tv);

		if (FD_ISSET(fd_stat, &fdset_rx)) {
			new_stat_client(fd_stat);
		}
		if (fd_trace_az > 0 && FD_ISSET(fd_trace_az, &fdset_rx)) {
			if (handle_trace(fd_trace_az, &traceval_az) < 0)
				fd_trace_az = -1;
		}
		if (fd_trace_el > 0 && FD_ISSET(fd_trace_el, &fdset_rx)) {
			if (handle_trace(fd_trace_el, &traceval_el) < 0)
				fd_trace_el = -1;
		}
		
		output();
	}

}