-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtelstat.c
565 lines (471 loc) · 14.4 KB
/
telstat.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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// hskymon from HDS OPE file Editor
// New SkyMonitor for Subaru Gen2
// telstat.c --- Telescope Status communication with Gen2
//
// 2012.10.22 A.Tajitsu
#include"main.h"
#include"version.h"
#ifdef USE_XMLRPC
static void
printStatus(const char *alias, xmlrpc_value *itemP, int rawflag)
{
int type;
int result_i;
double result_d;
char *result_s;
xmlrpc_env env;
xmlrpc_env_init(&env);
if (itemP == NULL)
{
fprintf(stderr, "%s=#\n", alias);
fprintf(stderr, "#\n");
return;
}
/* Unpack the return value. This is complicated by the fact that C
only has static typing.
*/
type = xmlrpc_value_type(itemP);
switch (type) {
case XMLRPC_TYPE_INT:
/* Get our status value and print it out. */
xmlrpc_decompose_value(&env, itemP,
"i", &result_i);
if (!rawflag)
{
fprintf(stderr, "%s=%d\n", alias, result_i);
}
else
{
fprintf(stderr,"%d\n", result_i);
}
break;
case XMLRPC_TYPE_DOUBLE:
/* Get our status value and print it out. */
xmlrpc_decompose_value(&env, itemP,
"d", &result_d);
if (!rawflag)
{
fprintf(stderr, "%s=%f\n", alias, result_d);
}
else
{
fprintf(stderr, "%f\n", result_d);
}
break;
case XMLRPC_TYPE_STRING:
/* Get our status value and print it out. */
xmlrpc_decompose_value(&env, itemP,
"s", &result_s);
if (!rawflag)
{
fprintf(stderr, "%s=%s\n", alias, result_s);
}
else
{
fprintf(stderr, "%s\n", result_s);
}
free(result_s);
break;
case XMLRPC_TYPE_BOOL:
case XMLRPC_TYPE_DATETIME:
case XMLRPC_TYPE_BASE64:
case XMLRPC_TYPE_ARRAY:
case XMLRPC_TYPE_STRUCT:
case XMLRPC_TYPE_C_PTR:
case XMLRPC_TYPE_NIL:
case XMLRPC_TYPE_DEAD:
fprintf(stderr, "#\n");
break;
}
}
static gdouble
getStatus_double(const char *alias, xmlrpc_value *itemP, int rawflag)
{
int type;
double result_d;
xmlrpc_env env;
xmlrpc_env_init(&env);
if (env.fault_occurred){
return(-100);
}
if (itemP == NULL)
{
fprintf(stderr, "%s=#\n", alias);
fprintf(stderr, "#\n");
return(-100);
}
/* Unpack the return value. This is complicated by the fact that C
only has static typing.
*/
type = xmlrpc_value_type(itemP);
if(type == XMLRPC_TYPE_DOUBLE) {
/* Get our status value and print it out. */
xmlrpc_decompose_value(&env, itemP,
"d", &result_d);
if (env.fault_occurred){
return(-100);
}
return(result_d);
}
else{
fprintf(stderr, "XMLRPC : Type Error (argument \"%s\" is not double).\n", alias);
return(-100);
}
}
static void
getStatus_string(const char *alias, xmlrpc_value *itemP, int rawflag,
gchar **str)
{
int type;
gchar *result_s;
xmlrpc_env env;
xmlrpc_env_init(&env);
if (env.fault_occurred){
if(*str) g_free(*str);
*str=NULL;
return;
}
if (itemP == NULL)
{
if(*str) g_free(*str);
*str=NULL;
return;
}
/* Unpack the return value. This is complicated by the fact that C
only has static typing.
*/
type = xmlrpc_value_type(itemP);
if(type == XMLRPC_TYPE_STRING) {
/* Get our status value and print it out. */
xmlrpc_decompose_value(&env, itemP,
"s", &result_s);
if(*str) g_free(*str);
*str=g_strdup(result_s);
g_free(result_s);
if (env.fault_occurred){
*str=NULL;
return;
}
return;
}
else{
fprintf(stderr, "XMLRPC : Type Error (argument \"%s\" is not string).\n", alias);
if(*str) g_free(*str);
*str=NULL;
return;
}
}
int get_rope(typHOE *hg, int mode){
char *serviceName;
int result, type;
xmlrpc_value *resultP=NULL, *paramsP=NULL, *structP=NULL, *itemP=NULL, *dummy;
int dash_r = 0;
char url[256];
remoteObjectProxy *rope_proxyP;
xmlrpc_env rope_env;
int len;
if(hg->telstat_error){
return(-2);
}
rope_proxyP=NULL;
printf_log(hg,"[GetOpePaths] starting to get opened OPE file names in IntegGUI from %s",
hg->ro_ns_host);
if(!hg->stat_initflag){
result = ro_init(hg);
if (result)
{
fprintf(stderr, "Error initializing remoteObjects subsystem: ret=%d\n",
result);
return(-1);
}
}
// Necessary for XML-RPC processing.
xmlrpc_env_init(&rope_env);
serviceName = g_strdup("integgui0");
// Create remoteObject proxy handle. Use simple HTTP authentication
// with the service name as the username/password combination.
result = ro_makeProxy(&rope_proxyP, serviceName);
if (result)
{
fprintf(stderr, "Error making proxy!\n");
return(-1);
}
g_free(serviceName);
structP = xmlrpc_struct_new(&rope_env);
paramsP = xmlrpc_array_new(&rope_env);
dummy = xmlrpc_int_new(&rope_env, 0);
xmlrpc_array_append_item(&rope_env, paramsP, dummy);
if(dummy) xmlrpc_DECREF(dummy);
result = get_status(&rope_env);
if (result)
{
fprintf(stderr, "Error building parameters to get_ope_paths(), result=%d\n",
result);
printf_log(hg, "[TelStat] Error building parameters to get_ope_paths(), result=%d.",
result);
close_telstat(hg);
hg->telstat_error=TRUE;
if(resultP) xmlrpc_DECREF(resultP);
if(paramsP) xmlrpc_DECREF(paramsP);
if(structP) xmlrpc_DECREF(structP);
return(-1);
}
// Call the remote method "fetch(dict)" in the Gen2 status server.
result = ro_callProxy_arg(rope_proxyP, &resultP, "get_ope_paths", paramsP);
if (result)
{
fprintf(stderr, "Error in remote call to get_ope_paths(), result=%d\n",
result);
printf_log(hg, "[GetOpePaths] Error in remote call to get_ope_paths(), result=%d.",
result);
close_telstat(hg);
hg->telstat_error=TRUE;
if(resultP) xmlrpc_DECREF(resultP);
if(paramsP) xmlrpc_DECREF(paramsP);
if(structP) xmlrpc_DECREF(structP);
return(-1);
}
// Process return value, which should be a dictionary of alias: value
// pairs.
type = xmlrpc_value_type(resultP);
if (type != XMLRPC_TYPE_ARRAY)
{
fprintf(stderr, "Error in remote call to status, unexpected result type=%d\n", type);
printf_log(hg, "[GetOpePaths] Error in remote call to status, unexpected result type=%d.", type);
close_telstat(hg);
hg->telstat_error=TRUE;
if(resultP) xmlrpc_DECREF(resultP);
if(paramsP) xmlrpc_DECREF(paramsP);
if(structP) xmlrpc_DECREF(structP);
return(-1);
}
{
int i;
gchar *tmp_char=NULL;
switch(mode){
case ROPE_ALL:
len = xmlrpc_array_size(&rope_env,resultP);
printf_log(hg, "[GetOpePaths] Element Number is %d", len);
if(len>MAX_ROPE) len=MAX_ROPE;
hg->max_rope=len;
if(len>0){
for(i=0;i<len;i++){
xmlrpc_array_read_item(&rope_env, resultP, i, &itemP);
if(hg->filename_rope[i]) g_free(hg->filename_rope[i]);
xmlrpc_decompose_value(&rope_env, itemP,
"s", &hg->filename_rope[i]);
printf_log(hg, "[GetOpePaths] OPE[%d] is %s", i,hg->filename_rope[i]);
//printf("OPE[%d] is %s\n", i,hg->filename_rope[i]);
if(i==len-1){
if(hg->dirname_rope) g_free(hg->dirname_rope);
hg->dirname_rope=g_dirname(hg->filename_rope[i]);
}
}
}
if(itemP) xmlrpc_DECREF(itemP);
break;
case ROPE_DIR:
len = xmlrpc_array_size(&rope_env,resultP);
printf_log(hg, "[GetOpePaths] Element Number is %d", len);
if(len>0){
xmlrpc_array_read_item(&rope_env, resultP, len-1, &itemP);
xmlrpc_decompose_value(&rope_env, itemP,
"s", &tmp_char);
if(hg->dirname_rope) g_free(hg->dirname_rope);
hg->dirname_rope=g_dirname(tmp_char);
printf_log(hg, "[GetOpePaths] The current OPE dir in IntegGUI is \"%s\"", hg->dirname_rope);
if(tmp_char) g_free(tmp_char);
if(itemP) xmlrpc_DECREF(itemP);
}
else{
if(hg->dirname_rope) g_free(hg->dirname_rope);
hg->dirname_rope=NULL;
}
break;
}
}
if(resultP) xmlrpc_DECREF(resultP);
if(paramsP) xmlrpc_DECREF(paramsP);
if(structP) xmlrpc_DECREF(structP);
ro_freeProxy(rope_proxyP,!hg->stat_initflag);
return(len);
}
int init_telstat(typHOE *hg){
char *serviceName;
int result, type;
hg->ro_proxyP=NULL;
result = ro_init(hg);
if (result)
{
fprintf(stderr, "Error initializing remoteObjects subsystem: ret=%d\n",
result);
return(-1);
}
// Necessary for XML-RPC processing.
xmlrpc_env_init(&hg->env);
serviceName = g_strdup("status");
// Create remoteObject proxy handle. Use simple HTTP authentication
// with the service name as the username/password combination.
result = ro_makeProxy(&hg->ro_proxyP, serviceName);
if (result)
{
fprintf(stderr, "Error making proxy!\n");
return(-1);
}
g_free(serviceName);
return(0);
}
int get_telstat(typHOE *hg){
xmlrpc_value *resultP=NULL, *paramsP=NULL, *structP=NULL, *itemP=NULL;
int result, type;
int dash_r = 0;
gchar *dummy=NULL;
if(!hg->stat_initflag){
//fprintf(stderr, "initializing telstat\n");
if(init_telstat(hg)==-1){
return(-1);
}
}
// Construct parameters to function. It's a tuple, containing a
// dict of key/values, where the keys are the status aliases, and the
// values are all 0
structP = xmlrpc_struct_new(&hg->env);
//printf("aho\n");
{
itemP = xmlrpc_build_value(&hg->env, "i", 0);
xmlrpc_struct_set_value(&hg->env, structP, "TSCS.AZ", itemP);
xmlrpc_DECREF(itemP);
itemP = xmlrpc_build_value(&hg->env, "i", 0);
xmlrpc_struct_set_value(&hg->env, structP, "TSCS.AZ_CMD", itemP);
xmlrpc_DECREF(itemP);
itemP = xmlrpc_build_value(&hg->env, "i", 0);
xmlrpc_struct_set_value(&hg->env, structP, "TSCS.EL", itemP);
xmlrpc_DECREF(itemP);
itemP = xmlrpc_build_value(&hg->env, "i", 0);
xmlrpc_struct_set_value(&hg->env, structP, "TSCS.EL_CMD", itemP);
xmlrpc_DECREF(itemP);
itemP = xmlrpc_build_value(&hg->env, "s", &dummy);
xmlrpc_struct_set_value(&hg->env, structP, "FITS.SBR.MAINOBCP", itemP);
xmlrpc_DECREF(itemP);
}
paramsP = xmlrpc_array_new(&hg->env);
xmlrpc_array_append_item(&hg->env, paramsP, structP);
result = get_status(&hg->env);
if (result)
{
fprintf(stderr, "Error building parameters to fetch(), result=%d\n",
result);
printf_log(hg, "[TelStat] Error building parameters to fetch(), result=%d.",
result);
close_telstat(hg);
hg->telstat_error=TRUE;
if(resultP) xmlrpc_DECREF(resultP);
if(paramsP) xmlrpc_DECREF(paramsP);
if(structP) xmlrpc_DECREF(structP);
return(-1);
}
//printf("aho1\n");
// Call the remote method "fetch(dict)" in the Gen2 status server.
result = ro_callProxy_arg(hg->ro_proxyP, &resultP, "fetch", paramsP);
//printf("aho2\n");
if (result)
{
fprintf(stderr, "Error in remote call to fetch(), result=%d\n",
result);
printf_log(hg, "[TelStat] Error in remote call to fetch(), result=%d.",
result);
close_telstat(hg);
hg->telstat_error=TRUE;
if(resultP) xmlrpc_DECREF(resultP);
if(paramsP) xmlrpc_DECREF(paramsP);
if(structP) xmlrpc_DECREF(structP);
return(-1);
}
// Process return value, which should be a dictionary of alias: value
// pairs.
type = xmlrpc_value_type(resultP);
if (type != XMLRPC_TYPE_STRUCT)
{
fprintf(stderr, "Error in remote call to status, unexpected result type=%d\n", type);
printf_log(hg, "[TelStat] Error in remote call to status, unexpected result type=%d.", type);
close_telstat(hg);
hg->telstat_error=TRUE;
if(resultP) xmlrpc_DECREF(resultP);
if(paramsP) xmlrpc_DECREF(paramsP);
if(structP) xmlrpc_DECREF(structP);
return(-1);
}
hg->stat_initflag=TRUE;
{
gdouble dAz, dEl;
// Az
itemP = (xmlrpc_value *)NULL;
xmlrpc_struct_find_value(&hg->env, resultP, "TSCS.AZ", &itemP);
hg->stat_az = getStatus_double("TSCS.AZ", itemP, dash_r);
xmlrpc_DECREF(itemP);
// Az (Commanded)
itemP = (xmlrpc_value *)NULL;
xmlrpc_struct_find_value(&hg->env, resultP, "TSCS.AZ_CMD", &itemP);
hg->stat_az_cmd = getStatus_double("TSCS.AZ_CMD", itemP, dash_r);
xmlrpc_DECREF(itemP);
// El
itemP = (xmlrpc_value *)NULL;
xmlrpc_struct_find_value(&hg->env, resultP, "TSCS.EL", &itemP);
hg->stat_el = getStatus_double("TSCS.EL", itemP, dash_r);
xmlrpc_DECREF(itemP);
// El (Commanded)
itemP = (xmlrpc_value *)NULL;
xmlrpc_struct_find_value(&hg->env, resultP, "TSCS.EL_CMD", &itemP);
hg->stat_el_cmd = getStatus_double("TSCS.EL_CMD", itemP, dash_r);
xmlrpc_DECREF(itemP);
dAz=fabs(hg->stat_az - hg->stat_az_cmd);
dEl=fabs(hg->stat_el - hg->stat_el_cmd);
if( (dAz<0.5) && (dEl<0.5) ) {
hg->stat_fixflag = TRUE;
hg->stat_reachtime=0;
}
else{
hg->stat_fixflag = FALSE;
if( (dAz/hg->vel_az) > (dEl/hg->vel_el) )
hg->stat_reachtime=dAz/hg->vel_az;
else
hg->stat_reachtime=dEl/hg->vel_el;
}
if(hg->stat_az_cmd<-180){
hg->stat_az_check=hg->stat_az_cmd+360;
}
else if(hg->stat_az>180){
hg->stat_az_check=hg->stat_az_cmd-360;
}
else{
hg->stat_az_check=hg->stat_az_cmd;
}
// Allocated OBCP
itemP = (xmlrpc_value *)NULL;
xmlrpc_struct_find_value(&hg->env, resultP, "FITS.SBR.MAINOBCP", &itemP);
getStatus_string("FITS.SBR.MAINOBCP",
itemP, dash_r, &hg->stat_obcp);
xmlrpc_DECREF(itemP);
}
if(resultP) xmlrpc_DECREF(resultP);
if(paramsP) xmlrpc_DECREF(paramsP);
if(structP) xmlrpc_DECREF(structP);
return(0);
}
int close_telstat(typHOE *hg){
// Dispose of our result value.
//if(ro_freeProxy)
ro_freeProxy(hg->ro_proxyP, TRUE);
if(hg->stat_obcp) g_free(hg->stat_obcp);
hg->stat_obcp=NULL;
hg->stat_initflag=FALSE;
if(hg->telstat_timer!=-1){
g_source_remove(hg->telstat_timer);
hg->telstat_timer=-1;
}
hg->telstat_flag=FALSE;
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(hg->skymon_button_telstat),
FALSE);
//fprintf(stderr, "closing telstat\n");
}
#endif //USE_XMLRPC