Mercurial > hg > fxanalyse
changeset 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 | 4de7f2c9f328 |
children | a4fcebb5941b |
files | FXAnalyse.c config.h data-provider.c logging.c logging.h sr-data-logger.c sr-data-logger.h utils.h |
diffstat | 8 files changed, 229 insertions(+), 83 deletions(-) [+] |
line wrap: on
line diff
--- a/FXAnalyse.c Fri Feb 14 16:58:08 2014 +0100 +++ b/FXAnalyse.c Fri Feb 14 16:58:19 2014 +0100 @@ -17,10 +17,11 @@ #include "stat.h" #include "future.h" #include "data-provider.h" +#include "sr-data-logger.h" +#include "config.h" +#include "logging.h" -#define CONFIGFILE "FXAnalyse.ini" -#define SR_LOGGER_IP "145.238.204.91" #define DATAFOLDER "Z:\\Measures-2014" #define FREP_STEP_SIZE 50000.0 @@ -51,14 +52,11 @@ double N1, N2, N3; double Ndiv = 8.0; double Sign1 = 1.0, Sign2 = 1.0, Sign3 = 0.0; - - void *MathParser1, *MathParser2, *MathParser3, *MathParser4, *MathParser5; // panels static int MainPanel; -static int LoggingPanel; static int CalcNPanel; static int EstimateNPanel; @@ -334,60 +332,7 @@ } -void logmsg(const char *frmt, ...) -{ - char msg[1024]; - int len = 0; - - // timestamp - len += sprintf(msg, "%014.3f ", utc); - time_t now = time(NULL); - struct tm *t = localtime(&now); - len += strftime(msg + len, sizeof(msg) - len, "%d-%m-%Y %H:%M:%S ", t); - - // message - va_list args; - va_start(args, frmt); - len += vsnprintf(msg + len, sizeof(msg) - len, frmt, args); - va_end(args); - - // add newline - len = MIN(len, sizeof(msg) - 2); - msg[len] = '\n'; - msg[len + 1] = '\0'; - - // display message - SetCtrlVal(LoggingPanel, LOGGING_LOGGING, msg); -} - - -int Sr_datalogger_enabled = FALSE; - - -// Sr data logger -int Sr_datalogger_send(const char* id, double utc, double data) -{ - static unsigned int handle = 0; - char buffer[1024]; - - // try to connect and quit if unsuccessfull - if (handle == 0) { - if (ConnectToTCPServer(&handle, 3491, SR_LOGGER_IP, NULL, NULL, 1) < 0) { - logmsg("Sr data logger connection error"); - return -1; - } - logmsg("connected to Sr data logger"); - } - - snprintf(buffer, sizeof(buffer), "%s %.7f %.8f", id, utc2mjd(utc), data); - if (ClientTCPWrite(handle, buffer, strlen(buffer) + 1, 0) < 0) { - // try to reconnect and resend - handle = 0; - Sr_datalogger_send(id, utc, data); - } - - return 0; -} +static struct datalogger datalogger; void * muParserNew() @@ -441,22 +386,19 @@ if ((MainPanel = LoadPanel (0, "FXAnalyse.uir", PANEL)) < 0) return -1; - if ((LoggingPanel = LoadPanel (0, "FXAnalyse.uir", LOGGING)) < 0) - return -1; if ((CalcNPanel = LoadPanel (MainPanel, "FXAnalyse.uir", CALCN)) < 0) return -1; if ((EstimateNPanel = LoadPanel (MainPanel, "FXAnalyse.uir", ESTIMATEN)) < 0) return -1; - // construct configuration file path - char pathname[MAX_PATHNAME_LEN]; - char project[MAX_PATHNAME_LEN]; - GetProjectDir(project); - MakePathname(project, CONFIGFILE, pathname); - + // logging + logger_init(); + // load configuration file + char path[MAX_PATHNAME_LEN]; + GetIniFilePath(path); IniText configuration = Ini_New(TRUE); - Ini_ReadFromFile(configuration, pathname); + Ini_ReadFromFile(configuration, path); // get AD9956 configuration parameters char host[256]; @@ -475,6 +417,9 @@ // dispose configuration Ini_Dispose(configuration); + // Sr data logger + sr_datalogger_init(&datalogger); + // dedrift DDS DDSFox_Initialize(&DDS1xAD9956, host, port, dedrift.freq0); @@ -619,7 +564,7 @@ case EVENT_COMMIT: if (acquiring) break; - + logmsg("start"); SetCtrlAttribute(panel, PANEL_STARTBUTTON, ATTR_DIMMED, TRUE); acquiring = 1; @@ -1324,8 +1269,7 @@ datafile_append(d, id, timestr); // send Sr frequency (Math4) to Sr data logger - if (Sr_datalogger_enabled) - Sr_datalogger_send("FEMTO2", utc, Math4); + sr_datalogger_send(&datalogger, utc, Math4); } break; } @@ -2055,7 +1999,7 @@ { case EVENT_COMMIT: GetCtrlVal(panel, PANEL_SHOWLOG, &visible); - SetPanelAttribute(LoggingPanel, ATTR_VISIBLE, visible); + logger_panel_visible(visible); break; } return 0; @@ -2067,7 +2011,7 @@ switch (event) { case EVENT_CLOSE: - SetPanelAttribute(LoggingPanel, ATTR_VISIBLE, 0); + logger_panel_visible(0); SetCtrlVal(MainPanel, PANEL_SHOWLOG, 0); break; } @@ -2155,7 +2099,7 @@ switch (event) { case EVENT_COMMIT: - GetCtrlVal(panel, control, &Sr_datalogger_enabled); + GetCtrlVal(panel, control, &datalogger.enabled); break; } return 0;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/config.h Fri Feb 14 16:58:19 2014 +0100 @@ -0,0 +1,14 @@ +#ifndef __CONFIG_H__ +#define __CONFIG_H__ + +#define CONFIGFILE "FXAnalyse.ini" + +#define GetIniFilePath(path) \ + do { \ + char project[MAX_PATHNAME_LEN]; \ + GetProjectDir(project); \ + MakePathname(project, CONFIGFILE, path); \ + } while (0) + + +#endif
--- a/data-provider.c Fri Feb 14 16:58:08 2014 +0100 +++ b/data-provider.c Fri Feb 14 16:58:19 2014 +0100 @@ -1,22 +1,23 @@ #include <ansi_c.h> #include <userint.h> +#include "logging.h" + /* message buffer */ #define DATA_PROVIDER_MSGLEN 256 #define DATA_PROVIDER_MSGNUM 16 static char messages[DATA_PROVIDER_MSGLEN][DATA_PROVIDER_MSGNUM]; static unsigned int messageid = 0; -/* logging function defined in main program */ -extern void logmsg(const char *frmt, ...); /* message callback */ void CVICALLBACK MessageCB (void *msg) { if (msg != NULL) - logmsg(msg); + logmessage(INFO, msg); } + void SendMessage(int threadId, const char *frmt, ...) { /* message buffer */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/logging.c Fri Feb 14 16:58:19 2014 +0100 @@ -0,0 +1,53 @@ +#include <ansi_c.h> + +#include "logging.h" + +/* this refers to the event time defined as a global variable in the main program */ +extern double utc; + + +int __logger_init(struct logger *l) +{ + int panel = LoadPanel(0, "FXAnalyse.uir", LOGGING); + if (panel < 0) + return -1; + l->panel = panel; + return 0; +} + + +void __logmessage(struct logger *l, enum loglevel level, const char *frmt, ...) +{ + static const char levels[][16] = { + "DEBUG", + "INFO", + "WARNING", + "ERROR", + }; + + char msg[1024]; + int len = 0; + + // timestamp + len += sprintf(msg, "%014.3f ", utc); + time_t now = time(NULL); + struct tm *t = localtime(&now); + len += strftime(msg + len, sizeof(msg) - len, "%d-%m-%Y %H:%M:%S ", t); + + // level + len += snprintf(msg + len, sizeof(msg) - len, "%s: ", levels[level]); + + // message + va_list args; + va_start(args, frmt); + len += vsnprintf(msg + len, sizeof(msg) - len, frmt, args); + va_end(args); + + // add newline + len = MIN(len, sizeof(msg) - 2); + msg[len] = '\n'; + msg[len + 1] = '\0'; + + // display message + SetCtrlVal(l->panel, LOGGING_LOGGING, msg); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/logging.h Fri Feb 14 16:58:19 2014 +0100 @@ -0,0 +1,28 @@ +#ifndef __LOGGING_H__ +#define __LOGGING_H__ + +/* required for the user interface defines */ +#include "FXAnalyse.h" +#include "utils.h" + +struct logger { + unsigned int panel; + int fd; +} __logger; + +enum loglevel { + DEBUG, + INFO, + WARNING, + ERROR, +}; + +int __logger_init(struct logger *l); +void __logmessage(struct logger *l, enum loglevel level, const char *frmt, ...); + +#define logger_init() __logger_init(&__logger) +#define logmessage(level, msg, ...) __logmessage(&__logger, (level), (msg), ##__VA_ARGS__) +#define logmsg(msg, ...) __logmessage(&__logger, INFO, msg, ##__VA_ARGS__) +#define logger_panel_visible(visible) SetPanelAttribute(__logger.panel, ATTR_VISIBLE, visible) + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sr-data-logger.c Fri Feb 14 16:58:19 2014 +0100 @@ -0,0 +1,86 @@ +#include <ansi_c.h> +#include <utility.h> +#include <inifile.h> +#include <tcpsupp.h> + +#include "config.h" +#include "logging.h" +#include "sr-data-logger.h" + + +#define CONF_SECTION_NAME "Sr data logger" + + +int sr_datalogger_init(struct datalogger *dl) +{ + int rv; + char path[MAX_PATHNAME_LEN]; + + /* load configuration file */ + GetIniFilePath(path); + IniText c = Ini_New(TRUE); + Ini_ReadFromFile(c, path); + + /* read configuration */ + rv = Ini_GetStringCopy(c, CONF_SECTION_NAME, "id", &(dl->id)); + if (rv <= 0) + return -1; + rv = Ini_GetStringCopy(c, CONF_SECTION_NAME, "host", &(dl->host)); + if (rv <= 0) + return -1; + rv = Ini_GetInt(c, CONF_SECTION_NAME, "port", &(dl->port)); + if (rv <= 0) + return -1; + + /* dispose configuration */ + Ini_Dispose(c); + + return 0; +} + + +void sr_datalogger_dispose(struct datalogger *dl) +{ + DisconnectFromTCPServer(dl->sock); + free(dl->host); + free(dl->id); +} + + +static int sr_datalogger_connect(struct datalogger *dl) +{ + return ConnectToTCPServer(&(dl->sock), dl->port, dl->host, NULL, NULL, 1); +} + + +/* MJD functiom used by the Sr programs */ +static inline double utc2mjd(double utc) +{ + return 15020.0 + utc / 86400.0; +} + + +int __sr_datalogger_send(struct datalogger *dl, double utc, double data) +{ + int rv; + char buffer[1024]; + + /* connect */ + if (! dl->sock) { + rv = sr_datalogger_connect(dl); + if (rv < 0) + return rv; + logmessage(INFO, "connected to Sr data logger %s:%d", dl->host, dl->port); + } + + snprintf(buffer, sizeof(buffer), "%s %.7f %.8f", dl->id, utc2mjd(utc), data); + rv = ClientTCPWrite(dl->sock, buffer, strlen(buffer) + 1, 0); + if (rv < 0) { + /* reconnect and resend */ + dl->sock = 0; + __sr_datalogger_send(dl, utc, data); + } + + return 0; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sr-data-logger.h Fri Feb 14 16:58:19 2014 +0100 @@ -0,0 +1,26 @@ +#ifndef __SR_DATA_LOGGER_H__ +#define __SR_DATA_LOGGER_H__ + +struct datalogger { + int enabled; + unsigned int sock; + /* configuration parameters */ + char *id; + char *host; + int port; +}; + + +int sr_datalogger_init(struct datalogger *dl); +void sr_datalogger_dispose(struct datalogger *dl); +int __sr_datalogger_send(struct datalogger *dl, double utc, double data); + + +static inline int sr_datalogger_send(struct datalogger *dl, double utc, double data) +{ + if (dl->enabled) + return __sr_datalogger_send(dl, utc, data); + return 0; +} + +#endif
--- a/utils.h Fri Feb 14 16:58:08 2014 +0100 +++ b/utils.h Fri Feb 14 16:58:19 2014 +0100 @@ -15,12 +15,6 @@ /* format floating points numbers accordingly to fmt and asdd thousands separator */ const char * thousands(char *buffer, int size, char *fmt, double val); -/* MJD functiom used by the Sr programs */ -static inline double utc2mjd(double utc) -{ - return 15020.0 + utc / 86400.0; -} - double Peta(double x); double Tera(double x); double Giga(double x);