Advertisement
Guest User

Untitled

a guest
May 15th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.44 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 Pour 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 pour 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/testrasp/TestModule_etC/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.  
  112. // -------------------- Debut connexion à la base ----------------
  113. // paramètre par défaut
  114. conninfo = "host=192.168.43.143 user = symfony port = 5454 dbname = connectedhospital"; // A verifier à chaque connection
  115.  
  116. /* connexion à la BDD */
  117. conn = PQconnectdb(conninfo);
  118.  
  119. /* test de la réussite de la connexion */
  120. while (PQstatus(conn) != CONNECTION_OK)
  121. {
  122. conn = PQconnectdb(conninfo);
  123. fprintf(stderr, "Connection to database failed: %s",
  124. PQerrorMessage(conn));
  125. sleep(1);
  126. //exit_nicely(conn);
  127. }
  128.  
  129. // -------------------- FIN connexion à la base ----------------
  130.  
  131.  
  132. // -------------------- Debut Récup données du patient pour config raspi ------------------
  133.  
  134. /* valeurs contenant le nombre de tuples, et le nombre de champs par tuple */
  135. int nfields, ntuples;
  136.  
  137. //char utilisateur[100]; // nom du docteur
  138. char requete[200]; // requête qui sera envoyée au serveur
  139.  
  140.  
  141. /* Récupération de la config du patient (son ID) */
  142. printf("requete config raspi\n");
  143. sprintf(requete,
  144. "SELECT patient_id FROM configuration WHERE name = '%s'",name);
  145. /* affichage de la requête obtenue */
  146. printf("%s\n",requete);
  147.  
  148.  
  149. /* envoi de la commande au serveur */
  150. res = PQexec(conn, requete);
  151.  
  152. /* test pour savoir si un résultat a été obtenu */
  153. if (PQresultStatus(res) != PGRES_TUPLES_OK)
  154. /* error() prend 4 paramètres.
  155. * Il ne faut pas oublier #include <error.h> */
  156. error(0,0,"%s",PQresultErrorMessage(res));
  157.  
  158. nfields = PQnfields(res);
  159. ntuples = PQntuples(res);
  160.  
  161. for(i = 0; i < ntuples; i++)
  162. {
  163. for(j = 0; j < nfields; j++)
  164. {
  165. printf("[%d,%d] %s\n", i, j, PQgetvalue(res, i, j));
  166. }
  167. }
  168. idPatient = atol(PQgetvalue(res, 0, 0));
  169. printf("ID PATIENT = %d\n",idPatient);
  170. PQclear(res);
  171.  
  172.  
  173. /* Récupération du temps d'interval d'envoie de données*/
  174. printf("requete temps d'interval raspi\n");
  175. sprintf(requete,
  176. "SELECT interval FROM configuration");
  177. /* affichage de la requête obtenue */
  178. printf("%s\n",requete);
  179.  
  180.  
  181. /* envoi de la commande au serveur */
  182. res = PQexec(conn, requete);
  183.  
  184. /* test pour savoir si un résultat a été obtenu */
  185. if (PQresultStatus(res) != PGRES_TUPLES_OK)
  186. /* error() prend 4 paramètres.
  187. * Il ne faut pas oublier #include <error.h> */
  188. error(0,0,"%s",PQresultErrorMessage(res));
  189.  
  190. nfields = PQnfields(res);
  191. ntuples = PQntuples(res);
  192.  
  193. for(i = 0; i < ntuples; i++)
  194. {
  195. for(j = 0; j < nfields; j++)
  196. {
  197. printf("[%d,%d] %s\n", i, j, PQgetvalue(res, i, j));
  198. }
  199. }
  200. interval = atol(PQgetvalue(res, 0, 0));
  201. printf("Interval = %d\n",interval);
  202. PQclear(res);
  203.  
  204. // -------------------- FIN Récup données du patient pour config raspi ------------------
  205.  
  206. printf("Insertion de données\n");
  207. // -------------------- Debut Partie Récupération des données --------------------
  208.  
  209. // -------------------- Debut Appel script Python --------------------
  210.  
  211. if (pModule != NULL) {
  212. pFunc = PyObject_GetAttrString(pModule, argv[2]);
  213. pFunc2 = PyObject_GetAttrString(pModule, argv[3]);
  214. /* pFunc is a new reference */
  215.  
  216. if (pFunc && PyCallable_Check(pFunc)) {
  217. pArgs = PyTuple_New(argc - 3);
  218. for (k = 0; k < argc - 3; ++k) {
  219. pValue = PyLong_FromLong(atoi(argv[k + 3]));
  220. if (!pValue) {
  221. Py_DECREF(pArgs);
  222. Py_DECREF(pModule);
  223. fprintf(stderr, "Cannot convert argument\n");
  224. return 1;
  225. }
  226. /* pValue reference stolen here: */
  227. PyTuple_SetItem(pArgs, k, pValue);
  228. }
  229.  
  230. }
  231. else {
  232. if (PyErr_Occurred())
  233. PyErr_Print();
  234. fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
  235. }
  236. Py_XDECREF(pFunc);
  237. Py_DECREF(pModule);
  238.  
  239. if (pFunc2 && PyCallable_Check(pFunc2)) {
  240. pArgs2 = PyTuple_New(argc - 4);
  241. for (k = 0; k < argc - 4; ++k) {
  242. pValue2 = PyLong_FromLong(atoi(argv[k + 4]));
  243. if (!pValue2) {
  244. Py_DECREF(pArgs2);
  245. Py_DECREF(pModule);
  246. fprintf(stderr, "Cannot convert argument\n");
  247. return 1;
  248. }
  249. /* pValue reference stolen here: */
  250. PyTuple_SetItem(pArgs2, k, pValue2);
  251. }
  252.  
  253. }
  254. else {
  255. if (PyErr_Occurred())
  256. PyErr_Print();
  257. fprintf(stderr, "Cannot find function \"%s\"\n", argv[3]);
  258. }
  259. Py_XDECREF(pFunc2);
  260. Py_DECREF(pModule);
  261.  
  262. }
  263. // -------------------- FIN Appel script Python --------------------
  264.  
  265. while(1) {
  266.  
  267.  
  268. puts("ici");
  269. // -------------------- Begin Transaction ----------------
  270. /*
  271. res2 = PQexec(conn, "BEGIN");
  272. puts("BEGIN\n");
  273.  
  274. if (PQresultStatus(res2) != PGRES_COMMAND_OK) {
  275.  
  276. printf("BEGIN command failed\n");
  277. PQclear(res2);
  278. do_exit(conn);
  279. }
  280.  
  281. PQclear(res2);
  282. */
  283. // -------------------- Fin Begin Transaction ----------------
  284.  
  285. // -------------------- Debut veriff python ----------------
  286. pValue = PyObject_CallObject(pFunc, NULL);
  287. pValue2 = PyObject_CallObject(pFunc2, NULL);
  288. Py_DECREF(pArgs);
  289. if (pValue != NULL) {
  290. printf("Result of call: %ld\n", PyLong_AsLong(pValue));
  291. bpmlu = PyLong_AsLong(pValue);
  292. Py_DECREF(pValue);
  293. }
  294. else {
  295. Py_DECREF(pFunc);
  296. Py_DECREF(pModule);
  297. PyErr_Print();
  298. fprintf(stderr,"Call failed\n");
  299. return 1;
  300. }
  301. Py_DECREF(pArgs);
  302. if (pValue2 != NULL) {
  303. printf("Result of call: %ld\n", PyLong_AsLong(pValue2));
  304. templu = PyLong_AsLong(pValue2);
  305. Py_DECREF(pValue2);
  306. }
  307. else {
  308. Py_DECREF(pFunc2);
  309. Py_DECREF(pModule);
  310. PyErr_Print();
  311. fprintf(stderr,"Call failed\n");
  312. return 1;
  313. }
  314. //printf("%d\n", count);
  315.  
  316. // -------------------- FIN veriff python ----------------
  317.  
  318.  
  319. // -------------------- Debut envois en base ----------------
  320.  
  321. //récuperation de la date pour l'envoie en base
  322. time (&currentDate);
  323. timeinfo = localtime(&currentDate);
  324. //id,bpm,gl,temp,tension,date,idP
  325. sprintf(requete,"INSERT INTO data VALUES ((SELECT max(idData) FROM data)+1,%d,40,%d,12,'%s',%d)",bpmlu,templu,asctime(timeinfo),idPatient);
  326. printf("%s\n",requete);
  327.  
  328. /* envoi de la chaîne au SGBD pour exécution */
  329. res2 = PQexec(conn,requete);
  330.  
  331. if (PQresultStatus(res2) != PGRES_COMMAND_OK) {
  332. /* error() prend 4 paramètres.
  333. * Il ne faut pas oublier #include <error.h> */
  334. error(0,0,"%s",PQresultErrorMessage(res2));
  335. }
  336.  
  337. sleep(interval);
  338.  
  339. // -------------------- Debut envois en base ----------------
  340.  
  341.  
  342. // -------------------- Commit Transaction ----------------
  343. /*
  344. res2 = PQexec(conn, "COMMIT");
  345. puts("COMMIT\n");
  346.  
  347. if (PQresultStatus(res2) != PGRES_COMMAND_OK) {
  348.  
  349. printf("COMMIT command failed\n");
  350. PQclear(res2);
  351. do_exit(conn);
  352. }
  353. */
  354.  
  355. // -------------------- Fin Commit Transaction ----------------
  356. PQclear(res2);
  357. }
  358. Py_Finalize();
  359.  
  360. // -------------------- Fin Partie Récupération des données --------------------
  361.  
  362. /* ferme la connexion */
  363. PQfinish(conn);
  364.  
  365. return 0;
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement