Mercurial > hg > fxanalyse
comparison sr-data-logger.c @ 169:97112b45b838
Get Sr data logger parameters from configuration file. Code reorganization.
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Fri, 14 Feb 2014 16:58:19 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
168:4de7f2c9f328 | 169:97112b45b838 |
---|---|
1 #include <ansi_c.h> | |
2 #include <utility.h> | |
3 #include <inifile.h> | |
4 #include <tcpsupp.h> | |
5 | |
6 #include "config.h" | |
7 #include "logging.h" | |
8 #include "sr-data-logger.h" | |
9 | |
10 | |
11 #define CONF_SECTION_NAME "Sr data logger" | |
12 | |
13 | |
14 int sr_datalogger_init(struct datalogger *dl) | |
15 { | |
16 int rv; | |
17 char path[MAX_PATHNAME_LEN]; | |
18 | |
19 /* load configuration file */ | |
20 GetIniFilePath(path); | |
21 IniText c = Ini_New(TRUE); | |
22 Ini_ReadFromFile(c, path); | |
23 | |
24 /* read configuration */ | |
25 rv = Ini_GetStringCopy(c, CONF_SECTION_NAME, "id", &(dl->id)); | |
26 if (rv <= 0) | |
27 return -1; | |
28 rv = Ini_GetStringCopy(c, CONF_SECTION_NAME, "host", &(dl->host)); | |
29 if (rv <= 0) | |
30 return -1; | |
31 rv = Ini_GetInt(c, CONF_SECTION_NAME, "port", &(dl->port)); | |
32 if (rv <= 0) | |
33 return -1; | |
34 | |
35 /* dispose configuration */ | |
36 Ini_Dispose(c); | |
37 | |
38 return 0; | |
39 } | |
40 | |
41 | |
42 void sr_datalogger_dispose(struct datalogger *dl) | |
43 { | |
44 DisconnectFromTCPServer(dl->sock); | |
45 free(dl->host); | |
46 free(dl->id); | |
47 } | |
48 | |
49 | |
50 static int sr_datalogger_connect(struct datalogger *dl) | |
51 { | |
52 return ConnectToTCPServer(&(dl->sock), dl->port, dl->host, NULL, NULL, 1); | |
53 } | |
54 | |
55 | |
56 /* MJD functiom used by the Sr programs */ | |
57 static inline double utc2mjd(double utc) | |
58 { | |
59 return 15020.0 + utc / 86400.0; | |
60 } | |
61 | |
62 | |
63 int __sr_datalogger_send(struct datalogger *dl, double utc, double data) | |
64 { | |
65 int rv; | |
66 char buffer[1024]; | |
67 | |
68 /* connect */ | |
69 if (! dl->sock) { | |
70 rv = sr_datalogger_connect(dl); | |
71 if (rv < 0) | |
72 return rv; | |
73 logmessage(INFO, "connected to Sr data logger %s:%d", dl->host, dl->port); | |
74 } | |
75 | |
76 snprintf(buffer, sizeof(buffer), "%s %.7f %.8f", dl->id, utc2mjd(utc), data); | |
77 rv = ClientTCPWrite(dl->sock, buffer, strlen(buffer) + 1, 0); | |
78 if (rv < 0) { | |
79 /* reconnect and resend */ | |
80 dl->sock = 0; | |
81 __sr_datalogger_send(dl, utc, data); | |
82 } | |
83 | |
84 return 0; | |
85 } | |
86 |