trace2port.c 4.66 KB
Newer Older
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema 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
39
40
41
42
43
44
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
/*
	Program to echo a trace periodically in plain text to a port

	Based on work from Jeroen Vreeken (pe1rxq@amsat.org)
	Adapted by Tammo Jan Dijkema (t.j.dijkema@camras.nl)
	Copyright Stichting C.A. Muller Radioastronomiestation, 2017

	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>
#include <stdbool.h>

#include <command/command.h>

#include <trace/trace.h>
#include <dt_port_numbers.h>
#include <utils/status_server.h>
#include <utils/dt_host.h>
#include <log/log.h>

#define TIME_OFFSET 2

int trace_port = CONSOLE_TRACE_PORT;
int stat_port = 11042;


void output(struct status_server *stat_srv,
	int ntraces,
	struct trace **tracevals,
        bool toDegrees)
{
	static time_t last = 0;
	time_t now;
	time_t now_t = time(NULL);
	char statline[200];
	int do_output = 1;

        int tracenum;

	now = tracevals[0]->value.t.tv_sec;
	for (tracenum=0; tracenum<ntraces; ++tracenum) {
		if (tracevals[tracenum]->value.t.tv_sec != now) {
			do_output = 0;
		}
	}

	if (now_t - now > 2) {
		printf("No trace for more than 2 seconds, using last known position\n");
		now = now_t;
	}

	if (now <= last) {
		do_output = 0;
	}

	if (do_output == 0) {
		return;
	} else {
		last = now;
	}

	int statlinelen = 0;
	for (tracenum=0; tracenum<ntraces; ++tracenum) {
		double val = tracevals[tracenum]->value.value.f;
		// Do not convert the status server values to degrees
		statlinelen += sprintf(statline+statlinelen, "%f ", val);

		// Convert console output to degrees if specified
		if (toDegrees) {
			val *= 360.0 / (2*M_PI);
		}
		printf("%f ", val);
	}
	printf("\n");
	sprintf(statline+statlinelen, "\n");

	status_server_send(stat_srv, statline);
}

static void handler_interval(struct trace *trace,
	struct timespec *interval, enum trace_interval_type type)
{
	printf("interval:  %ld.%09ld\n", interval->tv_sec, interval->tv_nsec);
}


int main(int argc, char **argv)
{
	bool toDegrees = false;
	int opt;
	while ((opt = getopt(argc, argv, "d")) != -1) {
		toDegrees = true;
		printf("Using degrees\n");
	}
	printf("optind=%d\n", optind);

	if (argc-optind<1) {
		printf("Usage: %s [-d] trace_host trace1 [trace2 ...]\n", argv[0]);
		printf("  -d: convert all traces from radians to degrees\n");
		printf("Example: trace2port -d console  Azimuth_Position Elevation_Position azimuth_setpoint_error.difference elevation_setpoint_error.difference\n");
		return 1;
	}
	int ntraces = argc-optind-1;

	struct trace *tracevals[ntraces];
	struct status_server *stat_srv;
	time_t lastt = 0;
	struct timespec t_int;

	signal(SIGPIPE, SIG_IGN);

	log_client_start(dt_host_console(), CONSOLE_LOG_PORT_IN,
		LOG_T_DEBUG, LOG_T_INFO, "trace/trace2port");

	printf("Number of traces: %d\n", ntraces);

	t_int.tv_sec = 1;
	t_int.tv_nsec = 0;
	int tracenum;
        printf("Using host %s\n", argv[optind]);
	for (tracenum=0; tracenum<ntraces; ++tracenum) {
		tracevals[tracenum] = trace_open(argv[optind], trace_port);
		trace_name_set(tracevals[tracenum], 0, argv[optind+tracenum+1]);
		trace_autorecover(tracevals[tracenum], true);
		tracevals[tracenum]->handler_interval = handler_interval;
		trace_interval_set(tracevals[tracenum], &t_int, TRACE_INTERVAL_TYPE_INTERVAL);
	}

	stat_srv = status_server_create(stat_port, 0, 100);

	while (1)
	{
		time_t t;
		int high = 0;
		fd_set fdset_rx;
		struct timeval tv;

		t = time(NULL);
		if (t != lastt) {
			lastt = t;
			t += TIME_OFFSET;
		}

		FD_ZERO(&fdset_rx);

		status_server_fdset_add(stat_srv, &fdset_rx, &high);
		for (tracenum=0; tracenum<ntraces; ++tracenum) {
			trace_fd_set(tracevals[tracenum], &fdset_rx, &high);
		}

		tv.tv_sec = 1;
		tv.tv_usec = 0;

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

		for (tracenum=0; tracenum<ntraces; ++tracenum) {
			trace_handle(tracevals[tracenum], &fdset_rx);
		}

		status_server_fdset_handle(stat_srv, &fdset_rx);

		output(stat_srv, ntraces, tracevals, toDegrees);
	}

}