Advertisement
Guest User

Untitled

a guest
May 9th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <postgresql/libpq-fe.h>
  6. #include <unistd.h>
  7.  
  8. #define NSENSORS 5
  9.  
  10. #define OUTDOOR 2
  11. #define HOME 3
  12. #define GARDEN 1
  13.  
  14. const char *conn = "host='db.fe.up.pt' user='sinfe32' password='UgeVMoBA'";
  15. static PGconn *dbconn;
  16.  
  17. long GATE=0,IRRSYS=0,FLOOR=0,LUZ_HOME=0,TEMP_HOME=0;
  18.  
  19. float temperatureSensors[NSENSORS];
  20. float humiditySensors[NSENSORS];
  21. float lightSensors[NSENSORS];
  22. float irlightSensors[NSENSORS];
  23.  
  24. double voltage(int pos1, int pos2)
  25. {
  26. double v;
  27. v = pos1*16*16+pos2;
  28. v = (v/4096)*1.5;
  29. return v;
  30. }
  31.  
  32. double vlight(int pos1, int pos2)
  33. {
  34. double v;
  35. v = pos1*16*16+pos2;
  36. v = (0.625*pow(10,6)*(v/4096)*1.5)/100;
  37. return v;
  38. }
  39.  
  40. double infra(int pos1, int pos2)
  41. {
  42. double i;
  43. i = pos1*16*16+pos2;
  44. i = (0.769*pow(10,5)*(i/4096)*1.5)/100;
  45. return i;
  46. }
  47.  
  48. double temperature(int pos1, int pos2)
  49. {
  50. double t;
  51. t = pos1*16*16+pos2;
  52. t = -39.6+0.01*t;
  53. return t;
  54. }
  55.  
  56. double relhumi(int pos1, int pos2)
  57. {
  58. double r;
  59. r = pos1*16*16+pos2;
  60. r = -2.0468+.0367*r-1.5955*pow(10,-6)*r*r;
  61. return r;
  62. }
  63.  
  64. double comphumi(int pos1, int pos2, int temptfunc, int relahumifunc)
  65. {
  66. double h;
  67. h = pos1*16*16+pos2;
  68. h = (temptfunc-25)*(0.01+0.00008*h)+relahumifunc;
  69. return h;
  70. }
  71.  
  72. void sql_init(){
  73.  
  74. dbconn = PQconnectdb(conn);
  75.  
  76. if (PQstatus(dbconn) == CONNECTION_BAD){
  77. printf("Unable to connect\n");
  78. exit(-1);
  79. }
  80. else{
  81. printf("Able to connect!\n");
  82. }
  83. }
  84.  
  85. void print_db(){
  86. char q[500];
  87. PGresult *query;
  88. sprintf(q,"SELECT nome,medicao FROM has.sensores");
  89. query = PQexec(dbconn,q);
  90. printf("------- SENSORES -------\n");
  91. for(int i = 0; i < PQntuples(query);i++){
  92. printf("%10s: %s \n", PQgetvalue(query,i,0),PQgetvalue(query,i,1));
  93. }
  94. printf("\n");
  95. sprintf(q,"SELECT nome,estado FROM has.atuadores");
  96. query = PQexec(dbconn,q);
  97. printf("------- ATUADORES -------\n");
  98. for(int i = 0; i < PQntuples(query);i++){
  99. printf("%10s: %s \n", PQgetvalue(query,i,0),PQgetvalue(query,i,1));
  100. }
  101. printf("\n");
  102. }
  103.  
  104. void update_state(int id){
  105. char q[500];
  106. int value;
  107. sprintf(q,"SELECT sensor,sinal,valor,atuador,estado FROM has.regras WHERE divisoes_id = %d", id);
  108. PGresult *query = PQexec(dbconn,q);
  109. for(int i = 0; i < PQntuples(query);i++){
  110. sprintf(q,"SELECT estado FROM has.atuadores WHERE nome=\'%s\'", PQgetvalue(query,i,3));
  111. value = atoi(PQgetvalue(PQexec(dbconn,q),0,0));
  112. sprintf(q,"UPDATE has.atuadores SET estado = CASE WHEN (SELECT medicao FROM has.sensores WHERE nome=\'%s\') %s %s THEN %s ELSE %d END WHERE nome = \'%s\'", PQgetvalue(query,i,0),PQgetvalue(query,i,1),PQgetvalue(query,i,2),PQgetvalue(query,i,4),value,PQgetvalue(query,i,3));
  113. //printf("%s\n",q);
  114. PQexec(dbconn,q);
  115. }
  116.  
  117.  
  118. }
  119.  
  120. void sql_update(int id){
  121. char q[500];
  122.  
  123. sprintf(q,"UPDATE has.sensores SET medicao= %5f, timestamp = now() WHERE mote_id=%d AND nome=\'temp1\' OR nome=\'temp3\'",temperatureSensors[id],id);
  124. PQexec(dbconn,q);
  125. sprintf(q,"UPDATE has.sensores SET medicao= %5f, timestamp = now() WHERE mote_id=%d AND nome=\'luz\'",lightSensors[id],id);
  126. PQexec(dbconn,q);
  127. sprintf(q,"UPDATE has.sensores SET medicao= %5f, timestamp = now() WHERE mote_id=%d AND nome=\'hum\'",humiditySensors[id],id);
  128. PQexec(dbconn,q);
  129. sprintf(q,"UPDATE has.sensores SET medicao= %5f, timestamp = now() WHERE mote_id=%d AND nome=\'irr\'",irlightSensors[id],id);
  130. PQexec(dbconn,q);
  131.  
  132. }
  133.  
  134. void print_rules(){
  135. char q[500];
  136. printf("------- REGRAS -------\n");
  137. sprintf(q,"SELECT sensor,sinal,valor,atuador,estado FROM has.regras");
  138. PGresult *query = PQexec(dbconn,q);
  139. for(int i = 0; i < PQntuples(query);i++){
  140. sprintf(q,"%s %s %s : %s = %s ", PQgetvalue(query,i,0),PQgetvalue(query,i,1),PQgetvalue(query,i,2),PQgetvalue(query,i,3),PQgetvalue(query,i,4));
  141. printf("%s\n",q);
  142. }
  143. printf("\n");
  144. }
  145.  
  146. int main(){
  147.  
  148. int str[23] = {0};
  149. char *token;
  150. char message[255];
  151. double volt;
  152. unsigned long id;
  153.  
  154. sql_init();
  155. print_rules();
  156.  
  157. while(1){
  158.  
  159. int i = 0;
  160.  
  161. fgets(message, 255, stdin);
  162. //printf("%s\n", message);
  163. token = strtok(message, " ");
  164.  
  165. while(token != NULL)
  166. {
  167. str[i] = strtol(token,NULL,16);
  168. //printf("%d ",str[i]);
  169. token = strtok(NULL, " ");
  170. i++;
  171. }
  172.  
  173. id = str[5]<<8 | str[6];
  174.  
  175. temperatureSensors[id] = temperature(str[16],str[17]);
  176. humiditySensors[id] = relhumi(str[18],str[19]);
  177.  
  178. lightSensors[id] = vlight(str[12],str[13]);
  179. irlightSensors[id] = infra(str[14],str[15]);
  180.  
  181. //printf("\n Temperatura: %fºC\n Humidade rel: %f%% \n Voltage: %fV\n Visible light: %flux \n Infrared light: %flux \n\n", temperatureSensors[id], humiditySensors[id], volt, lightSensors[id],irlightSensors[id]);
  182.  
  183. sql_update(id);
  184. update_state(id);
  185. print_db();
  186.  
  187. }
  188.  
  189. return 0;
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement