Advertisement
Guest User

Untitled

a guest
May 10th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 41.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. #include <math.h>
  6. #include <unistd.h>
  7. #include <pthread.h>
  8. #include <ctype.h>
  9. #include <postgresql/libpq-fe.h>
  10. #define MAX_SIZE 69
  11. #define NUMDIVIS 9
  12. #define NUMREGRAS 50
  13. #define NUMATU 6
  14.  
  15. typedef struct {
  16. int id;
  17. double temp;
  18. double relative_humidity;
  19. double visible_light;
  20. double infrared_light;
  21. } mote;
  22.  
  23. typedef struct {
  24. char atuador;
  25. int div;
  26. int on;
  27. } acao;
  28.  
  29. struct Divisao {
  30. char nomeDiv[50]; // String c/ nome da divisão
  31. char Sensores[10][20];// Array de strings com o nome dos sensores (MAX 10 Sensores) (20 Caracteres Máximo)
  32. mote sensor;
  33. char Atuadores[10][20]; // Array de strings com o nome dos atuadores (MAX 10 Atuadores) (20 Caracteres Máximo)
  34. };
  35. struct Divisao casa[NUMDIVIS + 1]; //Variável Global array das divioes da casa
  36. struct Regra {
  37. char nomeDiv[50]; // String c/ nome da divisão
  38. char Condicoes[10][20]; // Array de strings com o as condições (MAX 10 condições e operadores) (20 Caracteres Máximo cada)
  39. acao Acoes[10]; // Array de strings com o as acoes a efetuar (MAX 10 acoes) (20 Caracteres Máximo cada)
  40. };
  41. struct Regra regras[NUMREGRAS]; //Variável Global array das divioes da casa
  42. struct Atuador {
  43. char nomeAtuador; // Char c/ nome do atuador
  44. int Estado; // Ligado ou desligado
  45. };
  46. struct Atuador atuadores[NUMDIVIS + 1][NUMATU]; //Variável Global array 2D dos atuadores por divisao
  47.  
  48. //<--------------------------------------------------->
  49.  
  50. void calculate_real_values(long id, long temp, long humidity,
  51. long visible_light, long infrared_light) {
  52. casa[id].sensor.id = id;
  53. casa[id].sensor.temp = -39.6 + 0.01 * temp;
  54. casa[id].sensor.relative_humidity = -2.0468 + 0.0367 * humidity
  55. - 0.0000015955 * humidity * humidity;
  56. casa[id].sensor.visible_light = 2.28882 * visible_light;
  57. casa[id].sensor.infrared_light = 0.28161621093 * infrared_light;
  58. /* printf("ID: %s\n",casa[id].nomeDiv);
  59. printf("Temp: %f\n", casa[id].sensor.temp);
  60. printf("visible: %f\n", casa[id].sensor.visible_light);
  61. printf("IR: %f\n", casa[id].sensor.infrared_light);*/
  62.  
  63. }
  64.  
  65. void open_check_raw_values() {
  66. char check[MAX_SIZE] = { };
  67.  
  68. // while(1){
  69.  
  70. if (fgets(check, sizeof(check), stdin) != NULL) {
  71.  
  72. int size = strlen(check);
  73. if (size == 68
  74. && (check[0] == '7' && check[1] == 'E' && check[3] == '4'
  75. && check[4] == '5' && check[66] == '7'
  76. && check[67] == 'E')) {
  77. char id[] = { check[15], check[16], check[18], check[19], '\0' };
  78. char temp[] = { check[48], check[49], check[51], check[52], '\0' };
  79. char humidity[] =
  80. { check[54], check[55], check[57], check[58], '\0' };
  81. char visible_light[] = { check[36], check[37], check[39], check[40],
  82. '\0' };
  83. char infrared_light[] = { check[42], check[43], check[45],
  84. check[46], '\0' };
  85. long id1 = strtoul(id, NULL, 16);
  86. double raw_temp = strtoul(temp, NULL, 16);
  87. double raw_humidty = strtoul(humidity, NULL, 16);
  88. double raw_visible_light = strtoul(visible_light, NULL, 16);
  89. double raw_infrared_light = strtoul(infrared_light, NULL, 16);
  90.  
  91. //pthread_mutex_lock(&mutex1);
  92. calculate_real_values(id1, raw_temp, raw_humidty, raw_visible_light,
  93. raw_infrared_light);
  94. //pthread_mutex_unlock(&mutex1);
  95.  
  96. }
  97. //}
  98. }
  99. //return NULL;
  100. }
  101.  
  102. void ConfiguraCasa(FILE *f) {
  103.  
  104. char *str;
  105. char lido[100];
  106. char auxSens[90], auxAtu[90];
  107. int i = 1, c, n = 0;
  108.  
  109. //Enquanto tiver linhas para ler
  110.  
  111. while (fgets(lido, 100, f) != NULL) {
  112. c = 0;
  113. if (lido[strlen(lido) - 1] == '\n') //Confirma se último caracter é \n
  114. lido[strlen(lido) - 1] = '\0'; //Remove \n do fim da string lida
  115.  
  116. str = strtok(lido, ":");
  117. while (str != NULL) {
  118. //Separa a linha nas 3 divisões <nome>:<sensores>:<atuadores>
  119.  
  120. switch (c) {
  121. case 0:
  122. strcpy(casa[i].nomeDiv, str);// Guarda na posição i o nome da divisão
  123. break;
  124. case 1:
  125. strcpy(auxSens, str);// Guarda numa string auxiliar a lista dos sensores
  126. break;
  127. case 2:
  128. strcpy(auxAtu, str);// Guarda numa string auxiliar a lista dos atuadores
  129. break;
  130. default:
  131. break;
  132. }
  133.  
  134. str = strtok(NULL, ":");
  135. c++;
  136. }
  137.  
  138. //Separa Sensores em substrings
  139. c = 0;
  140. str = strtok(auxSens, ",");
  141.  
  142. while (str != NULL) {
  143. // Enquanto houver sensores
  144. strcpy(casa[i].Sensores[c], str);// Guarda na posição i da casa, na posição c da lista de sensores o nome do Sensor
  145.  
  146. str = strtok(NULL, ",");
  147. c++;
  148. }
  149.  
  150. //Separa Atuadores em substrings
  151. c = 0;
  152. n = 0;
  153. str = strtok(auxAtu, ",");
  154.  
  155. while (str != NULL) { // Enquanto houver atuadores
  156. strcpy(casa[i].Atuadores[c], str); // Guarda na posição i da casa, na posição c da lista de Atuadores o nome do atuador
  157. atuadores[i][n].nomeAtuador = str[0]; // Guarda na posição n da lista atuadores, o nome do atuador
  158. atuadores[i][n].Estado = 0;
  159.  
  160. str = strtok(NULL, ",");
  161. c++;
  162. n++;
  163.  
  164. }
  165.  
  166. i++; // Passa para a posição seguinte no array casa
  167. }
  168.  
  169. }
  170.  
  171. void store_house_config_db(PGconn *conn,FILE* f){
  172. if(f==NULL)
  173. exit(-1);
  174. int id=1;
  175. char *str;
  176. char lido[100];
  177. char auxSens[90], auxAtu[90],auxDivisao[90];
  178. int i = 1, c, n = 0;
  179. int init=1;
  180. PGresult *res_div=PQexec(conn,"SELECT * FROM divisao");
  181. PGresult *res_sens=PQexec(conn,"SELECT * FROM sensor");
  182. PGresult *res_act=PQexec(conn,"SELECT * FROM atuador");
  183. //Enquanto tiver linhas para ler
  184. while (fgets(lido, 100, f) != NULL) {
  185. c = 0;
  186. if (lido[strlen(lido) - 1] == '\n') //Confirma se último caracter é \n
  187. lido[strlen(lido) - 1] = '\0'; //Remove \n do fim da string lida
  188.  
  189. str = strtok(lido, ":");
  190. while (str != NULL) {
  191. //Separa a linha nas 3 divisões <nome>:<sensores>:<atuadores>
  192.  
  193. switch (c) {
  194. case 0:
  195. strcpy(auxDivisao, str);// Guarda na posição i o nome da divisão
  196. break;
  197. case 1:
  198. strcpy(auxSens, str);// Guarda numa string auxiliar a lista dos sensores
  199. break;
  200. case 2:
  201. strcpy(auxAtu, str);// Guarda numa string auxiliar a lista dos atuadores
  202. break;
  203. default:
  204. break;
  205. }
  206.  
  207. str = strtok(NULL, ":");
  208. c++;
  209. }
  210.  
  211. char query_divisao[200];
  212. sprintf(query_divisao,"INSERT INTO divisao (id, nome_div) VALUES (%i,'%s')",id,auxDivisao);
  213. PQexec(conn,query_divisao);
  214. //Separa Sensores em substrings
  215. c = 1;
  216. str = strtok(auxSens, ",");
  217. char query_sensor[200];
  218. while (str != NULL) {
  219. // Enquanto houver sensores
  220. sprintf(query_sensor,"INSERT INTO sensor (mote_id,sensor_name,div_id) VALUES (%i,'%s',%i)",id,str,id);
  221. PQexec(conn,query_sensor);
  222. str = strtok(NULL, ",");
  223. c++;
  224. }
  225.  
  226. //Separa Atuadores em substrings
  227. c = 0;
  228. n = 0;
  229. str = strtok(auxAtu, ",");
  230. char query_actuator[100];
  231. while (str != NULL) {
  232. sprintf(query_actuator, "INSERT INTO atuador (nome_atuador, div_id) VALUES ('%s',%i)",str,id);
  233. PQexec(conn,query_actuator);
  234. str = strtok(NULL, ",");
  235.  
  236. }
  237.  
  238. id++; // Passa para a posição seguinte no array casa
  239. }
  240.  
  241. }
  242. /*
  243. * Função que interpreta o SensorRules.txt
  244. */
  245. void ConfiguraRegras(FILE *f) {
  246.  
  247. char *str;
  248. char lido[100];
  249. char resto[20][100] = { '\0' };
  250. int i = 0, c, z = 0, q = 0;
  251. int err = 0;
  252. int tag_divis = 0, tag_atu = 0; // tags boleanas de controlo
  253.  
  254. //Enquanto tiver linhas para ler
  255. while (fgets(lido, 100, f) != NULL) {
  256. c = 0;
  257. if (lido[strlen(lido) - 1] == '\n') //Confirma se último caracter é \n
  258. lido[strlen(lido) - 1] = '\0'; //Remove \n do fim da string lida
  259.  
  260. str = strtok(lido, ":");
  261.  
  262. while ((str != NULL) && !tag_divis) {// Enquanto tiver texto e tiver uma divisão
  263.  
  264. if (0 == c) { // Se for a primeira ocorrência é o nome da divisão
  265. tag_divis = 1;
  266. for (z = 0; z < NUMDIVIS + 1 && tag_divis; z++) // Percorre todas as divisões da casa
  267. if (strcmp(str, casa[z].nomeDiv) == 0)//Se a divisão em causa for correspondente à que está na posição z, entao divisão existe
  268. tag_divis = 0;
  269.  
  270. if (tag_divis) {// Caso a tag seja verdadeira (divisão não encontrada) imprime na consola de erros juntamente com a linha correspondente
  271. fprintf(stderr, "Divisão não encontrada!!! Linha %i\n",
  272. i + 1);
  273. err = 1;
  274. continue;
  275. }
  276.  
  277. strcpy(regras[i].nomeDiv, str);
  278.  
  279. } else {
  280. strcpy(resto[c - 1], str);// Senão adiciona ao array resto na posição c-1 (começa em 0)
  281. }
  282. str = strtok(NULL, " ");
  283. c++;
  284. }
  285.  
  286. c -= 2;
  287. q = 0;
  288. str = strtok(resto[c], ":,");
  289. while ((str != NULL) && !tag_atu) { // Enquanto tiver texto e tiver uma divisão
  290.  
  291. if (!(strcmp(str, "ON") == 0 || strcmp(str, "OFF") == 0)) {
  292.  
  293. regras[i].Acoes[q].atuador = str[0];
  294. regras[i].Acoes[q].div = z - 1;
  295.  
  296. tag_atu = 1;
  297. for (int x = 0; x < 10 && tag_atu; x++)
  298. if (strcmp(str, casa[z - 1].Atuadores[x]) == 0)
  299. tag_atu = 0;
  300.  
  301. if (tag_atu) {// Caso a tag seja verdadeira (atuador não encontrado) imprime na consola de erros juntamente com a linha correspondente
  302. fprintf(stderr, "Atuador não encontrado - %s!!! Linha %i\n",
  303. str, i + 1);
  304. err = 1;
  305. continue;
  306. }
  307. } else {
  308. if (str[1] == 'N')
  309. regras[i].Acoes[q].on = 1;
  310. else
  311. regras[i].Acoes[q].on = 0;
  312. q++;
  313. }
  314. str = strtok(NULL, ":,");
  315. }
  316.  
  317. for (z = 0; c > z; z++) {
  318. strcpy(regras[i].Condicoes[z], resto[z]);
  319. }
  320.  
  321. if (err) {
  322. err = 0;
  323. regras[i].nomeDiv[0] = '\0';
  324. } else
  325. i++; // Passa para a posição seguinte no array regras
  326. }
  327. }
  328.  
  329. /*
  330. * Confirma se Regra é cumprida ou não
  331. */
  332. int efectRegra(char sensor, int id, char comp, double value) {
  333.  
  334. double read = 26;
  335.  
  336. switch (sensor) {
  337. case 'T':
  338. read = casa[id].sensor.temp;
  339. break;
  340. case 'H':
  341. read = casa[id].sensor.relative_humidity;
  342. break;
  343. case 'L':
  344. read = casa[id].sensor.visible_light;
  345. break;
  346. case 'I':
  347. read = casa[id].sensor.infrared_light;
  348. break;
  349. default:
  350. return 0;
  351. }
  352.  
  353. // Confirma se a condição é válida e retorna 1 (true)
  354. switch (comp) {
  355. case '<':
  356. if (read < value)
  357. return 1;
  358.  
  359. break;
  360. case '>':
  361. if (read > value)
  362. return 1;
  363.  
  364. break;
  365. case '=':
  366. if (read == value)
  367. return 1;
  368.  
  369. break;
  370. default:
  371. return 0;
  372. }
  373. return 0;
  374. }
  375.  
  376. /*
  377. * Função que interpreta o SensorRules.txt e imprime o resultado
  378. */
  379. void AtuaRegras() {
  380.  
  381. char aux[100];
  382. int c = 0, comp_pos = 0;
  383. double value = 0;
  384. int divi;
  385.  
  386. int tag_sens = 1; // tags boleanas de controlo
  387. int valida = 0;
  388.  
  389. /*while(1){
  390.  
  391. c=0;
  392. comp_pos=0;
  393. value=0;
  394. tag_sens=1; // tags boleanas de controlo
  395. valida=0;*/
  396.  
  397. //Leitura da lista das regras
  398. for (int i = 0; i < NUMREGRAS && strlen(regras[i].nomeDiv) > 0; i++) {
  399.  
  400. //Interpretação das Regras
  401. for (int z = 0; z < NUMDIVIS + 1; z++) {// Percorre todas as divisões da casa
  402. if (strcmp(regras[i].nomeDiv, casa[z].nomeDiv) == 0) {//Se a divisão em causa for correspondente à que está na posição z, entao.....
  403.  
  404. for (int k = 0; k < 10; k++) {// Percorre a totalidade do array resto
  405. if (strlen(regras[i].Condicoes[k]) == 0)// Se a posição estiver vazia entao salta o resto da execução à frente
  406. continue;
  407.  
  408. comp_pos = strcspn(regras[i].Condicoes[k], "<>=");// Verifica se existe na string o caracter '<' ou '>' ou '=', e retorna o tamanho da string antes da primeira ocorrência
  409. // Por outras palavras, devolve a posição do operador de comparaçao
  410.  
  411. divi = regras[i].Condicoes[k][comp_pos - 1] - '0';
  412.  
  413. for (int x = 0; x < 10; x++) {// Percorre toda a lista de Sensores
  414.  
  415. if (strncmp(regras[i].Condicoes[k],
  416. casa[divi].Sensores[x], comp_pos) == 0) {// Se o nome do sensor existe na lista.....
  417.  
  418. strcpy(aux, regras[i].Condicoes[k] + comp_pos + 1); // Copia para a string aux o valor a comparar - ou seja - a partir da posiçao do comparador
  419. value = atof(aux);// Transforma a string aux num float e guarda em value
  420. strncpy(aux, regras[i].Condicoes[k], comp_pos); // Copia para a string aux o nome do sensor
  421. aux[comp_pos] = '\0';// Coloca no final da string o caracter terminal
  422.  
  423. //pthread_mutex_lock(&mutex1);
  424. valida = efectRegra(aux[0], z,
  425. regras[i].Condicoes[k][comp_pos], value);// Confirma se a condição é válida
  426. //pthread_mutex_unlock(&mutex1);
  427.  
  428. k++;// Passa para a posição seguinte do array resto
  429. c = 0;
  430.  
  431. if ((strcmp(regras[i].Condicoes[k], "OR") != 0)
  432. && (strcmp(regras[i].Condicoes[k], "AND")
  433. != 0) && valida) {// Se não for OR e não for AND e a condição for válida.....
  434. while (regras[i].Acoes[c].atuador != '\0') {// Enquanto tiver valores a imprimir
  435. for (int l = 0; l < NUMATU; l++)
  436. //pthread_mutex_lock(&mutex2);
  437. if (atuadores[regras[i].Acoes[c].div][l].nomeAtuador
  438. == regras[i].Acoes[c].atuador) {
  439. atuadores[regras[i].Acoes[c].div][l].Estado =
  440. regras[i].Acoes[c].on;
  441. }
  442. c++;
  443. }
  444. k = 20; // Garante que execução do ciclo acaba e passa para a linha seguinte
  445. } else if ((strcmp(regras[i].Condicoes[k], "OR")
  446. == 0) && valida) {// Senão, se for OR e a condição for válida..... (resultado final válido)
  447. k = k + 2;// Salta para operador seguinte (caso haja)
  448.  
  449. while ((strcmp(regras[i].Condicoes[k], "OR")
  450. == 0)
  451. || (strcmp(regras[i].Condicoes[k],
  452. "AND") == 0))// Enquanto for um operador....
  453. k = k + 2;// Salta para operador seguinte (caso haja)
  454.  
  455. while (regras[i].Acoes[c].atuador != '\0') {// Enquanto tiver valores a imprimir
  456. for (int l = 0; l < NUMATU; l++)
  457. if (atuadores[regras[i].Acoes[c].div][l].nomeAtuador
  458. == regras[i].Acoes[c].atuador)
  459. atuadores[regras[i].Acoes[c].div][l].Estado =
  460. regras[i].Acoes[c].on;
  461. c++;
  462. }
  463. k = 20; // Garante que execução do ciclo acaba e passa para a linha seguinte
  464. } else if ((strcmp(regras[i].Condicoes[k], "AND")
  465. == 0) && !valida) { // Senão, se for AND e a condição não for válida..... (resultado final não válido)
  466. k = 20; // Garante que execução do ciclo acaba e passa para a linha seguinte
  467. }
  468. valida = 0; // Faz reset ao valor da tag, para fazer nova validação
  469. tag_sens = 0;// Torna a tag falsa -> ou seja, o sensor existe
  470. break;
  471. }
  472.  
  473. }
  474.  
  475. if (tag_sens) { // Caso a tag seja verdadeira (sensor não encontrado) imprime na consola de erros juntamente com a linha correspondente e nome sensor
  476. fprintf(stderr,
  477. "Sensor não encontrado!!! Linha %i - %s\n",
  478. i + 1, regras[i].Condicoes[k]);
  479. }
  480.  
  481. tag_sens = 1; // Retoma o valor de true à tag
  482. }
  483. }
  484.  
  485. }
  486. }
  487. //}
  488. //return NULL;
  489. }
  490.  
  491. //<------------------------------------------------------>
  492.  
  493. struct RGBcolor {
  494. unsigned char R;
  495. unsigned char G;
  496. unsigned char B;
  497. };
  498.  
  499. struct RGBcolor door_p, ac1, ac2, ac3, ac4, ac5, ac7, water, humidity5, blinds1,
  500. blinds2, blinds3, blinds5, lights1, lights2, lights3, lights5, lights6,
  501. lights7, ir, temp1, temp2, temp3, temp4, temp5, temp7, temp8, li8, li9; //cores dos pixeis do actuador em questao (consoante o estado) - numeros correspondem à divisao
  502.  
  503. /*
  504. * white [255,255,255]
  505. * black [0,0,0]
  506. * green [55,255,69]
  507. * light brown [205,134,11]
  508. * dark brown [102,65,0]
  509. * light grey [205,205,205]
  510. * dark grey [115,115,115]
  511. * yellow [255,255,0]
  512. * light blue [155,204,255]
  513. * dark blue [0,43,215]
  514. */
  515.  
  516. //light brown
  517. #define door_locked_R 205
  518. #define door_locked_G 134
  519. #define door_locked_B 11
  520.  
  521. //dark brown
  522. #define door_unlocked_R 102
  523. #define door_unlocked_G 65
  524. #define door_unlocked_B 0
  525.  
  526. //light blue
  527. #define ac_on_R 155
  528. #define ac_on_G 204
  529. #define ac_on_B 255
  530.  
  531. //dark grey
  532. #define ac_off_R 115
  533. #define ac_off_G 115
  534. #define ac_off_B 115
  535.  
  536. //dark blue
  537. #define water_on_R 0
  538. #define water_on_G 43
  539. #define water_on_B 215
  540.  
  541. //dark grey
  542. #define water_off_R 115
  543. #define water_off_G 115
  544. #define water_off_B 115
  545.  
  546. //green
  547. #define blinds_open_R 55
  548. #define blinds_open_G 255
  549. #define blinds_open_B 69
  550.  
  551. //dark grey
  552. #define blinds_closed_R 115
  553. #define blinds_closed_G 115
  554. #define blinds_closed_B 115
  555.  
  556. //yellow
  557. #define lights_on_R 255
  558. #define lights_on_G 255
  559. #define lights_on_B 0
  560.  
  561. //dark grey
  562. #define lights_off_R 115
  563. #define lights_off_G 115
  564. #define lights_off_B 115
  565.  
  566. //estado sensores temperatura
  567. //dark red
  568. #define ir_on_R 255
  569. #define ir_on_G 65
  570. #define ir_on_B 65
  571.  
  572. //light red
  573. #define ir_off_R 255
  574. #define ir_off_G 255
  575. #define ir_off_B 255
  576.  
  577. //estado sensores luminosidade
  578. //minimo 0
  579. #define lum0_R 255
  580. #define lum0_G 255
  581. #define lum0_B 255
  582.  
  583. //nivl 1
  584. #define lum1_R 255
  585. #define lum1_G 255
  586. #define lum1_B 153
  587.  
  588. //nivel 2
  589. #define lum2_R 255
  590. #define lum2_G 255
  591. #define lum2_B 51
  592.  
  593. //nivel 3
  594. #define lum3_R 255
  595. #define lum3_G 255
  596. #define lum3_B 0
  597.  
  598. //estado sensores temperatura
  599. //minimo 0
  600. #define temp0_R 255
  601. #define temp0_G 255
  602. #define temp0_B 255
  603.  
  604. //nivl 1
  605. #define temp1_R 255
  606. #define temp1_G 153
  607. #define temp1_B 153
  608.  
  609. //nivel 2
  610. #define temp2_R 255
  611. #define temp2_G 51
  612. #define temp2_B 51
  613.  
  614. //nivel 3
  615. #define temp3_R 255
  616. #define temp3_G 0
  617. #define temp3_B 0
  618.  
  619. //estado sensor humidade
  620. //minimo 0
  621. #define hum0_R 255
  622. #define hum0_G 255
  623. #define hum0_B 255
  624.  
  625. //nivl 1
  626. #define hum1_R 153
  627. #define hum1_G 255
  628. #define hum1_B 255
  629.  
  630. //nivel 2
  631. #define hum2_R 51
  632. #define hum2_G 255
  633. #define hum2_B 255
  634.  
  635. //nivel 3
  636. #define hum3_R 0
  637. #define hum3_G 255
  638. #define hum3_B 255
  639.  
  640. void display() {
  641. //while(1){
  642.  
  643. //sensores temp
  644. if ((casa[1].sensor.temp >= 15) && (casa[1].sensor.temp < 18)) {
  645. temp1.R = temp0_R;
  646. temp1.G = temp0_G;
  647. temp1.B = temp0_B;
  648. } else if ((casa[1].sensor.temp >= 18) && (casa[1].sensor.temp < 25)) {
  649. temp1.R = temp1_R;
  650. temp1.G = temp1_G;
  651. temp1.B = temp1_B;
  652. }
  653.  
  654. else if ((casa[1].sensor.temp >= 25) && (casa[1].sensor.temp < 27)) {
  655. temp1.R = temp2_R;
  656. temp1.G = temp2_G;
  657. temp1.B = temp2_B;
  658. }
  659.  
  660. else if ((casa[1].sensor.temp >= 27) && (casa[1].sensor.temp <= 35)) {
  661. temp1.R = temp3_R;
  662. temp1.G = temp3_G;
  663. temp1.B = temp3_B;
  664. }
  665.  
  666. if ((casa[2].sensor.temp >= 15) && (casa[2].sensor.temp < 22)) {
  667. temp2.R = temp0_R;
  668. temp2.G = temp0_G;
  669. temp2.B = temp0_B;
  670. }
  671.  
  672. else if ((casa[2].sensor.temp >= 22) && (casa[2].sensor.temp < 25)) {
  673. temp2.R = temp1_R;
  674. temp2.G = temp1_G;
  675. temp2.B = temp1_B;
  676. }
  677.  
  678. else if ((casa[2].sensor.temp >= 25) && (casa[2].sensor.temp < 27)) {
  679. temp2.R = temp2_R;
  680. temp2.G = temp2_G;
  681. temp2.B = temp2_B;
  682. }
  683.  
  684. else if ((casa[2].sensor.temp >= 27) && (casa[2].sensor.temp <= 35)) {
  685. temp2.R = temp3_R;
  686. temp2.G = temp3_G;
  687. temp2.B = temp3_B;
  688. }
  689.  
  690. if ((casa[3].sensor.temp >= 15) && (casa[3].sensor.temp < 18)) {
  691. temp3.R = temp0_R;
  692. temp3.G = temp0_G;
  693. temp3.B = temp0_B;
  694. }
  695.  
  696. else if ((casa[3].sensor.temp >= 18) && (casa[3].sensor.temp < 25)) {
  697. temp3.R = temp1_R;
  698. temp3.G = temp1_G;
  699. temp3.B = temp1_B;
  700. }
  701.  
  702. else if ((casa[3].sensor.temp >= 25) && (casa[3].sensor.temp < 27)) {
  703. temp3.R = temp2_R;
  704. temp3.G = temp2_G;
  705. temp3.B = temp2_B;
  706. }
  707.  
  708. else if ((casa[3].sensor.temp >= 27) && (casa[3].sensor.temp <= 35)) {
  709. temp3.R = temp3_R;
  710. temp3.G = temp3_G;
  711. temp3.B = temp3_B;
  712. }
  713.  
  714. if ((casa[4].sensor.temp >= 15) && (casa[4].sensor.temp < 18)) {
  715. temp4.R = temp0_R;
  716. temp4.G = temp0_G;
  717. temp4.B = temp0_B;
  718. }
  719.  
  720. else if ((casa[4].sensor.temp >= 18) && (casa[4].sensor.temp < 25)) {
  721. temp4.R = temp1_R;
  722. temp4.G = temp1_G;
  723. temp4.B = temp1_B;
  724. }
  725.  
  726. else if ((casa[4].sensor.temp >= 25) && (casa[4].sensor.temp < 27)) {
  727. temp4.R = temp2_R;
  728. temp4.G = temp2_G;
  729. temp4.B = temp2_B;
  730. }
  731.  
  732. else if ((casa[4].sensor.temp >= 27) && (casa[4].sensor.temp <= 35)) {
  733. temp4.R = temp3_R;
  734. temp4.G = temp3_G;
  735. temp4.B = temp3_B;
  736. }
  737.  
  738. if ((casa[5].sensor.temp >= 15) && (casa[5].sensor.temp < 18)) {
  739. temp5.R = temp0_R;
  740. temp5.G = temp0_G;
  741. temp5.B = temp0_B;
  742. }
  743.  
  744. else if ((casa[5].sensor.temp >= 18) && (casa[5].sensor.temp < 25)) {
  745. temp5.R = temp1_R;
  746. temp5.G = temp1_G;
  747. temp5.B = temp1_B;
  748. }
  749.  
  750. else if ((casa[5].sensor.temp >= 25) && (casa[5].sensor.temp < 27)) {
  751. temp5.R = temp2_R;
  752. temp5.G = temp2_G;
  753. temp5.B = temp2_B;
  754. }
  755.  
  756. else if ((casa[5].sensor.temp >= 27) && (casa[5].sensor.temp <= 35)) {
  757. temp5.R = temp3_R;
  758. temp5.G = temp3_G;
  759. temp5.B = temp3_B;
  760. }
  761.  
  762. if ((casa[7].sensor.temp >= 15) && (casa[7].sensor.temp < 18)) {
  763. temp7.R = temp0_R;
  764. temp7.G = temp0_G;
  765. temp7.B = temp0_B;
  766. }
  767.  
  768. else if ((casa[7].sensor.temp >= 18) && (casa[7].sensor.temp < 25)) {
  769. temp7.R = temp1_R;
  770. temp7.G = temp1_G;
  771. temp7.B = temp1_B;
  772. }
  773.  
  774. else if ((casa[7].sensor.temp >= 25) && (casa[7].sensor.temp < 27)) {
  775. temp7.R = temp2_R;
  776. temp7.G = temp2_G;
  777. temp7.B = temp2_B;
  778. }
  779.  
  780. else if ((casa[7].sensor.temp >= 27) && (casa[7].sensor.temp <= 35)) {
  781. temp7.R = temp3_R;
  782. temp7.G = temp3_G;
  783. temp7.B = temp3_B;
  784. }
  785.  
  786. if ((casa[8].sensor.temp >= 15) && (casa[8].sensor.temp < 20)) {
  787. temp8.R = temp0_R;
  788. temp8.G = temp0_G;
  789. temp8.B = temp0_B;
  790. }
  791.  
  792. else if ((casa[8].sensor.temp >= 20) && (casa[8].sensor.temp < 25)) {
  793. temp8.R = temp1_R;
  794. temp8.G = temp1_G;
  795. temp8.B = temp1_B;
  796. }
  797.  
  798. else if ((casa[8].sensor.temp >= 25) && (casa[8].sensor.temp < 30)) {
  799. temp8.R = temp2_R;
  800. temp8.G = temp2_G;
  801. temp8.B = temp2_B;
  802. }
  803.  
  804. else if ((casa[8].sensor.temp >= 30) && (casa[8].sensor.temp <= 35)) {
  805. temp8.R = temp3_R;
  806. temp8.G = temp3_G;
  807. temp8.B = temp3_B;
  808. }
  809.  
  810. //sensores lum
  811. if ((casa[8].sensor.visible_light >= 0)
  812. && (casa[8].sensor.visible_light < 125)) {
  813. li8.R = lum0_R;
  814. li8.G = lum0_G;
  815. li8.B = lum0_B;
  816. }
  817.  
  818. else if ((casa[8].sensor.visible_light >= 125)
  819. && (casa[8].sensor.visible_light < 250)) {
  820. li8.R = lum1_R;
  821. li8.G = lum1_G;
  822. li8.B = lum1_B;
  823. }
  824.  
  825. else if ((casa[8].sensor.visible_light >= 250)
  826. && (casa[8].sensor.visible_light < 375)) {
  827. li8.R = lum2_R;
  828. li8.G = lum2_G;
  829. li8.B = lum2_B;
  830. }
  831.  
  832. else if ((casa[8].sensor.visible_light >= 375)
  833. && (casa[8].sensor.visible_light <= 500)) {
  834. li8.R = lum3_R;
  835. li8.G = lum3_G;
  836. li8.B = lum3_B;
  837. }
  838.  
  839. if ((casa[9].sensor.visible_light >= 0)
  840. && (casa[9].sensor.visible_light < 125)) {
  841. li9.R = lum0_R;
  842. li9.G = lum0_G;
  843. li9.B = lum0_B;
  844. }
  845.  
  846. else if ((casa[9].sensor.visible_light >= 125)
  847. && (casa[9].sensor.visible_light < 250)) {
  848. li9.R = lum1_R;
  849. li9.G = lum1_G;
  850. li9.B = lum1_B;
  851. }
  852.  
  853. else if ((casa[9].sensor.visible_light >= 250)
  854. && (casa[9].sensor.visible_light < 375)) {
  855. li9.R = lum2_R;
  856. li9.G = lum2_G;
  857. li9.B = lum2_B;
  858. }
  859.  
  860. else if ((casa[9].sensor.visible_light >= 375)
  861. && (casa[9].sensor.visible_light <= 500)) {
  862. li9.R = lum3_R;
  863. li9.G = lum3_G;
  864. li9.B = lum3_B;
  865. }
  866.  
  867. //hum
  868. if ((casa[1].sensor.relative_humidity >= 30)
  869. && (casa[1].sensor.relative_humidity < 37)) {
  870. humidity5.R = hum0_R;
  871. humidity5.G = hum0_G;
  872. humidity5.B = hum0_B;
  873. }
  874.  
  875. else if ((casa[1].sensor.relative_humidity >= 37)
  876. && (casa[1].sensor.relative_humidity < 44)) {
  877. humidity5.R = hum1_R;
  878. humidity5.G = hum1_G;
  879. humidity5.B = hum1_B;
  880. }
  881.  
  882. else if ((casa[1].sensor.relative_humidity >= 44)
  883. && (casa[1].sensor.relative_humidity < 51)) {
  884. humidity5.R = hum2_R;
  885. humidity5.G = hum2_G;
  886. humidity5.B = hum2_B;
  887. }
  888.  
  889. else if ((casa[1].sensor.relative_humidity >= 51)
  890. && (casa[1].sensor.relative_humidity <= 60)) {
  891. humidity5.R = hum3_R;
  892. humidity5.G = hum3_G;
  893. humidity5.B = hum3_B;
  894. }
  895.  
  896. //infrared
  897. if (casa[6].sensor.infrared_light > 250) {
  898. ir.R = ir_on_R;
  899. ir.G = ir_on_G;
  900. ir.B = ir_on_B;
  901. } else {
  902. ir.R = ir_off_R;
  903. ir.G = ir_off_G;
  904. ir.B = ir_off_B;
  905. }
  906.  
  907. //actuadores
  908. //cozinha
  909. if (atuadores[1][0].Estado) { //on
  910. ac1.R = ac_on_R;
  911. ac1.G = ac_on_G;
  912. ac1.B = ac_on_B;
  913. } else { //off
  914. ac1.R = ac_off_R;
  915. ac1.G = ac_off_G;
  916. ac1.B = ac_off_B;
  917. }
  918.  
  919. //sala
  920. if (atuadores[2][0].Estado) {
  921. ac2.R = ac_on_R;
  922. ac2.G = ac_on_G;
  923. ac2.B = ac_on_B;
  924. } else {
  925. ac2.R = ac_off_R;
  926. ac2.G = ac_off_G;
  927. ac2.B = ac_off_B;
  928. }
  929.  
  930. //quarto1
  931. if (atuadores[3][0].Estado) {
  932. ac3.R = ac_on_R;
  933. ac3.G = ac_on_G;
  934. ac3.B = ac_on_B;
  935. } else {
  936. ac3.R = ac_off_R;
  937. ac3.G = ac_off_G;
  938. ac3.B = ac_off_B;
  939. }
  940.  
  941. //wc
  942. if (atuadores[4][0].Estado) {
  943. ac4.R = ac_on_R;
  944. ac4.G = ac_on_G;
  945. ac4.B = ac_on_B;
  946. } else {
  947. ac4.R = ac_off_R;
  948. ac4.G = ac_off_G;
  949. ac4.B = ac_off_B;
  950. }
  951.  
  952. //horta
  953. if (atuadores[5][0].Estado) {
  954. ac5.R = ac_on_R;
  955. ac5.G = ac_on_G;
  956. ac5.B = ac_on_B;
  957. } else {
  958. ac5.R = ac_off_R;
  959. ac5.G = ac_off_G;
  960. ac5.B = ac_off_B;
  961. }
  962.  
  963. //hall
  964. if (atuadores[7][0].Estado) {
  965. ac7.R = ac_on_R;
  966. ac7.G = ac_on_G;
  967. ac7.B = ac_on_B;
  968. } else {
  969. ac7.R = ac_off_R;
  970. ac7.G = ac_off_G;
  971. ac7.B = ac_off_B;
  972. }
  973.  
  974. //cozinha
  975. if (atuadores[1][2].Estado) { //on
  976. lights1.R = lights_on_R;
  977. lights1.G = lights_on_G;
  978. lights1.B = lights_on_B;
  979. } else { //off
  980. lights1.R = lights_off_R;
  981. lights1.G = lights_off_G;
  982. lights1.B = lights_off_B;
  983. }
  984.  
  985. //sala
  986. if (atuadores[2][2].Estado) {
  987. lights2.R = lights_on_R;
  988. lights2.G = lights_on_G;
  989. lights2.B = lights_on_B;
  990. } else {
  991. lights2.R = lights_off_R;
  992. lights2.G = lights_off_G;
  993. lights2.B = lights_off_B;
  994. }
  995.  
  996. //quarto
  997. if (atuadores[3][2].Estado) {
  998. lights3.R = lights_on_R;
  999. lights3.G = lights_on_G;
  1000. lights3.B = lights_on_B;
  1001. } else {
  1002. lights3.R = lights_off_R;
  1003. lights3.G = lights_off_G;
  1004. lights3.B = lights_off_B;
  1005. }
  1006.  
  1007. //horta
  1008. if (atuadores[5][2].Estado) {
  1009. lights5.R = lights_on_R;
  1010. lights5.G = lights_on_G;
  1011. lights5.B = lights_on_B;
  1012. } else {
  1013. lights5.R = lights_off_R;
  1014. lights5.G = lights_off_G;
  1015. lights5.B = lights_off_B;
  1016. }
  1017.  
  1018. //entrada
  1019. if (atuadores[6][0].Estado) {
  1020. lights6.R = lights_on_R;
  1021. lights6.G = lights_on_G;
  1022. lights6.B = lights_on_B;
  1023. } else {
  1024. lights6.R = lights_off_R;
  1025. lights6.G = lights_off_G;
  1026. lights6.B = lights_off_B;
  1027. }
  1028.  
  1029. //hall
  1030. if (atuadores[7][1].Estado) {
  1031. lights7.R = lights_on_R;
  1032. lights7.G = lights_on_G;
  1033. lights7.B = lights_on_B;
  1034. } else {
  1035. lights7.R = lights_off_R;
  1036. lights7.G = lights_off_G;
  1037. lights7.B = lights_off_B;
  1038. }
  1039.  
  1040. //cozinha
  1041. if (!atuadores[1][1].Estado) { //closed
  1042. blinds1.R = blinds_closed_R;
  1043. blinds1.G = blinds_closed_G;
  1044. blinds1.B = blinds_closed_B;
  1045. } else { //open
  1046. blinds1.R = blinds_open_R;
  1047. blinds1.G = blinds_open_G;
  1048. blinds1.B = blinds_open_B;
  1049. }
  1050.  
  1051. //sala
  1052. if (!atuadores[2][1].Estado) {
  1053. blinds2.R = blinds_closed_R;
  1054. blinds2.G = blinds_closed_G;
  1055. blinds2.B = blinds_closed_B;
  1056. } else {
  1057. blinds2.R = blinds_open_R;
  1058. blinds2.G = blinds_open_G;
  1059. blinds2.B = blinds_open_B;
  1060. }
  1061.  
  1062. //quarto
  1063. if (!atuadores[3][1].Estado) {
  1064. blinds3.R = blinds_closed_R;
  1065. blinds3.G = blinds_closed_G;
  1066. blinds3.B = blinds_closed_B;
  1067. } else {
  1068. blinds3.R = blinds_open_R;
  1069. blinds3.G = blinds_open_G;
  1070. blinds3.B = blinds_open_B;
  1071. }
  1072.  
  1073. //horta
  1074. if (!atuadores[5][1].Estado) {
  1075. blinds5.R = blinds_closed_R;
  1076. blinds5.G = blinds_closed_G;
  1077. blinds5.B = blinds_closed_B;
  1078. } else {
  1079. blinds5.R = blinds_open_R;
  1080. blinds5.G = blinds_open_G;
  1081. blinds5.B = blinds_open_B;
  1082. }
  1083.  
  1084. //porta principal
  1085. if (atuadores[6][1].Estado) { //trancada
  1086. door_p.R = door_locked_R;
  1087. door_p.G = door_locked_G;
  1088. door_p.B = door_locked_B;
  1089. } else { //destrancada
  1090. door_p.R = door_unlocked_R;
  1091. door_p.G = door_unlocked_G;
  1092. door_p.B = door_unlocked_B;
  1093. }
  1094.  
  1095. if (atuadores[5][3].Estado) { //ligada
  1096. water.R = water_on_R;
  1097. water.G = water_on_G;
  1098. water.B = water_on_B;
  1099. } else { //desligada
  1100. water.R = water_off_R;
  1101. water.G = water_off_G;
  1102. water.B = water_off_B;
  1103. }
  1104.  
  1105. printf(
  1106. "[[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[%d,%d,%d],[%d,%d,%d],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255]",
  1107. temp8.R, temp8.G, temp8.B, li8.R, li8.G, li8.B);
  1108. printf(
  1109. ",[255,255,255],[%d,%d,%d],[%d,%d,%d],[%d,%d,%d],[%d,%d,%d],[%d,%d,%d],[%d,%d,%d],[%d,%d,%d],[%d,%d,%d],[0,0,0],[0,0,0],[%d,%d,%d],[%d,%d,%d],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[255,255,255]",
  1110. blinds5.R, blinds5.G, blinds5.B, blinds5.R, blinds5.G, blinds5.B,
  1111. blinds5.R, blinds5.G, blinds5.B, blinds5.R, blinds5.G, blinds5.B,
  1112. blinds5.R, blinds5.G, blinds5.B, blinds5.R, blinds5.G, blinds5.B,
  1113. blinds5.R, blinds5.G, blinds5.B, blinds5.R, blinds5.G, blinds5.B,
  1114. blinds3.R, blinds3.G, blinds3.B, blinds3.R, blinds3.G, blinds3.B);
  1115. printf(
  1116. ",[255,255,255],[%d,%d,%d],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[0,0,0],[205,205,205],[205,205,205],[%d,%d,%d],[0,0,0],[255,255,255]",
  1117. blinds5.R, blinds5.G, blinds5.B, ac5.R, ac5.G, ac5.B, ac3.R, ac3.G,
  1118. ac3.B, ac4.R, ac4.G, ac4.B);
  1119. printf(
  1120. ",[255,255,255],[%d,%d,%d],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[0,0,0],[205,205,205],[205,205,205],[%d,%d,%d],[0,0,0],[255,255,255]",
  1121. blinds5.R, blinds5.G, blinds5.B, temp5.R, temp5.G, temp5.B, temp3.R,
  1122. temp3.G, temp3.B, temp4.R, temp4.G, temp4.B);
  1123. printf(
  1124. ",[255,255,255],[%d,%d,%d],[205,205,205],[205,205,205],[%d,%d,%d],[%d,%d,%d],[205,205,205],[205,205,205],[%d,%d,%d],[0,0,0],[205,205,205],[205,205,205],[%d,%d,%d],[205,205,205],[205,205,205],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[255,255,255]",
  1125. blinds5.R, blinds5.G, blinds5.B, lights5.R, lights5.G, lights5.B,
  1126. lights5.R, lights5.G, lights5.B, water.R, water.G, water.B,
  1127. lights3.R, lights3.G, lights3.B);
  1128. printf(
  1129. ",[255,255,255],[%d,%d,%d],[205,205,205],[205,205,205],[%d,%d,%d],[%d,%d,%d],[205,205,205],[205,205,205],[%d,%d,%d],[0,0,0],[205,205,205],[205,205,205],[%d,%d,%d],[205,205,205],[205,205,205],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[255,255,255]",
  1130. blinds5.R, blinds5.G, blinds5.B, lights5.R, lights5.G, lights5.B,
  1131. lights5.R, lights5.G, lights5.B, humidity5.R, humidity5.G,
  1132. humidity5.B, lights3.R, lights3.G, lights3.B);
  1133. printf(
  1134. ",[255,255,255],[%d,%d,%d],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[255,255,255]",
  1135. blinds5.R, blinds5.G, blinds5.B);
  1136. printf(
  1137. ",[255,255,255],[%d,%d,%d],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[255,255,255]",
  1138. blinds5.R, blinds5.G, blinds5.B);
  1139. printf(
  1140. ",[255,255,255],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[205,134,11],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[205,134,11],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[205,134,11],[0,0,0],[0,0,0],[%d,%d,%d]",
  1141. ir.R, ir.G, ir.B);
  1142. printf(
  1143. ",[255,255,255],[0,0,0],[205,205,205],[205,205,205],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[%d,%d,%d]",
  1144. lights6.R, lights6.G, lights6.B);
  1145. printf(
  1146. ",[255,255,255],[0,0,0],[205,205,205],[205,205,205],[0,0,0],[205,205,205],[205,205,205],[%d,%d,%d],[%d,%d,%d],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[%d,%d,%d],[205,205,205],[205,205,205],[%d,%d,%d],[%d,%d,%d]",
  1147. lights7.R, lights7.G, lights7.B, lights7.R, lights7.G, lights7.B,
  1148. lights7.R, lights7.G, lights7.B, lights7.R, lights7.G, lights7.B,
  1149. door_p.R, door_p.G, door_p.B, lights6.R, lights6.G, lights6.B);
  1150. printf(
  1151. ",[255,255,255],[0,0,0],[205,205,205],[205,205,205],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[%d,%d,%d],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[%d,%d,%d]",
  1152. temp7.R, temp7.G, temp7.B, ac7.R, ac7.G, ac7.B, lights6.R,
  1153. lights6.G, lights6.B);
  1154. printf(
  1155. ",[255,255,255],[0,0,0],[205,205,205],[205,205,205],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[205,134,11],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[205,134,11],[0,0,0],[0,0,0],[255,255,255]");
  1156. printf(
  1157. ",[255,255,255],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[255,255,255]");
  1158. printf(
  1159. ",[255,255,255],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[255,255,255]");
  1160. printf(
  1161. ",[255,255,255],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[%d,%d,%d],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,134,11],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[255,255,255]",
  1162. lights2.R, lights2.G, lights2.B, lights2.R, lights2.G, lights2.B,
  1163. lights1.R, lights1.G, lights1.B);
  1164. printf(
  1165. ",[255,255,255],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[%d,%d,%d],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[205,205,205],[205,205,205],[205,205,205],[0,0,0],[255,255,255]",
  1166. lights2.R, lights2.G, lights2.B, lights2.R, lights2.G, lights2.B,
  1167. lights1.R, lights1.G, lights1.B);
  1168. printf(
  1169. ",[255,255,255],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[0,0,0],[255,255,255]",
  1170. ac2.R, ac2.G, ac2.B, ac1.R, ac1.G, ac1.B);
  1171. printf(
  1172. ",[255,255,255],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[0,0,0],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[205,205,205],[%d,%d,%d],[0,0,0],[255,255,255]",
  1173. temp2.R, temp2.G, temp2.B, temp1.R, temp1.G, temp1.B);
  1174. printf(
  1175. ",[255,255,255],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[%d,%d,%d],[%d,%d,%d],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[%d,%d,%d],[%d,%d,%d],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[255,255,255]",
  1176. blinds2.R, blinds2.G, blinds2.B, blinds2.R, blinds2.G, blinds2.B,
  1177. blinds1.R, blinds1.G, blinds1.B, blinds1.R, blinds1.G, blinds1.B);
  1178. printf(
  1179. ",[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[%d,%d,%d],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,255,255]]\n",
  1180. li9.R, li9.G, li9.B);
  1181.  
  1182. // }
  1183.  
  1184. // return NULL;
  1185.  
  1186. }
  1187.  
  1188. const char *conn = "host='db.fe.up.pt' user='sinfa44' password='MWdOTyWZ'";
  1189. PGconn *dbconn;
  1190. //<----------------------------->
  1191. int query_check(PGresult *check){
  1192. if (PQresultStatus(check) != PGRES_TUPLES_OK) {
  1193. printf("Erro!\n");
  1194. PQclear(check);
  1195. PQfinish(dbconn);
  1196. return -1;
  1197. }
  1198. else return 1;
  1199.  
  1200. }
  1201. void db_rules_config(PGconn *conn){
  1202. char query[300]="SELECT rules_text FROM regras ORDER BY id";
  1203. PGresult *res_rules=PQexec(conn,query);
  1204. if(query_check(res_rules)==-1)
  1205. exit(-1);
  1206. FILE *f=fopen("rules_backup.txt","w+");
  1207. if(NULL==f)
  1208. exit(-1);
  1209. int n=PQntuples(res_rules);
  1210. for(int i=0; i<n;i++){
  1211. fprintf(f,"%s",PQgetvalue(res_rules,i,0));
  1212. if((i+1!=n))
  1213. fprintf(f,"\n");
  1214. }
  1215. fclose(f);
  1216. f=fopen("rules_backup.txt","r");
  1217. ConfiguraRegras(f);
  1218. fclose(f);
  1219. //Se for para guardar txt comentar proxima linha
  1220. //remove("rules_config_temp.txt");
  1221. }
  1222.  
  1223.  
  1224.  
  1225. void store_rules_db(PGconn *conn, FILE* f){
  1226. if(f==NULL)
  1227. exit(-1);
  1228. char lido[200];
  1229. char query[200];
  1230. int insert=0;
  1231. int init=1;
  1232. int i=1;
  1233. PGresult *res=NULL;
  1234. char query_init[300]="SELECT * FROM regras";
  1235. while (fgets(lido, 100, f) != NULL){
  1236. if(init==1){
  1237. res=PQexec(conn, query_init);
  1238. init=0;
  1239. }
  1240. if(PQntuples(res)==0 || PQntuples(res)<i)
  1241. insert=1;
  1242. if(insert==1){
  1243. if(lido[strlen(lido)-1]=='\n')
  1244. lido[strlen(lido)-1]='\0';
  1245. sprintf(query, "INSERT into regras (id,rules_text) VALUES (%i,'%s')",i, lido);
  1246. }
  1247. else{
  1248. if(lido[strlen(lido)-1]=='\n')
  1249. lido[strlen(lido)-1]='\0';
  1250. sprintf(query, "UPDATE regras SET rules_text='%s' WHERE id=%i",lido, i);
  1251. }
  1252. //printf("%s\n",query);
  1253. PQexec(conn, query);
  1254. i++;
  1255.  
  1256. }
  1257. }
  1258.  
  1259.  
  1260.  
  1261.  
  1262. void db_house_config(PGconn *conn) {
  1263. char divisao[300];
  1264. char query_divisao[300] = "SELECT id, trim(nome_div) from divisao order by id";
  1265. char query_sensor[300];
  1266. char query_actuator[300];
  1267. PGresult *res_divisao = PQexec(conn, query_divisao); //DIVISAO
  1268. /*if (PQresultStatus(res_divisao) != PGRES_TUPLES_OK) {
  1269. printf("Erro!\n");
  1270. PQclear(res_divisao);
  1271. PQfinish(conn);
  1272. exit(1);
  1273. }*/
  1274. if(query_check(res_divisao)==-1)
  1275. exit(-1);
  1276. FILE *config=fopen("config_backup.txt","w+");
  1277. if(NULL==config)
  1278. perror("Error:");
  1279. int n = PQntuples(res_divisao);
  1280. for (int i = 0; i < n; i++) {
  1281. //Divisao
  1282. strcpy(divisao, PQgetvalue(res_divisao, i, 1));
  1283. //printf("%s:",PQgetvalue(res,i,1));
  1284. strcat(divisao, ":");
  1285. //sensor
  1286.  
  1287. char id[5];
  1288. strcpy(id, PQgetvalue(res_divisao, i, 0));
  1289. sprintf(query_sensor,
  1290. "SELECT trim(sensor_name) FROM sensor WHERE div_id=%s", id);
  1291. PGresult *res_sensor = PQexec(conn, query_sensor);
  1292. if(query_check(res_sensor)==-1)
  1293. exit(-1);
  1294. for (int j = 0; j < (PQntuples(res_sensor)); j++) {
  1295. strcat(divisao, PQgetvalue(res_sensor, j, 0));
  1296. if ((j + 1) != (PQntuples(res_sensor)))
  1297. strcat(divisao, ",");
  1298. }
  1299.  
  1300. strcat(divisao, ":");
  1301. //Atuadores
  1302. sprintf(query_actuator,"SELECT trim(nome_atuador) FROM atuador WHERE div_id= %s", id);
  1303. PGresult *res_actuator = PQexec(conn, query_actuator);
  1304. if(query_check(res_actuator)==-1)
  1305. exit(-1);
  1306. for (int j = 0; j < (PQntuples(res_actuator)); j++) {
  1307. strcat(divisao, PQgetvalue(res_actuator, j, 0));
  1308. //printf("%s",PQgetvalue(res_sensor,j,0));
  1309. if ((j + 1) != (PQntuples(res_actuator)))
  1310. strcat(divisao, ",");
  1311. }
  1312.  
  1313. fprintf(config,"%s", divisao);
  1314. if((i+1!=n))
  1315. fprintf(config,"\n");
  1316. }
  1317. fclose(config);
  1318. config=fopen("config_backup.txt","r");
  1319. ConfiguraCasa(config);
  1320. fclose(config);
  1321. //Para guardar comentar linha a seguir
  1322. //remove("config_temp.txt");
  1323. }
  1324.  
  1325. void test(FILE* f){
  1326. char lido[100];
  1327. char lido_aux[100];
  1328. char* str_divisao;
  1329. char* str_sensor;
  1330. char* str_sensor_aux;
  1331. while(fgets(lido,100,f)!=NULL){
  1332. //Divisao
  1333. str_divisao=strtok(lido,": ");
  1334. printf("Divsao:%s\n",str_divisao);
  1335. //FAZER QUERIES
  1336. printf("Lido Antigo:%s\n",lido);
  1337. //Lido fica diferente
  1338. strcpy(lido,strtok(NULL,"\n"));
  1339. printf("Lido Novo:%s\n",lido);
  1340. //Sensores
  1341. str_sensor=strtok(lido," ");
  1342. printf("Lido sensor:%s\n",lido);
  1343. printf("Sensor a verificar:%s\n", str_sensor);
  1344. //Verificar nome sensor
  1345. str_sensor_aux=strtok(str_sensor, ">");
  1346. if(strcmp(str_sensor_aux,str_sensor)==0)
  1347. str_sensor_aux=strtok(str_sensor,"<");
  1348. if(NULL==str_sensor_aux)
  1349. exit(-1);
  1350. printf("SENSOR:%s\n",str_sensor_aux);
  1351. printf("LIDO depois do sensor: %s\n",lido);
  1352. //FAZER QUERIES E COISO
  1353.  
  1354.  
  1355.  
  1356.  
  1357. //str=strtok(NULL, ">");
  1358. //if(str==NULL)
  1359.  
  1360. //printf("%s\n",str);
  1361. }
  1362. }
  1363.  
  1364.  
  1365. //CODIGO ANDRE
  1366.  
  1367. int main() {
  1368. /* printf("Pretende-se conectar à Base de Dados? (Y,N)\n");
  1369. char s;
  1370. int check = -1;
  1371. while (1) {
  1372. s = getchar();
  1373. switch (s) {
  1374. case 'y':
  1375. case 'Y':
  1376. case 's':
  1377. case 'S':
  1378. check = 1;
  1379. break;
  1380. case 'n':
  1381. case 'N':
  1382. check = 0;
  1383. break;
  1384. default:
  1385. break;
  1386.  
  1387. }
  1388. if (0 == check || 1 == check)
  1389. break;
  1390. else
  1391. printf("Tente novamente.\n");
  1392. s = getchar();
  1393. }*/
  1394.  
  1395. int check=1;
  1396. if (1 == check) {
  1397. //printf("A conectar:\n");
  1398.  
  1399.  
  1400. PGconn *dbconn= PQconnectdb(conn);
  1401.  
  1402. if (PQstatus(dbconn) == CONNECTION_BAD) {
  1403. // printf("Unable to connect\n");
  1404. exit(-1);
  1405. } else {
  1406. //printf("Able to connect!\n");
  1407. }
  1408.  
  1409. FILE *f1_test=fopen("SensorRules.txt","r");
  1410. FILE *f2_test=fopen("SensorConfigurations.txt","r");
  1411. test(f1_test);
  1412. //store_house_config_db(dbconn,f2_test);
  1413. store_rules_db(dbconn, f1_test);
  1414. db_house_config(dbconn);
  1415. db_rules_config(dbconn);
  1416. } else {
  1417. char *filename1 = "SensorConfigurations.txt", *filename2 =
  1418. "SensorRules.txt"; //Define nomes dos ficheiros de configuração
  1419.  
  1420. FILE *f1, *f2;
  1421. f1 = fopen(filename1, "r"); // Abre ficheiro 1
  1422.  
  1423. if (f1 == NULL) { // Se não conseguir abrir, imprime erro e termina processo
  1424. fprintf(stderr, "ERRO!! Ficheiro %s não existe.\n", filename1);
  1425. return -1;
  1426. }
  1427. //printf("A configurar casa:\n");
  1428. ConfiguraCasa(f1); // Faz "setup" da casa
  1429. fclose(f1); // Fecha ficheiro 1
  1430.  
  1431. f2 = fopen(filename2, "r"); // Abre ficheiro 2
  1432. if (f2 == NULL) { // Se não conseguir abrir, imprime erro e termina processo
  1433. fprintf(stderr, "ERRO!! Ficheiro %s não existe.\n", filename2);
  1434. return -2;
  1435. }
  1436. //printf("A configurar Regras:\n");
  1437. ConfiguraRegras(f2);
  1438. fclose(f2); // Fecha ficheiro 2
  1439. }
  1440. /*pthread_t threads[3];
  1441. pthread_create(&threads[1],NULL,open_check_raw_values,NULL);
  1442. pthread_create(&threads[2],NULL,AtuaRegras,NULL);
  1443. pthread_create(&threads[3],NULL,display,NULL);*/
  1444. while (1) {
  1445. open_check_raw_values();
  1446.  
  1447. AtuaRegras(); // Confirma as regras e atualiza os atuadores
  1448. display();
  1449.  
  1450. }
  1451.  
  1452. return 0;
  1453. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement