MIDAS
Loading...
Searching...
No Matches
mchart.cxx
Go to the documentation of this file.
1/********************************************************************\
2
3 Name: mchart.c
4 Created by: Pierre-Andre Amaudruz
5
6 Contents: Periodic Midas chart graph data generator.
7 Generate the proper configuration and data files for
8 the gstripchart application or the GH (Gertjan Hofman)
9 stripchart.tcl
10 standard running description is:
11 mchart -f <file> -q <ODB tree> -c
12 ex: mchart -f trigger -q /equipment/trigger/statistics -c
13 produce trigger.conf and trigger (data)
14
15 mchart -f <file>[.conf] -g
16 ex: mchart -f trigger[.conf] -gg
17 produce periodically trigger and spawn gstripchart for
18 display of the data.
19 See mchart -h for further info.
20
21 $Id$
22
23\********************************************************************/
24
25#include <fcntl.h>
26#include "midas.h"
27#include "msystem.h"
28
29#define CONF_CREATE 1
30#define CONF_APPEND 2
31
32char color[][16] =
33 { "blue", "red", "brown", "black", "orange", "cyan", "yellow", "beige", "DarkCyan",
34 "DarkOliveGreen", "goldenrod", "DeepPink"
35};
40INT graph = 0, element = 0;
41
42INT mchart_compose(HNDLE hDB, char *confpath, char *datapath, char *eqpstr);
43INT conf_compose(INT action, char *confpath, char *datapath, char *field, float val);
44float toplimit = 0., botlimit = 0.;
45
46/*------------------------------------------------------------------*/
47INT conf_compose(INT action, char *confpath, char *datapath, char *field, float val)
48{
49 FILE *f;
50 float minval, maxval;
51
52 if (config_done)
53 return 0;
54
55 switch (action) {
56 case CONF_CREATE:
57 if (keep)
58 return 1;
59 /* open device */
60 f = fopen(confpath, "w+");
61 if (f == NULL) {
62 printf("Cannot open configuration strip file.\n");
63 return 0;
64 }
65 fprintf(f, "#Equipment: >%s", field);
66 fprintf(f, "\n");
67 fprintf(f, "menu: on\n");
68 fprintf(f, "slider: on\n");
69 fprintf(f, "type: gtk\n");
70
71 fprintf(f, "minor_ticks: 12\n");
72 fprintf(f, "major_ticks: 6\n");
73
74 fprintf(f, "chart-interval: 1.000\n");
75 fprintf(f, "chart-filter: 0.500\n");
76 fprintf(f, "slider-interval: 0.200\n");
77 fprintf(f, "slider-filter: 0.200\n");
78 fclose(f);
79 break;
80
81 case CONF_APPEND:
82 if (keep)
83 return 1;
84
85 f = fopen(confpath, "a+");
86 if (f == NULL) {
87 printf("Cannot open configuration strip file.\n");
88 return 0;
89 }
90 if ((element % max_element) == 0)
91 element = 0;
92
93 minval = maxval = val;
94 /* check upper limits */
95 if (toplimit == 0.) { /* use current value */
96 if (val == 0.)
97 maxval = 5.;
98 else if (val > 0.)
99 maxval *= 2.;
100 else
101 maxval /= 2.;
102 } else /* use arg value */
104
105 /* check lower limits */
106 if (botlimit == 0.) { /* use current value */
107 if (val == 0.)
108 minval = -5.;
109 else if (val > 0.)
110 minval /= 2.;
111 else
112 minval *= 2.;
113 } else /* use arg value */
115
116 fprintf(f, "begin: %s\n", field);
117 fprintf(f, " filename: %s\n", datapath);
118 fprintf(f, " fields: 2\n");
119 fprintf(f, " pattern: %s\n", field);
120 fprintf(f, " equation: $2\n");
121 fprintf(f, " color: %s\n", color[element++]);
122 fprintf(f, " maximum: %6.2f\n", maxval);
123 fprintf(f, " minimum: %6.2f\n", minval);
124 fprintf(f, " id_char: 1\n");
125 fprintf(f, "end: %s\n", field);
126 fclose(f);
127 break;
128 default:
129 printf(" unknown command \n");
130 }
131 return 1;
132}
133
134/*------------------------------------------------------------------*/
136{
137 char *pslash, *p;
138 char strtmp[128];
140 KEY key;
141 INT i, size;
142 BOOL bslash = FALSE;
143
144 /* convert to upper */
145 p = eqpstr;
146 for (i = 0; i < 128; i++)
147 strtmp[i] = 0;
148 int len_eqpstr = strlen(eqpstr);
149 for (i=0 ; i<len_eqpstr; i++)
150 eqpstr[i] = tolower(eqpstr[i]);
151
152 /* I don't remember what I'm doing here but it's useful! */
153 if (strncmp(eqpstr, "equipment/", 10) == 0)
154 bslash = TRUE;
155 if (strncmp(eqpstr, "/equipment/", 11) == 0) {
156 /* if the -q starts with /equipment and 2 more slash are following
157 OR
158 if the -q starts with equipment and 1 more slash are following
159 then I search the Settings for the names of the variables */
160 pslash = strchr(eqpstr, '/');
161 pslash = strchr(pslash + 1, '/');
162 if (!bslash)
163 pslash = strchr(pslash + 1, '/');
164 if (pslash) {
166 strcat(strtmp, "/settings");
167
168 if (db_find_key(hDB, 0, strtmp, &hKeyS) == DB_SUCCESS) {
170 for (i = 0;; i++) { /* search first for "Names element" */
172 if (!hSubKey)
173 break;
175 if (key.name[5] && key.type == TID_STRING) { /* fmt Names xxxx */
176 p = &(key.name[6]);
177 if (strstr(p, element) == NULL)
178 continue;
180 size = *esize * key.num_values;
181 *pname = (char*)malloc(size);
182 db_get_data(hDB, hSubKey, *pname, &size, key.type);
183 return key.num_values;
184 }
185 }
186 /* rescan dir for "Names" only now */
188 for (i = 0;; i++) {
190 if (!hSubKey)
191 break;
193 if (strncmp(key.name, "Names", 5) == 0) { /* fmt Names (only) */
195 size = *esize * key.num_values;
196 *pname = (char*)malloc(size);
197 db_get_data(hDB, hSubKey, *pname, &size, key.type);
198 return key.num_values;
199 }
200 }
201 }
202 }
203 }
204 return 0;
205}
206
207/*------------------------------------------------------------------*/
209 char *confpath, char *datapath)
210{
211 char field[128];
212 INT nfound, status, i, size, esize;
213 char *pspace, *pdata = NULL, *pname = NULL;
214 float value;
215
216 size = key.num_values * key.item_size;
217 pdata = (char*)malloc(size);
219 if (status != DB_SUCCESS) {
220 free(pdata);
221 return 0;
222 }
223
224 if (debug)
225 printf("%s : size:%d\n", key.name, size);
226
228
229 if (debug)
230 printf("#name:%d #Values:%d\n", nfound, key.num_values);
231 /* if names found but no array then use variable names */
232 if (key.num_values == 1)
233 nfound = FALSE;
234 for (i = 0; i < key.num_values; i++) {
235 if (nfound)
236 strcpy(field, (pname + (i * esize)));
237 else {
238 if (key.num_values == 1)
239 sprintf(field, "%s", key.name);
240 else /* if no settings/Names ... found then compose names here */
241 sprintf(field, "%s[%i]", key.name, i);
242 }
243
244 /* substitution for tcl */
245 while ((pspace = strstr(field, " ")))
246 *pspace = '_';
247 while ((pspace = strstr(field, "%")))
248 *pspace = '_';
249
250 /* fill data file */
251 if (key.type == TID_INT) {
252 value = (float) *((INT *) (pdata + (i * key.item_size)));
253 fprintf(f, "%s %d\n", field, *((INT *) (pdata + (i * key.item_size))));
254 } else if (key.type == TID_WORD) {
255 value = (float) *((WORD *) (pdata + (i * key.item_size)));
256 fprintf(f, "%s %d\n", field, *((WORD *) (pdata + (i * key.item_size))));
257 } else if (key.type == TID_DWORD) {
258 value = (float) *((DWORD *) (pdata + (i * key.item_size)));
259 fprintf(f, "%s %d\n", field, *((DWORD *) (pdata + (i * key.item_size))));
260 } else if (key.type == TID_FLOAT) {
261 value = *((float *) (pdata + (i * key.item_size)));
262 fprintf(f, "%s %f\n", field, *((float *) (pdata + (i * key.item_size))));
263 } else if (key.type == TID_DOUBLE) {
264 value = (float) *((double *) (pdata + (i * key.item_size)));
265 fprintf(f, "%s %e\n", field, *((double *) (pdata + (i * key.item_size))));
266 } else
267 continue;
268
269 if (!config_done) {
270 if (debug)
271 printf("Field:%s Values:%f\n", field, value);
273 }
274 }
275 if (pdata)
276 free(pdata);
277 if (pname)
278 free(pname);
279 return 1;
280}
281
282/*------------------------------------------------------------------*/
284{
285 FILE *fHandle;
286 INT i, status;
287 KEY key;
289
290 /* check if dir exists */
291 if ((status = db_find_key(hDB, 0, eqpstr, &hKey)) == DB_SUCCESS) {
292 fHandle = fopen(datapath, "w+");
293 if (fHandle == NULL) {
294 printf("Cannot open config %s file.\n", confpath);
295 return -1;
296 }
297
299 if (key.type != TID_KEY) {
301 if (status < 1) {
302 printf("Error: cannot get array for %s\n", eqpstr);
303 return -2;
304 }
305 } else
306 for (i = 0;; i++) {
308 if (!hSubkey)
309 break;
311 status =
313 if (status < 1) {
314 printf("Error: cannot get array for %s\n", eqpstr);
315 return -3;
316 }
317 } /* get value */
318 } /* equ found */
319 else {
320 printf("Error: Equipment:%s not found\n", eqpstr);
321 return 0;
322 }
325 return 1;
326}
327
328/*------------------------------------------------------------------*/
329int main(int argc, char **argv)
330{
331 char command[512];
333 INT status, last_time = 0;
334 //DWORD i;
335 HNDLE hDB, hKey;
337 char eqpstr[128] = { '\0' };
338 char cmdline[256];
339 signed char ch;
340 char mchart_dir[128] = { '\0' };
341 char mchart_data[128];
342 char mchart_conf[128+10];
343 INT msg, childpid;
344 int sys_err;
345 /* set default */
347 delta_time = 5000;
348 daemon = FALSE;
349
350 /* retrieve environment */
351 if (getenv("MCHART_DIR")) {
352 strcpy(mchart_dir, getenv("MCHART_DIR"));
355 }
356
357 /* get parameters parse command line parameters */
358 for (int i = 1; i < argc; i++) {
359 if (argv[i][0] == '-' && argv[i][1] == 'd')
360 debug = TRUE;
361 else if (argv[i][0] == '-' && argv[i][1] == 'c')
363 else if (argv[i][0] == '-' && argv[i][1] == 's')
364 keep = TRUE;
365 else if (argv[i][0] == '-' && argv[i][1] == 'g' && argv[i][2] == 'g')
366 graph = 2;
367 else if (argv[i][0] == '-' && argv[i][1] == 'g' && argv[i][2] == 'h')
368 graph = 3;
369 else if (argv[i][0] == '-' && argv[i][1] == 'g')
370 graph = 1;
371 else if (argv[i][0] == '-' && argv[i][1] == 'k')
373 else if (argv[i][0] == '-' && argv[i][1] == 'D')
374 daemon = TRUE;
375 else if (argv[i][0] == '-') {
376 if (i + 1 >= argc || (argv[i + 1][0] == '-' && !isdigit(argv[i + 1][1])))
377 goto usage;
378 if (strncmp(argv[i], "-u", 2) == 0)
379 delta_time = 1000 * (atoi(argv[++i]));
380 else if (strncmp(argv[i], "-f", 2) == 0) {
381 char *pt;
382 pt = strstr(argv[++i], ".conf");
383 if (pt)
384 *pt = '\0';
385 strcpy(mchart_data, mchart_dir);
387 sprintf(mchart_conf, "%s.conf", mchart_data);
388 } else if (strncmp(argv[i], "-e", 2) == 0)
389 strcpy(expt_name, argv[++i]);
390 else if (strncmp(argv[i], "-h", 2) == 0)
391 strcpy(host_name, argv[++i]);
392 else if (strncmp(argv[i], "-q", 2) == 0) {
393 if (argv[++i][0] != '/')
394 strcpy(eqpstr, "/");
395 strcat(eqpstr, argv[i]);
396 create = TRUE;
397 } else if (strncmp(argv[i], "-b", 2) == 0)
398 botlimit = (float) atof(argv[++i]);
399 else if (strncmp(argv[i], "-t", 2) == 0)
400 toplimit = (float) atof(argv[++i]);
401 } else {
402 usage:
403 printf("usage: mchart -u updates (5sec)\n");
404 printf(" -f configuration or data file name\n");
405 printf(" -q equipment\n");
406 printf(" (override all) -b bot_limit -t top_limit (only with -q) \n");
407 printf(" -c create config only \n");
408 printf(" -k keep spawned stripchart or gstripchart\n");
409 printf
410 (" -g spawn Hofman stripchart or gstripchart if possible\n");
411 printf(" -gg forces gstripchart spawning\n");
412 printf(" -gh forces stripchart spawning\n");
413 printf(" -D start as daemon (prevented if -c or -q)\n");
414 printf(" requires $MCHART_DIR\n");
415 printf(" [-h Hostname] [-e Experiment]\n\n");
416 printf("ex: Creation : mchart -e myexpt -h myhost -c \n");
417 printf(" -f mydata -q /equipement/myeqp/variables\n");
418 printf(" Running : mchart -e myexpt -h myhost -f mydata \n");
419 printf(" Run/Graph : mchart -e myexpt -h myhost -f mydata -g\n");
420 printf(" MCHART_DIR: environment variable for mchar directory\n\n");
421 return 0;
422 }
423 }
424
425/* Daemon start */
426 if (daemon && !(create || create_only)) {
427 printf("Becoming a daemon...\n");
429 }
430
431 if (daemon)
432 printf("arg -D ignored due to creation request! \n");
433
434/* connect to experiment */
436 if (status != CM_SUCCESS)
437 return 1;
438
439 if (debug)
441
442 /* turn off message display, turn on message logging */
444
445 /* connect to the database */
447
448 /* initialize ss_getchar() */
449 ss_getchar(0);
450
451 /* recompose command line */
452 sprintf(cmdline, "%s ", argv[1]);
453 for (int i = 2; i < argc; i++) {
454 strcat(cmdline, " ");
455 strcat(cmdline, argv[i]);
456 }
457
458 /* check if creation required */
459 if (!create) {
460 /* extract equipment key */
461 FILE *f;
462 char strtmp[128];
463 char *peqp;
464 char *s;
465
467 create = FALSE;
468 /* Overwrite the -q using the file.conf content */
469 f = fopen(mchart_conf, "r");
470 if (f == NULL) {
471 printf("Error: Cannot open %s\n", mchart_conf);
472 goto error;
473 }
474 s = fgets(strtmp, 128, f);
475 if (s == NULL) {
476 printf("Error: Cannot read %s\n", mchart_conf);
477 goto error;
478 }
479 if (strstr(strtmp, "#Equipment")) {
480 peqp = strstr(strtmp, ">");
481 sprintf(eqpstr, "%s", peqp + 1);
482 }
483 fclose(f);
484 peqp = strstr(eqpstr, "\n");
485 *peqp = 0;
486 } else {
487 /* creation of config file requested */
488 /* check if equipment string is a valifd key in order to
489 prevent overwriting the configuration file */
490 if (db_find_key(hDB, 0, eqpstr, &hKey) != DB_SUCCESS) {
491 printf("unknown odb path under -q arg. (%s)\n", eqpstr);
492 goto error;
493 }
495 if (status != 1)
496 goto error;
497 }
498
499 do {
500 if ((ss_millitime() - last_time) > delta_time) {
503 if (status != 1)
504 goto error;
505 if (create_only) {
506 printf("mchart file %s created\n", mchart_conf);
507 goto out;
508 }
509 }
510
511 /* spawn graph once if possible */
512 if (graph == 1) {
513 char command[512];
514 char *list = NULL;
515 INT i, nfile;
516
517 char strip[][32] = { "/usr/local/bin", "stripchart",
518 "/usr/bin", "gstripchart",
519 "/home/midas/bin", "stripchart",
520 "/usr/sbin", "gstripchart",
521 "/home/chaos/bin", "stripchart",
522 "/sbin", "gstripchart",
523 ""
524 };
525
526 i = 0;
527 while (strip[i][0]) {
528 nfile = ss_file_find(strip[i], strip[i + 1], &list);
529 free(list);
530 if (nfile == 1)
531 break;
532 i += 2;
533 }
534 if (strip[i][0] == 0) {
535 printf("No graphic package found in following dir:\n");
536 i = 0;
537 while (strip[i][0]) {
538 printf("Package : %s/%s \n", strip[i], strip[i + 1]);
539 i += 2;
540 }
541 break;
542 }
543 if (i % 2)
544 sprintf(command, "gstripchart -g 500x200-200-800 -f %s", mchart_conf);
545 else
546 sprintf(command, "stripchart %s", mchart_conf);
547
548 if (!daemon)
549 printf("Spawning graph with %s ...\nUse '!' to exit\n", command);
550 ss_exec(command, &childpid);
551 } else if (graph == 2) { /* Gnu graph */
552 sprintf(command, "gstripchart -g 500x200-200-800 -f %s", mchart_conf);
553 if (!daemon)
554 printf("Spawning graph with %s ...\nUse '!' to exit\n", command);
555 ss_exec(command, &childpid);
556 } else if (graph == 3) { /* Hofman graph */
557 if (debug) {
558 sprintf(command, "stripchart -debug %s", mchart_conf);
559 if (!daemon)
560 printf("Spawning graph with %s ...in DEBUG mode \nUse '!' to exit\n",
561 command);
562 sys_err = system(command);
563 printf("system error return %d \n", sys_err);
565 return 1;
566
567 } else {
568 sprintf(command, "stripchart %s", mchart_conf);
569 if (!daemon)
570 printf("Spawning graph with %s .... \nUse '!' to exit\n", command);
571 ss_exec(command, &childpid);
572 }
573
574 }
575
576 graph = 0;
577 ch = 0;
578 while (ss_kbhit()) {
579 ch = ss_getchar(0);
580 if (ch == -1)
581 ch = getchar();
582 if (ch == 'R')
584 if ((char) ch == '!')
585 break;
586 }
587 msg = cm_yield(200);
588
589 /* exit this process if no stripchart associated to it */
591 break;
592
593 } while (msg != RPC_SHUTDOWN && msg != SS_ABORT && ch != '!');
594
595 out:
596 error:
597 printf("\n");
598
599 /* reset terminal */
601
603 return 1;
604}
#define FALSE
Definition cfortran.h:309
static void usage()
INT cm_yield(INT millisec)
Definition midas.cxx:5642
INT cm_get_experiment_database(HNDLE *hDB, HNDLE *hKeyClient)
Definition midas.cxx:3011
INT cm_connect_experiment(const char *host_name, const char *exp_name, const char *client_name, void(*func)(char *))
Definition midas.cxx:2278
INT cm_disconnect_experiment(void)
Definition midas.cxx:2846
INT cm_get_environment(char *host_name, int host_name_size, char *exp_name, int exp_name_size)
Definition midas.cxx:2134
INT cm_set_watchdog_params(BOOL call_watchdog, DWORD timeout)
Definition midas.cxx:3283
#define CM_SUCCESS
Definition midas.h:582
#define DB_SUCCESS
Definition midas.h:631
#define SS_ABORT
Definition midas.h:677
#define RPC_SHUTDOWN
Definition midas.h:707
unsigned short int WORD
Definition mcstd.h:49
unsigned int DWORD
Definition mcstd.h:51
#define TID_DOUBLE
Definition midas.h:343
#define TID_KEY
Definition midas.h:349
#define MT_ALL
Definition midas.h:549
#define TID_WORD
Definition midas.h:332
#define TID_STRING
Definition midas.h:346
#define TID_INT
Definition midas.h:338
#define TID_FLOAT
Definition midas.h:341
#define TID_DWORD
Definition midas.h:336
BOOL ss_kbhit()
Definition system.cxx:3664
DWORD ss_millitime()
Definition system.cxx:3393
INT ss_getchar(BOOL reset)
Definition system.cxx:7503
INT ss_daemon_init(BOOL keep_stdout)
Definition system.cxx:2001
INT ss_exec(const char *command, INT *pid)
Definition system.cxx:2132
void ss_clear_screen()
Definition system.cxx:7299
BOOL ss_existpid(INT pid)
Definition system.cxx:2068
INT ss_file_find(const char *path, const char *pattern, char **plist)
Definition system.cxx:6713
INT cm_set_msg_print(INT system_mask, INT user_mask, int(*func)(const char *))
Definition midas.cxx:647
INT db_get_data(HNDLE hDB, HNDLE hKey, void *data, INT *buf_size, DWORD type)
Definition odb.cxx:6539
INT db_get_key(HNDLE hDB, HNDLE hKey, KEY *key)
Definition odb.cxx:6019
INT db_find_key(HNDLE hDB, HNDLE hKey, const char *key_name, HNDLE *subhKey)
Definition odb.cxx:4079
INT db_enum_key(HNDLE hDB, HNDLE hKey, INT idx, HNDLE *subkey_handle)
Definition odb.cxx:5586
int main()
Definition hwtest.cxx:23
HNDLE hKey
DWORD last_time
Definition mana.cxx:3070
BOOL daemon
Definition mana.cxx:258
HNDLE hDB
main ODB handle
Definition mana.cxx:207
char host_name[HOST_NAME_LENGTH]
Definition mana.cxx:242
float botlimit
Definition mchart.cxx:44
INT conf_compose(INT action, char *confpath, char *datapath, char *field, float val)
Definition mchart.cxx:47
BOOL create_only
Definition mchart.cxx:38
char color[][16]
Definition mchart.cxx:32
DWORD delta_time
Definition mchart.cxx:37
INT mchart_get_names(HNDLE hDB, char *eqpstr, char *element, char **pname, INT *esize)
Definition mchart.cxx:135
INT mchart_get_array(FILE *f, char *eqpstr, HNDLE hDB, HNDLE hSubkey, KEY key, char *confpath, char *datapath)
Definition mchart.cxx:208
BOOL create
Definition mchart.cxx:39
#define CONF_APPEND
Definition mchart.cxx:30
INT element
Definition mchart.cxx:40
BOOL debug
debug printouts
Definition mchart.cxx:38
BOOL config_done
Definition mchart.cxx:39
INT graph
Definition mchart.cxx:40
#define CONF_CREATE
Definition mchart.cxx:29
INT mchart_compose(HNDLE hDB, char *confpath, char *datapath, char *eqpstr)
Definition mchart.cxx:283
BOOL keep
Definition mchart.cxx:38
float toplimit
Definition mchart.cxx:44
INT max_element
Definition mchart.cxx:36
KEY key
Definition mdump.cxx:34
INT i
Definition mdump.cxx:32
HNDLE hSubkey
Definition mdump.cxx:35
char expt_name[NAME_LENGTH]
Definition mevb.cxx:44
#define DIR_SEPARATOR
Definition midas.h:193
INT HNDLE
Definition midas.h:132
#define HOST_NAME_LENGTH
Definition midas.h:273
DWORD BOOL
Definition midas.h:105
#define DIR_SEPARATOR_STR
Definition midas.h:194
int INT
Definition midas.h:129
#define TRUE
Definition midas.h:182
#define NAME_LENGTH
Definition midas.h:272
double value[100]
Definition odbhist.cxx:42
DWORD status
Definition odbhist.cxx:39
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
Definition midas.h:1026
INT num_values
Definition midas.h:1028
DWORD type
Definition midas.h:1027
char name[NAME_LENGTH]
Definition midas.h:1029
INT item_size
Definition midas.h:1032
static te_expr * list(state *s)
Definition tinyexpr.c:567