forked from cbassa/sattools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunsched.c
More file actions
296 lines (247 loc) · 7.28 KB
/
runsched.c
File metadata and controls
296 lines (247 loc) · 7.28 KB
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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#define PORT 7624
#define MAXPORT 7674
#define IP "127.0.0.1"
#define LIM 2048
#define NMAX 256
#define SCHEDULED 0
#define STARTED 1
#define FINISHED 2
// Observation struct contains observation time, celestial coords and camera name
struct observation {
char stime[32],sra[32],sde[32],camname[15],startstop[10];
time_t ptime;
float dt;
};
int fgetline(FILE *file,char *s,int lim);
void send_position(char *sra,char *sde,char *datadir,char *obsdir,char *camname);
void stop_obs(char *datadir,char *obsdir,char *camname);
time_t decode_time(char *stm);
int main(int argc, char *argv[])
{
int i=0,nobs,flag=0;
time_t rawtime,aimtime;
struct tm *ptm,*rtm;
char buf[20],line[LIM],stm[20],sra[32],sde[32],pra[32],pde[32];
FILE *file;
struct observation obs[NMAX];
char *env;
char datadir[128],obsdir[128];
int nextobs, dtnext;
// Get environment variables
env=getenv("ST_DATADIR");
if (env!=NULL) {
strcpy(datadir,env);
} else {
printf("ST_DATADIR environment variable not found.\n");
}
// Get environment variables
env=getenv("ST_OBSDIR");
if (env!=NULL) {
strcpy(obsdir,env);
} else {
printf("ST_OBSDIR environment variable not found.\n");
}
// For ever loop
for (;;) {
// Read file
i=0;
file=fopen("schedule.txt","r");
while (fgetline(file,line,LIM)>0) {
sscanf(line,"%s %s %s %s %s",obs[i].stime,obs[i].sra,obs[i].sde,obs[i].camname,obs[i].startstop);
obs[i].ptime=decode_time(obs[i].stime);
i++;
}
fclose(file);
nobs=i;
// Get local time
time(&rawtime);
//rawtime-=3600;
// Print UTC time
ptm=gmtime(&rawtime);
strftime(buf,32,"%Y-%m-%dT%H:%M:%S",ptm);
// Make raw time UTC to compare with scheduled time
rawtime=mktime(ptm);
// Show current raw time, just to check
// printf("%s\n",ctime(&rawtime));
// Compute time differences
for (i=0;i<nobs;i++) {
obs[i].dt=difftime(obs[i].ptime,rawtime);
}
nextobs=-1;
dtnext=9999999;
// Loop over observations
for (i=0;i<nobs;i++) {
if (obs[i].dt>0.0) {
if(obs[i].dt < dtnext){
nextobs=i;
dtnext=obs[i].dt;
}
// printf("%4.0f %s %s %s %s\n",obs[i].dt,obs[i].stime,obs[i].sra,obs[i].sde,obs[i].startstop);
// break;
} else if (obs[i].dt==0) {
if(strstr(obs[i].startstop,"tart")!=NULL){
//printf("Slewing to %s %s\n",obs[i].sra,obs[i].sde);
send_position(obs[i].sra,obs[i].sde,datadir,obsdir,obs[i].camname);
} else if(strstr(obs[i].startstop,"top")!=NULL){
stop_obs(datadir,obsdir,obs[i].camname);
}
}
}
if(nextobs>=0){
// print next observation data if any found
printf("%4.0f %s %s %s %s\n",obs[nextobs].dt,obs[nextobs].stime,obs[nextobs].sra,obs[nextobs].sde,obs[nextobs].startstop);
}
// Sleep
sleep(1);
}
return 0;
}
// Read a line of maximum length int lim from file FILE into string s
int fgetline(FILE *file,char *s,int lim)
{
int c,i=0;
while (--lim > 0 && (c=fgetc(file)) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
// Read data/cameras.txt in search of specified camera name and return complete camera details line
int read_cameras(char *camname,char *datadir,char *camera)
{
FILE *file;
char line[127],filename[127];
sprintf(filename,"%s/data/cameras.txt",datadir);
file=fopen(filename,"r");
if (file==NULL) {
printf("File with camera information not found!\n");
return -1;
}
while (fgets(line,LIM,file)!=NULL) {
// Skip commented lines
if (strstr(line,"#")!=NULL)
continue;
if(strstr(line, camname)!=NULL){
strcpy(camera, line);
return 0;
}
}
fclose(file);
return -1;
}
// Send new position to telescope
void send_position(char *sra,char *sde,char *datadir,char *obsdir,char *camname)
{
int skt, port;
struct hostent *he;
struct sockaddr_in addr;
char packet[LIM];
FILE *file;
float ra,de;
char camera[128],fname[128];
float f;
char s[31];
// Check if camera is fixed
// read complete line from data/cameras.txt describing the scheduled camera
read_cameras(camname,datadir,camera); // search for camera name
sscanf(camera,"%s %f %f %f %f %s", s, &f, &f, &f, &f, s);
// Look for "fix" string to jump over slewing routines.
if(strstr(s,"ix")==NULL){
// Old packet style
// sprintf(packet,"<newNumberVector device='Celestron GPS' name='EQUATORIAL_EOD_COORD_REQUEST'><oneNumber name='RA'>%s</oneNumber><oneNumber name='DEC'>%s</oneNumber></newNumberVector>",sra,sde);
// New packet style (as of 2013-08-20)
sprintf(packet,"<newNumberVector device='Celestron GPS' name='EQUATORIAL_EOD_COORD'><oneNumber name='RA'>%s</oneNumber><oneNumber name='DEC'>%s</oneNumber></newNumberVector>",sra,sde);
printf("Slewing to %s %s\n",sra,sde);
// Send TCP packet
skt=socket(AF_INET,SOCK_STREAM,0);
addr.sin_family=AF_INET;
port=PORT;
addr.sin_port=htons(port);
he=gethostbyname(IP);
bcopy(he->h_addr,(struct in_addr *) &addr.sin_addr,he->h_length);
while((connect(skt,(struct sockaddr *) &addr,sizeof(addr))<0) && (port < MAXPORT)) {
fprintf(stderr,"Connection refused by remote host on port %04d.\n",port);
port++;
// Skip port 7265... used by some other service?
if(port==7265) port++;
fprintf(stderr,"Trying port %04d.\n",port);
addr.sin_port=htons(port);
he=gethostbyname(IP);
bcopy(he->h_addr,(struct in_addr *) &addr.sin_addr,he->h_length);
}
if(port>=MAXPORT) return;
printf("Connected to Indi server on port %04d.\n",port);
write(skt,packet,strlen(packet));
close(skt);
}
printf("Starting new observation\n");
// Set restart
sprintf(fname,"%s/control/state.txt",obsdir);
file=fopen(fname,"w");
if (file!=NULL) {
fprintf(file,"restart");
fclose(file);
}
// Set position
sprintf(fname,"%s/control/position.txt",obsdir);
file=fopen(fname,"w");
if (file!=NULL) {
fprintf(file,"%s %s\n",sra,sde);
fclose(file);
}
// Set camera
sprintf(fname,"%s/control/camera.txt",obsdir);
file=fopen(fname,"w");
if (file!=NULL) {
fprintf(file,"%s",camera);
fclose(file);
}
return;
}
// Send stop observation signal
void stop_obs(char *datadir,char *obsdir,char *camname)
{
FILE *file;
char camera[128],fname[128];
float f;
char s[31];
// Retrieve Camera data
// read complete line from data/cameras.txt describing the scheduled camera
read_cameras(camname,datadir,camera); // search for camera name
sscanf(camera,"%s %f %f %f %f %s", s, &f, &f, &f, &f, s);
printf("Stop observation\n");
// Set stop
sprintf(fname,"%s/control/state.txt",obsdir);
file=fopen(fname,"w");
if (file!=NULL) {
fprintf(file,"stop");
fclose(file);
}
return;
}
// Decode time
time_t decode_time(char *stm)
{
time_t aimtime;
struct tm *rtm;
int d;
rtm=gmtime(&aimtime);
sscanf(stm,"%04d-%02d-%02dT%02d:%02d:%02d",&rtm->tm_year,&rtm->tm_mon,&rtm->tm_mday,&rtm->tm_hour,&rtm->tm_min,&rtm->tm_sec);
rtm->tm_year-=1900;
rtm->tm_mon--;
aimtime=mktime(rtm);
return aimtime;
}