forked from philcrump/groundstation-tracking-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.c
107 lines (98 loc) · 3.17 KB
/
schedule.c
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
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <stdlib.h>
#include <libpq-fe.h>
#include "config.h"
#include "time.h"
#include "tle.h"
#include "libpredict/predict.h"
void schedule_update_all(PGconn *conn)
{
PGresult *res = PQexec(conn, "SELECT id FROM spacecraft WHERE (SELECT updated FROM schedule_meta) < (SELECT MAX(track_updated) FROM spacecraft) ORDER BY priority ASC;");
if(PQresultStatus(res) != PGRES_TUPLES_OK)
{
printf("Error: Schedule Update Select query failed!\n");
printf("%s\n", PQresStatus(PQresultStatus(res)));
printf("%s\n", PQresultErrorMessage(res));
return;
}
int rows = PQntuples(res);
if(rows == 0)
{
printf(" * Up to date\n");
PQclear(res);
return;
}
printf(" * Running update..\n");
PGresult *cmd_res = PQexec(conn,"BEGIN;");
if(PQresultStatus(cmd_res) != PGRES_COMMAND_OK)
{
printf("Error: Schedule Transaction Begin failed!\n");
PQclear(res);
return;
}
PQclear(cmd_res);
cmd_res = PQexec(conn,"DELETE from schedule;");
if(PQresultStatus(cmd_res) != PGRES_COMMAND_OK)
{
printf("Error: Schedule Clear failed!\n");
PQclear(res);
return;
}
PQclear(cmd_res);
char visible_query[200];
char schedule_insert[200];
for(int i=0; i<rows; i++)
{
int spacecraft_id = atoi(PQgetvalue(res,i,0));
snprintf(visible_query, 200
, "SELECT time FROM observations WHERE spacecraft=%d AND elevation>0 ORDER BY TIME ASC;"
, spacecraft_id
);
PGresult *spacecraft_visible = PQexec(conn,visible_query);
if(PQresultStatus(spacecraft_visible) != PGRES_TUPLES_OK)
{
printf("Error: Spacecraft Visibility Select query failed!\n");
continue;
}
int visible_rows = PQntuples(spacecraft_visible);
for(int j=0; j<visible_rows; j++)
{
snprintf(schedule_insert,200
, "INSERT INTO schedule VALUES ('%s', %d, 'tracking') ON CONFLICT DO NOTHING;"
, PQgetvalue(spacecraft_visible,j,0)
, spacecraft_id
);
cmd_res = PQexec(conn, schedule_insert);
if(PQresultStatus(cmd_res) != PGRES_COMMAND_OK)
{
printf("Error: Schedule Insertion failed! : %s\n"
, schedule_insert
);
}
PQclear(cmd_res);
//if((timestamp - last_row_timestamp) > 1)
//{
/* Add return-to-zenith on end of last_row_timestamp
, add align-to-track on this timestamp */
//}
}
PQclear(spacecraft_visible);
}
cmd_res = PQexec(conn, "UPDATE schedule_meta SET updated=NOW();");
if(PQresultStatus(cmd_res) != PGRES_COMMAND_OK)
{
printf("Error: Schedule meta Update failed!\n");
}
PQclear(cmd_res);
cmd_res = PQexec(conn, "COMMIT;");
if(PQresultStatus(cmd_res) != PGRES_COMMAND_OK)
{
printf("Error: Schedule Transaction Commit failed!\n");
}
PQclear(cmd_res);
PQclear(res);
}