Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <math.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <ctype.h>
- #include <postgresql/libpq-fe.h>
- #define MAX_SIZE 69
- #define NUMDIVIS 9
- #define NUMREGRAS 50
- #define NUMATU 6
- typedef struct {
- int id;
- double temp;
- double relative_humidity;
- double visible_light;
- double infrared_light;
- } mote;
- typedef struct {
- char atuador;
- int div;
- int on;
- } acao;
- struct Divisao {
- char nomeDiv[50]; // String c/ nome da divisão
- char Sensores[10][20];// Array de strings com o nome dos sensores (MAX 10 Sensores) (20 Caracteres Máximo)
- mote sensor;
- char Atuadores[10][20]; // Array de strings com o nome dos atuadores (MAX 10 Atuadores) (20 Caracteres Máximo)
- };
- struct Divisao casa[NUMDIVIS + 1]; //Variável Global array das divioes da casa
- struct Regra {
- char nomeDiv[50]; // String c/ nome da divisão
- char Condicoes[10][20]; // Array de strings com o as condições (MAX 10 condições e operadores) (20 Caracteres Máximo cada)
- acao Acoes[10]; // Array de strings com o as acoes a efetuar (MAX 10 acoes) (20 Caracteres Máximo cada)
- };
- struct Regra regras[NUMREGRAS]; //Variável Global array das divioes da casa
- struct Atuador {
- char nomeAtuador; // Char c/ nome do atuador
- int Estado; // Ligado ou desligado
- };
- struct Atuador atuadores[NUMDIVIS + 1][NUMATU]; //Variável Global array 2D dos atuadores por divisao
- //<--------------------------------------------------->
- void calculate_real_values(long id, long temp, long humidity,
- long visible_light, long infrared_light) {
- casa[id].sensor.id = id;
- casa[id].sensor.temp = -39.6 + 0.01 * temp;
- casa[id].sensor.relative_humidity = -2.0468 + 0.0367 * humidity
- - 0.0000015955 * humidity * humidity;
- casa[id].sensor.visible_light = 2.28882 * visible_light;
- casa[id].sensor.infrared_light = 0.28161621093 * infrared_light;
- /* printf("ID: %s\n",casa[id].nomeDiv);
- printf("Temp: %f\n", casa[id].sensor.temp);
- printf("visible: %f\n", casa[id].sensor.visible_light);
- printf("IR: %f\n", casa[id].sensor.infrared_light);*/
- }
- void open_check_raw_values() {
- char check[MAX_SIZE] = { };
- // while(1){
- if (fgets(check, sizeof(check), stdin) != NULL) {
- int size = strlen(check);
- if (size == 68
- && (check[0] == '7' && check[1] == 'E' && check[3] == '4'
- && check[4] == '5' && check[66] == '7'
- && check[67] == 'E')) {
- char id[] = { check[15], check[16], check[18], check[19], '\0' };
- char temp[] = { check[48], check[49], check[51], check[52], '\0' };
- char humidity[] =
- { check[54], check[55], check[57], check[58], '\0' };
- char visible_light[] = { check[36], check[37], check[39], check[40],
- '\0' };
- char infrared_light[] = { check[42], check[43], check[45],
- check[46], '\0' };
- long id1 = strtoul(id, NULL, 16);
- double raw_temp = strtoul(temp, NULL, 16);
- double raw_humidty = strtoul(humidity, NULL, 16);
- double raw_visible_light = strtoul(visible_light, NULL, 16);
- double raw_infrared_light = strtoul(infrared_light, NULL, 16);
- //pthread_mutex_lock(&mutex1);
- calculate_real_values(id1, raw_temp, raw_humidty, raw_visible_light,
- raw_infrared_light);
- //pthread_mutex_unlock(&mutex1);
- }
- //}
- }
- //return NULL;
- }
- void ConfiguraCasa(FILE *f) {
- char *str;
- char lido[100];
- char auxSens[90], auxAtu[90];
- int i = 1, c, n = 0;
- //Enquanto tiver linhas para ler
- while (fgets(lido, 100, f) != NULL) {
- c = 0;
- if (lido[strlen(lido) - 1] == '\n') //Confirma se último caracter é \n
- lido[strlen(lido) - 1] = '\0'; //Remove \n do fim da string lida
- str = strtok(lido, ":");
- while (str != NULL) {
- //Separa a linha nas 3 divisões <nome>:<sensores>:<atuadores>
- switch (c) {
- case 0:
- strcpy(casa[i].nomeDiv, str);// Guarda na posição i o nome da divisão
- break;
- case 1:
- strcpy(auxSens, str);// Guarda numa string auxiliar a lista dos sensores
- break;
- case 2:
- strcpy(auxAtu, str);// Guarda numa string auxiliar a lista dos atuadores
- break;
- default:
- break;
- }
- str = strtok(NULL, ":");
- c++;
- }
- //Separa Sensores em substrings
- c = 0;
- str = strtok(auxSens, ",");
- while (str != NULL) {
- // Enquanto houver sensores
- 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
- str = strtok(NULL, ",");
- c++;
- }
- //Separa Atuadores em substrings
- c = 0;
- n = 0;
- str = strtok(auxAtu, ",");
- while (str != NULL) { // Enquanto houver atuadores
- 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
- atuadores[i][n].nomeAtuador = str[0]; // Guarda na posição n da lista atuadores, o nome do atuador
- atuadores[i][n].Estado = 0;
- str = strtok(NULL, ",");
- c++;
- n++;
- }
- i++; // Passa para a posição seguinte no array casa
- }
- }
- void store_house_config_db(PGconn *conn,FILE* f){
- if(f==NULL)
- exit(-1);
- int id=1;
- char *str;
- char lido[100];
- char auxSens[90], auxAtu[90],auxDivisao[90];
- int i = 1, c, n = 0;
- int init=1;
- PGresult *res_div=PQexec(conn,"SELECT * FROM divisao");
- PGresult *res_sens=PQexec(conn,"SELECT * FROM sensor");
- PGresult *res_act=PQexec(conn,"SELECT * FROM atuador");
- //Enquanto tiver linhas para ler
- while (fgets(lido, 100, f) != NULL) {
- c = 0;
- if (lido[strlen(lido) - 1] == '\n') //Confirma se último caracter é \n
- lido[strlen(lido) - 1] = '\0'; //Remove \n do fim da string lida
- str = strtok(lido, ":");
- while (str != NULL) {
- //Separa a linha nas 3 divisões <nome>:<sensores>:<atuadores>
- switch (c) {
- case 0:
- strcpy(auxDivisao, str);// Guarda na posição i o nome da divisão
- break;
- case 1:
- strcpy(auxSens, str);// Guarda numa string auxiliar a lista dos sensores
- break;
- case 2:
- strcpy(auxAtu, str);// Guarda numa string auxiliar a lista dos atuadores
- break;
- default:
- break;
- }
- str = strtok(NULL, ":");
- c++;
- }
- char query_divisao[200];
- sprintf(query_divisao,"INSERT INTO divisao (id, nome_div) VALUES (%i,'%s')",id,auxDivisao);
- PQexec(conn,query_divisao);
- //Separa Sensores em substrings
- c = 1;
- str = strtok(auxSens, ",");
- char query_sensor[200];
- while (str != NULL) {
- // Enquanto houver sensores
- sprintf(query_sensor,"INSERT INTO sensor (mote_id,sensor_name,div_id) VALUES (%i,'%s',%i)",id,str,id);
- PQexec(conn,query_sensor);
- str = strtok(NULL, ",");
- c++;
- }
- //Separa Atuadores em substrings
- c = 0;
- n = 0;
- str = strtok(auxAtu, ",");
- char query_actuator[100];
- while (str != NULL) {
- sprintf(query_actuator, "INSERT INTO atuador (nome_atuador, div_id) VALUES ('%s',%i)",str,id);
- PQexec(conn,query_actuator);
- str = strtok(NULL, ",");
- }
- id++; // Passa para a posição seguinte no array casa
- }
- }
- /*
- * Função que interpreta o SensorRules.txt
- */
- void ConfiguraRegras(FILE *f) {
- char *str;
- char lido[100];
- char resto[20][100] = { '\0' };
- int i = 0, c, z = 0, q = 0;
- int err = 0;
- int tag_divis = 0, tag_atu = 0; // tags boleanas de controlo
- //Enquanto tiver linhas para ler
- while (fgets(lido, 100, f) != NULL) {
- c = 0;
- if (lido[strlen(lido) - 1] == '\n') //Confirma se último caracter é \n
- lido[strlen(lido) - 1] = '\0'; //Remove \n do fim da string lida
- str = strtok(lido, ":");
- while ((str != NULL) && !tag_divis) {// Enquanto tiver texto e tiver uma divisão
- if (0 == c) { // Se for a primeira ocorrência é o nome da divisão
- tag_divis = 1;
- for (z = 0; z < NUMDIVIS + 1 && tag_divis; z++) // Percorre todas as divisões da casa
- 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
- tag_divis = 0;
- if (tag_divis) {// Caso a tag seja verdadeira (divisão não encontrada) imprime na consola de erros juntamente com a linha correspondente
- fprintf(stderr, "Divisão não encontrada!!! Linha %i\n",
- i + 1);
- err = 1;
- continue;
- }
- strcpy(regras[i].nomeDiv, str);
- } else {
- strcpy(resto[c - 1], str);// Senão adiciona ao array resto na posição c-1 (começa em 0)
- }
- str = strtok(NULL, " ");
- c++;
- }
- c -= 2;
- q = 0;
- str = strtok(resto[c], ":,");
- while ((str != NULL) && !tag_atu) { // Enquanto tiver texto e tiver uma divisão
- if (!(strcmp(str, "ON") == 0 || strcmp(str, "OFF") == 0)) {
- regras[i].Acoes[q].atuador = str[0];
- regras[i].Acoes[q].div = z - 1;
- tag_atu = 1;
- for (int x = 0; x < 10 && tag_atu; x++)
- if (strcmp(str, casa[z - 1].Atuadores[x]) == 0)
- tag_atu = 0;
- if (tag_atu) {// Caso a tag seja verdadeira (atuador não encontrado) imprime na consola de erros juntamente com a linha correspondente
- fprintf(stderr, "Atuador não encontrado - %s!!! Linha %i\n",
- str, i + 1);
- err = 1;
- continue;
- }
- } else {
- if (str[1] == 'N')
- regras[i].Acoes[q].on = 1;
- else
- regras[i].Acoes[q].on = 0;
- q++;
- }
- str = strtok(NULL, ":,");
- }
- for (z = 0; c > z; z++) {
- strcpy(regras[i].Condicoes[z], resto[z]);
- }
- if (err) {
- err = 0;
- regras[i].nomeDiv[0] = '\0';
- } else
- i++; // Passa para a posição seguinte no array regras
- }
- }
- /*
- * Confirma se Regra é cumprida ou não
- */
- int efectRegra(char sensor, int id, char comp, double value) {
- double read = 26;
- switch (sensor) {
- case 'T':
- read = casa[id].sensor.temp;
- break;
- case 'H':
- read = casa[id].sensor.relative_humidity;
- break;
- case 'L':
- read = casa[id].sensor.visible_light;
- break;
- case 'I':
- read = casa[id].sensor.infrared_light;
- break;
- default:
- return 0;
- }
- // Confirma se a condição é válida e retorna 1 (true)
- switch (comp) {
- case '<':
- if (read < value)
- return 1;
- break;
- case '>':
- if (read > value)
- return 1;
- break;
- case '=':
- if (read == value)
- return 1;
- break;
- default:
- return 0;
- }
- return 0;
- }
- /*
- * Função que interpreta o SensorRules.txt e imprime o resultado
- */
- void AtuaRegras() {
- char aux[100];
- int c = 0, comp_pos = 0;
- double value = 0;
- int divi;
- int tag_sens = 1; // tags boleanas de controlo
- int valida = 0;
- /*while(1){
- c=0;
- comp_pos=0;
- value=0;
- tag_sens=1; // tags boleanas de controlo
- valida=0;*/
- //Leitura da lista das regras
- for (int i = 0; i < NUMREGRAS && strlen(regras[i].nomeDiv) > 0; i++) {
- //Interpretação das Regras
- for (int z = 0; z < NUMDIVIS + 1; z++) {// Percorre todas as divisões da casa
- if (strcmp(regras[i].nomeDiv, casa[z].nomeDiv) == 0) {//Se a divisão em causa for correspondente à que está na posição z, entao.....
- for (int k = 0; k < 10; k++) {// Percorre a totalidade do array resto
- if (strlen(regras[i].Condicoes[k]) == 0)// Se a posição estiver vazia entao salta o resto da execução à frente
- continue;
- 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
- // Por outras palavras, devolve a posição do operador de comparaçao
- divi = regras[i].Condicoes[k][comp_pos - 1] - '0';
- for (int x = 0; x < 10; x++) {// Percorre toda a lista de Sensores
- if (strncmp(regras[i].Condicoes[k],
- casa[divi].Sensores[x], comp_pos) == 0) {// Se o nome do sensor existe na lista.....
- 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
- value = atof(aux);// Transforma a string aux num float e guarda em value
- strncpy(aux, regras[i].Condicoes[k], comp_pos); // Copia para a string aux o nome do sensor
- aux[comp_pos] = '\0';// Coloca no final da string o caracter terminal
- //pthread_mutex_lock(&mutex1);
- valida = efectRegra(aux[0], z,
- regras[i].Condicoes[k][comp_pos], value);// Confirma se a condição é válida
- //pthread_mutex_unlock(&mutex1);
- k++;// Passa para a posição seguinte do array resto
- c = 0;
- if ((strcmp(regras[i].Condicoes[k], "OR") != 0)
- && (strcmp(regras[i].Condicoes[k], "AND")
- != 0) && valida) {// Se não for OR e não for AND e a condição for válida.....
- while (regras[i].Acoes[c].atuador != '\0') {// Enquanto tiver valores a imprimir
- for (int l = 0; l < NUMATU; l++)
- //pthread_mutex_lock(&mutex2);
- if (atuadores[regras[i].Acoes[c].div][l].nomeAtuador
- == regras[i].Acoes[c].atuador) {
- atuadores[regras[i].Acoes[c].div][l].Estado =
- regras[i].Acoes[c].on;
- }
- c++;
- }
- k = 20; // Garante que execução do ciclo acaba e passa para a linha seguinte
- } else if ((strcmp(regras[i].Condicoes[k], "OR")
- == 0) && valida) {// Senão, se for OR e a condição for válida..... (resultado final válido)
- k = k + 2;// Salta para operador seguinte (caso haja)
- while ((strcmp(regras[i].Condicoes[k], "OR")
- == 0)
- || (strcmp(regras[i].Condicoes[k],
- "AND") == 0))// Enquanto for um operador....
- k = k + 2;// Salta para operador seguinte (caso haja)
- while (regras[i].Acoes[c].atuador != '\0') {// Enquanto tiver valores a imprimir
- for (int l = 0; l < NUMATU; l++)
- if (atuadores[regras[i].Acoes[c].div][l].nomeAtuador
- == regras[i].Acoes[c].atuador)
- atuadores[regras[i].Acoes[c].div][l].Estado =
- regras[i].Acoes[c].on;
- c++;
- }
- k = 20; // Garante que execução do ciclo acaba e passa para a linha seguinte
- } else if ((strcmp(regras[i].Condicoes[k], "AND")
- == 0) && !valida) { // Senão, se for AND e a condição não for válida..... (resultado final não válido)
- k = 20; // Garante que execução do ciclo acaba e passa para a linha seguinte
- }
- valida = 0; // Faz reset ao valor da tag, para fazer nova validação
- tag_sens = 0;// Torna a tag falsa -> ou seja, o sensor existe
- break;
- }
- }
- 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
- fprintf(stderr,
- "Sensor não encontrado!!! Linha %i - %s\n",
- i + 1, regras[i].Condicoes[k]);
- }
- tag_sens = 1; // Retoma o valor de true à tag
- }
- }
- }
- }
- //}
- //return NULL;
- }
- //<------------------------------------------------------>
- struct RGBcolor {
- unsigned char R;
- unsigned char G;
- unsigned char B;
- };
- struct RGBcolor door_p, ac1, ac2, ac3, ac4, ac5, ac7, water, humidity5, blinds1,
- blinds2, blinds3, blinds5, lights1, lights2, lights3, lights5, lights6,
- lights7, ir, temp1, temp2, temp3, temp4, temp5, temp7, temp8, li8, li9; //cores dos pixeis do actuador em questao (consoante o estado) - numeros correspondem à divisao
- /*
- * white [255,255,255]
- * black [0,0,0]
- * green [55,255,69]
- * light brown [205,134,11]
- * dark brown [102,65,0]
- * light grey [205,205,205]
- * dark grey [115,115,115]
- * yellow [255,255,0]
- * light blue [155,204,255]
- * dark blue [0,43,215]
- */
- //light brown
- #define door_locked_R 205
- #define door_locked_G 134
- #define door_locked_B 11
- //dark brown
- #define door_unlocked_R 102
- #define door_unlocked_G 65
- #define door_unlocked_B 0
- //light blue
- #define ac_on_R 155
- #define ac_on_G 204
- #define ac_on_B 255
- //dark grey
- #define ac_off_R 115
- #define ac_off_G 115
- #define ac_off_B 115
- //dark blue
- #define water_on_R 0
- #define water_on_G 43
- #define water_on_B 215
- //dark grey
- #define water_off_R 115
- #define water_off_G 115
- #define water_off_B 115
- //green
- #define blinds_open_R 55
- #define blinds_open_G 255
- #define blinds_open_B 69
- //dark grey
- #define blinds_closed_R 115
- #define blinds_closed_G 115
- #define blinds_closed_B 115
- //yellow
- #define lights_on_R 255
- #define lights_on_G 255
- #define lights_on_B 0
- //dark grey
- #define lights_off_R 115
- #define lights_off_G 115
- #define lights_off_B 115
- //estado sensores temperatura
- //dark red
- #define ir_on_R 255
- #define ir_on_G 65
- #define ir_on_B 65
- //light red
- #define ir_off_R 255
- #define ir_off_G 255
- #define ir_off_B 255
- //estado sensores luminosidade
- //minimo 0
- #define lum0_R 255
- #define lum0_G 255
- #define lum0_B 255
- //nivl 1
- #define lum1_R 255
- #define lum1_G 255
- #define lum1_B 153
- //nivel 2
- #define lum2_R 255
- #define lum2_G 255
- #define lum2_B 51
- //nivel 3
- #define lum3_R 255
- #define lum3_G 255
- #define lum3_B 0
- //estado sensores temperatura
- //minimo 0
- #define temp0_R 255
- #define temp0_G 255
- #define temp0_B 255
- //nivl 1
- #define temp1_R 255
- #define temp1_G 153
- #define temp1_B 153
- //nivel 2
- #define temp2_R 255
- #define temp2_G 51
- #define temp2_B 51
- //nivel 3
- #define temp3_R 255
- #define temp3_G 0
- #define temp3_B 0
- //estado sensor humidade
- //minimo 0
- #define hum0_R 255
- #define hum0_G 255
- #define hum0_B 255
- //nivl 1
- #define hum1_R 153
- #define hum1_G 255
- #define hum1_B 255
- //nivel 2
- #define hum2_R 51
- #define hum2_G 255
- #define hum2_B 255
- //nivel 3
- #define hum3_R 0
- #define hum3_G 255
- #define hum3_B 255
- void display() {
- //while(1){
- //sensores temp
- if ((casa[1].sensor.temp >= 15) && (casa[1].sensor.temp < 18)) {
- temp1.R = temp0_R;
- temp1.G = temp0_G;
- temp1.B = temp0_B;
- } else if ((casa[1].sensor.temp >= 18) && (casa[1].sensor.temp < 25)) {
- temp1.R = temp1_R;
- temp1.G = temp1_G;
- temp1.B = temp1_B;
- }
- else if ((casa[1].sensor.temp >= 25) && (casa[1].sensor.temp < 27)) {
- temp1.R = temp2_R;
- temp1.G = temp2_G;
- temp1.B = temp2_B;
- }
- else if ((casa[1].sensor.temp >= 27) && (casa[1].sensor.temp <= 35)) {
- temp1.R = temp3_R;
- temp1.G = temp3_G;
- temp1.B = temp3_B;
- }
- if ((casa[2].sensor.temp >= 15) && (casa[2].sensor.temp < 22)) {
- temp2.R = temp0_R;
- temp2.G = temp0_G;
- temp2.B = temp0_B;
- }
- else if ((casa[2].sensor.temp >= 22) && (casa[2].sensor.temp < 25)) {
- temp2.R = temp1_R;
- temp2.G = temp1_G;
- temp2.B = temp1_B;
- }
- else if ((casa[2].sensor.temp >= 25) && (casa[2].sensor.temp < 27)) {
- temp2.R = temp2_R;
- temp2.G = temp2_G;
- temp2.B = temp2_B;
- }
- else if ((casa[2].sensor.temp >= 27) && (casa[2].sensor.temp <= 35)) {
- temp2.R = temp3_R;
- temp2.G = temp3_G;
- temp2.B = temp3_B;
- }
- if ((casa[3].sensor.temp >= 15) && (casa[3].sensor.temp < 18)) {
- temp3.R = temp0_R;
- temp3.G = temp0_G;
- temp3.B = temp0_B;
- }
- else if ((casa[3].sensor.temp >= 18) && (casa[3].sensor.temp < 25)) {
- temp3.R = temp1_R;
- temp3.G = temp1_G;
- temp3.B = temp1_B;
- }
- else if ((casa[3].sensor.temp >= 25) && (casa[3].sensor.temp < 27)) {
- temp3.R = temp2_R;
- temp3.G = temp2_G;
- temp3.B = temp2_B;
- }
- else if ((casa[3].sensor.temp >= 27) && (casa[3].sensor.temp <= 35)) {
- temp3.R = temp3_R;
- temp3.G = temp3_G;
- temp3.B = temp3_B;
- }
- if ((casa[4].sensor.temp >= 15) && (casa[4].sensor.temp < 18)) {
- temp4.R = temp0_R;
- temp4.G = temp0_G;
- temp4.B = temp0_B;
- }
- else if ((casa[4].sensor.temp >= 18) && (casa[4].sensor.temp < 25)) {
- temp4.R = temp1_R;
- temp4.G = temp1_G;
- temp4.B = temp1_B;
- }
- else if ((casa[4].sensor.temp >= 25) && (casa[4].sensor.temp < 27)) {
- temp4.R = temp2_R;
- temp4.G = temp2_G;
- temp4.B = temp2_B;
- }
- else if ((casa[4].sensor.temp >= 27) && (casa[4].sensor.temp <= 35)) {
- temp4.R = temp3_R;
- temp4.G = temp3_G;
- temp4.B = temp3_B;
- }
- if ((casa[5].sensor.temp >= 15) && (casa[5].sensor.temp < 18)) {
- temp5.R = temp0_R;
- temp5.G = temp0_G;
- temp5.B = temp0_B;
- }
- else if ((casa[5].sensor.temp >= 18) && (casa[5].sensor.temp < 25)) {
- temp5.R = temp1_R;
- temp5.G = temp1_G;
- temp5.B = temp1_B;
- }
- else if ((casa[5].sensor.temp >= 25) && (casa[5].sensor.temp < 27)) {
- temp5.R = temp2_R;
- temp5.G = temp2_G;
- temp5.B = temp2_B;
- }
- else if ((casa[5].sensor.temp >= 27) && (casa[5].sensor.temp <= 35)) {
- temp5.R = temp3_R;
- temp5.G = temp3_G;
- temp5.B = temp3_B;
- }
- if ((casa[7].sensor.temp >= 15) && (casa[7].sensor.temp < 18)) {
- temp7.R = temp0_R;
- temp7.G = temp0_G;
- temp7.B = temp0_B;
- }
- else if ((casa[7].sensor.temp >= 18) && (casa[7].sensor.temp < 25)) {
- temp7.R = temp1_R;
- temp7.G = temp1_G;
- temp7.B = temp1_B;
- }
- else if ((casa[7].sensor.temp >= 25) && (casa[7].sensor.temp < 27)) {
- temp7.R = temp2_R;
- temp7.G = temp2_G;
- temp7.B = temp2_B;
- }
- else if ((casa[7].sensor.temp >= 27) && (casa[7].sensor.temp <= 35)) {
- temp7.R = temp3_R;
- temp7.G = temp3_G;
- temp7.B = temp3_B;
- }
- if ((casa[8].sensor.temp >= 15) && (casa[8].sensor.temp < 20)) {
- temp8.R = temp0_R;
- temp8.G = temp0_G;
- temp8.B = temp0_B;
- }
- else if ((casa[8].sensor.temp >= 20) && (casa[8].sensor.temp < 25)) {
- temp8.R = temp1_R;
- temp8.G = temp1_G;
- temp8.B = temp1_B;
- }
- else if ((casa[8].sensor.temp >= 25) && (casa[8].sensor.temp < 30)) {
- temp8.R = temp2_R;
- temp8.G = temp2_G;
- temp8.B = temp2_B;
- }
- else if ((casa[8].sensor.temp >= 30) && (casa[8].sensor.temp <= 35)) {
- temp8.R = temp3_R;
- temp8.G = temp3_G;
- temp8.B = temp3_B;
- }
- //sensores lum
- if ((casa[8].sensor.visible_light >= 0)
- && (casa[8].sensor.visible_light < 125)) {
- li8.R = lum0_R;
- li8.G = lum0_G;
- li8.B = lum0_B;
- }
- else if ((casa[8].sensor.visible_light >= 125)
- && (casa[8].sensor.visible_light < 250)) {
- li8.R = lum1_R;
- li8.G = lum1_G;
- li8.B = lum1_B;
- }
- else if ((casa[8].sensor.visible_light >= 250)
- && (casa[8].sensor.visible_light < 375)) {
- li8.R = lum2_R;
- li8.G = lum2_G;
- li8.B = lum2_B;
- }
- else if ((casa[8].sensor.visible_light >= 375)
- && (casa[8].sensor.visible_light <= 500)) {
- li8.R = lum3_R;
- li8.G = lum3_G;
- li8.B = lum3_B;
- }
- if ((casa[9].sensor.visible_light >= 0)
- && (casa[9].sensor.visible_light < 125)) {
- li9.R = lum0_R;
- li9.G = lum0_G;
- li9.B = lum0_B;
- }
- else if ((casa[9].sensor.visible_light >= 125)
- && (casa[9].sensor.visible_light < 250)) {
- li9.R = lum1_R;
- li9.G = lum1_G;
- li9.B = lum1_B;
- }
- else if ((casa[9].sensor.visible_light >= 250)
- && (casa[9].sensor.visible_light < 375)) {
- li9.R = lum2_R;
- li9.G = lum2_G;
- li9.B = lum2_B;
- }
- else if ((casa[9].sensor.visible_light >= 375)
- && (casa[9].sensor.visible_light <= 500)) {
- li9.R = lum3_R;
- li9.G = lum3_G;
- li9.B = lum3_B;
- }
- //hum
- if ((casa[1].sensor.relative_humidity >= 30)
- && (casa[1].sensor.relative_humidity < 37)) {
- humidity5.R = hum0_R;
- humidity5.G = hum0_G;
- humidity5.B = hum0_B;
- }
- else if ((casa[1].sensor.relative_humidity >= 37)
- && (casa[1].sensor.relative_humidity < 44)) {
- humidity5.R = hum1_R;
- humidity5.G = hum1_G;
- humidity5.B = hum1_B;
- }
- else if ((casa[1].sensor.relative_humidity >= 44)
- && (casa[1].sensor.relative_humidity < 51)) {
- humidity5.R = hum2_R;
- humidity5.G = hum2_G;
- humidity5.B = hum2_B;
- }
- else if ((casa[1].sensor.relative_humidity >= 51)
- && (casa[1].sensor.relative_humidity <= 60)) {
- humidity5.R = hum3_R;
- humidity5.G = hum3_G;
- humidity5.B = hum3_B;
- }
- //infrared
- if (casa[6].sensor.infrared_light > 250) {
- ir.R = ir_on_R;
- ir.G = ir_on_G;
- ir.B = ir_on_B;
- } else {
- ir.R = ir_off_R;
- ir.G = ir_off_G;
- ir.B = ir_off_B;
- }
- //actuadores
- //cozinha
- if (atuadores[1][0].Estado) { //on
- ac1.R = ac_on_R;
- ac1.G = ac_on_G;
- ac1.B = ac_on_B;
- } else { //off
- ac1.R = ac_off_R;
- ac1.G = ac_off_G;
- ac1.B = ac_off_B;
- }
- //sala
- if (atuadores[2][0].Estado) {
- ac2.R = ac_on_R;
- ac2.G = ac_on_G;
- ac2.B = ac_on_B;
- } else {
- ac2.R = ac_off_R;
- ac2.G = ac_off_G;
- ac2.B = ac_off_B;
- }
- //quarto1
- if (atuadores[3][0].Estado) {
- ac3.R = ac_on_R;
- ac3.G = ac_on_G;
- ac3.B = ac_on_B;
- } else {
- ac3.R = ac_off_R;
- ac3.G = ac_off_G;
- ac3.B = ac_off_B;
- }
- //wc
- if (atuadores[4][0].Estado) {
- ac4.R = ac_on_R;
- ac4.G = ac_on_G;
- ac4.B = ac_on_B;
- } else {
- ac4.R = ac_off_R;
- ac4.G = ac_off_G;
- ac4.B = ac_off_B;
- }
- //horta
- if (atuadores[5][0].Estado) {
- ac5.R = ac_on_R;
- ac5.G = ac_on_G;
- ac5.B = ac_on_B;
- } else {
- ac5.R = ac_off_R;
- ac5.G = ac_off_G;
- ac5.B = ac_off_B;
- }
- //hall
- if (atuadores[7][0].Estado) {
- ac7.R = ac_on_R;
- ac7.G = ac_on_G;
- ac7.B = ac_on_B;
- } else {
- ac7.R = ac_off_R;
- ac7.G = ac_off_G;
- ac7.B = ac_off_B;
- }
- //cozinha
- if (atuadores[1][2].Estado) { //on
- lights1.R = lights_on_R;
- lights1.G = lights_on_G;
- lights1.B = lights_on_B;
- } else { //off
- lights1.R = lights_off_R;
- lights1.G = lights_off_G;
- lights1.B = lights_off_B;
- }
- //sala
- if (atuadores[2][2].Estado) {
- lights2.R = lights_on_R;
- lights2.G = lights_on_G;
- lights2.B = lights_on_B;
- } else {
- lights2.R = lights_off_R;
- lights2.G = lights_off_G;
- lights2.B = lights_off_B;
- }
- //quarto
- if (atuadores[3][2].Estado) {
- lights3.R = lights_on_R;
- lights3.G = lights_on_G;
- lights3.B = lights_on_B;
- } else {
- lights3.R = lights_off_R;
- lights3.G = lights_off_G;
- lights3.B = lights_off_B;
- }
- //horta
- if (atuadores[5][2].Estado) {
- lights5.R = lights_on_R;
- lights5.G = lights_on_G;
- lights5.B = lights_on_B;
- } else {
- lights5.R = lights_off_R;
- lights5.G = lights_off_G;
- lights5.B = lights_off_B;
- }
- //entrada
- if (atuadores[6][0].Estado) {
- lights6.R = lights_on_R;
- lights6.G = lights_on_G;
- lights6.B = lights_on_B;
- } else {
- lights6.R = lights_off_R;
- lights6.G = lights_off_G;
- lights6.B = lights_off_B;
- }
- //hall
- if (atuadores[7][1].Estado) {
- lights7.R = lights_on_R;
- lights7.G = lights_on_G;
- lights7.B = lights_on_B;
- } else {
- lights7.R = lights_off_R;
- lights7.G = lights_off_G;
- lights7.B = lights_off_B;
- }
- //cozinha
- if (!atuadores[1][1].Estado) { //closed
- blinds1.R = blinds_closed_R;
- blinds1.G = blinds_closed_G;
- blinds1.B = blinds_closed_B;
- } else { //open
- blinds1.R = blinds_open_R;
- blinds1.G = blinds_open_G;
- blinds1.B = blinds_open_B;
- }
- //sala
- if (!atuadores[2][1].Estado) {
- blinds2.R = blinds_closed_R;
- blinds2.G = blinds_closed_G;
- blinds2.B = blinds_closed_B;
- } else {
- blinds2.R = blinds_open_R;
- blinds2.G = blinds_open_G;
- blinds2.B = blinds_open_B;
- }
- //quarto
- if (!atuadores[3][1].Estado) {
- blinds3.R = blinds_closed_R;
- blinds3.G = blinds_closed_G;
- blinds3.B = blinds_closed_B;
- } else {
- blinds3.R = blinds_open_R;
- blinds3.G = blinds_open_G;
- blinds3.B = blinds_open_B;
- }
- //horta
- if (!atuadores[5][1].Estado) {
- blinds5.R = blinds_closed_R;
- blinds5.G = blinds_closed_G;
- blinds5.B = blinds_closed_B;
- } else {
- blinds5.R = blinds_open_R;
- blinds5.G = blinds_open_G;
- blinds5.B = blinds_open_B;
- }
- //porta principal
- if (atuadores[6][1].Estado) { //trancada
- door_p.R = door_locked_R;
- door_p.G = door_locked_G;
- door_p.B = door_locked_B;
- } else { //destrancada
- door_p.R = door_unlocked_R;
- door_p.G = door_unlocked_G;
- door_p.B = door_unlocked_B;
- }
- if (atuadores[5][3].Estado) { //ligada
- water.R = water_on_R;
- water.G = water_on_G;
- water.B = water_on_B;
- } else { //desligada
- water.R = water_off_R;
- water.G = water_off_G;
- water.B = water_off_B;
- }
- printf(
- "[[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]",
- temp8.R, temp8.G, temp8.B, li8.R, li8.G, li8.B);
- printf(
- ",[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]",
- blinds5.R, blinds5.G, blinds5.B, blinds5.R, blinds5.G, blinds5.B,
- blinds5.R, blinds5.G, blinds5.B, blinds5.R, blinds5.G, blinds5.B,
- blinds5.R, blinds5.G, blinds5.B, blinds5.R, blinds5.G, blinds5.B,
- blinds5.R, blinds5.G, blinds5.B, blinds5.R, blinds5.G, blinds5.B,
- blinds3.R, blinds3.G, blinds3.B, blinds3.R, blinds3.G, blinds3.B);
- printf(
- ",[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]",
- blinds5.R, blinds5.G, blinds5.B, ac5.R, ac5.G, ac5.B, ac3.R, ac3.G,
- ac3.B, ac4.R, ac4.G, ac4.B);
- printf(
- ",[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]",
- blinds5.R, blinds5.G, blinds5.B, temp5.R, temp5.G, temp5.B, temp3.R,
- temp3.G, temp3.B, temp4.R, temp4.G, temp4.B);
- printf(
- ",[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]",
- blinds5.R, blinds5.G, blinds5.B, lights5.R, lights5.G, lights5.B,
- lights5.R, lights5.G, lights5.B, water.R, water.G, water.B,
- lights3.R, lights3.G, lights3.B);
- printf(
- ",[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]",
- blinds5.R, blinds5.G, blinds5.B, lights5.R, lights5.G, lights5.B,
- lights5.R, lights5.G, lights5.B, humidity5.R, humidity5.G,
- humidity5.B, lights3.R, lights3.G, lights3.B);
- printf(
- ",[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]",
- blinds5.R, blinds5.G, blinds5.B);
- printf(
- ",[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]",
- blinds5.R, blinds5.G, blinds5.B);
- printf(
- ",[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]",
- ir.R, ir.G, ir.B);
- printf(
- ",[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]",
- lights6.R, lights6.G, lights6.B);
- printf(
- ",[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]",
- lights7.R, lights7.G, lights7.B, lights7.R, lights7.G, lights7.B,
- lights7.R, lights7.G, lights7.B, lights7.R, lights7.G, lights7.B,
- door_p.R, door_p.G, door_p.B, lights6.R, lights6.G, lights6.B);
- printf(
- ",[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]",
- temp7.R, temp7.G, temp7.B, ac7.R, ac7.G, ac7.B, lights6.R,
- lights6.G, lights6.B);
- printf(
- ",[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]");
- printf(
- ",[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]");
- printf(
- ",[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]");
- printf(
- ",[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]",
- lights2.R, lights2.G, lights2.B, lights2.R, lights2.G, lights2.B,
- lights1.R, lights1.G, lights1.B);
- printf(
- ",[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]",
- lights2.R, lights2.G, lights2.B, lights2.R, lights2.G, lights2.B,
- lights1.R, lights1.G, lights1.B);
- printf(
- ",[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]",
- ac2.R, ac2.G, ac2.B, ac1.R, ac1.G, ac1.B);
- printf(
- ",[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]",
- temp2.R, temp2.G, temp2.B, temp1.R, temp1.G, temp1.B);
- printf(
- ",[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]",
- blinds2.R, blinds2.G, blinds2.B, blinds2.R, blinds2.G, blinds2.B,
- blinds1.R, blinds1.G, blinds1.B, blinds1.R, blinds1.G, blinds1.B);
- printf(
- ",[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",
- li9.R, li9.G, li9.B);
- // }
- // return NULL;
- }
- const char *conn = "host='db.fe.up.pt' user='sinfa44' password='MWdOTyWZ'";
- PGconn *dbconn;
- //<----------------------------->
- int query_check(PGresult *check){
- if (PQresultStatus(check) != PGRES_TUPLES_OK) {
- printf("Erro!\n");
- PQclear(check);
- PQfinish(dbconn);
- return -1;
- }
- else return 1;
- }
- void db_rules_config(PGconn *conn){
- char query[300]="SELECT rules_text FROM regras ORDER BY id";
- PGresult *res_rules=PQexec(conn,query);
- if(query_check(res_rules)==-1)
- exit(-1);
- FILE *f=fopen("rules_backup.txt","w+");
- if(NULL==f)
- exit(-1);
- int n=PQntuples(res_rules);
- for(int i=0; i<n;i++){
- fprintf(f,"%s",PQgetvalue(res_rules,i,0));
- if((i+1!=n))
- fprintf(f,"\n");
- }
- fclose(f);
- f=fopen("rules_backup.txt","r");
- ConfiguraRegras(f);
- fclose(f);
- //Se for para guardar txt comentar proxima linha
- //remove("rules_config_temp.txt");
- }
- void store_rules_db(PGconn *conn, FILE* f){
- if(f==NULL)
- exit(-1);
- char lido[200];
- char query[200];
- int insert=0;
- int init=1;
- int i=1;
- PGresult *res=NULL;
- char query_init[300]="SELECT * FROM regras";
- while (fgets(lido, 100, f) != NULL){
- if(init==1){
- res=PQexec(conn, query_init);
- init=0;
- }
- if(PQntuples(res)==0 || PQntuples(res)<i)
- insert=1;
- if(insert==1){
- if(lido[strlen(lido)-1]=='\n')
- lido[strlen(lido)-1]='\0';
- sprintf(query, "INSERT into regras (id,rules_text) VALUES (%i,'%s')",i, lido);
- }
- else{
- if(lido[strlen(lido)-1]=='\n')
- lido[strlen(lido)-1]='\0';
- sprintf(query, "UPDATE regras SET rules_text='%s' WHERE id=%i",lido, i);
- }
- //printf("%s\n",query);
- PQexec(conn, query);
- i++;
- }
- }
- void db_house_config(PGconn *conn) {
- char divisao[300];
- char query_divisao[300] = "SELECT id, trim(nome_div) from divisao order by id";
- char query_sensor[300];
- char query_actuator[300];
- PGresult *res_divisao = PQexec(conn, query_divisao); //DIVISAO
- /*if (PQresultStatus(res_divisao) != PGRES_TUPLES_OK) {
- printf("Erro!\n");
- PQclear(res_divisao);
- PQfinish(conn);
- exit(1);
- }*/
- if(query_check(res_divisao)==-1)
- exit(-1);
- FILE *config=fopen("config_backup.txt","w+");
- if(NULL==config)
- perror("Error:");
- int n = PQntuples(res_divisao);
- for (int i = 0; i < n; i++) {
- //Divisao
- strcpy(divisao, PQgetvalue(res_divisao, i, 1));
- //printf("%s:",PQgetvalue(res,i,1));
- strcat(divisao, ":");
- //sensor
- char id[5];
- strcpy(id, PQgetvalue(res_divisao, i, 0));
- sprintf(query_sensor,
- "SELECT trim(sensor_name) FROM sensor WHERE div_id=%s", id);
- PGresult *res_sensor = PQexec(conn, query_sensor);
- if(query_check(res_sensor)==-1)
- exit(-1);
- for (int j = 0; j < (PQntuples(res_sensor)); j++) {
- strcat(divisao, PQgetvalue(res_sensor, j, 0));
- if ((j + 1) != (PQntuples(res_sensor)))
- strcat(divisao, ",");
- }
- strcat(divisao, ":");
- //Atuadores
- sprintf(query_actuator,"SELECT trim(nome_atuador) FROM atuador WHERE div_id= %s", id);
- PGresult *res_actuator = PQexec(conn, query_actuator);
- if(query_check(res_actuator)==-1)
- exit(-1);
- for (int j = 0; j < (PQntuples(res_actuator)); j++) {
- strcat(divisao, PQgetvalue(res_actuator, j, 0));
- //printf("%s",PQgetvalue(res_sensor,j,0));
- if ((j + 1) != (PQntuples(res_actuator)))
- strcat(divisao, ",");
- }
- fprintf(config,"%s", divisao);
- if((i+1!=n))
- fprintf(config,"\n");
- }
- fclose(config);
- config=fopen("config_backup.txt","r");
- ConfiguraCasa(config);
- fclose(config);
- //Para guardar comentar linha a seguir
- //remove("config_temp.txt");
- }
- void test(FILE* f){
- char lido[100];
- char lido_aux[100];
- char* str_divisao;
- char* str_sensor;
- char* str_sensor_aux;
- while(fgets(lido,100,f)!=NULL){
- //Divisao
- str_divisao=strtok(lido,": ");
- printf("Divsao:%s\n",str_divisao);
- //FAZER QUERIES
- printf("Lido Antigo:%s\n",lido);
- //Lido fica diferente
- strcpy(lido,strtok(NULL,"\n"));
- printf("Lido Novo:%s\n",lido);
- //Sensores
- str_sensor=strtok(lido," ");
- printf("Lido sensor:%s\n",lido);
- printf("Sensor a verificar:%s\n", str_sensor);
- //Verificar nome sensor
- str_sensor_aux=strtok(str_sensor, ">");
- if(strcmp(str_sensor_aux,str_sensor)==0)
- str_sensor_aux=strtok(str_sensor,"<");
- if(NULL==str_sensor_aux)
- exit(-1);
- printf("SENSOR:%s\n",str_sensor_aux);
- printf("LIDO depois do sensor: %s\n",lido);
- //FAZER QUERIES E COISO
- //str=strtok(NULL, ">");
- //if(str==NULL)
- //printf("%s\n",str);
- }
- }
- //CODIGO ANDRE
- int main() {
- /* printf("Pretende-se conectar à Base de Dados? (Y,N)\n");
- char s;
- int check = -1;
- while (1) {
- s = getchar();
- switch (s) {
- case 'y':
- case 'Y':
- case 's':
- case 'S':
- check = 1;
- break;
- case 'n':
- case 'N':
- check = 0;
- break;
- default:
- break;
- }
- if (0 == check || 1 == check)
- break;
- else
- printf("Tente novamente.\n");
- s = getchar();
- }*/
- int check=1;
- if (1 == check) {
- //printf("A conectar:\n");
- PGconn *dbconn= PQconnectdb(conn);
- if (PQstatus(dbconn) == CONNECTION_BAD) {
- // printf("Unable to connect\n");
- exit(-1);
- } else {
- //printf("Able to connect!\n");
- }
- FILE *f1_test=fopen("SensorRules.txt","r");
- FILE *f2_test=fopen("SensorConfigurations.txt","r");
- test(f1_test);
- //store_house_config_db(dbconn,f2_test);
- store_rules_db(dbconn, f1_test);
- db_house_config(dbconn);
- db_rules_config(dbconn);
- } else {
- char *filename1 = "SensorConfigurations.txt", *filename2 =
- "SensorRules.txt"; //Define nomes dos ficheiros de configuração
- FILE *f1, *f2;
- f1 = fopen(filename1, "r"); // Abre ficheiro 1
- if (f1 == NULL) { // Se não conseguir abrir, imprime erro e termina processo
- fprintf(stderr, "ERRO!! Ficheiro %s não existe.\n", filename1);
- return -1;
- }
- //printf("A configurar casa:\n");
- ConfiguraCasa(f1); // Faz "setup" da casa
- fclose(f1); // Fecha ficheiro 1
- f2 = fopen(filename2, "r"); // Abre ficheiro 2
- if (f2 == NULL) { // Se não conseguir abrir, imprime erro e termina processo
- fprintf(stderr, "ERRO!! Ficheiro %s não existe.\n", filename2);
- return -2;
- }
- //printf("A configurar Regras:\n");
- ConfiguraRegras(f2);
- fclose(f2); // Fecha ficheiro 2
- }
- /*pthread_t threads[3];
- pthread_create(&threads[1],NULL,open_check_raw_values,NULL);
- pthread_create(&threads[2],NULL,AtuaRegras,NULL);
- pthread_create(&threads[3],NULL,display,NULL);*/
- while (1) {
- open_check_raw_values();
- AtuaRegras(); // Confirma as regras e atualiza os atuadores
- display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement