EPICS Controls Argonne National Laboratory

Experimental Physics and
Industrial Control System

1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  <20192020  2021  2022  2023  2024  Index 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  <20192020  2021  2022  2023  2024 
<== Date ==> <== Thread ==>

Subject: Re: MySQL Statement Doesn't Work
From: "Arms, Dohn A. via Tech-talk" <[email protected]>
To: "[email protected]" <[email protected]>
Date: Wed, 4 Dec 2019 15:53:26 +0000
Hello,

I am not saying this solves your problem, but you are creating connections to the MySQL database with every event,
and then not closing them, which will leak memory and sockets. You either need to close the connection after
writing the event, or just connect once at the start of the program.

Dohn



On 12/4/19 7:12 AM, Aaron Brown via Tech-talk wrote:
Hello all,

I have added a MySQL statement into the caMonitor code below, but for some reason it will not populate the database.
I have checked to make sure that the values are being passed and stored properly and that the connection to the database is valid,
but for some reason this still will not actually populate the database.

Any thoughts, or suggestions would be greatly appreciated.

Thanks,
Aaron Brown
Detector Support Group
Jefferson Lab

/*caMonitor.c*/

/* This example accepts the name of a file containing a list of pvs to monitor.
 * It prints a message for all ca events: connection, access rights and monitor.
 */

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <mysql/mysql.h>

#include "cadef.h"
#include "dbDefs.h"
#include "epicsString.h"
#include "cantProceed.h"

#define MAX_PV 1000
#define MAX_PV_NAME_LEN 40

typedef struct{
    char value[20];
    chid mychid;
    evid myevid;
} MYNODE;

static void printChidInfo(chid chid, char *message)
{
    printf("\n%s\n",message);
    printf("pv: %s  type(%d) nelements(%ld) host(%s)",
ca_name(chid),ca_field_type(chid),ca_element_count(chid),
ca_host_name(chid));
    printf(" read(%d) write(%d) state(%d)\n",
ca_read_access(chid),ca_write_access(chid),ca_state(chid));
}

static void exceptionCallback(struct exception_handler_args args)
{
    chid chid = args.chid;
    long stat = args.stat; /* Channel access status code*/
    const char  *channel;
    static char *noname = "unknown";

    channel = (chid ? ca_name(chid) : noname);


    if(chid) printChidInfo(chid,"exceptionCallback");
    printf("exceptionCallback stat %s channel %s\n",
        ca_message(stat),channel);
}

static void connectionCallback(struct connection_handler_args args)
{
    chid chid = args.chid;

    printChidInfo(chid,"connectionCallback");
}

static void accessRightsCallback(struct access_rights_handler_args args)
{
    chid chid = args.chid;

    printChidInfo(chid,"accessRightsCallback");
}
static void eventCallback(struct event_handler_args eha)
{
    chid chid = eha.chid;
   
    char        query[2000];
    MYSQL *mysql_init(MYSQL *mysql);
    MYSQL *MySQLConRet;
    MYSQL *conn;
    conn = mysql_init(NULL);
    MySQLConRet = mysql_real_connect(conn, NULL, "root", NULL, "testAMB", 0, NULL, 0);
   
    if (conn == NULL)
    {
        printf("ERROR");
    }

    time_t timer;
    char buffer[26];
    struct tm* tm_info;
   
    time(&timer);
    tm_info = localtime(&timer);
   
    strftime(buffer, 26, "%d-%m-%Y--%H:%M:%S", tm_info);

    if(eha.status!=ECA_NORMAL) {
printChidInfo(chid,"eventCallback");
    } else {
char *pdata = (char *)eha.dbr;

        sprintf(query, "INSERT INTO PV_Table (Timestamp,Name,Value) VALUES (%s, %s, %s)", buffer, ca_name(eha.chid), pdata);

printf("%s %s %s\n",buffer,ca_name(eha.chid),pdata);
           
        mysql_query(conn, query);
       
    }
}

int main(int argc,char **argv)
{
    char *filename;
    int npv = 0;
    MYNODE *pmynode[MAX_PV];
    char *pname[MAX_PV];
    int i;
    char tempStr[MAX_PV_NAME_LEN];
    char *pstr;
    FILE *fp;
   

    if (argc != 2) {
        fprintf(stderr,"usage: caMonitor filename\n");
        exit(1);
    }
    filename = argv[1];
    fp = fopen(filename,"r");
    if (!fp) {
        perror("fopen failed");
        return(1);
    }
    while (npv < MAX_PV) {
        size_t len;

        pstr = fgets(tempStr, MAX_PV_NAME_LEN, fp);
        if (!pstr) break;

        len = strlen(pstr);
        if (len <= 1) continue;

        pstr[len - 1] = '\0'; /* Strip newline */
        pname[npv] = epicsStrDup(pstr);
        pmynode[npv] = callocMustSucceed(1, sizeof(MYNODE), "caMonitor");
        npv++;
    }
   
    fclose(fp);
    SEVCHK(ca_context_create(ca_disable_preemptive_callback),"ca_context_create");
    SEVCHK(ca_add_exception_event(exceptionCallback,NULL),
"ca_add_exception_event");
    for (i=0; i<npv; i++) {
SEVCHK(ca_create_channel(pname[i],connectionCallback,
pmynode[i],20,&pmynode[i]->mychid),
"ca_create_channel");
SEVCHK(ca_replace_access_rights_event(pmynode[i]->mychid,
accessRightsCallback),
"ca_replace_access_rights_event");
SEVCHK(ca_create_subscription(DBR_STRING,1,pmynode[i]->mychid,
DBE_VALUE,eventCallback,pmynode[i],&pmynode[i]->myevid),
"ca_create_subscription");
    }
    /*Should never return from following call*/
    SEVCHK(ca_pend_event(0.0),"ca_pend_event");
    return 0;
}


Replies:
Re: MySQL Statement Doesn't Work Kasemir, Kay via Tech-talk
References:
MySQL Statement Doesn't Work Aaron Brown via Tech-talk

Navigate by Date:
Prev: Re: [EXTERNAL] Re: MySQL Statement Doesn't Work Aaron Brown via Tech-talk
Next: Re: MySQL Statement Doesn't Work Kasemir, Kay via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  <20192020  2021  2022  2023  2024 
Navigate by Thread:
Prev: Re: [EXTERNAL] Re: MySQL Statement Doesn't Work Aaron Brown via Tech-talk
Next: Re: MySQL Statement Doesn't Work Kasemir, Kay via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  <20192020  2021  2022  2023  2024 
ANJ, 04 Dec 2019 Valid HTML 4.01! · Home · News · About · Base · Modules · Extensions · Distributions · Download ·
· Search · EPICS V4 · IRMIS · Talk · Bugs · Documents · Links · Licensing ·