Advertisement
Guest User

Untitled

a guest
May 15th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.84 KB | None | 0 0
  1. /*
  2. * Test de libpq.
  3. * Voir http://www.postgresql.org/docs/9.5/static/libpq-exec.html
  4. *
  5. * Compiler à l'aide de la commande:
  6. * 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
  7. * (exécutable de 14 Ko)
  8. *
  9. * ou
  10. *
  11. * gcc -o programme programme.c /stockage/Logiciels/postgresql-9.5.1/lib/libpq.so
  12. * (exécutable de 14 Ko)
  13. *
  14. * ou
  15. *
  16. * gcc -o programme programme.c /stockage/Logiciels/postgresql-9.5.1/lib/libpq.a -lpthread
  17. * (si libpq est installée dans un sous-dossier, exécutable de 144 Ko, édition de liens statique)
  18. *
  19. * Il est nécessaire de définir une variable qui désigne le dossier
  20. * contenant les librairies:
  21. * export LD_LIBRARY_PATH=/stockage/Logiciels/postgresql-9.5.1/lib/
  22. *
  23. * Exécuter en passant la chaîne de paramètres de connexion au programme:
  24. * ./programme "host=127.0.0.1 port=5432 user=nom dbname=labdd password=lepasse"
  25. */
  26.  
  27. /* inclusion des fichiers .h nécessaires au programme */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <error.h>
  31. #include <time.h>
  32. /* prototypes des fonctions fournies par la librairie libpq */
  33. #include <postgresql/libpq-fe.h>
  34. /* <...> si le fichier d'en-tête est présent sur le système */
  35.  
  36. // #include "/home/isen/Documents/RevisionsBDD/postgresql-9.5.1/include/libpq-fe.h"
  37. /* "..." si le fichier est présent dans un dossier connu
  38. * du seul utilisateur */
  39.  
  40. /* Fonction de fermeture des connexions */
  41.  
  42. // Include Partie Python
  43. #include <string.h>
  44. #include <stddef.h>
  45. #include <unistd.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <python3.4/Python.h>
  49. // Fin include Partie Python
  50.  
  51.  
  52.  
  53. static void exit_nicely(PGconn *conn)
  54. {
  55. PQfinish(conn);
  56. // renvoie 1 pour signaler un problème
  57. exit(1);
  58. }
  59.  
  60. static void do_exit(PGconn *conn)
  61. {
  62. PQfinish(conn);
  63. // renvoie 1 pour signaler un problème
  64. exit(1);
  65. }
  66.  
  67. int main(int argc, char **argv)
  68. {
  69. /* variables */
  70. const char *conninfo;
  71. PGconn *conn;
  72. PGresult *res, *res2;
  73. int nFields;
  74. int i,j;
  75. time_t currentDate;
  76. struct tm * timeinfo;
  77.  
  78.  
  79. // -------------------- Variables Python ----------------
  80. PyObject *pName, *pModule, *pDict, *pFunc, *pFunc2;
  81. PyObject *pArgs, *pValue, *pArgs2, *pValue2;
  82. int k;
  83.  
  84. int templu;
  85. int bpmlu;
  86. int count;
  87. int idPatient;
  88. int interval;
  89. char name[] = "RASPBERRY_02";
  90.  
  91. // -------------------- Fin Variables Python ----------------
  92. // -------------------- Debut Partie Python --------------------
  93. if (argc < 4) {
  94. fprintf(stderr,"Usage: program pythonfile funcname [args]\n");
  95. return 1;
  96. }
  97.  
  98. Py_Initialize();
  99.  
  100. PyRun_SimpleString("import sys\n"
  101. "sys.path.append('/home/pi/Desktop/ConnectedHospital/Sensors/Modules_Python/modules/')\n");
  102.  
  103. pName = PyUnicode_FromString(argv[1]);
  104. /* Error checking of pName left out */
  105.  
  106. pModule = PyImport_Import(pName);
  107. Py_DECREF(pName);
  108.  
  109. // -------------------- Fin Partier Python --------------------
  110.  
  111. // paramètre par défaut
  112. conninfo = "host=192.168.43.143 user = symfony port = 5454 dbname = connectedhospital"; // A verifier à chaque connection
  113.  
  114. /* connexion à la BDD */
  115. conn = PQconnectdb(conninfo);
  116.  
  117. /* test de la réussite de la connexion */
  118. while (PQstatus(conn) != CONNECTION_OK)
  119. {
  120. conn = PQconnectdb(conninfo);
  121. fprintf(stderr, "Connection to database failed: %s",
  122. PQerrorMessage(conn));
  123. sleep(1);
  124. //exit_nicely(conn);
  125. }
  126.  
  127. /* valeurs contenant le nombre de tuples, et le nombre de champs par tuple */
  128. int nfields, ntuples;
  129.  
  130. //char utilisateur[100]; // nom du docteur
  131. char requete[200]; // requête qui sera envoyée au serveur
  132.  
  133.  
  134. /* Récupération de la config du patient (son ID) */
  135. printf("requete config raspi\n");
  136. sprintf(requete,
  137. "SELECT patient_id FROM configuration WHERE name = '%s'",name);
  138. /* affichage de la requête obtenue */
  139. printf("%s\n",requete);
  140.  
  141.  
  142. /* envoi de la commande au serveur */
  143. res = PQexec(conn, requete);
  144.  
  145. /* test pour savoir si un résultat a été obtenu */
  146. if (PQresultStatus(res) != PGRES_TUPLES_OK)
  147. /* error() prend 4 paramètres.
  148. * Il ne faut pas oublier #include <error.h> */
  149. error(0,0,"%s",PQresultErrorMessage(res));
  150.  
  151. nfields = PQnfields(res);
  152. ntuples = PQntuples(res);
  153.  
  154. for(i = 0; i < ntuples; i++)
  155. {
  156. for(j = 0; j < nfields; j++)
  157. {
  158. printf("[%d,%d] %s\n", i, j, PQgetvalue(res, i, j));
  159. }
  160. }
  161. idPatient = atol(PQgetvalue(res, 0, 0));
  162. printf("ID PATIENT = %d\n",idPatient);
  163. PQclear(res);
  164.  
  165.  
  166. /* Récupération du temps d'interval d'envoie de données*/
  167. printf("requete temps d'interval raspi\n");
  168. sprintf(requete,
  169. "SELECT interval FROM configuration");
  170. /* affichage de la requête obtenue */
  171. printf("%s\n",requete);
  172.  
  173.  
  174. /* envoi de la commande au serveur */
  175. res = PQexec(conn, requete);
  176.  
  177. /* test pour savoir si un résultat a été obtenu */
  178. if (PQresultStatus(res) != PGRES_TUPLES_OK)
  179. /* error() prend 4 paramètres.
  180. * Il ne faut pas oublier #include <error.h> */
  181. error(0,0,"%s",PQresultErrorMessage(res));
  182.  
  183. nfields = PQnfields(res);
  184. ntuples = PQntuples(res);
  185.  
  186. for(i = 0; i < ntuples; i++)
  187. {
  188. for(j = 0; j < nfields; j++)
  189. {
  190. printf("[%d,%d] %s\n", i, j, PQgetvalue(res, i, j));
  191. }
  192. }
  193. interval = atol(PQgetvalue(res, 0, 0));
  194. printf("Interval = %d\n",interval);
  195. PQclear(res);
  196.  
  197.  
  198.  
  199. printf("Insertion de données\n");
  200. // -------------------- Debut Partie Récupération des données (appel script Python) --------------------
  201.  
  202. if (pModule != NULL) {
  203. pFunc = PyObject_GetAttrString(pModule, argv[2]);
  204. pFunc2 = PyObject_GetAttrString(pModule, argv[3]);
  205. /* pFunc is a new reference */
  206.  
  207. if (pFunc && PyCallable_Check(pFunc)) {
  208. pArgs = PyTuple_New(argc - 3);
  209. for (k = 0; k < argc - 3; ++k) {
  210. pValue = PyLong_FromLong(atoi(argv[k + 3]));
  211. if (!pValue) {
  212. Py_DECREF(pArgs);
  213. Py_DECREF(pModule);
  214. fprintf(stderr, "Cannot convert argument\n");
  215. return 1;
  216. }
  217. /* pValue reference stolen here: */
  218. PyTuple_SetItem(pArgs, k, pValue);
  219. }
  220.  
  221. pValue = PyObject_CallObject(pFunc, NULL);
  222. Py_DECREF(pArgs);
  223. if (pValue != NULL) {
  224. printf("Result of call: %ld\n", PyLong_AsLong(pValue));
  225. Py_DECREF(pValue);
  226. }
  227. else {
  228. Py_DECREF(pFunc);
  229. Py_DECREF(pModule);
  230. PyErr_Print();
  231. fprintf(stderr,"Call failed\n");
  232. return 1;
  233. }
  234. }
  235. else {
  236. if (PyErr_Occurred())
  237. PyErr_Print();
  238. fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
  239. }
  240. Py_XDECREF(pFunc);
  241. Py_DECREF(pModule);
  242.  
  243. if (pFunc2 && PyCallable_Check(pFunc2)) {
  244. pArgs2 = PyTuple_New(argc - 4);
  245. for (k = 0; k < argc - 4; ++k) {
  246. pValue2 = PyLong_FromLong(atoi(argv[k + 4]));
  247. if (!pValue2) {
  248. Py_DECREF(pArgs2);
  249. Py_DECREF(pModule);
  250. fprintf(stderr, "Cannot convert argument\n");
  251. return 1;
  252. }
  253. /* pValue reference stolen here: */
  254. PyTuple_SetItem(pArgs2, k, pValue2);
  255. }
  256.  
  257. pValue2 = PyObject_CallObject(pFunc2, NULL);
  258. Py_DECREF(pArgs2);
  259. if (pValue2 != NULL) {
  260. printf("Result of call: %ld\n", PyLong_AsLong(pValue2));
  261. Py_DECREF(pValue2);
  262. }
  263. else {
  264. Py_DECREF(pFunc2);
  265. Py_DECREF(pModule);
  266. PyErr_Print();
  267. fprintf(stderr,"Call failed\n");
  268. return 1;
  269. }
  270. }
  271. else {
  272. if (PyErr_Occurred())
  273. PyErr_Print();
  274. fprintf(stderr, "Cannot find function \"%s\"\n", argv[3]);
  275. }
  276. Py_XDECREF(pFunc2);
  277. Py_DECREF(pModule);
  278.  
  279. }
  280.  
  281. while(1) {
  282. // -------------------- Check Last ID ----------------
  283. puts("Avant requete last ID");
  284. sprintf(requete,
  285. "SELECT max(idData) FROM data");
  286. res = PQexec(conn, requete);
  287. puts("Ares requete last ID");
  288.  
  289.  
  290. /* test pour savoir si un résultat a été obtenu */
  291. if (PQresultStatus(res) != PGRES_TUPLES_OK)
  292. /* error() prend 4 paramètres.
  293. * Il ne faut pas oublier #include <error.h> */
  294. error(0,0,"%s",PQresultErrorMessage(res));
  295.  
  296. nfields = PQnfields(res);
  297. ntuples = PQntuples(res);
  298.  
  299. for(i = 0; i < ntuples; i++)
  300. {
  301. for(j = 0; j < nfields; j++)
  302. {
  303. printf("[%d,%d] %s\n", i, j, PQgetvalue(res, i, j));
  304. }
  305. }
  306. count = atol(PQgetvalue(res, 0, 0))+1;
  307. printf("Last ID = %d\n",count);
  308. PQclear(res);
  309.  
  310. // -------------------- Fin check Last ID ----------------
  311. puts("ici");
  312. // -------------------- Begin Transaction ----------------
  313. res2 = PQexec(conn, "BEGIN");
  314. puts("BEGIN\n");
  315.  
  316. if (PQresultStatus(res2) != PGRES_COMMAND_OK) {
  317.  
  318. printf("BEGIN command failed\n");
  319. PQclear(res2);
  320. do_exit(conn);
  321. }
  322.  
  323. PQclear(res2);
  324.  
  325. /*printf("requete last ID\n");
  326. sprintf(requete,
  327. "SELECT max(idData) FROM data");
  328. printf("%s\n",requete);
  329. */
  330.  
  331.  
  332.  
  333.  
  334. // -------------------- Begin Transaction ----------------
  335.  
  336. pValue = PyObject_CallObject(pFunc, NULL);
  337. pValue2 = PyObject_CallObject(pFunc2, NULL);
  338. Py_DECREF(pArgs);
  339. if (pValue != NULL) {
  340. printf("Result of call: %ld\n", PyLong_AsLong(pValue));
  341. bpmlu = PyLong_AsLong(pValue);
  342. Py_DECREF(pValue);
  343. }
  344. else {
  345. Py_DECREF(pFunc);
  346. Py_DECREF(pModule);
  347. PyErr_Print();
  348. fprintf(stderr,"Call failed\n");
  349. return 1;
  350. }
  351. Py_DECREF(pArgs);
  352. if (pValue2 != NULL) {
  353. printf("Result of call: %ld\n", PyLong_AsLong(pValue2));
  354. templu = PyLong_AsLong(pValue2);
  355. Py_DECREF(pValue2);
  356. }
  357. else {
  358. Py_DECREF(pFunc2);
  359. Py_DECREF(pModule);
  360. PyErr_Print();
  361. fprintf(stderr,"Call failed\n");
  362. return 1;
  363. }
  364. printf("%d\n", count);
  365.  
  366. //récuperation de la date pour l'envoie en base
  367. time (&currentDate);
  368. timeinfo = localtime(&currentDate);
  369. //id,bpm,gl,temp,tension,date,idP
  370. sprintf(requete,"INSERT INTO data VALUES (%d,%d,40,%d,12,'%s',%d)",count,bpmlu,templu,asctime(timeinfo),idPatient);
  371. printf("%s\n",requete);
  372.  
  373. /* envoi de la chaîne au SGBD pour exécution */
  374. res2 = PQexec(conn,requete);
  375.  
  376. if (PQresultStatus(res2) != PGRES_COMMAND_OK) {
  377. /* error() prend 4 paramètres.
  378. * Il ne faut pas oublier #include <error.h> */
  379. error(0,0,"%s",PQresultErrorMessage(res2));
  380. }
  381.  
  382.  
  383. sleep(interval);
  384.  
  385. res2 = PQexec(conn, "COMMIT");
  386. puts("COMMIT\n");
  387.  
  388. if (PQresultStatus(res2) != PGRES_COMMAND_OK) {
  389.  
  390. printf("COMMIT command failed\n");
  391. PQclear(res2);
  392. do_exit(conn);
  393. }
  394.  
  395. PQclear(res2);
  396. }
  397. Py_Finalize();
  398.  
  399. // -------------------- Fin Partie Récupération des données (appel script Python) --------------------
  400.  
  401. /* ferme la connexion */
  402. PQfinish(conn);
  403.  
  404. return 0;
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement