Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Test de libpq.
- * Voir http://www.postgresql.org/docs/9.5/static/libpq-exec.html
- *
- * Compiler à l'aide de la commande:
- * gcc -o BDDconnectedHospital BDDconnectedHospital.c -lpq -L`usr/share/doc/libpython3.4` `/usr/bin/python3.4-config --cflags` `/usr/bin/python3.4-config --ldflags` `/usr/bin/python3.4-config --libs` (si libpq est installée sur le système) A UTILISER
- * (exécutable de 14 Ko)
- *
- * ou
- *
- * gcc -o programme programme.c /stockage/Logiciels/postgresql-9.5.1/lib/libpq.so
- * (exécutable de 14 Ko)
- *
- * ou
- *
- * gcc -o programme programme.c /stockage/Logiciels/postgresql-9.5.1/lib/libpq.a -lpthread
- * (si libpq est installée dans un sous-dossier, exécutable de 144 Ko, édition de liens statique)
- *
- * Il est nécessaire de définir une variable qui désigne le dossier
- * contenant les librairies:
- * export LD_LIBRARY_PATH=/stockage/Logiciels/postgresql-9.5.1/lib/
- *
- * Exécuter en passant la chaîne de paramètres de connexion au programme:
- * ./programme "host=127.0.0.1 port=5432 user=nom dbname=labdd password=lepasse"
- */
- /* inclusion des fichiers .h nécessaires au programme */
- #include <stdio.h>
- #include <stdlib.h>
- #include <error.h>
- #include <time.h>
- /* prototypes des fonctions fournies par la librairie libpq */
- #include <postgresql/libpq-fe.h>
- /* <...> si le fichier d'en-tête est présent sur le système */
- // #include "/home/isen/Documents/RevisionsBDD/postgresql-9.5.1/include/libpq-fe.h"
- /* "..." si le fichier est présent dans un dossier connu
- * du seul utilisateur */
- /* Fonction de fermeture des connexions */
- // Include Pour python
- #include <string.h>
- #include <stddef.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <python3.4/Python.h>
- // Fin include pour python
- static void exit_nicely(PGconn *conn)
- {
- PQfinish(conn);
- // renvoie 1 pour signaler un problème
- exit(1);
- }
- static void do_exit(PGconn *conn)
- {
- PQfinish(conn);
- // renvoie 1 pour signaler un problème
- exit(1);
- }
- int main(int argc, char **argv)
- {
- /* variables */
- const char *conninfo;
- PGconn *conn;
- PGresult *res, *res2;
- int nFields;
- int i,j;
- time_t currentDate;
- struct tm * timeinfo;
- // -------------------- Variables Python ----------------
- PyObject *pName, *pModule, *pDict, *pFunc, *pFunc2;
- PyObject *pArgs, *pValue, *pArgs2, *pValue2;
- int k;
- int templu;
- int bpmlu;
- int count;
- int idPatient;
- int interval;
- char name[] = "RASPBERRY_02";
- // -------------------- Fin Variables Python ----------------
- // -------------------- Debut Partie Python --------------------
- if (argc < 4) {
- fprintf(stderr,"Usage: program pythonfile funcname [args]\n");
- return 1;
- }
- Py_Initialize();
- PyRun_SimpleString("import sys\n"
- "sys.path.append('/home/pi/Desktop/testrasp/TestModule_etC/Modules_Python/modules/')\n");
- pName = PyUnicode_FromString(argv[1]);
- /* Error checking of pName left out */
- pModule = PyImport_Import(pName);
- Py_DECREF(pName);
- // -------------------- Fin Partier Python --------------------
- // -------------------- Debut connexion à la base ----------------
- // paramètre par défaut
- conninfo = "host=192.168.43.143 user = symfony port = 5454 dbname = connectedhospital"; // A verifier à chaque connection
- /* connexion à la BDD */
- conn = PQconnectdb(conninfo);
- /* test de la réussite de la connexion */
- while (PQstatus(conn) != CONNECTION_OK)
- {
- conn = PQconnectdb(conninfo);
- fprintf(stderr, "Connection to database failed: %s",
- PQerrorMessage(conn));
- sleep(1);
- //exit_nicely(conn);
- }
- // -------------------- FIN connexion à la base ----------------
- // -------------------- Debut Récup données du patient pour config raspi ------------------
- /* valeurs contenant le nombre de tuples, et le nombre de champs par tuple */
- int nfields, ntuples;
- //char utilisateur[100]; // nom du docteur
- char requete[200]; // requête qui sera envoyée au serveur
- /* Récupération de la config du patient (son ID) */
- printf("requete config raspi\n");
- sprintf(requete,
- "SELECT patient_id FROM configuration WHERE name = '%s'",name);
- /* affichage de la requête obtenue */
- printf("%s\n",requete);
- /* envoi de la commande au serveur */
- res = PQexec(conn, requete);
- /* test pour savoir si un résultat a été obtenu */
- if (PQresultStatus(res) != PGRES_TUPLES_OK)
- /* error() prend 4 paramètres.
- * Il ne faut pas oublier #include <error.h> */
- error(0,0,"%s",PQresultErrorMessage(res));
- nfields = PQnfields(res);
- ntuples = PQntuples(res);
- for(i = 0; i < ntuples; i++)
- {
- for(j = 0; j < nfields; j++)
- {
- printf("[%d,%d] %s\n", i, j, PQgetvalue(res, i, j));
- }
- }
- idPatient = atol(PQgetvalue(res, 0, 0));
- printf("ID PATIENT = %d\n",idPatient);
- PQclear(res);
- /* Récupération du temps d'interval d'envoie de données*/
- printf("requete temps d'interval raspi\n");
- sprintf(requete,
- "SELECT interval FROM configuration");
- /* affichage de la requête obtenue */
- printf("%s\n",requete);
- /* envoi de la commande au serveur */
- res = PQexec(conn, requete);
- /* test pour savoir si un résultat a été obtenu */
- if (PQresultStatus(res) != PGRES_TUPLES_OK)
- /* error() prend 4 paramètres.
- * Il ne faut pas oublier #include <error.h> */
- error(0,0,"%s",PQresultErrorMessage(res));
- nfields = PQnfields(res);
- ntuples = PQntuples(res);
- for(i = 0; i < ntuples; i++)
- {
- for(j = 0; j < nfields; j++)
- {
- printf("[%d,%d] %s\n", i, j, PQgetvalue(res, i, j));
- }
- }
- interval = atol(PQgetvalue(res, 0, 0));
- printf("Interval = %d\n",interval);
- PQclear(res);
- // -------------------- FIN Récup données du patient pour config raspi ------------------
- printf("Insertion de données\n");
- // -------------------- Debut Partie Récupération des données --------------------
- // -------------------- Debut Appel script Python --------------------
- if (pModule != NULL) {
- pFunc = PyObject_GetAttrString(pModule, argv[2]);
- pFunc2 = PyObject_GetAttrString(pModule, argv[3]);
- /* pFunc is a new reference */
- if (pFunc && PyCallable_Check(pFunc)) {
- pArgs = PyTuple_New(argc - 3);
- for (k = 0; k < argc - 3; ++k) {
- pValue = PyLong_FromLong(atoi(argv[k + 3]));
- if (!pValue) {
- Py_DECREF(pArgs);
- Py_DECREF(pModule);
- fprintf(stderr, "Cannot convert argument\n");
- return 1;
- }
- /* pValue reference stolen here: */
- PyTuple_SetItem(pArgs, k, pValue);
- }
- }
- else {
- if (PyErr_Occurred())
- PyErr_Print();
- fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
- }
- Py_XDECREF(pFunc);
- Py_DECREF(pModule);
- if (pFunc2 && PyCallable_Check(pFunc2)) {
- pArgs2 = PyTuple_New(argc - 4);
- for (k = 0; k < argc - 4; ++k) {
- pValue2 = PyLong_FromLong(atoi(argv[k + 4]));
- if (!pValue2) {
- Py_DECREF(pArgs2);
- Py_DECREF(pModule);
- fprintf(stderr, "Cannot convert argument\n");
- return 1;
- }
- /* pValue reference stolen here: */
- PyTuple_SetItem(pArgs2, k, pValue2);
- }
- }
- else {
- if (PyErr_Occurred())
- PyErr_Print();
- fprintf(stderr, "Cannot find function \"%s\"\n", argv[3]);
- }
- Py_XDECREF(pFunc2);
- Py_DECREF(pModule);
- }
- // -------------------- FIN Appel script Python --------------------
- while(1) {
- puts("ici");
- // -------------------- Begin Transaction ----------------
- /*
- res2 = PQexec(conn, "BEGIN");
- puts("BEGIN\n");
- if (PQresultStatus(res2) != PGRES_COMMAND_OK) {
- printf("BEGIN command failed\n");
- PQclear(res2);
- do_exit(conn);
- }
- PQclear(res2);
- */
- // -------------------- Fin Begin Transaction ----------------
- // -------------------- Debut veriff python ----------------
- pValue = PyObject_CallObject(pFunc, NULL);
- pValue2 = PyObject_CallObject(pFunc2, NULL);
- Py_DECREF(pArgs);
- if (pValue != NULL) {
- printf("Result of call: %ld\n", PyLong_AsLong(pValue));
- bpmlu = PyLong_AsLong(pValue);
- Py_DECREF(pValue);
- }
- else {
- Py_DECREF(pFunc);
- Py_DECREF(pModule);
- PyErr_Print();
- fprintf(stderr,"Call failed\n");
- return 1;
- }
- Py_DECREF(pArgs);
- if (pValue2 != NULL) {
- printf("Result of call: %ld\n", PyLong_AsLong(pValue2));
- templu = PyLong_AsLong(pValue2);
- Py_DECREF(pValue2);
- }
- else {
- Py_DECREF(pFunc2);
- Py_DECREF(pModule);
- PyErr_Print();
- fprintf(stderr,"Call failed\n");
- return 1;
- }
- //printf("%d\n", count);
- // -------------------- FIN veriff python ----------------
- // -------------------- Debut envois en base ----------------
- //récuperation de la date pour l'envoie en base
- time (¤tDate);
- timeinfo = localtime(¤tDate);
- //id,bpm,gl,temp,tension,date,idP
- sprintf(requete,"INSERT INTO data VALUES ((SELECT max(idData) FROM data)+1,%d,40,%d,12,'%s',%d)",bpmlu,templu,asctime(timeinfo),idPatient);
- printf("%s\n",requete);
- /* envoi de la chaîne au SGBD pour exécution */
- res2 = PQexec(conn,requete);
- if (PQresultStatus(res2) != PGRES_COMMAND_OK) {
- /* error() prend 4 paramètres.
- * Il ne faut pas oublier #include <error.h> */
- error(0,0,"%s",PQresultErrorMessage(res2));
- }
- sleep(interval);
- // -------------------- Debut envois en base ----------------
- // -------------------- Commit Transaction ----------------
- /*
- res2 = PQexec(conn, "COMMIT");
- puts("COMMIT\n");
- if (PQresultStatus(res2) != PGRES_COMMAND_OK) {
- printf("COMMIT command failed\n");
- PQclear(res2);
- do_exit(conn);
- }
- */
- // -------------------- Fin Commit Transaction ----------------
- PQclear(res2);
- }
- Py_Finalize();
- // -------------------- Fin Partie Récupération des données --------------------
- /* ferme la connexion */
- PQfinish(conn);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement